Skip to content

Commit

Permalink
o test database schema is now dropped after each test is run.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rahul Thakur committed Dec 28, 2006
1 parent 6496226 commit b76e5e1
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,23 @@
import org.codehaus.plexus.PlexusTestCase;
import org.codehaus.plexus.jdo.DefaultConfigurableJdoFactory;
import org.codehaus.plexus.jdo.JdoFactory;
import org.codehaus.plexus.jdo.PlexusJdoUtils;
import org.codehaus.plexus.logging.LogEnabled;
import org.jpox.SchemaTool;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.Transaction;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

Expand Down Expand Up @@ -84,33 +89,32 @@ public abstract class AbstractJdoStoreTestCase extends PlexusTestCase
/**
* JDBC URL to connect to the target test database instance.
*/
private final String URL_TEST_DATABASE = "jdbc:hsqldb:mem:" + getName();
private final String URL_TEST_DATABASE = "jdbc:hsqldb:mem:" + getName() + new Date().getTime();

/**
* DDL for Database creation.
*/
private static final File SQL_DATABSE_SCHEMA = getTestFile( getBasedir(), "src/test/resources/schema.sql" );

/**
* Provides an interface to clients to execute queries on the underlying
* database.
*/
private PersistenceManager persistenceManager;

private DefaultConfigurableJdoFactory jdoFactory;

/**
* @see org.codehaus.plexus.PlexusTestCase#setUp()
*/
protected void setUp() throws Exception
{
super.setUp();

DefaultConfigurableJdoFactory jdoFactory = (DefaultConfigurableJdoFactory) lookup( JdoFactory.ROLE );

jdoFactory.setUrl( URL_TEST_DATABASE );

jdoFactory.setDriverName( DRIVER_TEST_DATABASE );

jdoFactory.setUserName( USERNAME_TEST_DATABASE );

jdoFactory.setPassword( PASSWORD_TEST_DATABASE );
jdoFactory = createJdoFactory();

persistenceManager = jdoFactory.getPersistenceManagerFactory().getPersistenceManager();
// persistenceManager =
// jdoFactory.getPersistenceManagerFactory().getPersistenceManager();
}

