r2449 - in trunk: topia-persistence-hibernate topia-persistence-hibernate/src/main/resources/i18n topia-persistence-hibernate/src/test/java/org/nuiton/topia topia-persistence-hibernate/src/test/java/org/nuiton/topia/framework topia-persistence-hibernate/src/test/java/org/nuiton/topia/persistence topia-persistence-tck/src/test/java/org/nuiton/topia/framework topia-persistence-tck/src/test/java/org/nuiton/topia/persistence
Author: athimel Date: 2012-05-09 16:31:12 +0200 (Wed, 09 May 2012) New Revision: 2449 Url: http://nuiton.org/repositories/revision/topia/2449 Log: Move Hibernate dependant tests from t-p-tck to t-p-hibernate Added: trunk/topia-persistence-hibernate/src/main/resources/i18n/topia-persistence-hibernate_en_GB.properties trunk/topia-persistence-hibernate/src/main/resources/i18n/topia-persistence-hibernate_es_ES.properties trunk/topia-persistence-hibernate/src/test/java/org/nuiton/topia/framework/TopiaConnectionProviderHardCoded.java trunk/topia-persistence-hibernate/src/test/java/org/nuiton/topia/framework/TopiaConnectionProviderTest.java Removed: trunk/topia-persistence-hibernate/src/test/java/org/nuiton/topia/TestHelper.java trunk/topia-persistence-hibernate/src/test/java/org/nuiton/topia/TopiaDatabase.java trunk/topia-persistence-hibernate/src/test/java/org/nuiton/topia/persistence/TopiaDAOTest.java trunk/topia-persistence-tck/src/test/java/org/nuiton/topia/framework/TopiaConnectionProviderHardCoded.java trunk/topia-persistence-tck/src/test/java/org/nuiton/topia/framework/TopiaConnectionProviderTest.java Modified: trunk/topia-persistence-hibernate/pom.xml trunk/topia-persistence-hibernate/topia-persistence-hibernate.iml trunk/topia-persistence-tck/src/test/java/org/nuiton/topia/persistence/NaturalIdTest.java Modified: trunk/topia-persistence-hibernate/pom.xml =================================================================== --- trunk/topia-persistence-hibernate/pom.xml 2012-05-09 14:22:27 UTC (rev 2448) +++ trunk/topia-persistence-hibernate/pom.xml 2012-05-09 14:31:12 UTC (rev 2449) @@ -66,6 +66,13 @@ <scope>test</scope> </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>topia-persistence-tck</artifactId> + <version>${project.version}</version> + <scope>test</scope> + </dependency> + </dependencies> <!-- ************************************************************* --> @@ -154,7 +161,7 @@ </executions> </plugin> - <plugin> + <!--plugin> <groupId>org.nuiton.eugene</groupId> <artifactId>maven-eugene-plugin</artifactId> <configuration> @@ -182,7 +189,7 @@ </goals> </execution> </executions> - </plugin> + </plugin--> <!-- expose tests --> <plugin> Added: trunk/topia-persistence-hibernate/src/main/resources/i18n/topia-persistence-hibernate_en_GB.properties =================================================================== Added: trunk/topia-persistence-hibernate/src/main/resources/i18n/topia-persistence-hibernate_es_ES.properties =================================================================== Deleted: trunk/topia-persistence-hibernate/src/test/java/org/nuiton/topia/TestHelper.java =================================================================== --- trunk/topia-persistence-hibernate/src/test/java/org/nuiton/topia/TestHelper.java 2012-05-09 14:22:27 UTC (rev 2448) +++ trunk/topia-persistence-hibernate/src/test/java/org/nuiton/topia/TestHelper.java 2012-05-09 14:31:12 UTC (rev 2449) @@ -1,158 +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.hibernate.cfg.Environment; -import org.junit.Ignore; -import org.nuiton.util.FileUtil; - -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.util.Properties; - -/** - * Helper for all topia tests. - * - * @author tchemit <chemit@codelutin.com> - * @since 2.5 - */ -@Ignore -// this is not a test :) -public class TestHelper { - - private static final Log log = LogFactory.getLog(TestHelper.class); - - protected static File testBasedir; - - protected static File targetdir; - - protected static File dirDatabase; - - public static final String DEFAULT_CONFIGURATION_LOCATION = "/TopiaContextImpl.properties"; - - public static File getTestWorkdir() { - if (testBasedir == null) { - String base = System.getProperty("java.io.tmpdir"); - if (base == null || base.isEmpty()) { - base = new File("").getAbsolutePath(); - } - testBasedir = new File(base); - log.info("basedir for test " + testBasedir); - } - return testBasedir; - } - - public static File getTestBasedir(Class<?> testClass) throws IOException { - File dir = getTestWorkdir(); - File result = new File(dir, testClass.getName()); - if (result.exists()) { - - // when calling this method (always in a BeforeClass method), we wants - // to clean the directory, this is a new build - FileUtil.deleteRecursively(result); - } - - // always create the directory - FileUtil.createDirectoryIfNecessary(result); - return result; - } - - public static TopiaContext initTopiaContext(File testDirectory, - String dbname) - throws IOException, TopiaNotFoundException { - - - TopiaContext topiaContext = initTopiaContext( - testDirectory, - DEFAULT_CONFIGURATION_LOCATION, - dbname - ); - return topiaContext; - } - - public static TopiaContext initTopiaContext(File testDirectory, - String dbPropertiesPath, - String dbname) - throws IOException, TopiaNotFoundException { - - Properties configuration = initTopiaContextConfiguration( - testDirectory, - dbPropertiesPath, - dbname); - return TopiaContextFactory.getContext(configuration); - } - - public static Properties initTopiaContextConfiguration(File testDirectory, - String dbPropertiesPath, - String dbname) - throws IOException { - - Properties configuration = loadHibernateConfiguration(dbPropertiesPath); - - // make sure we always use a different directory - - String dbPath = getDbName(testDirectory, dbname); - - if (log.isInfoEnabled()) { - log.info("dbPath = " + dbPath); - } - configuration.setProperty( - TopiaContextFactory.CONFIG_URL, - "jdbc:h2:file:" + dbPath); - - return configuration; - } - - public static Properties loadHibernateConfiguration(String dbPropertiesPath) throws IOException { - InputStream stream = TestHelper.class.getResourceAsStream(dbPropertiesPath); - - Properties configuration = new Properties(); - - configuration.load(stream); - configuration.setProperty( - TopiaContextFactory.CONFIG_PERSISTENCE_CLASSES, - TopiaTestDAOHelper.getImplementationClassesAsString()); - return configuration; - } - - public static String getDbName(File testDirectory, String dbname) { - return new File(testDirectory, - dbname + '_' + System.nanoTime()).getAbsolutePath(); - } - - public static Properties initTopiaContextConfiguration(File testDirectory, - String dbname) - throws IOException { - - return initTopiaContextConfiguration( - testDirectory, - DEFAULT_CONFIGURATION_LOCATION, - dbname - ); - } -} Deleted: trunk/topia-persistence-hibernate/src/test/java/org/nuiton/topia/TopiaDatabase.java =================================================================== --- trunk/topia-persistence-hibernate/src/test/java/org/nuiton/topia/TopiaDatabase.java 2012-05-09 14:22:27 UTC (rev 2448) +++ trunk/topia-persistence-hibernate/src/test/java/org/nuiton/topia/TopiaDatabase.java 2012-05-09 14:31:12 UTC (rev 2449) @@ -1,192 +0,0 @@ -/* - * #%L - * ToPIA :: Persistence - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2004 - 2012 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.rules.TestWatcher; -import org.junit.runner.Description; - -import java.io.File; -import java.io.InputStream; -import java.util.Properties; - -/** - * Put this class as a Rule in test to obtain a new isolated db for each test. - * <p/> - * Here is a simple example of usage : - * <pre> - * public class MyTest { - * - * \@Rule - * public final TopiaDatabase db = new TopiaDatabase(); - * - * \@Test - * public void testMethod() throws TopiaException { - * - * TopiaContext tx = db.beginTransaction(); - * ... - * } - * </pre> - * The db created will be unique for each test method (and for each build also). - * <p/> - * You don't need to close any transaction, it will be done for you and the end - * of each method test. - * - * @author tchemit <chemit@codelutin.com> - * @since 2.6.8 - */ -public class TopiaDatabase extends TestWatcher { - - /** Logger. */ - private static final Log log = LogFactory.getLog(TopiaDatabase.class); - - /** A time-stamp, allow to make multiple build and keep the tests data. */ - public static final String TIMESTAMP = String.valueOf(System.nanoTime()); - - private File testBasedir; - - private Properties dbConfiguration; - - private TopiaContext rootCtxt; - - private final String configurationPath; - - public TopiaDatabase() { - this(TestHelper.DEFAULT_CONFIGURATION_LOCATION); - } - - public TopiaDatabase(String configurationPath) { - this.configurationPath = configurationPath; - } - - @Override - protected void starting(Description description) { - - // get test directory - testBasedir = getTestSpecificDirectory( - description.getTestClass(), - description.getMethodName()); - - if (log.isDebugEnabled()) { - log.debug("testBasedir = " + testBasedir); - } - - // create the root context - try { - - dbConfiguration = new Properties(); - InputStream stream = - getClass().getResourceAsStream(configurationPath); - - try { - dbConfiguration.load(stream); - } finally { - stream.close(); - } - dbConfiguration.setProperty( - TopiaContextFactory.CONFIG_PERSISTENCE_CLASSES, - TopiaTestDAOHelper.getImplementationClassesAsString()); - - // make sure we always use a different directory - - String dbPath = new File(testBasedir, "db").getAbsolutePath(); - - if (log.isDebugEnabled()) { - log.debug("dbPath = " + dbPath); - } - dbConfiguration.setProperty( - TopiaContextFactory.CONFIG_URL, "jdbc:h2:file:" + dbPath); - - onDbConfigurationCreate(dbConfiguration, testBasedir, dbPath); - - rootCtxt = TopiaContextFactory.getContext(dbConfiguration); - } catch (Exception e) { - throw new IllegalStateException( - "Could not start db at " + testBasedir, e); - } - } - - @Override - public void finished(Description description) { - - if (rootCtxt != null && !rootCtxt.isClosed()) { - try { - rootCtxt.closeContext(); - } catch (TopiaException e) { - if (log.isErrorEnabled()) { - log.error("Could not close topia root context", e); - } - } - } - rootCtxt = null; - dbConfiguration = null; - } - - public File getTestBasedir() { - return testBasedir; - } - - public TopiaContext getRootCtxt() { - return rootCtxt; - } - - public Properties getDbConfiguration() { - return dbConfiguration; - } - - public TopiaContext beginTransaction() throws TopiaException { - return rootCtxt.beginTransaction(); - } - - protected void onDbConfigurationCreate(Properties configuration, - File testDir, - String dbPath) { - - } - - public static File getTestSpecificDirectory(Class<?> testClassName, String methodName) { - // Trying to look for the temporary folder to store data for the test - String tempDirPath = System.getProperty("java.io.tmpdir"); - if (tempDirPath == null) { - // can this really occur ? - tempDirPath = ""; - if (log.isWarnEnabled()) { - log.warn("'\"java.io.tmpdir\" not defined"); - } - } - File tempDirFile = new File(tempDirPath); - - // create the directory to store database data - String dataBasePath = testClassName.getName() - + File.separator // a directory with the test class name - + methodName// a sub-directory with the method name - + '_' - + TIMESTAMP; // and a timestamp - File databaseFile = new File(tempDirFile, dataBasePath); - return databaseFile; - } -} - Copied: trunk/topia-persistence-hibernate/src/test/java/org/nuiton/topia/framework/TopiaConnectionProviderHardCoded.java (from rev 2446, trunk/topia-persistence-tck/src/test/java/org/nuiton/topia/framework/TopiaConnectionProviderHardCoded.java) =================================================================== --- trunk/topia-persistence-hibernate/src/test/java/org/nuiton/topia/framework/TopiaConnectionProviderHardCoded.java (rev 0) +++ trunk/topia-persistence-hibernate/src/test/java/org/nuiton/topia/framework/TopiaConnectionProviderHardCoded.java 2012-05-09 14:31:12 UTC (rev 2449) @@ -0,0 +1,222 @@ +/* + * #%L + * ToPIA :: Persistence + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2011 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.framework; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.hibernate.HibernateException; +import org.hibernate.cfg.Environment; +import org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator; +import org.hibernate.service.jdbc.connections.spi.ConnectionProvider; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.Properties; + +/** + * Customized connection provider. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.5.3 + */ +public class TopiaConnectionProviderHardCoded implements ConnectionProvider { + + private String url; + + private Properties connectionProps; + + private Integer isolation; + + private final ArrayList pool = new ArrayList(); + + private int poolSize; + + private int checkedOut = 0; + + private boolean autocommit; + + /** Logger. */ + private static final Log log = + LogFactory.getLog(TopiaConnectionProviderHardCoded.class); + + public TopiaConnectionProviderHardCoded() { + System.out.println("created"); + } + public void configure(Properties props) throws HibernateException { + String driverClass = props.getProperty(Environment.DRIVER); + +// poolSize = PropertiesHelper.getInt(Environment.POOL_SIZE, props, 20); //default pool size 20 + poolSize = 20; //default pool size 20 + log.info("Using Hibernate built-in connection pool (not for production use!)"); + log.info("Hibernate connection pool size: " + poolSize); + +// autocommit = PropertiesHelper.getBoolean(Environment.AUTOCOMMIT, props); + autocommit = true; + log.info("autocommit mode: " + autocommit); + +// isolation = PropertiesHelper.getInteger(Environment.ISOLATION, props); + isolation = 0; + if (isolation != null) + log.info("JDBC isolation level: " + Environment.isolationLevelToString(isolation)); + + if (driverClass == null) { + log.warn("no JDBC Driver class was specified by property " + Environment.DRIVER); + } else { + try { + // trying via forName() first to be as close to DriverManager's semantics + Class.forName(driverClass); + } catch (ClassNotFoundException cnfe) { +// try { +// ReflectHelper.classForName(driverClass); +// } catch (ClassNotFoundException e) { +// String msg = "JDBC Driver class not found: " + driverClass; +// log.error(msg, e); +// throw new HibernateException(msg, e); +// } + } + } + + // use a dummy directory to make sure only the connection provider knows + // the real directory where db is and then make sure hibernate always + // use the connection provider... + String directory = + props.getProperty(TopiaConnectionProviderTest.TEST_URL); + + url = directory; +// url = props.getProperty(Environment.URL); + +// if (url == null) { +// String msg = "JDBC URL was not specified by property " + Environment.URL; +// log.error(msg); +// throw new HibernateException(msg); +// } + + connectionProps = ConnectionProviderInitiator.getConnectionProperties(props); + + log.info("using driver: " + driverClass + " at URL: " + url); + // if debug level is enabled, then log the password, otherwise mask it + if (log.isDebugEnabled()) { + log.info("connection properties: " + connectionProps); + } else if (log.isInfoEnabled()) { +// log.info("connection properties: " + PropertiesHelper.maskOut(connectionProps, "password")); + } + } + + @Override + public Connection getConnection() throws SQLException { + if (log.isTraceEnabled()) + log.trace("total checked-out connections: " + checkedOut); + + synchronized (pool) { + if (!pool.isEmpty()) { + int last = pool.size() - 1; + if (log.isTraceEnabled()) { + log.trace("using pooled JDBC connection, pool size: " + last); + } + checkedOut++; + Connection pooled = (Connection) pool.remove(last); + if (isolation != null) + pooled.setTransactionIsolation(isolation.intValue()); + if (pooled.getAutoCommit() != autocommit) + pooled.setAutoCommit(autocommit); + return pooled; + } + } + + log.debug("opening new JDBC connection"); + Connection conn = DriverManager.getConnection(url, connectionProps); + if (isolation != null) conn.setTransactionIsolation(isolation); + if (conn.getAutoCommit() != autocommit) conn.setAutoCommit(autocommit); + + if (log.isDebugEnabled()) { + log.debug("created connection to: " + url + ", Isolation Level: " + conn.getTransactionIsolation()); + } +// if ( log.isTraceEnabled() ) + checkedOut++; + + return conn; + } + + @Override + public void closeConnection(Connection conn) throws SQLException { +// if ( log.isDebugEnabled() ) + checkedOut--; + + synchronized (pool) { + int currentSize = pool.size(); + if (currentSize < poolSize) { + if (log.isTraceEnabled()) { + log.trace("returning connection to pool, pool size: " + (currentSize + 1)); + } + pool.add(conn); + return; + } + } + + log.debug("closing JDBC connection"); + + conn.close(); + } + + @Override + protected void finalize() throws Throwable { + super.finalize(); + close(); + } + + public void close() { + + log.info("cleaning up connection pool: " + url); + + Iterator iter = pool.iterator(); + while (iter.hasNext()) { + try { + ((Connection) iter.next()).close(); + } catch (SQLException sqle) { + log.warn("problem closing pooled connection", sqle); + } + } + pool.clear(); + + } + + @Override + public boolean supportsAggressiveRelease() { + return false; + } + + @Override + public boolean isUnwrappableAs(Class unwrapType) { + return false; + } + + @Override + public <T> T unwrap(Class<T> unwrapType) { + return null; + } +} Copied: trunk/topia-persistence-hibernate/src/test/java/org/nuiton/topia/framework/TopiaConnectionProviderTest.java (from rev 2448, trunk/topia-persistence-tck/src/test/java/org/nuiton/topia/framework/TopiaConnectionProviderTest.java) =================================================================== --- trunk/topia-persistence-hibernate/src/test/java/org/nuiton/topia/framework/TopiaConnectionProviderTest.java (rev 0) +++ trunk/topia-persistence-hibernate/src/test/java/org/nuiton/topia/framework/TopiaConnectionProviderTest.java 2012-05-09 14:31:12 UTC (rev 2449) @@ -0,0 +1,140 @@ +/* + * #%L + * ToPIA :: Persistence + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2011 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.framework; + +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.nuiton.topia.TopiaContext; +import org.nuiton.topia.TopiaContextFactory; +import org.nuiton.topia.TopiaTckItDAOHelper; +import org.nuiton.topia.tck.TopiaDatabase; +import org.nuiton.topia.TopiaException; +import org.nuiton.topia.tck.it.Personne; +import org.nuiton.topia.test.entities.Person; +import org.nuiton.topia.test.entities.PersonDAO; + +import java.io.File; +import java.util.Locale; +import java.util.Properties; + +import static org.junit.Assert.assertNotNull; + +/** + * To test the {@link TopiaConnectionProvider} and make sure all connections + * are done from here... + * + * @author tchemit <chemit@codelutin.com> + * @since 2.5.3 + */ +public class TopiaConnectionProviderTest { + +// private static final Log log = +// LogFactory.getLog(TopiaConnectionProviderTest.class); + + public static final String TEST_URL = "testURL"; + + @Rule + public final TopiaDatabase db = + new TopiaDatabase("/TopiaConnectionProviderHardcoded.properties") { + + @Override + protected void onDbConfigurationCreate(Properties configuration, + File testdir, + String dbPath) { + + Assert.assertFalse(testdir.exists()); + + String dbPathFake = new File(testdir, "fake" + File.separator + "db").getAbsolutePath(); + + Assert.assertFalse(new File(dbPathFake).getParentFile().exists()); + + configuration.setProperty("dbPath", dbPath); + configuration.setProperty("dbPathFake", dbPathFake); + + // give the path where connection provider will create db + configuration.setProperty(TEST_URL, + "jdbc:h2:file:" + dbPath); + + // give a fake db path (we will make sure it is never create after hibernate usage). + configuration.setProperty(TopiaContextFactory.CONFIG_URL, + "jdbc:h2:file:" + dbPathFake); + } + }; + + @Test + public void testWithHardcoded() throws Exception { + +// Properties dbProperties = TestHelper.loadHibernateConfiguration( +// "/TopiaConnectionProviderHardcoded.properties"); +// +// File directory = new File(TestHelper.getDbName(testBasedir, "testWithHardcoded")); + + String dbPath = (String) db.getDbConfiguration().get("dbPath"); + String dbPathFake = (String) db.getDbConfiguration().get("dbPathFake"); + +// new File(directory, "real" + File.separator + "db").getAbsolutePath(); +// Assert.assertFalse(new File(dbPath).getParentFile().exists()); + +// String dbPathFake = new File(directory, "fake" + File.separator + "db").getAbsolutePath(); + +// Assert.assertFalse(new File(dbPathFake).getParentFile().exists()); + +// // give the path where connection provider will create db +// dbProperties.setProperty(TEST_URL, "jdbc:h2:file:" + dbPath); +// +// // give a fake db path (we will make sure it is never create after hibernate usage). +// dbProperties.setProperty(Environment.URL, "jdbc:h2:file:" + dbPathFake); +// +// root = TopiaContextFactory.getContext(dbProperties); + + Locale.setDefault(Locale.FRANCE); + + doStuffOnDb(); + + // the db file must have been created + Assert.assertTrue(new File(dbPath).getParentFile().exists()); + + // make sure the fake db path was never used + Assert.assertFalse(new File(dbPathFake).getParentFile().exists()); + } + + private void doStuffOnDb() throws TopiaException { + TopiaContext transaction = db.beginTransaction(); + + try { + PersonDAO dao = TopiaTckItDAOHelper.getPersonDAO(transaction); + + Person personne = dao.create(Personne.PROPERTY_NAME, "Jack Bauer"); + transaction.commitTransaction(); + String idPersonne = personne.getTopiaId(); + assertNotNull(idPersonne); + + transaction.commitTransaction(); + } finally { + transaction.closeContext(); + } + } +} Deleted: trunk/topia-persistence-hibernate/src/test/java/org/nuiton/topia/persistence/TopiaDAOTest.java =================================================================== --- trunk/topia-persistence-hibernate/src/test/java/org/nuiton/topia/persistence/TopiaDAOTest.java 2012-05-09 14:22:27 UTC (rev 2448) +++ trunk/topia-persistence-hibernate/src/test/java/org/nuiton/topia/persistence/TopiaDAOTest.java 2012-05-09 14:31:12 UTC (rev 2449) @@ -1,89 +0,0 @@ -/* - * #%L - * ToPIA :: Persistence - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2004 - 2010 CodeLutin, Chatellier Eric - * %% - * 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.persistence; - -import org.junit.Assert; -import org.junit.Rule; -import org.junit.Test; -import org.junit.matchers.JUnitMatchers; -import org.nuiton.topia.TopiaContext; -import org.nuiton.topia.TopiaDatabase; -import org.nuiton.topia.TopiaTestDAOHelper; -import org.nuiton.topia.test.entities.Person; -import org.nuiton.topia.test.entities.PersonDAO; - -import java.util.List; - -/** - * Test on {@link TopiaDAO}. - * <p/> - * Last update : $Date$ - * By : $Author$ - * - * @author chatellier - * @version $Revision$ - */ -public class TopiaDAOTest { - - @Rule - public final TopiaDatabase db = new TopiaDatabase(); - - /** - * Test de creer une entité et de verifier qu'elle est - * présente dans la persistence au sein de la transaction. - * - * @throws Exception if any exception while test - */ - @Test - public void testCreateAndFindInTransaction() throws Exception { - - TopiaContext context = db.beginTransaction(); - - PersonDAO personDAO = TopiaTestDAOHelper.getPersonDAO(context); - - // appel 1 find all - Person person = personDAO.create(Person.PROPERTY_NAME, "toto"); - List<Person> allPerson = personDAO.findAll(); - Assert.assertEquals(1, allPerson.size()); - context.commitTransaction(); - - // recherce la personne créée dans la même transaction - Person person2 = personDAO.create(Person.PROPERTY_NAME, "titi"); - allPerson = personDAO.findAll(); - Assert.assertEquals(2, allPerson.size()); - Assert.assertThat(allPerson, JUnitMatchers.hasItem(person2)); - - context.rollbackTransaction(); - - // meme test apres roolback - Person person3 = personDAO.create(Person.PROPERTY_NAME, "tata"); - allPerson = personDAO.findAll(); - Assert.assertEquals(2, allPerson.size()); - Assert.assertThat(allPerson, JUnitMatchers.hasItem(person3)); - - context.commitTransaction(); - } -} Modified: trunk/topia-persistence-hibernate/topia-persistence-hibernate.iml =================================================================== --- trunk/topia-persistence-hibernate/topia-persistence-hibernate.iml 2012-05-09 14:22:27 UTC (rev 2448) +++ trunk/topia-persistence-hibernate/topia-persistence-hibernate.iml 2012-05-09 14:31:12 UTC (rev 2449) @@ -3,12 +3,17 @@ <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_6" inherit-compiler-output="false"> <output url="file://$MODULE_DIR$/target/classes" /> <output-test url="file://$MODULE_DIR$/target/test-classes" /> - <exclude-output /> <content url="file://$MODULE_DIR$"> <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src/main/resources" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" /> - <excludeFolder url="file://$MODULE_DIR$/target" /> + <sourceFolder url="file://$MODULE_DIR$/target/generated-sources/i18n" isTestSource="false" /> + <sourceFolder url="file://$MODULE_DIR$/target/generated-sources/test-xmi" isTestSource="false" /> + <sourceFolder url="file://$MODULE_DIR$/target/generated-sources/annotations" isTestSource="false" /> + <excludeFolder url="file://$MODULE_DIR$/target/antrun" /> + <excludeFolder url="file://$MODULE_DIR$/target/classes" /> + <excludeFolder url="file://$MODULE_DIR$/target/processed-sources" /> + <excludeFolder url="file://$MODULE_DIR$/target/surefire-workdir" /> </content> <orderEntry type="inheritedJdk" /> <orderEntry type="sourceFolder" forTests="false" /> @@ -38,6 +43,7 @@ <orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.10" level="project" /> <orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.1" level="project" /> <orderEntry type="library" scope="TEST" name="Maven: log4j:log4j:1.2.16" level="project" /> + <orderEntry type="module" module-name="topia-persistence-tck" scope="TEST" /> </component> </module> Deleted: trunk/topia-persistence-tck/src/test/java/org/nuiton/topia/framework/TopiaConnectionProviderHardCoded.java =================================================================== --- trunk/topia-persistence-tck/src/test/java/org/nuiton/topia/framework/TopiaConnectionProviderHardCoded.java 2012-05-09 14:22:27 UTC (rev 2448) +++ trunk/topia-persistence-tck/src/test/java/org/nuiton/topia/framework/TopiaConnectionProviderHardCoded.java 2012-05-09 14:31:12 UTC (rev 2449) @@ -1,222 +0,0 @@ -/* - * #%L - * ToPIA :: Persistence - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2004 - 2011 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.framework; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.hibernate.HibernateException; -import org.hibernate.cfg.Environment; -import org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator; -import org.hibernate.service.jdbc.connections.spi.ConnectionProvider; - -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.Properties; - -/** - * Customized connection provider. - * - * @author tchemit <chemit@codelutin.com> - * @since 2.5.3 - */ -public class TopiaConnectionProviderHardCoded implements ConnectionProvider { - - private String url; - - private Properties connectionProps; - - private Integer isolation; - - private final ArrayList pool = new ArrayList(); - - private int poolSize; - - private int checkedOut = 0; - - private boolean autocommit; - - /** Logger. */ - private static final Log log = - LogFactory.getLog(TopiaConnectionProviderHardCoded.class); - - public TopiaConnectionProviderHardCoded() { - System.out.println("created"); - } - public void configure(Properties props) throws HibernateException { - String driverClass = props.getProperty(Environment.DRIVER); - -// poolSize = PropertiesHelper.getInt(Environment.POOL_SIZE, props, 20); //default pool size 20 - poolSize = 20; //default pool size 20 - log.info("Using Hibernate built-in connection pool (not for production use!)"); - log.info("Hibernate connection pool size: " + poolSize); - -// autocommit = PropertiesHelper.getBoolean(Environment.AUTOCOMMIT, props); - autocommit = true; - log.info("autocommit mode: " + autocommit); - -// isolation = PropertiesHelper.getInteger(Environment.ISOLATION, props); - isolation = 0; - if (isolation != null) - log.info("JDBC isolation level: " + Environment.isolationLevelToString(isolation)); - - if (driverClass == null) { - log.warn("no JDBC Driver class was specified by property " + Environment.DRIVER); - } else { - try { - // trying via forName() first to be as close to DriverManager's semantics - Class.forName(driverClass); - } catch (ClassNotFoundException cnfe) { -// try { -// ReflectHelper.classForName(driverClass); -// } catch (ClassNotFoundException e) { -// String msg = "JDBC Driver class not found: " + driverClass; -// log.error(msg, e); -// throw new HibernateException(msg, e); -// } - } - } - - // use a dummy directory to make sure only the connection provider knows - // the real directory where db is and then make sure hibernate always - // use the connection provider... - String directory = - props.getProperty(TopiaConnectionProviderTest.TEST_URL); - - url = directory; -// url = props.getProperty(Environment.URL); - -// if (url == null) { -// String msg = "JDBC URL was not specified by property " + Environment.URL; -// log.error(msg); -// throw new HibernateException(msg); -// } - - connectionProps = ConnectionProviderInitiator.getConnectionProperties(props); - - log.info("using driver: " + driverClass + " at URL: " + url); - // if debug level is enabled, then log the password, otherwise mask it - if (log.isDebugEnabled()) { - log.info("connection properties: " + connectionProps); - } else if (log.isInfoEnabled()) { -// log.info("connection properties: " + PropertiesHelper.maskOut(connectionProps, "password")); - } - } - - @Override - public Connection getConnection() throws SQLException { - if (log.isTraceEnabled()) - log.trace("total checked-out connections: " + checkedOut); - - synchronized (pool) { - if (!pool.isEmpty()) { - int last = pool.size() - 1; - if (log.isTraceEnabled()) { - log.trace("using pooled JDBC connection, pool size: " + last); - } - checkedOut++; - Connection pooled = (Connection) pool.remove(last); - if (isolation != null) - pooled.setTransactionIsolation(isolation.intValue()); - if (pooled.getAutoCommit() != autocommit) - pooled.setAutoCommit(autocommit); - return pooled; - } - } - - log.debug("opening new JDBC connection"); - Connection conn = DriverManager.getConnection(url, connectionProps); - if (isolation != null) conn.setTransactionIsolation(isolation); - if (conn.getAutoCommit() != autocommit) conn.setAutoCommit(autocommit); - - if (log.isDebugEnabled()) { - log.debug("created connection to: " + url + ", Isolation Level: " + conn.getTransactionIsolation()); - } -// if ( log.isTraceEnabled() ) - checkedOut++; - - return conn; - } - - @Override - public void closeConnection(Connection conn) throws SQLException { -// if ( log.isDebugEnabled() ) - checkedOut--; - - synchronized (pool) { - int currentSize = pool.size(); - if (currentSize < poolSize) { - if (log.isTraceEnabled()) { - log.trace("returning connection to pool, pool size: " + (currentSize + 1)); - } - pool.add(conn); - return; - } - } - - log.debug("closing JDBC connection"); - - conn.close(); - } - - @Override - protected void finalize() throws Throwable { - super.finalize(); - close(); - } - - public void close() { - - log.info("cleaning up connection pool: " + url); - - Iterator iter = pool.iterator(); - while (iter.hasNext()) { - try { - ((Connection) iter.next()).close(); - } catch (SQLException sqle) { - log.warn("problem closing pooled connection", sqle); - } - } - pool.clear(); - - } - - @Override - public boolean supportsAggressiveRelease() { - return false; - } - - @Override - public boolean isUnwrappableAs(Class unwrapType) { - return false; - } - - @Override - public <T> T unwrap(Class<T> unwrapType) { - return null; - } -} Deleted: trunk/topia-persistence-tck/src/test/java/org/nuiton/topia/framework/TopiaConnectionProviderTest.java =================================================================== --- trunk/topia-persistence-tck/src/test/java/org/nuiton/topia/framework/TopiaConnectionProviderTest.java 2012-05-09 14:22:27 UTC (rev 2448) +++ trunk/topia-persistence-tck/src/test/java/org/nuiton/topia/framework/TopiaConnectionProviderTest.java 2012-05-09 14:31:12 UTC (rev 2449) @@ -1,140 +0,0 @@ -/* - * #%L - * ToPIA :: Persistence - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2004 - 2011 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.framework; - -import org.junit.Assert; -import org.junit.Rule; -import org.junit.Test; -import org.nuiton.topia.TopiaContext; -import org.nuiton.topia.TopiaContextFactory; -import org.nuiton.topia.TopiaTckItDAOHelper; -import org.nuiton.topia.tck.TopiaDatabase; -import org.nuiton.topia.TopiaException; -import org.nuiton.topia.tck.it.Personne; -import org.nuiton.topia.test.entities.Person; -import org.nuiton.topia.test.entities.PersonDAO; - -import java.io.File; -import java.util.Locale; -import java.util.Properties; - -import static org.junit.Assert.assertNotNull; - -/** - * To test the {@link TopiaConnectionProvider} and make sure all connections - * are done from here... - * - * @author tchemit <chemit@codelutin.com> - * @since 2.5.3 - */ -public class TopiaConnectionProviderTest { - -// private static final Log log = -// LogFactory.getLog(TopiaConnectionProviderTest.class); - - public static final String TEST_URL = "testURL"; - - @Rule - public final TopiaDatabase db = - new TopiaDatabase("/TopiaConnectionProviderHardcoded.properties") { - - @Override - protected void onDbConfigurationCreate(Properties configuration, - File testdir, - String dbPath) { - - Assert.assertFalse(testdir.exists()); - - String dbPathFake = new File(testdir, "fake" + File.separator + "db").getAbsolutePath(); - - Assert.assertFalse(new File(dbPathFake).getParentFile().exists()); - - configuration.setProperty("dbPath", dbPath); - configuration.setProperty("dbPathFake", dbPathFake); - - // give the path where connection provider will create db - configuration.setProperty(TEST_URL, - "jdbc:h2:file:" + dbPath); - - // give a fake db path (we will make sure it is never create after hibernate usage). - configuration.setProperty(TopiaContextFactory.CONFIG_URL, - "jdbc:h2:file:" + dbPathFake); - } - }; - - @Test - public void testWithHardcoded() throws Exception { - -// Properties dbProperties = TestHelper.loadHibernateConfiguration( -// "/TopiaConnectionProviderHardcoded.properties"); -// -// File directory = new File(TestHelper.getDbName(testBasedir, "testWithHardcoded")); - - String dbPath = (String) db.getDbConfiguration().get("dbPath"); - String dbPathFake = (String) db.getDbConfiguration().get("dbPathFake"); - -// new File(directory, "real" + File.separator + "db").getAbsolutePath(); -// Assert.assertFalse(new File(dbPath).getParentFile().exists()); - -// String dbPathFake = new File(directory, "fake" + File.separator + "db").getAbsolutePath(); - -// Assert.assertFalse(new File(dbPathFake).getParentFile().exists()); - -// // give the path where connection provider will create db -// dbProperties.setProperty(TEST_URL, "jdbc:h2:file:" + dbPath); -// -// // give a fake db path (we will make sure it is never create after hibernate usage). -// dbProperties.setProperty(Environment.URL, "jdbc:h2:file:" + dbPathFake); -// -// root = TopiaContextFactory.getContext(dbProperties); - - Locale.setDefault(Locale.FRANCE); - - doStuffOnDb(); - - // the db file must have been created - Assert.assertTrue(new File(dbPath).getParentFile().exists()); - - // make sure the fake db path was never used - Assert.assertFalse(new File(dbPathFake).getParentFile().exists()); - } - - private void doStuffOnDb() throws TopiaException { - TopiaContext transaction = db.beginTransaction(); - - try { - PersonDAO dao = TopiaTckItDAOHelper.getPersonDAO(transaction); - - Person personne = dao.create(Personne.PROPERTY_NAME, "Jack Bauer"); - transaction.commitTransaction(); - String idPersonne = personne.getTopiaId(); - assertNotNull(idPersonne); - - transaction.commitTransaction(); - } finally { - transaction.closeContext(); - } - } -} Modified: trunk/topia-persistence-tck/src/test/java/org/nuiton/topia/persistence/NaturalIdTest.java =================================================================== --- trunk/topia-persistence-tck/src/test/java/org/nuiton/topia/persistence/NaturalIdTest.java 2012-05-09 14:22:27 UTC (rev 2448) +++ trunk/topia-persistence-tck/src/test/java/org/nuiton/topia/persistence/NaturalIdTest.java 2012-05-09 14:31:12 UTC (rev 2449) @@ -27,7 +27,6 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -//import org.hibernate.PropertyValueException; import org.junit.Assert; import org.junit.Rule; import org.junit.Test;
participants (1)
-
athimel@users.nuiton.org