Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.nuiton.topia.junit;
  
  /*
   * #%L
   * ToPIA :: JUnit
   * $Id: AbstractDatabaseResource.java 2981 2014-01-17 17:38:55Z athimel $
   * $HeadURL: https://svn.nuiton.org/topia/tags/topia-3.0-beta-2/topia-junit/src/main/java/org/nuiton/topia/junit/AbstractDatabaseResource.java $
   * %%
   * Copyright (C) 2004 - 2014 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%
  */
 
 
 import java.io.File;
Put this class as a Rule in test to obtain a new isolated db for each test.

Here is a simple example of usage :

 public class MyTest {

   \@Rule
   public final TopiaDatabase db = new TopiaDatabase();

   \@Test
   public void testMethod() throws TopiaException {

       TopiaContext tx = db.beginTransaction();
       ...
 }
 
The db created will be unique for each test method (and for each build also).

You don't need to close any transaction, it will be done for you and the end of each method test.

Created on 11/22/13.

Author(s):
Tony Chemit <chemit@codelutin.com>
Since:
3.0
 
 public abstract class AbstractDatabaseResource<PersistenceContext extends TopiaPersistenceContext, ApplicationContext extends AbstractTopiaApplicationContext<PersistenceContext>> extends TestWatcher {

    
Logger.
 
     private static final Log log = LogFactory.getLog(AbstractDatabaseResource.class);
 
     private File testBasedir;
 
     private Properties dbConfiguration;
 
     private ApplicationContext applicationContext;
 
     private Configuration hibernateCfg;
 
     private final String configurationPath;
 
     protected abstract ApplicationContext createApplicationContext(Properties dbConfiguration);
 
     protected abstract String getImplementationClassesAsString();
 
     public AbstractDatabaseResource(String configurationPath) {
         this. = configurationPath;
     }
 
     @Override
     protected void starting(Description description) {
 
         // get test directory
          = ConfigurationHelper.getTestSpecificDirectory(
                 description.getTestClass(),
                description.getMethodName());
        if (.isDebugEnabled()) {
            .debug("testBasedir = " + );
        }
        // create the root context
        try {
             = new Properties();
            InputStream stream =
                    getClass().getResourceAsStream();
            try {
                .load(stream);
            } finally {
                stream.close();
            }
            .setProperty(
                    .,
                    getImplementationClassesAsString());
            // make sure we always use a different directory
            String dbPath = new File("db").getAbsolutePath();
            if (.isDebugEnabled()) {
                .debug("dbPath = " + dbPath);
            }
            .setProperty(
                    ."jdbc:h2:file:" + dbPath);
            onDbConfigurationCreate(dbPath);
            Field field = FieldUtils.getField(AbstractTopiaApplicationContext.class"hibernateProvider"true);
            HibernateProvider hibernateProvider = (HibernateProviderfield.get();
             = hibernateProvider.getHibernateConfiguration();
        } catch (Exception e) {
            throw new IllegalStateException(
                    "Could not start db at " + e);
        }
    }
    @Override
    public void finished(Description description) {
        if ( != null && !.isClosed()) {
            try {
                .closeContext();
            } catch (TopiaException e) {
                if (.isErrorEnabled()) {
                    .error("Could not close topia root context"e);
                }
            }
        }
         = null;
         = null;
         = null;
    }
    public File getTestBasedir() {
        return ;
    }
    public Properties getDbConfiguration() {
        return ;
    }
    public ApplicationContext getApplicationContext() {
        return ;
    }
    public Configuration getHibernateCfg() {
        return ;
    }
    public PersistenceContext beginTransaction() throws TopiaException {
        return .newPersistenceContext();
    }
    protected void onDbConfigurationCreate(Properties configuration,
                                           File testDir,
                                           String dbPath) {
    }
New to GrepCode? Check out our FAQ X