/**
Expand All @@ -131,6 +135,8 @@ protected void tearDown() throws Exception
*/
protected void createBuildDatabase() throws Exception
{
if ( null == persistenceManager || persistenceManager.isClosed() )
persistenceManager = jdoFactory.getPersistenceManagerFactory().getPersistenceManager();

Connection connection = (Connection) persistenceManager.getDataStoreConnection().getNativeConnection();

Expand Down Expand Up @@ -158,23 +164,81 @@ protected void createBuildDatabase() throws Exception
*/
protected void teardownBuildDatabase() throws Exception
{
PlexusJdoUtils.removeAll( getPersistenceManager(), Project.class );
PlexusJdoUtils.removeAll( getPersistenceManager(), ProjectGroup.class );
PlexusJdoUtils.removeAll( getPersistenceManager(), Schedule.class );
PlexusJdoUtils.removeAll( getPersistenceManager(), Profile.class );
PlexusJdoUtils.removeAll( getPersistenceManager(), Installation.class );
PlexusJdoUtils.removeAll( getPersistenceManager(), ScmResult.class );
PlexusJdoUtils.removeAll( getPersistenceManager(), BuildResult.class );
PlexusJdoUtils.removeAll( getPersistenceManager(), TestResult.class );
PlexusJdoUtils.removeAll( getPersistenceManager(), SuiteResult.class );
PlexusJdoUtils.removeAll( getPersistenceManager(), TestCaseFailure.class );
PlexusJdoUtils.removeAll( getPersistenceManager(), SystemConfiguration.class );
PlexusJdoUtils.removeAll( getPersistenceManager(), ProjectNotifier.class );
PlexusJdoUtils.removeAll( getPersistenceManager(), ProjectDeveloper.class );
PlexusJdoUtils.removeAll( getPersistenceManager(), ProjectDependency.class );
PlexusJdoUtils.removeAll( getPersistenceManager(), ChangeSet.class );
PlexusJdoUtils.removeAll( getPersistenceManager(), ChangeFile.class );
PlexusJdoUtils.removeAll( getPersistenceManager(), BuildDefinition.class );
if ( null == persistenceManager || persistenceManager.isClosed() )
persistenceManager = jdoFactory.getPersistenceManagerFactory().getPersistenceManager();

// deleteAllEntities( Project.class );
// deleteAllEntities( ProjectGroup.class );
// deleteAllEntities( Schedule.class );
// deleteAllEntities( Profile.class );
// deleteAllEntities( Installation.class );
// deleteAllEntities( ScmResult.class );
// deleteAllEntities( BuildResult.class );
// deleteAllEntities( TestResult.class );
// deleteAllEntities( SuiteResult.class );
// deleteAllEntities( TestCaseFailure.class );
// deleteAllEntities( SystemConfiguration.class );
// deleteAllEntities( ProjectNotifier.class );
// deleteAllEntities( ProjectDeveloper.class );
// deleteAllEntities( ProjectDependency.class );
// deleteAllEntities( ChangeSet.class );
// deleteAllEntities( ChangeFile.class );
// deleteAllEntities( BuildDefinition.class );

URL[] jdoFiles = new URL[] { this.getClass().getClassLoader().getResource( "META-INF/package.jdo" ) };

URL[] classFiles =
new URL[] { this.getClass().getClassLoader().getResource( Project.class.getCanonicalName() ),
this.getClass().getClassLoader().getResource( ProjectGroup.class.getCanonicalName() ),
this.getClass().getClassLoader().getResource( Schedule.class.getCanonicalName() ),
this.getClass().getClassLoader().getResource( Profile.class.getCanonicalName() ),
this.getClass().getClassLoader().getResource( Installation.class.getCanonicalName() ),
this.getClass().getClassLoader().getResource( ScmResult.class.getCanonicalName() ),
this.getClass().getClassLoader().getResource( BuildResult.class.getCanonicalName() ),
this.getClass().getClassLoader().getResource( TestResult.class.getCanonicalName() ),
this.getClass().getClassLoader().getResource( SuiteResult.class.getCanonicalName() ),
this.getClass().getClassLoader().getResource( TestCaseFailure.class.getCanonicalName() ),
this.getClass().getClassLoader().getResource( SystemConfiguration.class.getCanonicalName() ),
this.getClass().getClassLoader().getResource( ProjectNotifier.class.getCanonicalName() ),
this.getClass().getClassLoader().getResource( ProjectDeveloper.class.getCanonicalName() ),
this.getClass().getClassLoader().getResource( ProjectDependency.class.getCanonicalName() ),
this.getClass().getClassLoader().getResource( ChangeSet.class.getCanonicalName() ),
this.getClass().getClassLoader().getResource( BuildDefinition.class.getCanonicalName() ) };

// prepare System properties that the SchemaTool expects
System.setProperty( SchemaTool.JDO_DATASTORE_DRIVERNAME_PROPERTY, DRIVER_TEST_DATABASE );
System.setProperty( SchemaTool.JDO_DATASTORE_URL_PROPERTY, URL_TEST_DATABASE );
System.setProperty( SchemaTool.JDO_DATASTORE_USERNAME_PROPERTY, USERNAME_TEST_DATABASE );
System.setProperty( SchemaTool.JDO_DATASTORE_PASSWORD_PROPERTY, PASSWORD_TEST_DATABASE );
SchemaTool.deleteSchemaTables( jdoFiles, null, true );
}

/**
* Deletes records for a given entity.
*
* @param klass Entity class for which the records are to be deleted.
*
*/
private void deleteAllEntities( Class klass )
{
Transaction tx = persistenceManager.currentTransaction();

try
{
tx.begin();

Query query = persistenceManager.newQuery( klass );
query.deletePersistentAll();

tx.commit();
}
finally
{
if ( tx.isActive() )
{
tx.rollback();
}
}
}

/**
Expand All @@ -187,6 +251,21 @@ protected PersistenceManager getPersistenceManager()
return persistenceManager;
}

/**
* Extensions are allowed to implement and return a list of SQL script
* {@link File} instances that are to be read and loaded into the target
* test database.
*
* @return List of locations of SQL scripts
*/
protected List getSQLScripts()
{
List list = new ArrayList();
// add default test data source.
list.add( getTestFile( getBasedir(), SQL_TEST_DATA ) );
return list;
}

/**
* Reads SQL statements from the specified file and uses the passed in
* {@link Connection} to populate the target database instance.
Expand Down Expand Up @@ -232,7 +311,7 @@ private void loadSQL( File sqlData, Connection connection ) throws Exception
{
String sql = (String) it.next();

System.out.println( sql );
// System.out.println( sql );

try
{
Expand All @@ -250,17 +329,22 @@ private void loadSQL( File sqlData, Connection connection ) throws Exception
}

/**
* Extensions are allowed to implement and return a list of SQL script
* {@link File} instances that are to be read and loaded into the target
* test database.
*
* @return List of locations of SQL scripts
* @return
* @throws Exception
*/
protected List getSQLScripts()
private DefaultConfigurableJdoFactory createJdoFactory() throws Exception
{
List list = new ArrayList();
// add default test data source.
list.add( getTestFile( getBasedir(), SQL_TEST_DATA ) );
return list;
DefaultConfigurableJdoFactory jdoFactory = (DefaultConfigurableJdoFactory) lookup( JdoFactory.ROLE );

jdoFactory.setUrl( URL_TEST_DATABASE );

jdoFactory.setDriverName( DRIVER_TEST_DATABASE );

jdoFactory.setUserName( USERNAME_TEST_DATABASE );

jdoFactory.setPassword( PASSWORD_TEST_DATABASE );

jdoFactory.setProperty( "javax.jdo.option.NontransactionalRead", "true" );
return jdoFactory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void testComponentLookup() throws Exception
assertNotNull( store );
}

public void testVerifyDatabase() throws Exception
public void testGetAllProjectGroups() throws Exception
{
ProjectGroupStore store = (ProjectGroupStore) lookup( ProjectGroupStore.ROLE, "jdo" );
List list = store.getAllProjectGroups();
Expand Down

0 comments on commit b76e5e1

Please sign in to comment.