Author: fdesbois Date: 2012-06-06 17:37:46 +0200 (Wed, 06 Jun 2012) New Revision: 2575 Url: http://nuiton.org/repositories/revision/topia/2575 Log: Move TopiaDatabase to src folder Added: trunk/topia-persistence-tck/src/main/java/org/nuiton/topia/tck/TopiaDatabase.java Removed: trunk/topia-persistence-tck/src/test/java/org/nuiton/topia/tck/TopiaDatabase.java Copied: trunk/topia-persistence-tck/src/main/java/org/nuiton/topia/tck/TopiaDatabase.java (from rev 2574, trunk/topia-persistence-tck/src/test/java/org/nuiton/topia/tck/TopiaDatabase.java) =================================================================== --- trunk/topia-persistence-tck/src/main/java/org/nuiton/topia/tck/TopiaDatabase.java (rev 0) +++ trunk/topia-persistence-tck/src/main/java/org/nuiton/topia/tck/TopiaDatabase.java 2012-06-06 15:37:46 UTC (rev 2575) @@ -0,0 +1,239 @@ +/* + * #%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.tck; + +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.rules.TestWatcher; +import org.junit.runner.Description; +import org.nuiton.topia.TopiaContext; +import org.nuiton.topia.TopiaContextFactory; +import org.nuiton.topia.TopiaException; +import org.nuiton.topia.persistence.TopiaEntity; + +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 abstract 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 Description description; + + private final String configurationPath; + + private final String classifier; + + protected TopiaDatabase(String configurationPath, String classifier) { + this.configurationPath = configurationPath; + this.classifier = classifier; + } + + public TopiaDatabase(String configurationPath) { + this(configurationPath, ""); + } + + /** + * Obtain all types persisted in this db. + * + * @return all persisted types in this db. + * @since 3.0 + */ + protected abstract Class<? extends TopiaEntity>[] getImplementationClasses(); + + @Override + protected void starting(Description description) { + + this.description=description; + + // get test directory + testBasedir = getTestSpecificDirectory( + description.getTestClass(), + description.getMethodName(), + classifier); + + 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, + getImplementationClassesAsString()); + + // make sure we always use a different directory + + String dbPath = new File(testBasedir, "db").getAbsolutePath(); + + String jdbcUrl = "jdbc:h2:file:" + dbPath; + + if (log.isInfoEnabled()) { + log.info("Use " + jdbcUrl); + } + + dbConfiguration.setProperty( + TopiaContextFactory.CONFIG_URL, jdbcUrl); + + 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(); + } + + public Description getDescription() { + return description; + } + + protected void onDbConfigurationCreate(Properties configuration, + File testDir, + String dbPath) { + + } + + protected final String getImplementationClassesAsString() { + StringBuilder buffer = new StringBuilder(); + for (Class<? extends TopiaEntity> aClass : getImplementationClasses()) { + buffer.append(',').append(aClass.getName()); + } + return buffer.substring(1); + } + + public static File getTestSpecificDirectory(Class<?> testClassName, + String methodName) { + return getTestSpecificDirectory(testClassName, methodName, ""); + } + + public static File getTestSpecificDirectory(Class<?> testClassName, + String methodName, + String classifier) { + // 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 + + if (StringUtils.isNotBlank(classifier)) { + dataBasePath += classifier; + } + dataBasePath += '_' + + TIMESTAMP; // and a timestamp + File databaseFile = new File(tempDirFile, dataBasePath); + return databaseFile; + } +} + Property changes on: trunk/topia-persistence-tck/src/main/java/org/nuiton/topia/tck/TopiaDatabase.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Deleted: trunk/topia-persistence-tck/src/test/java/org/nuiton/topia/tck/TopiaDatabase.java =================================================================== --- trunk/topia-persistence-tck/src/test/java/org/nuiton/topia/tck/TopiaDatabase.java 2012-06-01 16:02:06 UTC (rev 2574) +++ trunk/topia-persistence-tck/src/test/java/org/nuiton/topia/tck/TopiaDatabase.java 2012-06-06 15:37:46 UTC (rev 2575) @@ -1,239 +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.tck; - -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.junit.rules.TestWatcher; -import org.junit.runner.Description; -import org.nuiton.topia.TopiaContext; -import org.nuiton.topia.TopiaContextFactory; -import org.nuiton.topia.TopiaException; -import org.nuiton.topia.persistence.TopiaEntity; - -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 abstract 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 Description description; - - private final String configurationPath; - - private final String classifier; - - protected TopiaDatabase(String configurationPath, String classifier) { - this.configurationPath = configurationPath; - this.classifier = classifier; - } - - public TopiaDatabase(String configurationPath) { - this(configurationPath, ""); - } - - /** - * Obtain all types persisted in this db. - * - * @return all persisted types in this db. - * @since 3.0 - */ - protected abstract Class<? extends TopiaEntity>[] getImplementationClasses(); - - @Override - protected void starting(Description description) { - - this.description=description; - - // get test directory - testBasedir = getTestSpecificDirectory( - description.getTestClass(), - description.getMethodName(), - classifier); - - 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, - getImplementationClassesAsString()); - - // make sure we always use a different directory - - String dbPath = new File(testBasedir, "db").getAbsolutePath(); - - String jdbcUrl = "jdbc:h2:file:" + dbPath; - - if (log.isInfoEnabled()) { - log.info("Use " + jdbcUrl); - } - - dbConfiguration.setProperty( - TopiaContextFactory.CONFIG_URL, jdbcUrl); - - 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(); - } - - public Description getDescription() { - return description; - } - - protected void onDbConfigurationCreate(Properties configuration, - File testDir, - String dbPath) { - - } - - protected final String getImplementationClassesAsString() { - StringBuilder buffer = new StringBuilder(); - for (Class<? extends TopiaEntity> aClass : getImplementationClasses()) { - buffer.append(',').append(aClass.getName()); - } - return buffer.substring(1); - } - - public static File getTestSpecificDirectory(Class<?> testClassName, - String methodName) { - return getTestSpecificDirectory(testClassName, methodName, ""); - } - - public static File getTestSpecificDirectory(Class<?> testClassName, - String methodName, - String classifier) { - // 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 - - if (StringUtils.isNotBlank(classifier)) { - dataBasePath += classifier; - } - dataBasePath += '_' - + TIMESTAMP; // and a timestamp - File databaseFile = new File(tempDirFile, dataBasePath); - return databaseFile; - } -} -