Commit 0a07b992585cc71c8aef4adfb71c07f69fff4913

Authored by Dancovich
1 parent b17f5cb6
Exists in master

-Melhorada cobertura de testes existentes

-Criada classe de teste para QueryProxy
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,46 @@ @@ -0,0 +1,46 @@
  1 +package br.gov.frameworkdemoiselle.domain;
  2 +
  3 +import java.io.Serializable;
  4 +
  5 +import javax.persistence.Entity;
  6 +import javax.persistence.GeneratedValue;
  7 +import javax.persistence.Id;
  8 +
  9 +/**
  10 + * Simle entity for test classes
  11 + * @author serpro
  12 + *
  13 + */
  14 +@Entity
  15 +public class Client implements Serializable {
  16 +
  17 + private static final long serialVersionUID = 1L;
  18 +
  19 + private Long id;
  20 +
  21 + private String name;
  22 +
  23 + @Id
  24 + @GeneratedValue
  25 + public Long getId() {
  26 + return id;
  27 + }
  28 +
  29 +
  30 + public void setId(Long id) {
  31 + this.id = id;
  32 + }
  33 +
  34 +
  35 + public String getName() {
  36 + return name;
  37 + }
  38 +
  39 +
  40 + public void setName(String name) {
  41 + this.name = name;
  42 + }
  43 +
  44 +
  45 +
  46 +}
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 {
impl/extension/jpa/src/test/java/br/gov/frameworkdemoiselle/internal/proxy/QueryProxyTest.java 0 → 100644
@@ -0,0 +1,148 @@ @@ -0,0 +1,148 @@
  1 +package br.gov.frameworkdemoiselle.internal.proxy;
  2 +
  3 +import java.util.Collections;
  4 +import java.util.HashMap;
  5 +import java.util.List;
  6 +import java.util.Map;
  7 +
  8 +import javax.persistence.EntityManager;
  9 +import javax.persistence.EntityManagerFactory;
  10 +import javax.persistence.Persistence;
  11 +import javax.persistence.Query;
  12 +
  13 +import org.easymock.EasyMock;
  14 +import org.junit.Assert;
  15 +import org.junit.Before;
  16 +import org.junit.Test;
  17 +import org.junit.runner.RunWith;
  18 +import org.powermock.api.easymock.PowerMock;
  19 +import org.powermock.core.classloader.annotations.PrepareForTest;
  20 +import org.powermock.modules.junit4.PowerMockRunner;
  21 +import org.powermock.reflect.Whitebox;
  22 +
  23 +import br.gov.frameworkdemoiselle.domain.Client;
  24 +import br.gov.frameworkdemoiselle.internal.producer.EntityManagerProducer;
  25 +import br.gov.frameworkdemoiselle.util.Beans;
  26 +
  27 +/**
  28 + * Test the proxied
  29 + * @author 81986912515
  30 + *
  31 + */
  32 +@RunWith(PowerMockRunner.class)
  33 +@PrepareForTest({Beans.class})
  34 +public class QueryProxyTest {
  35 +
  36 + private EntityManager manager;
  37 + private EntityManagerProducer producer;
  38 +
  39 + @Before
  40 + public void setUp(){
  41 +
  42 + Map<String, Object> configOverrides = new HashMap<String, Object>();
  43 + configOverrides.put("javax.persistence.provider", "org.hibernate.ejb.HibernatePersistence");
  44 + configOverrides.put("javax.persistence.jdbc.url", "jdbc:hsqldb:hsql:.");
  45 + configOverrides.put("hibernate.show_sql", "false");
  46 + configOverrides.put("hibernate.hbm2ddl.auto", "create-drop");
  47 +
  48 + EntityManagerFactory factory = Persistence.createEntityManagerFactory("pu1", configOverrides);
  49 + EntityManager delegate = factory.createEntityManager();
  50 +
  51 + Map<String, EntityManager> cache = Collections.synchronizedMap(new HashMap<String, EntityManager>());
  52 + cache.put("pu1", delegate);
  53 +
  54 + producer = new EntityManagerProducer();
  55 + Whitebox.setInternalState(producer, "cache", cache);
  56 +
  57 + PowerMock.mockStatic(Beans.class);
  58 + EasyMock.expect(Beans.getReference(EntityManagerProducer.class)).andReturn(producer).times(12);
  59 + PowerMock.replayAll();
  60 +
  61 + manager = new EntityManagerProxy("pu1");
  62 +
  63 + manager.getTransaction().begin();
  64 + manager.createQuery("delete from Client").executeUpdate();
  65 +
  66 + Client client = new Client();
  67 + client.setName("Cliente 1");
  68 + manager.persist(client);
  69 +
  70 + client = new Client();
  71 + client.setName("Cliente 2");
  72 + manager.persist(client);
  73 +
  74 + client = new Client();
  75 + client.setName("Cliente 3");
  76 + manager.persist(client);
  77 +
  78 + manager.flush();
  79 + manager.getTransaction().commit();
  80 + manager.clear();
  81 +
  82 + PowerMock.resetAll();
  83 + }
  84 +
  85 + private QueryProxy getQueryProxy(String jpql,Object... params){
  86 + Query q = manager.createQuery(jpql);
  87 + if (!(q instanceof QueryProxy)){
  88 + Assert.fail("Query não é instância de QueryProxy");
  89 + }
  90 +
  91 + if (params!=null){
  92 + int count = 1;
  93 + for (Object param : params){
  94 + q.setParameter(count++, param);
  95 + }
  96 + }
  97 +
  98 + return (QueryProxy)q;
  99 + }
  100 +
  101 + @Test
  102 + public void testResultList(){
  103 + EasyMock.expect(Beans.getReference(EntityManagerProducer.class)).andReturn(producer).times(2);
  104 + PowerMock.replay(Beans.class);
  105 +
  106 + List<?> retorno = getQueryProxy("select c from Client c").getResultList();
  107 + Assert.assertNotNull(retorno);
  108 + Assert.assertFalse(retorno.isEmpty());
  109 + }
  110 +
  111 + @Test
  112 + public void testSingleResult(){
  113 + EasyMock.expect(Beans.getReference(EntityManagerProducer.class)).andReturn(producer).times(2);
  114 + PowerMock.replay(Beans.class);
  115 +
  116 + Client retorno = (Client)getQueryProxy("select c from Client c where c.name=?1","Cliente 1").getSingleResult();
  117 + Assert.assertNotNull(retorno);
  118 + }
  119 +
  120 + @Test
  121 + public void testExecuteUpdate(){
  122 + EasyMock.expect(Beans.getReference(EntityManagerProducer.class)).andReturn(producer).times(4);
  123 + PowerMock.replay(Beans.class);
  124 +
  125 + manager.getTransaction().begin();
  126 + int linesAffected = getQueryProxy("update Client set name=?1 where name=?2","Novo Cliente","Cliente 1").executeUpdate();
  127 + manager.getTransaction().commit();
  128 + Assert.assertEquals(1, linesAffected);
  129 + }
  130 +
  131 + @Test
  132 + public void testPagination(){
  133 + EasyMock.expect(Beans.getReference(EntityManagerProducer.class)).andReturn(producer).times(4);
  134 + PowerMock.replay(Beans.class);
  135 +
  136 + QueryProxy proxy = getQueryProxy("select c from Client c");
  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 +}
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;
@@ -63,6 +62,8 @@ import javax.persistence.criteria.CriteriaQuery; @@ -63,6 +62,8 @@ import javax.persistence.criteria.CriteriaQuery;
63 import javax.persistence.criteria.Predicate; 62 import javax.persistence.criteria.Predicate;
64 import javax.persistence.criteria.Root; 63 import javax.persistence.criteria.Root;
65 64
  65 +import junit.framework.Assert;
  66 +
66 import org.easymock.EasyMock; 67 import org.easymock.EasyMock;
67 import org.junit.Before; 68 import org.junit.Before;
68 import org.junit.Test; 69 import org.junit.Test;
@@ -76,10 +77,11 @@ import br.gov.frameworkdemoiselle.configuration.Configuration; @@ -76,10 +77,11 @@ import br.gov.frameworkdemoiselle.configuration.Configuration;
76 import br.gov.frameworkdemoiselle.internal.implementation.PaginationImpl; 77 import br.gov.frameworkdemoiselle.internal.implementation.PaginationImpl;
77 import br.gov.frameworkdemoiselle.pagination.Pagination; 78 import br.gov.frameworkdemoiselle.pagination.Pagination;
78 import br.gov.frameworkdemoiselle.pagination.PaginationContext; 79 import br.gov.frameworkdemoiselle.pagination.PaginationContext;
  80 +import br.gov.frameworkdemoiselle.util.Beans;
79 import br.gov.frameworkdemoiselle.util.ResourceBundle; 81 import br.gov.frameworkdemoiselle.util.ResourceBundle;
80 -@Ignore 82 +
81 @RunWith(PowerMockRunner.class) 83 @RunWith(PowerMockRunner.class)
82 -@PrepareForTest({ ResourceBundle.class, Instance.class }) 84 +@PrepareForTest({ ResourceBundle.class, Instance.class, Beans.class })
83 public class JPACrudTest { 85 public class JPACrudTest {
84 86
85 private EntityManager entityManager; 87 private EntityManager entityManager;
@@ -144,12 +146,7 @@ public class JPACrudTest { @@ -144,12 +146,7 @@ public class JPACrudTest {
144 146
145 Pagination pagination = new PaginationImpl(); 147 Pagination pagination = new PaginationImpl();
146 pagination.setPageSize(10); 148 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); 149 + setInternalState(this.contactDAO, "pagination", pagination);
153 150
154 TypedQuery<Contact> typeQuery = makeTypedQuery(); 151 TypedQuery<Contact> typeQuery = makeTypedQuery();
155 152
@@ -157,9 +154,10 @@ public class JPACrudTest { @@ -157,9 +154,10 @@ public class JPACrudTest {
157 expect(query.getSingleResult()).andReturn(10L); 154 expect(query.getSingleResult()).andReturn(10L);
158 155
159 expect(this.entityManager.createQuery("select this from Contact this", Contact.class)).andReturn(typeQuery); 156 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); 157 + //expect(this.entityManager.createQuery("SELECT COUNT(THIS) FROM CONTACT THIS")).andReturn(query);
  158 + expect(this.entityManager.createQuery("select COUNT(this) from Contact this")).andReturn(query);
161 159
162 - replayAll(typeQuery, query, this.entityManager, paginationContext); 160 + replayAll(typeQuery, query, this.entityManager);
163 161
164 List<Contact> find = this.contactDAO.findAll(); 162 List<Contact> find = this.contactDAO.findAll();
165 163
@@ -173,12 +171,7 @@ public class JPACrudTest { @@ -173,12 +171,7 @@ public class JPACrudTest {
173 public void testFailCountAll() { 171 public void testFailCountAll() {
174 172
175 Pagination pagination = new PaginationImpl(); 173 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); 174 + setInternalState(this.contactDAO, "pagination", pagination);
182 175
183 TypedQuery<Contact> typeQuery = makeTypedQuery(); 176 TypedQuery<Contact> typeQuery = makeTypedQuery();
184 177
@@ -186,9 +179,9 @@ public class JPACrudTest { @@ -186,9 +179,9 @@ public class JPACrudTest {
186 179
187 expect(query.getSingleResult()).andThrow(new DemoiselleException("")); 180 expect(query.getSingleResult()).andThrow(new DemoiselleException(""));
188 expect(this.entityManager.createQuery("select this from Contact this", Contact.class)).andReturn(typeQuery); 181 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); 182 + expect(this.entityManager.createQuery("select COUNT(this) from Contact this")).andReturn(query);
190 183
191 - replayAll(query, this.entityManager, paginationContext); 184 + replayAll(query, this.entityManager);
192 185
193 try { 186 try {
194 this.contactDAO.findAll(); 187 this.contactDAO.findAll();
@@ -294,12 +287,7 @@ public class JPACrudTest { @@ -294,12 +287,7 @@ public class JPACrudTest {
294 287
295 Pagination pagination = new PaginationImpl(); 288 Pagination pagination = new PaginationImpl();
296 pagination.setPageSize(10); 289 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); 290 + setInternalState(this.contactDAO, "pagination", pagination);
303 291
304 TypedQuery<Contact> typeQuery = makeTypedQuery(); 292 TypedQuery<Contact> typeQuery = makeTypedQuery();
305 293
@@ -307,9 +295,9 @@ public class JPACrudTest { @@ -307,9 +295,9 @@ public class JPACrudTest {
307 expect(query.getSingleResult()).andReturn(10L); 295 expect(query.getSingleResult()).andReturn(10L);
308 296
309 expect(this.entityManager.createQuery("select this from Contact this", Contact.class)).andReturn(typeQuery); 297 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); 298 + expect(this.entityManager.createQuery("select COUNT(this) from Contact this")).andReturn(query);
311 299
312 - replayAll(typeQuery, query, this.entityManager, paginationContext); 300 + replayAll(typeQuery, query, this.entityManager);
313 301
314 List<Contact> find = this.contactDAO.findAll(); 302 List<Contact> find = this.contactDAO.findAll();
315 303
@@ -324,10 +312,12 @@ public class JPACrudTest { @@ -324,10 +312,12 @@ public class JPACrudTest {
324 312
325 PaginationContext actualContext = PowerMock.createMock(PaginationContext.class); 313 PaginationContext actualContext = PowerMock.createMock(PaginationContext.class);
326 expect(actualContext.getPagination(Contact.class)).andReturn(null); 314 expect(actualContext.getPagination(Contact.class)).andReturn(null);
327 - @SuppressWarnings("unchecked") 315 + /*@SuppressWarnings("unchecked")
328 Instance<PaginationContext> paginationContext = PowerMock.createMock(Instance.class); 316 Instance<PaginationContext> paginationContext = PowerMock.createMock(Instance.class);
329 - expect(paginationContext.get()).andReturn(actualContext);  
330 - setInternalState(this.contactDAO, "paginationContext", paginationContext); 317 + expect(paginationContext.get()).andReturn(actualContext);*/
  318 +
  319 + PowerMock.mockStatic(Beans.class);
  320 + expect(Beans.getReference(PaginationContext.class)).andReturn(actualContext);
331 321
332 @SuppressWarnings("unchecked") 322 @SuppressWarnings("unchecked")
333 TypedQuery<Contact> typeQuery = EasyMock.createMock(TypedQuery.class); 323 TypedQuery<Contact> typeQuery = EasyMock.createMock(TypedQuery.class);
@@ -336,7 +326,7 @@ public class JPACrudTest { @@ -336,7 +326,7 @@ public class JPACrudTest {
336 Query query = EasyMock.createMock(Query.class); 326 Query query = EasyMock.createMock(Query.class);
337 expect(this.entityManager.createQuery("select this from Contact this", Contact.class)).andReturn(typeQuery); 327 expect(this.entityManager.createQuery("select this from Contact this", Contact.class)).andReturn(typeQuery);
338 328
339 - replayAll(typeQuery, query, this.entityManager, paginationContext); 329 + replayAll(typeQuery, query, this.entityManager);
340 330
341 try { 331 try {
342 this.contactDAO.findAll(); 332 this.contactDAO.findAll();
@@ -422,5 +412,19 @@ public class JPACrudTest { @@ -422,5 +412,19 @@ public class JPACrudTest {
422 } 412 }
423 verify(this.entityManager); 413 verify(this.entityManager);
424 } 414 }
  415 +
  416 + /**
  417 + * Test if the JPACrud will correctly obtain a new entity manager the first
  418 + * time it is called and the entity manager is still null.
  419 + */
  420 + @Test
  421 + public void testCreateEntityManagerIfNotExist(){
  422 + PowerMock.mockStatic(Beans.class);
  423 + expect(Beans.getReference(EntityManager.class)).andReturn(entityManager);
  424 + PowerMock.replay(Beans.class);
  425 +
  426 + setInternalState(this.contactDAO, EntityManager.class, (EntityManager)null);
  427 + Assert.assertNotNull(this.contactDAO.getEntityManager());
  428 + }
425 429
426 } 430 }
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