Author: athimel Date: 2012-05-23 16:21:19 +0200 (Wed, 23 May 2012) New Revision: 2516 Url: http://nuiton.org/repositories/revision/topia/2516 Log: Fix TopiaContextFactoryTest Added: trunk/topia-persistence-tck/src/test/java/org/nuiton/topia/DummyPersistenceProvider.java trunk/topia-persistence-tck/src/test/java/org/nuiton/topia/TopiaContextFactoryTest.java trunk/topia-persistence-tck/src/test/resources/META-INF/services/javax.persistence.spi.PersistenceProvider Removed: trunk/topia-persistence-tck/src/main/java/org/nuiton/topia/TopiaContextFactoryTest.java Modified: trunk/pom.xml trunk/topia-persistence-tck/pom.xml Modified: trunk/pom.xml =================================================================== --- trunk/pom.xml 2012-05-23 13:51:23 UTC (rev 2515) +++ trunk/pom.xml 2012-05-23 14:21:19 UTC (rev 2516) @@ -77,6 +77,7 @@ <hibernateVersion>4.1.3.Final</hibernateVersion> <sl4jVersion>1.6.4</sl4jVersion> <h2Version>1.3.166</h2Version> + <mockitoVersion>1.9.0</mockitoVersion> <geronimoJpaSpecVersion>1.1</geronimoJpaSpecVersion> <!-- i18n configuration --> @@ -189,6 +190,13 @@ <scope>test</scope> </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + <version>${mockitoVersion}</version> + <scope>test</scope> + </dependency> + </dependencies> </dependencyManagement> Modified: trunk/topia-persistence-tck/pom.xml =================================================================== --- trunk/topia-persistence-tck/pom.xml 2012-05-23 13:51:23 UTC (rev 2515) +++ trunk/topia-persistence-tck/pom.xml 2012-05-23 14:21:19 UTC (rev 2516) @@ -68,6 +68,11 @@ <scope>test</scope> </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + </dependency> + </dependencies> <build> Deleted: trunk/topia-persistence-tck/src/main/java/org/nuiton/topia/TopiaContextFactoryTest.java =================================================================== --- trunk/topia-persistence-tck/src/main/java/org/nuiton/topia/TopiaContextFactoryTest.java 2012-05-23 13:51:23 UTC (rev 2515) +++ trunk/topia-persistence-tck/src/main/java/org/nuiton/topia/TopiaContextFactoryTest.java 2012-05-23 14:21:19 UTC (rev 2516) @@ -1,190 +0,0 @@ -/* - * #%L - * ToPIA :: Persistence - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2004 - 2010 CodeLutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * 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 Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ -package org.nuiton.topia; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.junit.Assert; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.nuiton.topia.framework.TopiaContextImpl; -//import org.nuiton.topia.tck.TopiaDatabase; - -import java.io.File; -import java.io.IOException; -import java.util.List; -import java.util.Properties; - -/** - * Created: 8 mai 2010 - * - * @author fdesbois <fdesbois@codelutin.com> - * @version $Id$ - */ -public class TopiaContextFactoryTest { - - private static final Log log = - LogFactory.getLog(TopiaContextFactoryTest.class); - - protected static File testBasedir; - - protected Properties properties; - - @BeforeClass - public static void init() throws IOException { - -// testBasedir = TopiaDatabase.getTestSpecificDirectory(TopiaContextFactoryTest.class, "dummy"); - - } - - @Before - public void setUp() throws Exception { - properties = new Properties(); - properties.setProperty("prop1", "value1"); - properties.setProperty("prop2", "value2"); - TopiaContextFactory.contextCache.clear(); - } - - @Test - public void testGetContextOpened() throws Exception { - log.debug("## testGetContextOpened"); - - /** PREPARE DATA **/ - String databaseName = "h2data-testGetContextByPropertie"; - File dbDirectory = new File(testBasedir, databaseName); - String url = "jdbc:h2:file:" + dbDirectory; - properties.setProperty(TopiaContextFactory.CONFIG_URL, url); - - TopiaContextImpl test = new TopiaContextImpl(properties); - TopiaContextFactory.contextCache.put(properties, test); - - /** EXEC METHOD **/ - List<String> result = TopiaContextFactory.getContextOpened(); - Assert.assertEquals(1, result.size()); - Assert.assertEquals(url, result.get(0)); - } - - @Test - public void testRemoveContext() throws Exception { - log.debug("## testRemoveContext"); - - /** PREPARE DATA **/ - TopiaContextImpl test = new TopiaContextImpl(properties); - TopiaContextFactory.contextCache.put(properties, test); - - /** EXEC METHOD **/ - TopiaContextFactory.removeContext(test); - Assert.assertEquals(0, TopiaContextFactory.contextCache.size()); - } - - //@Test - - public void testGetContext() throws Exception { - // TODO-fdesbois-20100508 : only used TopiaUtil.getProperties, need tests for this method - } - - @Test - public void testGetContextByProperties() throws Exception { - log.debug("## testGetContextByProperties"); - - /** PREPARE DATA **/ - Properties propertiesParent = new Properties(properties); - propertiesParent.setProperty("prop3", "value3"); - - Properties propertiesAll = new Properties(); - propertiesAll.setProperty("prop1", "value1"); - propertiesAll.setProperty("prop2", "value2"); - propertiesAll.setProperty("prop3", "value3"); - - /** EXEC METHOD **/ - - log.info("test 0 : add null properties"); - try { - TopiaContextFactory.getContext(null); - } catch (Exception eee) { - Assert.assertEquals(NullPointerException.class, eee.getClass()); - } - - log.info("test 1 : add new properties, will instantiate a new" + - " TopiaContext"); - TopiaContext test1 = TopiaContextFactory.getContext(propertiesParent); - Assert.assertNotNull(test1); - Assert.assertEquals(1, TopiaContextFactory.contextCache.size()); - - log.info("test 2 : with same properties, will retrieve existing" + - " TopiaContext"); - TopiaContext test2 = TopiaContextFactory.getContext(propertiesParent); - Assert.assertEquals(test1, test2); - Assert.assertEquals(1, TopiaContextFactory.contextCache.size()); - - log.info("test 3 : use other properties, will instantiate a different" + - "TopiaContext"); - TopiaContext test3 = TopiaContextFactory.getContext(properties); - log.debug("cache size : " + TopiaContextFactory.contextCache.size()); - log.debug("result : " + test1); - log.debug("result3 : " + test3); - Assert.assertNotSame(test1, test3); - Assert.assertEquals(2, TopiaContextFactory.contextCache.size()); - - log.info("test 4 : use other properties but equivalent to existing " + - "TopiaContext"); - // Test flating of properties - TopiaContext test4 = TopiaContextFactory.getContext(propertiesAll); - Assert.assertEquals(test1, test4); - Assert.assertEquals(2, TopiaContextFactory.contextCache.size()); - - log.info("test5a : reinstantiate new TopiaContext after one is closed."); - // TEST - // Strange behavior the closed flag of context stay true if - // entityManagerFactory is not loaded from real properties -// test1.closeContext(); -// Assert.assertTrue(test1.isClosed()); - - // Add properties for Hibernate to have real opened topiaContext - String databaseName = "h2data-testGetContextByPropertie"; - File f = new File(testBasedir, databaseName); - - properties.setProperty("hibernate.connection.username", "sa"); - properties.setProperty("hibernate.connection.password", ""); - properties.setProperty("hibernate.connection.driver_class", "org.h2.Driver"); - properties.setProperty("hibernate.connection.url", - "jdbc:h2:file:" + f.getAbsolutePath()); - - - TopiaContext test5 = TopiaContextFactory.getContext(properties); - Assert.assertNotSame(test1, test5); - Assert.assertEquals(3, TopiaContextFactory.contextCache.size()); - - log.info("test5b : beginTransaction to properly close the context"); - test5.beginTransaction(); - - test5.closeContext(); - - TopiaContext result = TopiaContextFactory.getContext(properties); - Assert.assertNotSame(test5, result); - Assert.assertEquals(3, TopiaContextFactory.contextCache.size()); - } -} Added: trunk/topia-persistence-tck/src/test/java/org/nuiton/topia/DummyPersistenceProvider.java =================================================================== --- trunk/topia-persistence-tck/src/test/java/org/nuiton/topia/DummyPersistenceProvider.java (rev 0) +++ trunk/topia-persistence-tck/src/test/java/org/nuiton/topia/DummyPersistenceProvider.java 2012-05-23 14:21:19 UTC (rev 2516) @@ -0,0 +1,33 @@ +package org.nuiton.topia; + +import org.apache.commons.lang3.tuple.Pair; +import org.mockito.Mockito; +import org.nuiton.topia.framework.TopiaPersistenceProvider; +import org.nuiton.topia.framework.TopiaSpecificUtil; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityTransaction; +import java.util.Map; +import java.util.Set; + +/** + * @author Arnaud Thimel <thimel@codelutin.com> + */ +public class DummyPersistenceProvider extends TopiaPersistenceProvider { + + @Override + public Pair<EntityManagerFactory, TopiaSpecificUtil> configureSpecific( + String entityManagerName, Map<String, Object> properties, Set<Class<?>> entities) { + + EntityManagerFactory emf = Mockito.mock(EntityManagerFactory.class); + EntityManager em = Mockito.mock(EntityManager.class); + EntityTransaction tx = Mockito.mock(EntityTransaction.class); + + Mockito.when(emf.createEntityManager()).thenReturn(em); + Mockito.when(em.getTransaction()).thenReturn(tx); + + return Pair.of(emf, null); + } + +} Copied: trunk/topia-persistence-tck/src/test/java/org/nuiton/topia/TopiaContextFactoryTest.java (from rev 2514, trunk/topia-persistence-tck/src/main/java/org/nuiton/topia/TopiaContextFactoryTest.java) =================================================================== --- trunk/topia-persistence-tck/src/test/java/org/nuiton/topia/TopiaContextFactoryTest.java (rev 0) +++ trunk/topia-persistence-tck/src/test/java/org/nuiton/topia/TopiaContextFactoryTest.java 2012-05-23 14:21:19 UTC (rev 2516) @@ -0,0 +1,190 @@ +/* + * #%L + * ToPIA :: Persistence + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2010 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * 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 Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ +package org.nuiton.topia; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.nuiton.topia.framework.TopiaContextImpl; +import org.nuiton.topia.tck.TopiaDatabase; + +import java.io.File; +import java.io.IOException; +import java.util.List; +import java.util.Properties; + +/** + * Created: 8 mai 2010 + * + * @author fdesbois <fdesbois@codelutin.com> + * @version $Id$ + */ +public class TopiaContextFactoryTest { + + private static final Log log = + LogFactory.getLog(TopiaContextFactoryTest.class); + + protected static File testBasedir; + + protected Properties properties; + + @BeforeClass + public static void init() throws IOException { + + testBasedir = TopiaDatabase.getTestSpecificDirectory(TopiaContextFactoryTest.class, "dummy"); + + } + + @Before + public void setUp() throws Exception { + properties = new Properties(); + properties.setProperty("prop1", "value1"); + properties.setProperty("prop2", "value2"); + TopiaContextFactory.contextCache.clear(); + } + + @Test + public void testGetContextOpened() throws Exception { + log.debug("## testGetContextOpened"); + + /** PREPARE DATA **/ + String databaseName = "h2data-testGetContextByPropertie"; + File dbDirectory = new File(testBasedir, databaseName); + String url = "jdbc:h2:file:" + dbDirectory; + properties.setProperty(TopiaContextFactory.CONFIG_URL, url); + + TopiaContextImpl test = new TopiaContextImpl(properties); + TopiaContextFactory.contextCache.put(properties, test); + + /** EXEC METHOD **/ + List<String> result = TopiaContextFactory.getContextOpened(); + Assert.assertEquals(1, result.size()); + Assert.assertEquals(url, result.get(0)); + } + + @Test + public void testRemoveContext() throws Exception { + log.debug("## testRemoveContext"); + + /** PREPARE DATA **/ + TopiaContextImpl test = new TopiaContextImpl(properties); + TopiaContextFactory.contextCache.put(properties, test); + + /** EXEC METHOD **/ + TopiaContextFactory.removeContext(test); + Assert.assertEquals(0, TopiaContextFactory.contextCache.size()); + } + + //@Test + + public void testGetContext() throws Exception { + // TODO-fdesbois-20100508 : only used TopiaUtil.getProperties, need tests for this method + } + + @Test + public void testGetContextByProperties() throws Exception { + log.debug("## testGetContextByProperties"); + + /** PREPARE DATA **/ + Properties propertiesParent = new Properties(properties); + propertiesParent.setProperty("prop3", "value3"); + + Properties propertiesAll = new Properties(); + propertiesAll.setProperty("prop1", "value1"); + propertiesAll.setProperty("prop2", "value2"); + propertiesAll.setProperty("prop3", "value3"); + + /** EXEC METHOD **/ + + log.info("test 0 : add null properties"); + try { + TopiaContextFactory.getContext(null); + } catch (Exception eee) { + Assert.assertEquals(NullPointerException.class, eee.getClass()); + } + + log.info("test 1 : add new properties, will instantiate a new" + + " TopiaContext"); + TopiaContext test1 = TopiaContextFactory.getContext(propertiesParent); + Assert.assertNotNull(test1); + Assert.assertEquals(1, TopiaContextFactory.contextCache.size()); + + log.info("test 2 : with same properties, will retrieve existing" + + " TopiaContext"); + TopiaContext test2 = TopiaContextFactory.getContext(propertiesParent); + Assert.assertEquals(test1, test2); + Assert.assertEquals(1, TopiaContextFactory.contextCache.size()); + + log.info("test 3 : use other properties, will instantiate a different" + + "TopiaContext"); + TopiaContext test3 = TopiaContextFactory.getContext(properties); + log.debug("cache size : " + TopiaContextFactory.contextCache.size()); + log.debug("result : " + test1); + log.debug("result3 : " + test3); + Assert.assertNotSame(test1, test3); + Assert.assertEquals(2, TopiaContextFactory.contextCache.size()); + + log.info("test 4 : use other properties but equivalent to existing " + + "TopiaContext"); + // Test flating of properties + TopiaContext test4 = TopiaContextFactory.getContext(propertiesAll); + Assert.assertEquals(test1, test4); + Assert.assertEquals(2, TopiaContextFactory.contextCache.size()); + + log.info("test5a : reinstantiate new TopiaContext after one is closed."); + // TEST + // Strange behavior the closed flag of context stay true if + // entityManagerFactory is not loaded from real properties +// test1.closeContext(); +// Assert.assertTrue(test1.isClosed()); + + // Add properties for Hibernate to have real opened topiaContext + String databaseName = "h2data-testGetContextByPropertie"; + File f = new File(testBasedir, databaseName); + + properties.setProperty("hibernate.connection.username", "sa"); + properties.setProperty("hibernate.connection.password", ""); + properties.setProperty("hibernate.connection.driver_class", "org.h2.Driver"); + properties.setProperty("hibernate.connection.url", + "jdbc:h2:file:" + f.getAbsolutePath()); + + + TopiaContext test5 = TopiaContextFactory.getContext(properties); + Assert.assertNotSame(test1, test5); + Assert.assertEquals(3, TopiaContextFactory.contextCache.size()); + + log.info("test5b : beginTransaction to properly close the context"); + test5.beginTransaction(); + + test5.closeContext(); + + TopiaContext result = TopiaContextFactory.getContext(properties); + Assert.assertNotSame(test5, result); + Assert.assertEquals(3, TopiaContextFactory.contextCache.size()); + } +} Property changes on: trunk/topia-persistence-tck/src/test/java/org/nuiton/topia/TopiaContextFactoryTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Copied: trunk/topia-persistence-tck/src/test/resources/META-INF/services/javax.persistence.spi.PersistenceProvider (from rev 2515, trunk/topia-persistence-hibernate/src/main/resources/META-INF/services/javax.persistence.spi.PersistenceProvider) =================================================================== --- trunk/topia-persistence-tck/src/test/resources/META-INF/services/javax.persistence.spi.PersistenceProvider (rev 0) +++ trunk/topia-persistence-tck/src/test/resources/META-INF/services/javax.persistence.spi.PersistenceProvider 2012-05-23 14:21:19 UTC (rev 2516) @@ -0,0 +1 @@ +org.nuiton.topia.DummyPersistenceProvider \ No newline at end of file