From dcc1ddee15bbffb6f1bf87ff9bfea478fd0e2d45 Mon Sep 17 00:00:00 2001 From: Dancovich Date: Wed, 3 Oct 2012 16:26:46 -0300 Subject: [PATCH] Aumentando cobertura de testes. --- impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java | 21 +++++++++++++++------ impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/EntityManagerFactoryProducer.java | 9 ++++++++- impl/extension/jpa/src/test/java/br/gov/frameworkdemoiselle/internal/configuration/EntityManagerConfigTest.java | 203 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------------------------------------------------------------------- impl/extension/jpa/src/test/java/br/gov/frameworkdemoiselle/internal/producer/EntityManagerFactoryProducerTest.java | 39 ++++++++++++++++++++++++++++++--------- 4 files changed, 155 insertions(+), 117 deletions(-) diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java index af83ece..337118b 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java @@ -79,9 +79,11 @@ public class ConfigurationLoader implements Serializable { private static final long serialVersionUID = 1L; - private static ResourceBundle bundle; + private ResourceBundle bundle; - private static Logger logger; + private Logger logger; + + private CoreBootstrap bootstrap; /** * Loads a config class filling it with the corresponding values. @@ -92,9 +94,8 @@ public class ConfigurationLoader implements Serializable { */ public void load(Object object) throws ConfigurationException { Class config = object.getClass(); - CoreBootstrap bootstrap = Beans.getReference(CoreBootstrap.class); - if (!bootstrap.isAnnotatedType(config)) { + if (!getBootstrap().isAnnotatedType(config)) { config = config.getSuperclass(); getLogger().debug(getBundle().getString("proxy-detected", config, config.getClass().getSuperclass())); } @@ -429,7 +430,7 @@ public class ConfigurationLoader implements Serializable { return classLoader != null ? classLoader.getResource(resource) : null; } - private static ResourceBundle getBundle() { + private ResourceBundle getBundle() { if (bundle == null) { bundle = ResourceBundleProducer.create("demoiselle-core-bundle"); } @@ -437,11 +438,19 @@ public class ConfigurationLoader implements Serializable { return bundle; } - private static Logger getLogger() { + private Logger getLogger() { if (logger == null) { logger = LoggerProducer.create(ConfigurationLoader.class); } return logger; } + + private CoreBootstrap getBootstrap(){ + if (bootstrap == null){ + bootstrap = Beans.getReference(CoreBootstrap.class); + } + + return bootstrap; + } } diff --git a/impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/EntityManagerFactoryProducer.java b/impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/EntityManagerFactoryProducer.java index 14937b5..ec33cfe 100644 --- a/impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/EntityManagerFactoryProducer.java +++ b/impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/EntityManagerFactoryProducer.java @@ -110,7 +110,14 @@ public class EntityManagerFactoryProducer implements Serializable { public void loadPersistenceUnits() { ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); for (String persistenceUnit : loadPersistenceUnitFromClassloader(contextClassLoader)) { - create(persistenceUnit); + + try{ + create(persistenceUnit); + } + catch(Throwable t){ + throw new DemoiselleException(t); + } + logger.debug(bundle.getString("persistence-unit-name-found", persistenceUnit)); } } diff --git a/impl/extension/jpa/src/test/java/br/gov/frameworkdemoiselle/internal/configuration/EntityManagerConfigTest.java b/impl/extension/jpa/src/test/java/br/gov/frameworkdemoiselle/internal/configuration/EntityManagerConfigTest.java index 59123ee..d280e23 100644 --- a/impl/extension/jpa/src/test/java/br/gov/frameworkdemoiselle/internal/configuration/EntityManagerConfigTest.java +++ b/impl/extension/jpa/src/test/java/br/gov/frameworkdemoiselle/internal/configuration/EntityManagerConfigTest.java @@ -1,101 +1,102 @@ -///* -// * Demoiselle Framework -// * Copyright (C) 2010 SERPRO -// * ---------------------------------------------------------------------------- -// * This file is part of Demoiselle Framework. -// * -// * Demoiselle Framework is free software; you can redistribute it and/or -// * modify it under the terms of the GNU Lesser General Public License version 3 -// * as published by the Free Software Foundation. -// * -// * This program is distributed in the hope that it will be useful, -// * but WITHOUT ANY WARRANTY; without even the implied warranty of -// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// * GNU General Public License for more details. -// * -// * You should have received a copy of the GNU Lesser General Public License version 3 -// * along with this program; if not, see -// * or write to the Free Software Foundation, Inc., 51 Franklin Street, -// * Fifth Floor, Boston, MA 02110-1301, USA. -// * ---------------------------------------------------------------------------- -// * Este arquivo é parte do Framework Demoiselle. -// * -// * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou -// * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação -// * do Software Livre (FSF). -// * -// * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA -// * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou -// * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português -// * para maiores detalhes. -// * -// * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título -// * "LICENCA.txt", junto com esse programa. Se não, acesse -// * ou escreva para a Fundação do Software Livre (FSF) Inc., -// * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. -// */ -//package br.gov.frameworkdemoiselle.internal.configuration; -//import org.junit.Ignore; -//import static org.easymock.EasyMock.expect; -//import static org.junit.Assert.assertEquals; -//import static org.powermock.api.easymock.PowerMock.mockStatic; -// -//import java.util.Locale; -// -//import org.junit.After; -//import org.junit.Before; -//import org.junit.Test; -//import org.junit.runner.RunWith; -//import org.powermock.api.easymock.PowerMock; -//import org.powermock.core.classloader.annotations.PrepareForTest; -//import org.powermock.modules.junit4.PowerMockRunner; -//import org.powermock.reflect.Whitebox; -//import org.slf4j.Logger; -// -//import br.gov.frameworkdemoiselle.internal.bootstrap.CoreBootstrap; -//import br.gov.frameworkdemoiselle.util.ResourceBundle; -// -///** -// * @author e-saito -// */ -///** -// * @author 80342167553 -// */ -//@Ignore -//@RunWith(PowerMockRunner.class) -//@PrepareForTest(CoreBootstrap.class) -//public class EntityManagerConfigTest { -// -// private EntityManagerConfig config = new EntityManagerConfig(); -// -// @Before -// public void setUp() throws Exception { -// Logger logger = PowerMock.createMock(Logger.class); -// ResourceBundle bundle = new ResourceBundle("demoiselle-core-bundle", Locale.getDefault()); -// -// ConfigurationLoader configurationLoader = new ConfigurationLoader(); -// -// Whitebox.setInternalState(configurationLoader, "bundle", bundle); -// Whitebox.setInternalState(configurationLoader, "logger", logger); -// -// mockStatic(CoreBootstrap.class); -// expect(CoreBootstrap.isAnnotatedType(config.getClass())).andReturn(true); -// PowerMock.replay(CoreBootstrap.class); -// -// configurationLoader.load(config); -// } -// -// @After -// public void tearDown() throws Exception { -// config = null; -// } -// -// /** -// * Test method for -// * {@link br.gov.frameworkdemoiselle.internal.configuration.EntityManagerConfig#getPersistenceUnitName()}. -// */ -// @Test -// public void testGetPersistenceUnitName() { -// assertEquals("PersistenceUnitName", config.getPersistenceUnitName()); -// } -//} +/* + * Demoiselle Framework + * Copyright (C) 2010 SERPRO + * ---------------------------------------------------------------------------- + * This file is part of Demoiselle Framework. + * + * Demoiselle Framework is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License version 3 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License version 3 + * along with this program; if not, see + * or write to the Free Software Foundation, Inc., 51 Franklin Street, + * Fifth Floor, Boston, MA 02110-1301, USA. + * ---------------------------------------------------------------------------- + * Este arquivo é parte do Framework Demoiselle. + * + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação + * do Software Livre (FSF). + * + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português + * para maiores detalhes. + * + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título + * "LICENCA.txt", junto com esse programa. Se não, acesse + * ou escreva para a Fundação do Software Livre (FSF) Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. + */ +package br.gov.frameworkdemoiselle.internal.configuration; +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.expect; +import static org.junit.Assert.assertEquals; + +import java.util.Locale; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.api.easymock.PowerMock; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.reflect.Whitebox; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import br.gov.frameworkdemoiselle.internal.bootstrap.CoreBootstrap; +import br.gov.frameworkdemoiselle.util.ResourceBundle; + +/** + * @author e-saito + */ +/** + * @author 80342167553 + */ +@RunWith(PowerMockRunner.class) +@PrepareForTest(CoreBootstrap.class) +public class EntityManagerConfigTest { + + private EntityManagerConfig config = new EntityManagerConfig(); + + @Before + public void setUp() throws Exception { + Logger logger = LoggerFactory.getLogger(this.getClass()); + ResourceBundle bundle = new ResourceBundle("demoiselle-core-bundle", Locale.getDefault()); + + ConfigurationLoader configurationLoader = new ConfigurationLoader(); + + Whitebox.setInternalState(configurationLoader, "bundle", bundle); + Whitebox.setInternalState(configurationLoader, "logger", logger); + + CoreBootstrap bootstrap = createMock(CoreBootstrap.class); + expect(bootstrap.isAnnotatedType(config.getClass())).andReturn(true); + PowerMock.replay(bootstrap); + + Whitebox.setInternalState(configurationLoader, "bootstrap", bootstrap); + + configurationLoader.load(config); + } + + @After + public void tearDown() throws Exception { + config = null; + } + + /** + * Test method for + * {@link br.gov.frameworkdemoiselle.internal.configuration.EntityManagerConfig#getPersistenceUnitName()}. + */ + @Test + public void testGetPersistenceUnitName() { + assertEquals("PersistenceUnitName", config.getPersistenceUnitName()); + } +} diff --git a/impl/extension/jpa/src/test/java/br/gov/frameworkdemoiselle/internal/producer/EntityManagerFactoryProducerTest.java b/impl/extension/jpa/src/test/java/br/gov/frameworkdemoiselle/internal/producer/EntityManagerFactoryProducerTest.java index a94067e..049c76e 100644 --- a/impl/extension/jpa/src/test/java/br/gov/frameworkdemoiselle/internal/producer/EntityManagerFactoryProducerTest.java +++ b/impl/extension/jpa/src/test/java/br/gov/frameworkdemoiselle/internal/producer/EntityManagerFactoryProducerTest.java @@ -1,5 +1,4 @@ package br.gov.frameworkdemoiselle.internal.producer; -import org.junit.Ignore; import static org.easymock.EasyMock.createMock; import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.verify; @@ -26,14 +25,14 @@ import org.slf4j.Logger; import br.gov.frameworkdemoiselle.DemoiselleException; import br.gov.frameworkdemoiselle.util.ResourceBundle; -@Ignore + @RunWith(PowerMockRunner.class) @PrepareForTest(Persistence.class) public class EntityManagerFactoryProducerTest { private EntityManagerFactory emFactory; private EntityManagerFactoryProducer producer; - private Map cache; + private Map> cache; private Logger logger; private ResourceBundle bundle; @@ -42,8 +41,8 @@ public class EntityManagerFactoryProducerTest { logger = createMock(Logger.class); bundle = ResourceBundleProducer.create("demoiselle-jpa-bundle", Locale.getDefault()); producer = new EntityManagerFactoryProducer(); - cache = Collections.synchronizedMap(new HashMap()); - setInternalState(producer, Map.class, cache); + cache = Collections.synchronizedMap(new HashMap>()); + setInternalState(producer, "factoryCache", cache); setInternalState(producer, Logger.class, logger); setInternalState(producer, ResourceBundle.class, bundle); emFactory = createMock(EntityManagerFactory.class); @@ -51,7 +50,11 @@ public class EntityManagerFactoryProducerTest { @Test public void testCreateWithUnitPersistenceExisting() { - cache.put("pu1", emFactory); + ClassLoader cl = this.getClass().getClassLoader(); + HashMap emEntry = new HashMap(); + emEntry.put("pu1", emFactory); + cache.put(cl,emEntry); + Assert.assertEquals(emFactory, producer.create("pu1")); } @@ -73,7 +76,12 @@ public class EntityManagerFactoryProducerTest { replay(Persistence.class); producer.loadPersistenceUnits(); - Assert.assertEquals(emFactory, cache.get("pu1")); + + ClassLoader cl = this.getClass().getClassLoader(); + Map internalCache = cache.get(cl); + + Assert.assertNotNull(internalCache); + Assert.assertEquals(emFactory, internalCache.get("pu1")); } @Test @@ -88,12 +96,25 @@ public class EntityManagerFactoryProducerTest { @Test public void testGetCache() { - Assert.assertEquals(cache, producer.getCache()); + ClassLoader cl = this.getClass().getClassLoader(); + HashMap emEntry = new HashMap(); + emEntry.put("pu1", emFactory); + cache.put(cl, emEntry); + + mockStatic(Persistence.class); + expect(Persistence.createEntityManagerFactory("pu1")).andReturn(emFactory); + replay(Persistence.class); + + Assert.assertEquals(cache.get(this.getClass().getClassLoader()), producer.getCache()); } @Test public void testClose() { - cache.put("pu1", emFactory); + ClassLoader cl = this.getClass().getClassLoader(); + HashMap emEntry = new HashMap(); + emEntry.put("pu1", emFactory); + cache.put(cl, emEntry); + emFactory.close(); replay(emFactory); producer.close(); -- libgit2 0.21.2