Commit dcc1ddee15bbffb6f1bf87ff9bfea478fd0e2d45

Authored by Dancovich
1 parent 54b02e18
Exists in master

Aumentando cobertura de testes.

impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java
@@ -79,9 +79,11 @@ public class ConfigurationLoader implements Serializable { @@ -79,9 +79,11 @@ public class ConfigurationLoader implements Serializable {
79 79
80 private static final long serialVersionUID = 1L; 80 private static final long serialVersionUID = 1L;
81 81
82 - private static ResourceBundle bundle; 82 + private ResourceBundle bundle;
83 83
84 - private static Logger logger; 84 + private Logger logger;
  85 +
  86 + private CoreBootstrap bootstrap;
85 87
86 /** 88 /**
87 * Loads a config class filling it with the corresponding values. 89 * Loads a config class filling it with the corresponding values.
@@ -92,9 +94,8 @@ public class ConfigurationLoader implements Serializable { @@ -92,9 +94,8 @@ public class ConfigurationLoader implements Serializable {
92 */ 94 */
93 public void load(Object object) throws ConfigurationException { 95 public void load(Object object) throws ConfigurationException {
94 Class<?> config = object.getClass(); 96 Class<?> config = object.getClass();
95 - CoreBootstrap bootstrap = Beans.getReference(CoreBootstrap.class);  
96 97
97 - if (!bootstrap.isAnnotatedType(config)) { 98 + if (!getBootstrap().isAnnotatedType(config)) {
98 config = config.getSuperclass(); 99 config = config.getSuperclass();
99 getLogger().debug(getBundle().getString("proxy-detected", config, config.getClass().getSuperclass())); 100 getLogger().debug(getBundle().getString("proxy-detected", config, config.getClass().getSuperclass()));
100 } 101 }
@@ -429,7 +430,7 @@ public class ConfigurationLoader implements Serializable { @@ -429,7 +430,7 @@ public class ConfigurationLoader implements Serializable {
429 return classLoader != null ? classLoader.getResource(resource) : null; 430 return classLoader != null ? classLoader.getResource(resource) : null;
430 } 431 }
431 432
432 - private static ResourceBundle getBundle() { 433 + private ResourceBundle getBundle() {
433 if (bundle == null) { 434 if (bundle == null) {
434 bundle = ResourceBundleProducer.create("demoiselle-core-bundle"); 435 bundle = ResourceBundleProducer.create("demoiselle-core-bundle");
435 } 436 }
@@ -437,11 +438,19 @@ public class ConfigurationLoader implements Serializable { @@ -437,11 +438,19 @@ public class ConfigurationLoader implements Serializable {
437 return bundle; 438 return bundle;
438 } 439 }
439 440
440 - private static Logger getLogger() { 441 + private Logger getLogger() {
441 if (logger == null) { 442 if (logger == null) {
442 logger = LoggerProducer.create(ConfigurationLoader.class); 443 logger = LoggerProducer.create(ConfigurationLoader.class);
443 } 444 }
444 445
445 return logger; 446 return logger;
446 } 447 }
  448 +
  449 + private CoreBootstrap getBootstrap(){
  450 + if (bootstrap == null){
  451 + bootstrap = Beans.getReference(CoreBootstrap.class);
  452 + }
  453 +
  454 + return bootstrap;
  455 + }
447 } 456 }
impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/EntityManagerFactoryProducer.java
@@ -110,7 +110,14 @@ public class EntityManagerFactoryProducer implements Serializable { @@ -110,7 +110,14 @@ public class EntityManagerFactoryProducer implements Serializable {
110 public void loadPersistenceUnits() { 110 public void loadPersistenceUnits() {
111 ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); 111 ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
112 for (String persistenceUnit : loadPersistenceUnitFromClassloader(contextClassLoader)) { 112 for (String persistenceUnit : loadPersistenceUnitFromClassloader(contextClassLoader)) {
113 - create(persistenceUnit); 113 +
  114 + try{
  115 + create(persistenceUnit);
  116 + }
  117 + catch(Throwable t){
  118 + throw new DemoiselleException(t);
  119 + }
  120 +
114 logger.debug(bundle.getString("persistence-unit-name-found", persistenceUnit)); 121 logger.debug(bundle.getString("persistence-unit-name-found", persistenceUnit));
115 } 122 }
116 } 123 }
impl/extension/jpa/src/test/java/br/gov/frameworkdemoiselle/internal/configuration/EntityManagerConfigTest.java
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 -//package br.gov.frameworkdemoiselle.internal.configuration;  
38 -//import org.junit.Ignore;  
39 -//import static org.easymock.EasyMock.expect;  
40 -//import static org.junit.Assert.assertEquals;  
41 -//import static org.powermock.api.easymock.PowerMock.mockStatic;  
42 -//  
43 -//import java.util.Locale;  
44 -//  
45 -//import org.junit.After;  
46 -//import org.junit.Before;  
47 -//import org.junit.Test;  
48 -//import org.junit.runner.RunWith;  
49 -//import org.powermock.api.easymock.PowerMock;  
50 -//import org.powermock.core.classloader.annotations.PrepareForTest;  
51 -//import org.powermock.modules.junit4.PowerMockRunner;  
52 -//import org.powermock.reflect.Whitebox;  
53 -//import org.slf4j.Logger;  
54 -//  
55 -//import br.gov.frameworkdemoiselle.internal.bootstrap.CoreBootstrap;  
56 -//import br.gov.frameworkdemoiselle.util.ResourceBundle;  
57 -//  
58 -///**  
59 -// * @author e-saito  
60 -// */  
61 -///**  
62 -// * @author 80342167553  
63 -// */  
64 -//@Ignore  
65 -//@RunWith(PowerMockRunner.class)  
66 -//@PrepareForTest(CoreBootstrap.class)  
67 -//public class EntityManagerConfigTest {  
68 -//  
69 -// private EntityManagerConfig config = new EntityManagerConfig();  
70 -//  
71 -// @Before  
72 -// public void setUp() throws Exception {  
73 -// Logger logger = PowerMock.createMock(Logger.class);  
74 -// ResourceBundle bundle = new ResourceBundle("demoiselle-core-bundle", Locale.getDefault());  
75 -//  
76 -// ConfigurationLoader configurationLoader = new ConfigurationLoader();  
77 -//  
78 -// Whitebox.setInternalState(configurationLoader, "bundle", bundle);  
79 -// Whitebox.setInternalState(configurationLoader, "logger", logger);  
80 -//  
81 -// mockStatic(CoreBootstrap.class);  
82 -// expect(CoreBootstrap.isAnnotatedType(config.getClass())).andReturn(true);  
83 -// PowerMock.replay(CoreBootstrap.class);  
84 -//  
85 -// configurationLoader.load(config);  
86 -// }  
87 -//  
88 -// @After  
89 -// public void tearDown() throws Exception {  
90 -// config = null;  
91 -// }  
92 -//  
93 -// /**  
94 -// * Test method for  
95 -// * {@link br.gov.frameworkdemoiselle.internal.configuration.EntityManagerConfig#getPersistenceUnitName()}.  
96 -// */  
97 -// @Test  
98 -// public void testGetPersistenceUnitName() {  
99 -// assertEquals("PersistenceUnitName", config.getPersistenceUnitName());  
100 -// }  
101 -//} 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 +package br.gov.frameworkdemoiselle.internal.configuration;
  38 +import static org.easymock.EasyMock.createMock;
  39 +import static org.easymock.EasyMock.expect;
  40 +import static org.junit.Assert.assertEquals;
  41 +
  42 +import java.util.Locale;
  43 +
  44 +import org.junit.After;
  45 +import org.junit.Before;
  46 +import org.junit.Test;
  47 +import org.junit.runner.RunWith;
  48 +import org.powermock.api.easymock.PowerMock;
  49 +import org.powermock.core.classloader.annotations.PrepareForTest;
  50 +import org.powermock.modules.junit4.PowerMockRunner;
  51 +import org.powermock.reflect.Whitebox;
  52 +import org.slf4j.Logger;
  53 +import org.slf4j.LoggerFactory;
  54 +
  55 +import br.gov.frameworkdemoiselle.internal.bootstrap.CoreBootstrap;
  56 +import br.gov.frameworkdemoiselle.util.ResourceBundle;
  57 +
  58 +/**
  59 + * @author e-saito
  60 + */
  61 +/**
  62 + * @author 80342167553
  63 + */
  64 +@RunWith(PowerMockRunner.class)
  65 +@PrepareForTest(CoreBootstrap.class)
  66 +public class EntityManagerConfigTest {
  67 +
  68 + private EntityManagerConfig config = new EntityManagerConfig();
  69 +
  70 + @Before
  71 + public void setUp() throws Exception {
  72 + Logger logger = LoggerFactory.getLogger(this.getClass());
  73 + ResourceBundle bundle = new ResourceBundle("demoiselle-core-bundle", Locale.getDefault());
  74 +
  75 + ConfigurationLoader configurationLoader = new ConfigurationLoader();
  76 +
  77 + Whitebox.setInternalState(configurationLoader, "bundle", bundle);
  78 + Whitebox.setInternalState(configurationLoader, "logger", logger);
  79 +
  80 + CoreBootstrap bootstrap = createMock(CoreBootstrap.class);
  81 + expect(bootstrap.isAnnotatedType(config.getClass())).andReturn(true);
  82 + PowerMock.replay(bootstrap);
  83 +
  84 + Whitebox.setInternalState(configurationLoader, "bootstrap", bootstrap);
  85 +
  86 + configurationLoader.load(config);
  87 + }
  88 +
  89 + @After
  90 + public void tearDown() throws Exception {
  91 + config = null;
  92 + }
  93 +
  94 + /**
  95 + * Test method for
  96 + * {@link br.gov.frameworkdemoiselle.internal.configuration.EntityManagerConfig#getPersistenceUnitName()}.
  97 + */
  98 + @Test
  99 + public void testGetPersistenceUnitName() {
  100 + assertEquals("PersistenceUnitName", config.getPersistenceUnitName());
  101 + }
  102 +}
impl/extension/jpa/src/test/java/br/gov/frameworkdemoiselle/internal/producer/EntityManagerFactoryProducerTest.java
1 package br.gov.frameworkdemoiselle.internal.producer; 1 package br.gov.frameworkdemoiselle.internal.producer;
2 -import org.junit.Ignore;  
3 import static org.easymock.EasyMock.createMock; 2 import static org.easymock.EasyMock.createMock;
4 import static org.easymock.EasyMock.expect; 3 import static org.easymock.EasyMock.expect;
5 import static org.easymock.EasyMock.verify; 4 import static org.easymock.EasyMock.verify;
@@ -26,14 +25,14 @@ import org.slf4j.Logger; @@ -26,14 +25,14 @@ import org.slf4j.Logger;
26 25
27 import br.gov.frameworkdemoiselle.DemoiselleException; 26 import br.gov.frameworkdemoiselle.DemoiselleException;
28 import br.gov.frameworkdemoiselle.util.ResourceBundle; 27 import br.gov.frameworkdemoiselle.util.ResourceBundle;
29 -@Ignore 28 +
30 @RunWith(PowerMockRunner.class) 29 @RunWith(PowerMockRunner.class)
31 @PrepareForTest(Persistence.class) 30 @PrepareForTest(Persistence.class)
32 public class EntityManagerFactoryProducerTest { 31 public class EntityManagerFactoryProducerTest {
33 32
34 private EntityManagerFactory emFactory; 33 private EntityManagerFactory emFactory;
35 private EntityManagerFactoryProducer producer; 34 private EntityManagerFactoryProducer producer;
36 - private Map<String, EntityManagerFactory> cache; 35 + private Map<ClassLoader, Map<String, EntityManagerFactory>> cache;
37 private Logger logger; 36 private Logger logger;
38 private ResourceBundle bundle; 37 private ResourceBundle bundle;
39 38
@@ -42,8 +41,8 @@ public class EntityManagerFactoryProducerTest { @@ -42,8 +41,8 @@ public class EntityManagerFactoryProducerTest {
42 logger = createMock(Logger.class); 41 logger = createMock(Logger.class);
43 bundle = ResourceBundleProducer.create("demoiselle-jpa-bundle", Locale.getDefault()); 42 bundle = ResourceBundleProducer.create("demoiselle-jpa-bundle", Locale.getDefault());
44 producer = new EntityManagerFactoryProducer(); 43 producer = new EntityManagerFactoryProducer();
45 - cache = Collections.synchronizedMap(new HashMap<String, EntityManagerFactory>());  
46 - setInternalState(producer, Map.class, cache); 44 + cache = Collections.synchronizedMap(new HashMap<ClassLoader, Map<String, EntityManagerFactory>>());
  45 + setInternalState(producer, "factoryCache", cache);
47 setInternalState(producer, Logger.class, logger); 46 setInternalState(producer, Logger.class, logger);
48 setInternalState(producer, ResourceBundle.class, bundle); 47 setInternalState(producer, ResourceBundle.class, bundle);
49 emFactory = createMock(EntityManagerFactory.class); 48 emFactory = createMock(EntityManagerFactory.class);
@@ -51,7 +50,11 @@ public class EntityManagerFactoryProducerTest { @@ -51,7 +50,11 @@ public class EntityManagerFactoryProducerTest {
51 50
52 @Test 51 @Test
53 public void testCreateWithUnitPersistenceExisting() { 52 public void testCreateWithUnitPersistenceExisting() {
54 - cache.put("pu1", emFactory); 53 + ClassLoader cl = this.getClass().getClassLoader();
  54 + HashMap<String, EntityManagerFactory> emEntry = new HashMap<String, EntityManagerFactory>();
  55 + emEntry.put("pu1", emFactory);
  56 + cache.put(cl,emEntry);
  57 +
55 Assert.assertEquals(emFactory, producer.create("pu1")); 58 Assert.assertEquals(emFactory, producer.create("pu1"));
56 } 59 }
57 60
@@ -73,7 +76,12 @@ public class EntityManagerFactoryProducerTest { @@ -73,7 +76,12 @@ public class EntityManagerFactoryProducerTest {
73 replay(Persistence.class); 76 replay(Persistence.class);
74 77
75 producer.loadPersistenceUnits(); 78 producer.loadPersistenceUnits();
76 - Assert.assertEquals(emFactory, cache.get("pu1")); 79 +
  80 + ClassLoader cl = this.getClass().getClassLoader();
  81 + Map<String, EntityManagerFactory> internalCache = cache.get(cl);
  82 +
  83 + Assert.assertNotNull(internalCache);
  84 + Assert.assertEquals(emFactory, internalCache.get("pu1"));
77 } 85 }
78 86
79 @Test 87 @Test
@@ -88,12 +96,25 @@ public class EntityManagerFactoryProducerTest { @@ -88,12 +96,25 @@ public class EntityManagerFactoryProducerTest {
88 96
89 @Test 97 @Test
90 public void testGetCache() { 98 public void testGetCache() {
91 - Assert.assertEquals(cache, producer.getCache()); 99 + ClassLoader cl = this.getClass().getClassLoader();
  100 + HashMap<String, EntityManagerFactory> emEntry = new HashMap<String, EntityManagerFactory>();
  101 + emEntry.put("pu1", emFactory);
  102 + cache.put(cl, emEntry);
  103 +
  104 + mockStatic(Persistence.class);
  105 + expect(Persistence.createEntityManagerFactory("pu1")).andReturn(emFactory);
  106 + replay(Persistence.class);
  107 +
  108 + Assert.assertEquals(cache.get(this.getClass().getClassLoader()), producer.getCache());
92 } 109 }
93 110
94 @Test 111 @Test
95 public void testClose() { 112 public void testClose() {
96 - cache.put("pu1", emFactory); 113 + ClassLoader cl = this.getClass().getClassLoader();
  114 + HashMap<String, EntityManagerFactory> emEntry = new HashMap<String, EntityManagerFactory>();
  115 + emEntry.put("pu1", emFactory);
  116 + cache.put(cl, emEntry);
  117 +
97 emFactory.close(); 118 emFactory.close();
98 replay(emFactory); 119 replay(emFactory);
99 producer.close(); 120 producer.close();