Commit cd3a3fefa79c377c7ee999824f225da9a93358c6

Authored by Ednara Oliveira
2 parents f0613a40 fe6509ae
Exists in master

Merge branch 'master' of git@github.com:demoiselle/framework.git

impl/extension/jpa/.gitignore
@@ -3,3 +3,5 @@ @@ -3,3 +3,5 @@
3 /.classpath 3 /.classpath
4 /.settings 4 /.settings
5 /.externalToolBuilders 5 /.externalToolBuilders
  6 +/hsql:..properties
  7 +/hsql:..script
impl/extension/jpa/pom.xml
1 -<!--  
2 - Demoiselle Framework  
3 - Copyright (C) 2010 SERPRO  
4 - ============================================================================  
5 - This file is part of Demoiselle Framework.  
6 -  
7 - Demoiselle Framework is free software; you can redistribute it and/or  
8 - modify it under the terms of the GNU Lesser General Public License version 3  
9 - as published by the Free Software Foundation.  
10 -  
11 - This program is distributed in the hope that it will be useful,  
12 - but WITHOUT ANY WARRANTY; without even the implied warranty of  
13 - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the  
14 - GNU General Public License for more details.  
15 -  
16 - You should have received a copy of the GNU Lesser General Public License version 3  
17 - along with this program; if not, see <http://www.gnu.org/licenses />  
18 - or write to the Free Software Foundation, Inc., 51 Franklin Street,  
19 - Fifth Floor, Boston, MA 02110-1301, USA.  
20 - ============================================================================  
21 - Este arquivo é parte do Framework Demoiselle.  
22 -  
23 - O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou  
24 - modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação  
25 - do Software Livre (FSF).  
26 -  
27 - Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA  
28 - GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou  
29 - APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português  
30 - para maiores detalhes.  
31 -  
32 - Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título  
33 - "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses />  
34 - ou escreva para a Fundação do Software Livre (FSF) Inc.,  
35 - 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.  
36 --->  
37 -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 1 +<!-- Demoiselle Framework Copyright (C) 2010 SERPRO ============================================================================
  2 + This file is part of Demoiselle Framework. Demoiselle Framework is free software;
  3 + you can redistribute it and/or modify it under the terms of the GNU Lesser
  4 + General Public License version 3 as published by the Free Software Foundation.
  5 + This program is distributed in the hope that it will be useful, but WITHOUT
  6 + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  7 + FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  8 + You should have received a copy of the GNU Lesser General Public License
  9 + version 3 along with this program; if not, see <http://www.gnu.org/licenses
  10 + /> or write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  11 + Floor, Boston, MA 02110-1301, USA. ============================================================================
  12 + Este arquivo é parte do Framework Demoiselle. O Framework Demoiselle é um
  13 + software livre; você pode redistribuí-lo e/ou modificá-lo dentro dos termos
  14 + da GNU LGPL versão 3 como publicada pela Fundação do Software Livre (FSF).
  15 + Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
  16 + GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou APLICAÇÃO
  17 + EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português para maiores
  18 + detalhes. Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
  19 + "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses
  20 + /> ou escreva para a Fundação do Software Livre (FSF) Inc., 51 Franklin St,
  21 + Fifth Floor, Boston, MA 02111-1301, USA. -->
  22 +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  23 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
38 24
39 <modelVersion>4.0.0</modelVersion> 25 <modelVersion>4.0.0</modelVersion>
40 26
@@ -71,6 +57,22 @@ @@ -71,6 +57,22 @@
71 <groupId>org.eclipse.persistence</groupId> 57 <groupId>org.eclipse.persistence</groupId>
72 <artifactId>javax.persistence</artifactId> 58 <artifactId>javax.persistence</artifactId>
73 </dependency> 59 </dependency>
  60 + <dependency>
  61 + <groupId>org.hibernate</groupId>
  62 + <artifactId>hibernate-entitymanager</artifactId>
  63 + <scope>test</scope>
  64 + </dependency>
  65 + <dependency>
  66 + <groupId>br.gov.frameworkdemoiselle.component</groupId>
  67 + <artifactId>demoiselle-junit</artifactId>
  68 + <version>2.3.0-RC2-SNAPSHOT</version>
  69 + <scope>test</scope>
  70 + </dependency>
  71 + <dependency>
  72 + <groupId>hsqldb</groupId>
  73 + <artifactId>hsqldb</artifactId>
  74 + <scope>test</scope>
  75 + </dependency>
74 </dependencies> 76 </dependencies>
75 77
76 <repositories> 78 <repositories>
impl/extension/jpa/src/test/java/br/gov/frameworkdemoiselle/domain/Client.java 0 → 100644
@@ -0,0 +1,65 @@ @@ -0,0 +1,65 @@
  1 +package br.gov.frameworkdemoiselle.domain;
  2 +
  3 +import java.io.Serializable;
  4 +import java.util.Date;
  5 +
  6 +import javax.persistence.Entity;
  7 +import javax.persistence.GeneratedValue;
  8 +import javax.persistence.Id;
  9 +import javax.persistence.Temporal;
  10 +import javax.persistence.TemporalType;
  11 +
  12 +/**
  13 + * Simle entity for test classes
  14 + * @author serpro
  15 + *
  16 + */
  17 +@Entity
  18 +public class Client implements Serializable {
  19 +
  20 + private static final long serialVersionUID = 1L;
  21 +
  22 + private Long id;
  23 +
  24 + private String name;
  25 +
  26 + private Date birthDate;
  27 +
  28 + @Id
  29 + @GeneratedValue
  30 + public Long getId() {
  31 + return id;
  32 + }
  33 +
  34 +
  35 + public void setId(Long id) {
  36 + this.id = id;
  37 + }
  38 +
  39 +
  40 + public String getName() {
  41 + return name;
  42 + }
  43 +
  44 +
  45 + public void setName(String name) {
  46 + this.name = name;
  47 + }
  48 +
  49 +
  50 + @Temporal(TemporalType.DATE)
  51 + public Date getBirthDate() {
  52 + return birthDate;
  53 + }
  54 +
  55 +
  56 +
  57 + public void setBirthDate(Date birthDate) {
  58 + this.birthDate = birthDate;
  59 + }
  60 +
  61 +
  62 +
  63 +
  64 +
  65 +}
impl/extension/jpa/src/test/java/br/gov/frameworkdemoiselle/internal/producer/EntityManagerFactoryProducerTest.java
@@ -58,6 +58,7 @@ public class EntityManagerFactoryProducerTest { @@ -58,6 +58,7 @@ public class EntityManagerFactoryProducerTest {
58 Assert.assertEquals(emFactory, producer.create("pu1")); 58 Assert.assertEquals(emFactory, producer.create("pu1"));
59 } 59 }
60 60
  61 +
61 @Test 62 @Test
62 public void testCreateWithUnitPersistenceNotExisting() { 63 public void testCreateWithUnitPersistenceNotExisting() {
63 64
@@ -69,6 +70,22 @@ public class EntityManagerFactoryProducerTest { @@ -69,6 +70,22 @@ public class EntityManagerFactoryProducerTest {
69 Assert.assertEquals(emFactory, producer.create("pu1")); 70 Assert.assertEquals(emFactory, producer.create("pu1"));
70 } 71 }
71 72
  73 + /**
  74 + * Test if after producing an entity manager, the EntityManagerFactory is correctly stored in the
  75 + * cache associated with the current class loader.
  76 + */
  77 + @Test
  78 + public void testStorageCacheAfterCreate(){
  79 + mockStatic(Persistence.class);
  80 + expect(Persistence.createEntityManagerFactory("pu1")).andReturn(emFactory);
  81 + replay(Persistence.class);
  82 +
  83 + Map<String,EntityManagerFactory> producerCache = producer.getCache();
  84 + Assert.assertNotNull(producerCache);
  85 + Assert.assertTrue(producerCache.containsKey("pu1"));
  86 + Assert.assertTrue(producerCache.containsValue(emFactory));
  87 + }
  88 +
72 @Test 89 @Test
73 public void testInitWithoutError() { 90 public void testInitWithoutError() {
74 mockStatic(Persistence.class); 91 mockStatic(Persistence.class);
@@ -84,15 +101,16 @@ public class EntityManagerFactoryProducerTest { @@ -84,15 +101,16 @@ public class EntityManagerFactoryProducerTest {
84 Assert.assertEquals(emFactory, internalCache.get("pu1")); 101 Assert.assertEquals(emFactory, internalCache.get("pu1"));
85 } 102 }
86 103
87 - @Test 104 + /*@Test
88 public void testInitWithError() { 105 public void testInitWithError() {
89 try { 106 try {
  107 +
90 producer.loadPersistenceUnits(); 108 producer.loadPersistenceUnits();
91 Assert.fail(); 109 Assert.fail();
92 }catch(DemoiselleException cause) { 110 }catch(DemoiselleException cause) {
93 Assert.assertTrue(true); 111 Assert.assertTrue(true);
94 } 112 }
95 - } 113 + }*/
96 114
97 @Test 115 @Test
98 public void testGetCache() { 116 public void testGetCache() {
@@ -120,4 +138,41 @@ public class EntityManagerFactoryProducerTest { @@ -120,4 +138,41 @@ public class EntityManagerFactoryProducerTest {
120 producer.close(); 138 producer.close();
121 verify(emFactory); 139 verify(emFactory);
122 } 140 }
  141 +
  142 + /**
  143 + * Test if detecting the persistence unit with an empty name throws correct error.
  144 + */
  145 + @Test
  146 + public void testEmptyPersistenceUnitName(){
  147 + EntityManagerFactoryProducer.ENTITY_MANAGER_RESOURCE = "persistence-empty-name.xml";
  148 +
  149 + try{
  150 + producer.getCache();
  151 + Assert.fail();
  152 + }
  153 + catch(DemoiselleException de){
  154 + //
  155 + }
  156 + }
  157 +
  158 + /**
  159 + * Test if asking to create an entity manager still not in the cache will correctly create it and put it in the cache.
  160 + */
  161 + @Test
  162 + public void testCreatePersistenceUnitNotInCache() {
  163 + mockStatic(Persistence.class);
  164 + expect(Persistence.createEntityManagerFactory("pu1")).andReturn(emFactory);
  165 + replay(Persistence.class);
  166 +
  167 + ClassLoader cl = this.getClass().getClassLoader();
  168 + HashMap<String, EntityManagerFactory> emEntry = new HashMap<String, EntityManagerFactory>();
  169 + cache.put(cl,emEntry);
  170 +
  171 + producer.create("pu1");
  172 +
  173 + Map<String,EntityManagerFactory> producerCache = producer.getCache();
  174 + Assert.assertNotNull(producerCache);
  175 + Assert.assertTrue(producerCache.containsKey("pu1"));
  176 + Assert.assertTrue(producerCache.containsValue(emFactory));
  177 + }
123 } 178 }
impl/extension/jpa/src/test/java/br/gov/frameworkdemoiselle/internal/producer/EntityManagerProducerTest.java
@@ -35,7 +35,6 @@ @@ -35,7 +35,6 @@
35 * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. 35 * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
36 */ 36 */
37 package br.gov.frameworkdemoiselle.internal.producer; 37 package br.gov.frameworkdemoiselle.internal.producer;
38 -import org.junit.Ignore;  
39 import static org.easymock.EasyMock.createMock; 38 import static org.easymock.EasyMock.createMock;
40 import static org.easymock.EasyMock.expect; 39 import static org.easymock.EasyMock.expect;
41 import static org.easymock.EasyMock.verify; 40 import static org.easymock.EasyMock.verify;
@@ -69,7 +68,8 @@ import br.gov.frameworkdemoiselle.annotation.Name; @@ -69,7 +68,8 @@ import br.gov.frameworkdemoiselle.annotation.Name;
69 import br.gov.frameworkdemoiselle.internal.configuration.EntityManagerConfig; 68 import br.gov.frameworkdemoiselle.internal.configuration.EntityManagerConfig;
70 import br.gov.frameworkdemoiselle.internal.proxy.EntityManagerProxy; 69 import br.gov.frameworkdemoiselle.internal.proxy.EntityManagerProxy;
71 import br.gov.frameworkdemoiselle.util.ResourceBundle; 70 import br.gov.frameworkdemoiselle.util.ResourceBundle;
72 -@Ignore 71 +
  72 +
73 @RunWith(PowerMockRunner.class) 73 @RunWith(PowerMockRunner.class)
74 @PrepareForTest(Persistence.class) 74 @PrepareForTest(Persistence.class)
75 public class EntityManagerProducerTest { 75 public class EntityManagerProducerTest {
impl/extension/jpa/src/test/java/br/gov/frameworkdemoiselle/internal/proxy/EntityManagerProxyTest.java
@@ -35,7 +35,6 @@ @@ -35,7 +35,6 @@
35 * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. 35 * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
36 */ 36 */
37 package br.gov.frameworkdemoiselle.internal.proxy; 37 package br.gov.frameworkdemoiselle.internal.proxy;
38 -import org.junit.Ignore;  
39 import static org.easymock.EasyMock.expect; 38 import static org.easymock.EasyMock.expect;
40 import static org.easymock.EasyMock.replay; 39 import static org.easymock.EasyMock.replay;
41 import static org.easymock.EasyMock.verify; 40 import static org.easymock.EasyMock.verify;
@@ -67,7 +66,7 @@ import org.powermock.modules.junit4.PowerMockRunner; @@ -67,7 +66,7 @@ import org.powermock.modules.junit4.PowerMockRunner;
67 import br.gov.frameworkdemoiselle.internal.producer.EntityManagerProducer; 66 import br.gov.frameworkdemoiselle.internal.producer.EntityManagerProducer;
68 import br.gov.frameworkdemoiselle.internal.producer.FakeEntityManager; 67 import br.gov.frameworkdemoiselle.internal.producer.FakeEntityManager;
69 import br.gov.frameworkdemoiselle.util.Beans; 68 import br.gov.frameworkdemoiselle.util.Beans;
70 -@Ignore 69 +
71 @RunWith(PowerMockRunner.class) 70 @RunWith(PowerMockRunner.class)
72 @PrepareForTest({ Beans.class }) 71 @PrepareForTest({ Beans.class })
73 public class EntityManagerProxyTest { 72 public class EntityManagerProxyTest {
@@ -77,7 +76,7 @@ public class EntityManagerProxyTest { @@ -77,7 +76,7 @@ public class EntityManagerProxyTest {
77 private EntityManager entityManager; 76 private EntityManager entityManager;
78 77
79 private EntityManagerProducer entityManagerContext; 78 private EntityManagerProducer entityManagerContext;
80 - 79 +
81 @Before 80 @Before
82 public void setUp() throws Exception { 81 public void setUp() throws Exception {
83 mockStatic(Beans.class); 82 mockStatic(Beans.class);
@@ -87,6 +86,8 @@ public class EntityManagerProxyTest { @@ -87,6 +86,8 @@ public class EntityManagerProxyTest {
87 expect(Beans.getReference(EntityManagerProducer.class)).andReturn(this.entityManagerContext).anyTimes(); 86 expect(Beans.getReference(EntityManagerProducer.class)).andReturn(this.entityManagerContext).anyTimes();
88 expect(this.entityManagerContext.getEntityManager("teste")).andReturn(this.entityManager).anyTimes(); 87 expect(this.entityManagerContext.getEntityManager("teste")).andReturn(this.entityManager).anyTimes();
89 replay(this.entityManagerContext); 88 replay(this.entityManagerContext);
  89 + //expect(this.entityManager.getTransaction()).andReturn(transaction).anyTimes();
  90 + //replay(this.entityManager);
90 replayAll(); 91 replayAll();
91 92
92 this.entityManagerProxy = new EntityManagerProxy("teste"); 93 this.entityManagerProxy = new EntityManagerProxy("teste");
@@ -336,44 +337,49 @@ public class EntityManagerProxyTest { @@ -336,44 +337,49 @@ public class EntityManagerProxyTest {
336 337
337 @Test 338 @Test
338 public void testCreateQuery() { 339 public void testCreateQuery() {
339 - Query query = null; 340 + Query query = EasyMock.createMock(Query.class);
340 expect(this.entityManager.createQuery("teste")).andReturn(query); 341 expect(this.entityManager.createQuery("teste")).andReturn(query);
341 replay(this.entityManager); 342 replay(this.entityManager);
342 - assertEquals(QueryProxy.class, this.entityManagerProxy.createQuery("teste").getClass()); 343 + assertTrue(Query.class.isAssignableFrom(this.entityManagerProxy.createQuery("teste").getClass()) );
343 verify(this.entityManager); 344 verify(this.entityManager);
344 } 345 }
345 346
346 @Test 347 @Test
347 public void testCreateQueryWithParamCriteria() { 348 public void testCreateQueryWithParamCriteria() {
348 - TypedQuery<Object> typedQuery = null; 349 + @SuppressWarnings("unchecked")
  350 + TypedQuery<Object> typedQuery = EasyMock.createMock(TypedQuery.class);
  351 +
349 CriteriaQuery<Object> criteriaQuery = null; 352 CriteriaQuery<Object> criteriaQuery = null;
350 expect(this.entityManager.createQuery(criteriaQuery)).andReturn(typedQuery); 353 expect(this.entityManager.createQuery(criteriaQuery)).andReturn(typedQuery);
351 replay(this.entityManager); 354 replay(this.entityManager);
352 - assertEquals(TypedQueryProxy.class, this.entityManagerProxy.createQuery(criteriaQuery).getClass()); 355 + assertTrue(TypedQuery.class.isAssignableFrom(this.entityManagerProxy.createQuery(criteriaQuery).getClass()));
353 verify(this.entityManager); 356 verify(this.entityManager);
354 } 357 }
355 358
356 @Test 359 @Test
357 public void testCreateQueryWithParamStringAndClass() { 360 public void testCreateQueryWithParamStringAndClass() {
358 - TypedQuery<String> typeQuery = null; 361 + @SuppressWarnings("unchecked")
  362 + TypedQuery<String> typeQuery = EasyMock.createMock(TypedQuery.class);
  363 +
359 expect(this.entityManager.createQuery("teste", String.class)).andReturn(typeQuery); 364 expect(this.entityManager.createQuery("teste", String.class)).andReturn(typeQuery);
360 replay(this.entityManager); 365 replay(this.entityManager);
361 - assertEquals(TypedQueryProxy.class, this.entityManagerProxy.createQuery("teste", String.class).getClass()); 366 + assertTrue(TypedQuery.class.isAssignableFrom(this.entityManagerProxy.createQuery("teste", String.class).getClass()));
362 verify(this.entityManager); 367 verify(this.entityManager);
363 } 368 }
364 369
365 @Test 370 @Test
366 public void testCreateNamedQuery() { 371 public void testCreateNamedQuery() {
367 - Query query = null; 372 + Query query = EasyMock.createMock(Query.class);
368 expect(this.entityManager.createNamedQuery("teste")).andReturn(query); 373 expect(this.entityManager.createNamedQuery("teste")).andReturn(query);
369 replay(this.entityManager); 374 replay(this.entityManager);
370 - assertEquals(QueryProxy.class, this.entityManagerProxy.createNamedQuery("teste").getClass()); 375 + assertTrue(Query.class.isAssignableFrom(this.entityManagerProxy.createNamedQuery("teste").getClass()));
371 verify(this.entityManager); 376 verify(this.entityManager);
372 } 377 }
373 378
374 @Test 379 @Test
375 public void testCreateNamedQueryWithParamsStringAndClass() { 380 public void testCreateNamedQueryWithParamsStringAndClass() {
376 - TypedQuery<String> typedQuery = null; 381 + @SuppressWarnings("unchecked")
  382 + TypedQuery<String> typedQuery = EasyMock.createMock(TypedQuery.class);
377 expect(this.entityManager.createNamedQuery("teste", String.class)).andReturn(typedQuery); 383 expect(this.entityManager.createNamedQuery("teste", String.class)).andReturn(typedQuery);
378 replay(this.entityManager); 384 replay(this.entityManager);
379 assertEquals(typedQuery, this.entityManagerProxy.createNamedQuery("teste", String.class)); 385 assertEquals(typedQuery, this.entityManagerProxy.createNamedQuery("teste", String.class));
impl/extension/jpa/src/test/java/br/gov/frameworkdemoiselle/internal/proxy/QueryProxyTest.java 0 → 100644
@@ -0,0 +1,255 @@ @@ -0,0 +1,255 @@
  1 +package br.gov.frameworkdemoiselle.internal.proxy;
  2 +
  3 +import java.util.Collections;
  4 +import java.util.Date;
  5 +import java.util.HashMap;
  6 +import java.util.List;
  7 +import java.util.Map;
  8 +
  9 +import javax.persistence.EntityManager;
  10 +import javax.persistence.EntityManagerFactory;
  11 +import javax.persistence.FlushModeType;
  12 +import javax.persistence.LockModeType;
  13 +import javax.persistence.NoResultException;
  14 +import javax.persistence.Persistence;
  15 +import javax.persistence.Query;
  16 +import javax.persistence.TemporalType;
  17 +
  18 +import org.easymock.EasyMock;
  19 +import org.junit.Assert;
  20 +import org.junit.Before;
  21 +import org.junit.Ignore;
  22 +import org.junit.Test;
  23 +import org.junit.runner.RunWith;
  24 +import org.powermock.api.easymock.PowerMock;
  25 +import org.powermock.core.classloader.annotations.PrepareForTest;
  26 +import org.powermock.modules.junit4.PowerMockRunner;
  27 +import org.powermock.reflect.Whitebox;
  28 +
  29 +import br.gov.frameworkdemoiselle.domain.Client;
  30 +import br.gov.frameworkdemoiselle.internal.producer.EntityManagerProducer;
  31 +import br.gov.frameworkdemoiselle.util.Beans;
  32 +
  33 +/**
  34 + * Test the proxied {@link Query} class, {@link QueryProxy}.
  35 + * @author 81986912515
  36 + *
  37 + */
  38 +@Ignore
  39 +@RunWith(PowerMockRunner.class)
  40 +@PrepareForTest({Beans.class})
  41 +public class QueryProxyTest {
  42 +
  43 + private EntityManager manager;
  44 + private EntityManagerProducer producer;
  45 +
  46 + @Before
  47 + public void setUp(){
  48 +
  49 + Map<String, Object> configOverrides = new HashMap<String, Object>();
  50 + configOverrides.put("javax.persistence.provider", "org.hibernate.ejb.HibernatePersistence");
  51 + configOverrides.put("javax.persistence.jdbc.url", "jdbc:hsqldb:hsql:.");
  52 + configOverrides.put("hibernate.show_sql", "true");
  53 + configOverrides.put("hibernate.hbm2ddl.auto", "create-drop");
  54 +
  55 + EntityManagerFactory factory = Persistence.createEntityManagerFactory("pu1", configOverrides);
  56 + EntityManager delegate = factory.createEntityManager();
  57 +
  58 + Map<String, EntityManager> cache = Collections.synchronizedMap(new HashMap<String, EntityManager>());
  59 + cache.put("pu1", delegate);
  60 +
  61 + producer = new EntityManagerProducer();
  62 + Whitebox.setInternalState(producer, "cache", cache);
  63 +
  64 + PowerMock.mockStatic(Beans.class);
  65 + EasyMock.expect(Beans.getReference(EntityManagerProducer.class)).andReturn(producer).times(12);
  66 + PowerMock.replayAll();
  67 +
  68 + manager = new EntityManagerProxy("pu1");
  69 +
  70 + manager.getTransaction().begin();
  71 + manager.createQuery("delete from Client").executeUpdate();
  72 +
  73 + Client client = new Client();
  74 + client.setName("Cliente 1");
  75 + client.setBirthDate(new Date());
  76 + manager.persist(client);
  77 +
  78 + client = new Client();
  79 + client.setName("Cliente 2");
  80 + client.setBirthDate(new Date());
  81 + manager.persist(client);
  82 +
  83 + client = new Client();
  84 + client.setName("Cliente 3");
  85 + client.setBirthDate(new Date());
  86 + manager.persist(client);
  87 +
  88 + manager.flush();
  89 + manager.getTransaction().commit();
  90 + manager.clear();
  91 +
  92 + PowerMock.resetAll();
  93 + }
  94 +
  95 + private QueryProxy getQueryProxy(String jpql,Object... params){
  96 + Query q = manager.createQuery(jpql);
  97 + if (!(q instanceof QueryProxy)){
  98 + Assert.fail("Query não é instância de QueryProxy");
  99 + }
  100 +
  101 + if (params!=null){
  102 + int count = 1;
  103 + for (Object param : params){
  104 + q.setParameter(count++, param);
  105 + }
  106 + }
  107 +
  108 + return (QueryProxy)q;
  109 + }
  110 +
  111 + @Test
  112 + public void testResultList(){
  113 + EasyMock.expect(Beans.getReference(EntityManagerProducer.class)).andReturn(producer).times(2);
  114 + PowerMock.replay(Beans.class);
  115 +
  116 + List<?> retorno = getQueryProxy("select c from Client c").getResultList();
  117 + Assert.assertNotNull(retorno);
  118 + Assert.assertFalse(retorno.isEmpty());
  119 + }
  120 +
  121 + @Test
  122 + public void testSingleResult(){
  123 + EasyMock.expect(Beans.getReference(EntityManagerProducer.class)).andReturn(producer).times(2);
  124 + PowerMock.replay(Beans.class);
  125 +
  126 + Client retorno = (Client)getQueryProxy("select c from Client c where c.name=?1","Cliente 1").getSingleResult();
  127 + Assert.assertNotNull(retorno);
  128 + }
  129 +
  130 + @Test
  131 + public void testExecuteUpdate(){
  132 + EasyMock.expect(Beans.getReference(EntityManagerProducer.class)).andReturn(producer).times(4);
  133 + PowerMock.replay(Beans.class);
  134 +
  135 + manager.getTransaction().begin();
  136 + int linesAffected = getQueryProxy("update Client set name=?1 where name=?2","Novo Cliente","Cliente 1").executeUpdate();
  137 + manager.getTransaction().commit();
  138 + Assert.assertEquals(1, linesAffected);
  139 + }
  140 +
  141 + @Test
  142 + public void testPagination(){
  143 + EasyMock.expect(Beans.getReference(EntityManagerProducer.class)).andReturn(producer).times(2);
  144 + PowerMock.replay(Beans.class);
  145 +
  146 + QueryProxy proxy = getQueryProxy("select c from Client c");
  147 +
  148 + proxy.setMaxResults(2);
  149 + Assert.assertEquals(2, proxy.getMaxResults());
  150 +
  151 + proxy.setFirstResult(1);
  152 + Assert.assertEquals(1, proxy.getFirstResult());
  153 +
  154 + List<?> result = proxy.getResultList();
  155 + Assert.assertEquals(2, result.size());
  156 + }
  157 +
  158 + @Test
  159 + public void testHint(){
  160 + EasyMock.expect(Beans.getReference(EntityManagerProducer.class)).andReturn(producer).times(14);
  161 + PowerMock.replay(Beans.class);
  162 +
  163 + //Consulta um cliente definindo a hint readOnly, que torna a entidade retornada não atualizável.
  164 + manager.getTransaction().begin();
  165 + QueryProxy proxy = getQueryProxy("select c from Client c where c.name=?1","Cliente 1");
  166 + proxy.setHint("org.hibernate.readOnly", true);
  167 + Assert.assertFalse( proxy.getHints().isEmpty() );
  168 +
  169 + //Tenta atualizar a entidade e limpar o cache de primeiro nível
  170 + Client c = (Client)proxy.getSingleResult();
  171 + c.setName("Cliente 1 Alterado");
  172 + manager.flush();
  173 + manager.getTransaction().commit();
  174 + manager.clear();
  175 +
  176 + //Reconsultar a entidade tem que retornar 1 resultado, pois o nome "Cliente 1" não deve ter sido alterado.
  177 + manager.getTransaction().begin();
  178 + proxy = getQueryProxy("select c from Client c where c.name=?1","Cliente 1");
  179 + c = (Client)proxy.getSingleResult();
  180 + Assert.assertNotNull(c);
  181 +
  182 + //Mudar a entidade agora tem que funcionar, pois não foi informado o hint
  183 + c.setName("Cliente 1 Alterado");
  184 + manager.flush();
  185 + manager.getTransaction().commit();
  186 + manager.clear();
  187 +
  188 + proxy = getQueryProxy("select c from Client c where c.name=?1","Cliente 1");
  189 +
  190 + try{
  191 + proxy.getSingleResult();
  192 + Assert.fail();
  193 + }
  194 + catch(NoResultException ne){
  195 + }
  196 +
  197 + PowerMock.verifyAll();
  198 + }
  199 +
  200 + @Test
  201 + public void testParameters(){
  202 +
  203 + EasyMock.expect(Beans.getReference(EntityManagerProducer.class)).andReturn(producer).times(2);
  204 + PowerMock.replay(Beans.class);
  205 +
  206 + QueryProxy proxy = getQueryProxy("select c.name from Client c where 'Named Parameter'=:name and c.birthDate=:dateName and c.name=?1 and c.birthDate=?2");
  207 +
  208 + Date dateValue = new Date();
  209 +
  210 + proxy.setParameter("name", "Named Parameter");
  211 + proxy.setParameter("dateName", dateValue, TemporalType.DATE);
  212 +
  213 + proxy.setParameter(1, "Cliente 1");
  214 + proxy.setParameter(2, dateValue,TemporalType.DATE);
  215 +
  216 + Assert.assertEquals(proxy.getParameterValue("name"),"Named Parameter");
  217 + Assert.assertEquals(proxy.getParameterValue(1), "Cliente 1");
  218 +
  219 + @SuppressWarnings("unchecked")
  220 + List<Map<String, Object>> result = proxy.getResultList();
  221 +
  222 + Assert.assertNotNull(result);
  223 + Assert.assertFalse(result.isEmpty());
  224 +
  225 + PowerMock.verifyAll();
  226 + }
  227 +
  228 + @Test
  229 + public void testFlushMode(){
  230 + EasyMock.expect(Beans.getReference(EntityManagerProducer.class)).andReturn(producer).times(3);
  231 + PowerMock.replay(Beans.class);
  232 +
  233 + manager.getTransaction().begin();
  234 + QueryProxy proxy = getQueryProxy("update Client set name=?1 where name=?2","Cliente 1 Alterado","Cliente 1");
  235 + proxy.setFlushMode(FlushModeType.COMMIT);
  236 + Assert.assertEquals(proxy.getFlushMode(), FlushModeType.COMMIT);
  237 + manager.getTransaction().commit();
  238 +
  239 + PowerMock.verifyAll();
  240 + }
  241 +
  242 + @Test
  243 + public void testLockMode(){
  244 + EasyMock.expect(Beans.getReference(EntityManagerProducer.class)).andReturn(producer).times(4);
  245 + PowerMock.replay(Beans.class);
  246 +
  247 + manager.getTransaction().begin();
  248 + QueryProxy proxy = getQueryProxy("update Client set name=?1 where name=?2","Cliente 1 Alterado","Cliente 1");
  249 + proxy.setLockMode(LockModeType.OPTIMISTIC);
  250 + Assert.assertEquals(proxy.getLockMode(), LockModeType.OPTIMISTIC);
  251 + manager.getTransaction().commit();
  252 +
  253 + PowerMock.verifyAll();
  254 + }
  255 +}
impl/extension/jpa/src/test/java/br/gov/frameworkdemoiselle/internal/proxy/TypedQueryProxyTest.java 0 → 100644
@@ -0,0 +1,244 @@ @@ -0,0 +1,244 @@
  1 +package br.gov.frameworkdemoiselle.internal.proxy;
  2 +
  3 +import java.util.Collections;
  4 +import java.util.Date;
  5 +import java.util.HashMap;
  6 +import java.util.List;
  7 +import java.util.Map;
  8 +
  9 +import javax.persistence.EntityManager;
  10 +import javax.persistence.EntityManagerFactory;
  11 +import javax.persistence.FlushModeType;
  12 +import javax.persistence.LockModeType;
  13 +import javax.persistence.NoResultException;
  14 +import javax.persistence.Persistence;
  15 +import javax.persistence.Query;
  16 +import javax.persistence.TemporalType;
  17 +import javax.persistence.TypedQuery;
  18 +
  19 +import org.easymock.EasyMock;
  20 +import org.junit.Assert;
  21 +import org.junit.Before;
  22 +import org.junit.Ignore;
  23 +import org.junit.Test;
  24 +import org.junit.runner.RunWith;
  25 +import org.powermock.api.easymock.PowerMock;
  26 +import org.powermock.core.classloader.annotations.PrepareForTest;
  27 +import org.powermock.modules.junit4.PowerMockRunner;
  28 +import org.powermock.reflect.Whitebox;
  29 +
  30 +import br.gov.frameworkdemoiselle.domain.Client;
  31 +import br.gov.frameworkdemoiselle.internal.producer.EntityManagerProducer;
  32 +import br.gov.frameworkdemoiselle.util.Beans;
  33 +
  34 +/**
  35 + * Test the proxied {@link Query} class, {@link TypedQueryProxy}.
  36 + * @author 81986912515
  37 + *
  38 + */
  39 +@Ignore
  40 +@RunWith(PowerMockRunner.class)
  41 +@PrepareForTest({Beans.class})
  42 +public class TypedQueryProxyTest {
  43 +
  44 + private EntityManager manager;
  45 + private EntityManagerProducer producer;
  46 +
  47 + @Before
  48 + public void setUp(){
  49 +
  50 + Map<String, Object> configOverrides = new HashMap<String, Object>();
  51 + configOverrides.put("javax.persistence.provider", "org.hibernate.ejb.HibernatePersistence");
  52 + configOverrides.put("javax.persistence.jdbc.url", "jdbc:hsqldb:hsql:.");
  53 + configOverrides.put("hibernate.show_sql", "true");
  54 + configOverrides.put("hibernate.hbm2ddl.auto", "create-drop");
  55 +
  56 + EntityManagerFactory factory = Persistence.createEntityManagerFactory("pu1", configOverrides);
  57 + EntityManager delegate = factory.createEntityManager();
  58 +
  59 + Map<String, EntityManager> cache = Collections.synchronizedMap(new HashMap<String, EntityManager>());
  60 + cache.put("pu1", delegate);
  61 +
  62 + producer = new EntityManagerProducer();
  63 + Whitebox.setInternalState(producer, "cache", cache);
  64 +
  65 + PowerMock.mockStatic(Beans.class);
  66 + EasyMock.expect(Beans.getReference(EntityManagerProducer.class)).andReturn(producer).times(12);
  67 + PowerMock.replayAll();
  68 +
  69 + manager = new EntityManagerProxy("pu1");
  70 +
  71 + manager.getTransaction().begin();
  72 + manager.createQuery("delete from Client").executeUpdate();
  73 +
  74 + Client client = new Client();
  75 + client.setName("Cliente 1");
  76 + client.setBirthDate(new Date());
  77 + manager.persist(client);
  78 +
  79 + client = new Client();
  80 + client.setName("Cliente 2");
  81 + client.setBirthDate(new Date());
  82 + manager.persist(client);
  83 +
  84 + client = new Client();
  85 + client.setName("Cliente 3");
  86 + client.setBirthDate(new Date());
  87 + manager.persist(client);
  88 +
  89 + manager.flush();
  90 + manager.getTransaction().commit();
  91 + manager.clear();
  92 +
  93 + PowerMock.resetAll();
  94 + }
  95 +
  96 + private <T> TypedQueryProxy<T> getQueryProxy(String jpql,Class<T> classType,Object... params){
  97 + TypedQuery<T> q = manager.createQuery(jpql,classType);
  98 + if (!(q instanceof TypedQueryProxy)){
  99 + Assert.fail("Query não é instância de QueryProxy");
  100 + }
  101 +
  102 + if (params!=null){
  103 + int count = 1;
  104 + for (Object param : params){
  105 + q.setParameter(count++, param);
  106 + }
  107 + }
  108 +
  109 + return (TypedQueryProxy<T>)q;
  110 + }
  111 +
  112 + @Test
  113 + public void testResultList(){
  114 + EasyMock.expect(Beans.getReference(EntityManagerProducer.class)).andReturn(producer).times(2);
  115 + PowerMock.replay(Beans.class);
  116 +
  117 + List<?> retorno = getQueryProxy("select c from Client c",Client.class).getResultList();
  118 + Assert.assertNotNull(retorno);
  119 + Assert.assertFalse(retorno.isEmpty());
  120 + }
  121 +
  122 + @Test
  123 + public void testSingleResult(){
  124 + EasyMock.expect(Beans.getReference(EntityManagerProducer.class)).andReturn(producer).times(2);
  125 + PowerMock.replay(Beans.class);
  126 +
  127 + Client retorno = (Client)getQueryProxy("select c from Client c where c.name=?1",Client.class,"Cliente 1").getSingleResult();
  128 + Assert.assertNotNull(retorno);
  129 + }
  130 +
  131 + @Test
  132 + public void testPagination(){
  133 + EasyMock.expect(Beans.getReference(EntityManagerProducer.class)).andReturn(producer).times(2);
  134 + PowerMock.replay(Beans.class);
  135 +
  136 + TypedQueryProxy<Client> proxy = getQueryProxy("select c from Client c",Client.class);
  137 +
  138 + proxy.setMaxResults(2);
  139 + Assert.assertEquals(2, proxy.getMaxResults());
  140 +
  141 + proxy.setFirstResult(1);
  142 + Assert.assertEquals(1, proxy.getFirstResult());
  143 +
  144 + List<?> result = proxy.getResultList();
  145 + Assert.assertEquals(2, result.size());
  146 + }
  147 +
  148 + @Test
  149 + public void testHint(){
  150 + EasyMock.expect(Beans.getReference(EntityManagerProducer.class)).andReturn(producer).times(14);
  151 + PowerMock.replay(Beans.class);
  152 +
  153 + //Consulta um cliente definindo a hint readOnly, que torna a entidade retornada não atualizável.
  154 + manager.getTransaction().begin();
  155 + TypedQueryProxy<Client> proxy = getQueryProxy("select c from Client c where c.name=?1",Client.class,"Cliente 1");
  156 + proxy.setHint("org.hibernate.readOnly", true);
  157 + Assert.assertFalse( proxy.getHints().isEmpty() );
  158 +
  159 + //Tenta atualizar a entidade e limpar o cache de primeiro nível
  160 + Client c = (Client)proxy.getSingleResult();
  161 + c.setName("Cliente 1 Alterado");
  162 + manager.flush();
  163 + manager.getTransaction().commit();
  164 + manager.clear();
  165 +
  166 + //Reconsultar a entidade tem que retornar 1 resultado, pois o nome "Cliente 1" não deve ter sido alterado.
  167 + manager.getTransaction().begin();
  168 + proxy = getQueryProxy("select c from Client c where c.name=?1",Client.class,"Cliente 1");
  169 + c = (Client)proxy.getSingleResult();
  170 + Assert.assertNotNull(c);
  171 +
  172 + //Mudar a entidade agora tem que funcionar, pois não foi informado o hint
  173 + c.setName("Cliente 1 Alterado");
  174 + manager.flush();
  175 + manager.getTransaction().commit();
  176 + manager.clear();
  177 +
  178 + proxy = getQueryProxy("select c from Client c where c.name=?1",Client.class,"Cliente 1");
  179 +
  180 + try{
  181 + proxy.getSingleResult();
  182 + Assert.fail();
  183 + }
  184 + catch(NoResultException ne){
  185 + }
  186 +
  187 + PowerMock.verifyAll();
  188 + }
  189 +
  190 + @Test
  191 + public void testParameters(){
  192 +
  193 + EasyMock.expect(Beans.getReference(EntityManagerProducer.class)).andReturn(producer).times(2);
  194 + PowerMock.replay(Beans.class);
  195 +
  196 + TypedQueryProxy<Client> proxy = getQueryProxy("select c from Client c where 'Named Parameter'=:name and c.birthDate=:dateName and c.name=?1 and c.birthDate=?2",Client.class);
  197 +
  198 + Date dateValue = new Date();
  199 +
  200 + proxy.setParameter("name", "Named Parameter");
  201 + proxy.setParameter("dateName", dateValue, TemporalType.DATE);
  202 +
  203 + proxy.setParameter(1, "Cliente 1");
  204 + proxy.setParameter(2, dateValue,TemporalType.DATE);
  205 +
  206 + Assert.assertEquals(proxy.getParameterValue("name"),"Named Parameter");
  207 + Assert.assertEquals(proxy.getParameterValue(1), "Cliente 1");
  208 +
  209 + List<Client> result = proxy.getResultList();
  210 +
  211 + Assert.assertNotNull(result);
  212 + Assert.assertFalse(result.isEmpty());
  213 +
  214 + PowerMock.verifyAll();
  215 + }
  216 +
  217 + @Test
  218 + public void testFlushMode(){
  219 + EasyMock.expect(Beans.getReference(EntityManagerProducer.class)).andReturn(producer).times(3);
  220 + PowerMock.replay(Beans.class);
  221 +
  222 + manager.getTransaction().begin();
  223 + TypedQueryProxy<Client> proxy = getQueryProxy("select c from Client c where c.name=?1",Client.class,"Cliente 1");
  224 + proxy.setFlushMode(FlushModeType.COMMIT);
  225 + Assert.assertEquals(proxy.getFlushMode(), FlushModeType.COMMIT);
  226 + manager.getTransaction().commit();
  227 +
  228 + PowerMock.verifyAll();
  229 + }
  230 +
  231 + @Test
  232 + public void testLockMode(){
  233 + EasyMock.expect(Beans.getReference(EntityManagerProducer.class)).andReturn(producer).times(3);
  234 + PowerMock.replay(Beans.class);
  235 +
  236 + manager.getTransaction().begin();
  237 + TypedQueryProxy<Client> proxy = getQueryProxy("select c from Client c where c.name=?1",Client.class,"Cliente 1");
  238 + proxy.setLockMode(LockModeType.OPTIMISTIC);
  239 + Assert.assertEquals(proxy.getLockMode(), LockModeType.OPTIMISTIC);
  240 + manager.getTransaction().commit();
  241 +
  242 + PowerMock.verifyAll();
  243 + }
  244 +}
impl/extension/jpa/src/test/java/br/gov/frameworkdemoiselle/template/JPACrudTest.java
@@ -35,7 +35,6 @@ @@ -35,7 +35,6 @@
35 * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. 35 * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
36 */ 36 */
37 package br.gov.frameworkdemoiselle.template; 37 package br.gov.frameworkdemoiselle.template;
38 -import org.junit.Ignore;  
39 import static org.easymock.EasyMock.expect; 38 import static org.easymock.EasyMock.expect;
40 import static org.easymock.EasyMock.replay; 39 import static org.easymock.EasyMock.replay;
41 import static org.easymock.EasyMock.verify; 40 import static org.easymock.EasyMock.verify;
@@ -48,13 +47,18 @@ import static org.powermock.reflect.Whitebox.setInternalState; @@ -48,13 +47,18 @@ import static org.powermock.reflect.Whitebox.setInternalState;
48 47
49 import java.io.Serializable; 48 import java.io.Serializable;
50 import java.util.ArrayList; 49 import java.util.ArrayList;
  50 +import java.util.HashMap;
51 import java.util.List; 51 import java.util.List;
  52 +import java.util.Map;
52 53
53 import javax.enterprise.inject.Instance; 54 import javax.enterprise.inject.Instance;
54 import javax.persistence.Column; 55 import javax.persistence.Column;
  56 +import javax.persistence.Entity;
55 import javax.persistence.EntityManager; 57 import javax.persistence.EntityManager;
  58 +import javax.persistence.EntityManagerFactory;
56 import javax.persistence.GeneratedValue; 59 import javax.persistence.GeneratedValue;
57 import javax.persistence.Id; 60 import javax.persistence.Id;
  61 +import javax.persistence.Persistence;
58 import javax.persistence.Query; 62 import javax.persistence.Query;
59 import javax.persistence.TransactionRequiredException; 63 import javax.persistence.TransactionRequiredException;
60 import javax.persistence.TypedQuery; 64 import javax.persistence.TypedQuery;
@@ -63,6 +67,8 @@ import javax.persistence.criteria.CriteriaQuery; @@ -63,6 +67,8 @@ import javax.persistence.criteria.CriteriaQuery;
63 import javax.persistence.criteria.Predicate; 67 import javax.persistence.criteria.Predicate;
64 import javax.persistence.criteria.Root; 68 import javax.persistence.criteria.Root;
65 69
  70 +import junit.framework.Assert;
  71 +
66 import org.easymock.EasyMock; 72 import org.easymock.EasyMock;
67 import org.junit.Before; 73 import org.junit.Before;
68 import org.junit.Test; 74 import org.junit.Test;
@@ -70,16 +76,18 @@ import org.junit.runner.RunWith; @@ -70,16 +76,18 @@ import org.junit.runner.RunWith;
70 import org.powermock.api.easymock.PowerMock; 76 import org.powermock.api.easymock.PowerMock;
71 import org.powermock.core.classloader.annotations.PrepareForTest; 77 import org.powermock.core.classloader.annotations.PrepareForTest;
72 import org.powermock.modules.junit4.PowerMockRunner; 78 import org.powermock.modules.junit4.PowerMockRunner;
  79 +import org.powermock.reflect.Whitebox;
73 80
74 import br.gov.frameworkdemoiselle.DemoiselleException; 81 import br.gov.frameworkdemoiselle.DemoiselleException;
75 import br.gov.frameworkdemoiselle.configuration.Configuration; 82 import br.gov.frameworkdemoiselle.configuration.Configuration;
76 import br.gov.frameworkdemoiselle.internal.implementation.PaginationImpl; 83 import br.gov.frameworkdemoiselle.internal.implementation.PaginationImpl;
77 import br.gov.frameworkdemoiselle.pagination.Pagination; 84 import br.gov.frameworkdemoiselle.pagination.Pagination;
78 import br.gov.frameworkdemoiselle.pagination.PaginationContext; 85 import br.gov.frameworkdemoiselle.pagination.PaginationContext;
  86 +import br.gov.frameworkdemoiselle.util.Beans;
79 import br.gov.frameworkdemoiselle.util.ResourceBundle; 87 import br.gov.frameworkdemoiselle.util.ResourceBundle;
80 -@Ignore 88 +
81 @RunWith(PowerMockRunner.class) 89 @RunWith(PowerMockRunner.class)
82 -@PrepareForTest({ ResourceBundle.class, Instance.class }) 90 +@PrepareForTest({ ResourceBundle.class, Instance.class, Beans.class })
83 public class JPACrudTest { 91 public class JPACrudTest {
84 92
85 private EntityManager entityManager; 93 private EntityManager entityManager;
@@ -102,6 +110,7 @@ public class JPACrudTest { @@ -102,6 +110,7 @@ public class JPACrudTest {
102 setInternalState(this.contactDAO, EntityManager.class, this.entityManager); 110 setInternalState(this.contactDAO, EntityManager.class, this.entityManager);
103 } 111 }
104 112
  113 + @Entity
105 class Contact implements Serializable { 114 class Contact implements Serializable {
106 115
107 private static final long serialVersionUID = 1L; 116 private static final long serialVersionUID = 1L;
@@ -144,12 +153,7 @@ public class JPACrudTest { @@ -144,12 +153,7 @@ public class JPACrudTest {
144 153
145 Pagination pagination = new PaginationImpl(); 154 Pagination pagination = new PaginationImpl();
146 pagination.setPageSize(10); 155 pagination.setPageSize(10);
147 - PaginationContext actualContext = PowerMock.createMock(PaginationContext.class);  
148 - expect(actualContext.getPagination(Contact.class)).andReturn(pagination);  
149 - @SuppressWarnings("unchecked")  
150 - Instance<PaginationContext> paginationContext = PowerMock.createMock(Instance.class);  
151 - expect(paginationContext.get()).andReturn(actualContext);  
152 - setInternalState(this.contactDAO, "paginationContext", paginationContext); 156 + setInternalState(this.contactDAO, "pagination", pagination);
153 157
154 TypedQuery<Contact> typeQuery = makeTypedQuery(); 158 TypedQuery<Contact> typeQuery = makeTypedQuery();
155 159
@@ -157,9 +161,10 @@ public class JPACrudTest { @@ -157,9 +161,10 @@ public class JPACrudTest {
157 expect(query.getSingleResult()).andReturn(10L); 161 expect(query.getSingleResult()).andReturn(10L);
158 162
159 expect(this.entityManager.createQuery("select this from Contact this", Contact.class)).andReturn(typeQuery); 163 expect(this.entityManager.createQuery("select this from Contact this", Contact.class)).andReturn(typeQuery);
160 - expect(this.entityManager.createQuery("SELECT COUNT(THIS) FROM CONTACT THIS")).andReturn(query); 164 + //expect(this.entityManager.createQuery("SELECT COUNT(THIS) FROM CONTACT THIS")).andReturn(query);
  165 + expect(this.entityManager.createQuery("select COUNT(this) from Contact this")).andReturn(query);
161 166
162 - replayAll(typeQuery, query, this.entityManager, paginationContext); 167 + replayAll(typeQuery, query, this.entityManager);
163 168
164 List<Contact> find = this.contactDAO.findAll(); 169 List<Contact> find = this.contactDAO.findAll();
165 170
@@ -173,12 +178,7 @@ public class JPACrudTest { @@ -173,12 +178,7 @@ public class JPACrudTest {
173 public void testFailCountAll() { 178 public void testFailCountAll() {
174 179
175 Pagination pagination = new PaginationImpl(); 180 Pagination pagination = new PaginationImpl();
176 - PaginationContext actualContext = PowerMock.createMock(PaginationContext.class);  
177 - expect(actualContext.getPagination(Contact.class)).andReturn(pagination);  
178 - @SuppressWarnings("unchecked")  
179 - Instance<PaginationContext> paginationContext = PowerMock.createMock(Instance.class);  
180 - expect(paginationContext.get()).andReturn(actualContext);  
181 - setInternalState(this.contactDAO, "paginationContext", paginationContext); 181 + setInternalState(this.contactDAO, "pagination", pagination);
182 182
183 TypedQuery<Contact> typeQuery = makeTypedQuery(); 183 TypedQuery<Contact> typeQuery = makeTypedQuery();
184 184
@@ -186,9 +186,9 @@ public class JPACrudTest { @@ -186,9 +186,9 @@ public class JPACrudTest {
186 186
187 expect(query.getSingleResult()).andThrow(new DemoiselleException("")); 187 expect(query.getSingleResult()).andThrow(new DemoiselleException(""));
188 expect(this.entityManager.createQuery("select this from Contact this", Contact.class)).andReturn(typeQuery); 188 expect(this.entityManager.createQuery("select this from Contact this", Contact.class)).andReturn(typeQuery);
189 - expect(this.entityManager.createQuery("SELECT COUNT(THIS) FROM CONTACT THIS")).andReturn(query); 189 + expect(this.entityManager.createQuery("select COUNT(this) from Contact this")).andReturn(query);
190 190
191 - replayAll(query, this.entityManager, paginationContext); 191 + replayAll(query, this.entityManager);
192 192
193 try { 193 try {
194 this.contactDAO.findAll(); 194 this.contactDAO.findAll();
@@ -294,12 +294,7 @@ public class JPACrudTest { @@ -294,12 +294,7 @@ public class JPACrudTest {
294 294
295 Pagination pagination = new PaginationImpl(); 295 Pagination pagination = new PaginationImpl();
296 pagination.setPageSize(10); 296 pagination.setPageSize(10);
297 - PaginationContext actualContext = PowerMock.createMock(PaginationContext.class);  
298 - expect(actualContext.getPagination(Contact.class)).andReturn(pagination);  
299 - @SuppressWarnings("unchecked")  
300 - Instance<PaginationContext> paginationContext = PowerMock.createMock(Instance.class);  
301 - expect(paginationContext.get()).andReturn(actualContext);  
302 - setInternalState(this.contactDAO, "paginationContext", paginationContext); 297 + setInternalState(this.contactDAO, "pagination", pagination);
303 298
304 TypedQuery<Contact> typeQuery = makeTypedQuery(); 299 TypedQuery<Contact> typeQuery = makeTypedQuery();
305 300
@@ -307,9 +302,9 @@ public class JPACrudTest { @@ -307,9 +302,9 @@ public class JPACrudTest {
307 expect(query.getSingleResult()).andReturn(10L); 302 expect(query.getSingleResult()).andReturn(10L);
308 303
309 expect(this.entityManager.createQuery("select this from Contact this", Contact.class)).andReturn(typeQuery); 304 expect(this.entityManager.createQuery("select this from Contact this", Contact.class)).andReturn(typeQuery);
310 - expect(this.entityManager.createQuery("SELECT COUNT(THIS) FROM CONTACT THIS")).andReturn(query); 305 + expect(this.entityManager.createQuery("select COUNT(this) from Contact this")).andReturn(query);
311 306
312 - replayAll(typeQuery, query, this.entityManager, paginationContext); 307 + replayAll(typeQuery, query, this.entityManager);
313 308
314 List<Contact> find = this.contactDAO.findAll(); 309 List<Contact> find = this.contactDAO.findAll();
315 310
@@ -324,10 +319,12 @@ public class JPACrudTest { @@ -324,10 +319,12 @@ public class JPACrudTest {
324 319
325 PaginationContext actualContext = PowerMock.createMock(PaginationContext.class); 320 PaginationContext actualContext = PowerMock.createMock(PaginationContext.class);
326 expect(actualContext.getPagination(Contact.class)).andReturn(null); 321 expect(actualContext.getPagination(Contact.class)).andReturn(null);
327 - @SuppressWarnings("unchecked") 322 + /*@SuppressWarnings("unchecked")
328 Instance<PaginationContext> paginationContext = PowerMock.createMock(Instance.class); 323 Instance<PaginationContext> paginationContext = PowerMock.createMock(Instance.class);
329 - expect(paginationContext.get()).andReturn(actualContext);  
330 - setInternalState(this.contactDAO, "paginationContext", paginationContext); 324 + expect(paginationContext.get()).andReturn(actualContext);*/
  325 +
  326 + PowerMock.mockStatic(Beans.class);
  327 + expect(Beans.getReference(PaginationContext.class)).andReturn(actualContext);
331 328
332 @SuppressWarnings("unchecked") 329 @SuppressWarnings("unchecked")
333 TypedQuery<Contact> typeQuery = EasyMock.createMock(TypedQuery.class); 330 TypedQuery<Contact> typeQuery = EasyMock.createMock(TypedQuery.class);
@@ -336,7 +333,7 @@ public class JPACrudTest { @@ -336,7 +333,7 @@ public class JPACrudTest {
336 Query query = EasyMock.createMock(Query.class); 333 Query query = EasyMock.createMock(Query.class);
337 expect(this.entityManager.createQuery("select this from Contact this", Contact.class)).andReturn(typeQuery); 334 expect(this.entityManager.createQuery("select this from Contact this", Contact.class)).andReturn(typeQuery);
338 335
339 - replayAll(typeQuery, query, this.entityManager, paginationContext); 336 + replayAll(typeQuery, query, this.entityManager);
340 337
341 try { 338 try {
342 this.contactDAO.findAll(); 339 this.contactDAO.findAll();
@@ -422,5 +419,40 @@ public class JPACrudTest { @@ -422,5 +419,40 @@ public class JPACrudTest {
422 } 419 }
423 verify(this.entityManager); 420 verify(this.entityManager);
424 } 421 }
  422 +
  423 + /**
  424 + * Test if the JPACrud will correctly obtain a new entity manager the first
  425 + * time it is called and the entity manager is still null.
  426 + */
  427 + @Test
  428 + public void testCreateEntityManagerIfNotExist(){
  429 + PowerMock.mockStatic(Beans.class);
  430 + expect(Beans.getReference(EntityManager.class)).andReturn(entityManager);
  431 + PowerMock.replay(Beans.class);
  432 +
  433 + setInternalState(this.contactDAO, EntityManager.class, (EntityManager)null);
  434 + Assert.assertNotNull(this.contactDAO.getEntityManager());
  435 + }
  436 +
  437 + @Test
  438 + public void testCriteriaQuery(){
  439 + Map<String, Object> configOverrides = new HashMap<String, Object>();
  440 + configOverrides.put("javax.persistence.provider", "org.hibernate.ejb.HibernatePersistence");
  441 + configOverrides.put("javax.persistence.jdbc.url", "jdbc:hsqldb:hsql:.");
  442 + configOverrides.put("hibernate.show_sql", "true");
  443 + configOverrides.put("hibernate.hbm2ddl.auto", "create-drop");
  444 +
  445 + EntityManagerFactory factory = Persistence.createEntityManagerFactory("pu1", configOverrides);
  446 + this.entityManager = factory.createEntityManager();
  447 + Whitebox.setInternalState(this.contactDAO, EntityManager.class, (EntityManager)null);
  448 +
  449 + PowerMock.mockStatic(Beans.class);
  450 + expect(Beans.getReference(EntityManager.class)).andReturn(entityManager);
  451 + PowerMock.replay(Beans.class);
  452 +
  453 + CriteriaQuery<Contact> query = this.contactDAO.createCriteriaQuery();
  454 + query.select( query.from(Contact.class) );
  455 +
  456 + }
425 457
426 } 458 }
impl/extension/jpa/src/test/java/br/gov/frameworkdemoiselle/transaction/JPATransactionTest.java
@@ -35,7 +35,6 @@ @@ -35,7 +35,6 @@
35 // * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. 35 // * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
36 // */ 36 // */
37 package br.gov.frameworkdemoiselle.transaction; 37 package br.gov.frameworkdemoiselle.transaction;
38 -import org.junit.Ignore;  
39 import static org.easymock.EasyMock.createMock; 38 import static org.easymock.EasyMock.createMock;
40 import static org.easymock.EasyMock.expect; 39 import static org.easymock.EasyMock.expect;
41 import static org.easymock.EasyMock.replay; 40 import static org.easymock.EasyMock.replay;
@@ -62,7 +61,6 @@ import br.gov.frameworkdemoiselle.internal.producer.EntityManagerProducer; @@ -62,7 +61,6 @@ import br.gov.frameworkdemoiselle.internal.producer.EntityManagerProducer;
62 * @author SERPRO 61 * @author SERPRO
63 * @see JPATransaction 62 * @see JPATransaction
64 */ 63 */
65 -@Ignore  
66 public class JPATransactionTest { 64 public class JPATransactionTest {
67 65
68 private JPATransaction tx; 66 private JPATransaction tx;
impl/extension/jpa/src/test/resources/META-INF/persistence.xml
@@ -42,7 +42,7 @@ @@ -42,7 +42,7 @@
42 <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver" /> 42 <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver" />
43 <property name="javax.persistence.jdbc.user" value="sa" /> 43 <property name="javax.persistence.jdbc.user" value="sa" />
44 <property name="javax.persistence.jdbc.password" value="" /> 44 <property name="javax.persistence.jdbc.password" value="" />
45 - <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:hsql://localhost:9001/contactlist" /> 45 + <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:hsql:." />
46 </properties> 46 </properties>
47 </persistence-unit> 47 </persistence-unit>
48 </persistence> 48 </persistence>
49 \ No newline at end of file 49 \ No newline at end of file
impl/extension/jpa/src/test/resources/persistence-empty-name.xml 0 → 100644
@@ -0,0 +1,48 @@ @@ -0,0 +1,48 @@
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<!--
  3 + Demoiselle Framework
  4 + Copyright (C) 2010 SERPRO
  5 + ============================================================================
  6 + This file is part of Demoiselle Framework.
  7 +
  8 + Demoiselle Framework is free software; you can redistribute it and/or
  9 + modify it under the terms of the GNU Lesser General Public License version 3
  10 + as published by the Free Software Foundation.
  11 +
  12 + This program is distributed in the hope that it will be useful,
  13 + but WITHOUT ANY WARRANTY; without even the implied warranty of
  14 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15 + GNU General Public License for more details.
  16 +
  17 + You should have received a copy of the GNU Lesser General Public License version 3
  18 + along with this program; if not, see <http://www.gnu.org/licenses/>
  19 + or write to the Free Software Foundation, Inc., 51 Franklin Street,
  20 + Fifth Floor, Boston, MA 02110-1301, USA.
  21 + ============================================================================
  22 + Este arquivo é parte do Framework Demoiselle.
  23 +
  24 + O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou
  25 + modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
  26 + do Software Livre (FSF).
  27 +
  28 + Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
  29 + GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
  30 + APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
  31 + para maiores detalhes.
  32 +
  33 + Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
  34 + "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/>
  35 + ou escreva para a Fundação do Software Livre (FSF) Inc.,
  36 + 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
  37 +-->
  38 +<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  39 + xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  40 + <persistence-unit name="" transaction-type="RESOURCE_LOCAL">
  41 + <properties>
  42 + <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver" />
  43 + <property name="javax.persistence.jdbc.user" value="sa" />
  44 + <property name="javax.persistence.jdbc.password" value="" />
  45 + <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:hsql://localhost:9001/contactlist" />
  46 + </properties>
  47 + </persistence-unit>
  48 +</persistence>
0 \ No newline at end of file 49 \ No newline at end of file