continuum-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ri...@apache.org
Subject svn commit: r490836 - in /maven/continuum/branches/key-based-refactor/continuum-store/src/test/java/org/apache/maven/continuum/store/jdo: AbstractJdoStoreTestCase.java JdoProjectGroupTestCase.java
Date Thu, 28 Dec 2006 21:46:22 GMT
Author: rinku
Date: Thu Dec 28 13:46:21 2006
New Revision: 490836

URL: http://svn.apache.org/viewvc?view=rev&rev=490836
Log:
o  test database schema is now dropped after each test is run.

Modified:
    maven/continuum/branches/key-based-refactor/continuum-store/src/test/java/org/apache/maven/continuum/store/jdo/AbstractJdoStoreTestCase.java
    maven/continuum/branches/key-based-refactor/continuum-store/src/test/java/org/apache/maven/continuum/store/jdo/JdoProjectGroupTestCase.java

Modified: maven/continuum/branches/key-based-refactor/continuum-store/src/test/java/org/apache/maven/continuum/store/jdo/AbstractJdoStoreTestCase.java
URL: http://svn.apache.org/viewvc/maven/continuum/branches/key-based-refactor/continuum-store/src/test/java/org/apache/maven/continuum/store/jdo/AbstractJdoStoreTestCase.java?view=diff&rev=490836&r1=490835&r2=490836
==============================================================================
--- maven/continuum/branches/key-based-refactor/continuum-store/src/test/java/org/apache/maven/continuum/store/jdo/AbstractJdoStoreTestCase.java
(original)
+++ maven/continuum/branches/key-based-refactor/continuum-store/src/test/java/org/apache/maven/continuum/store/jdo/AbstractJdoStoreTestCase.java
Thu Dec 28 13:46:21 2006
@@ -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;
 
@@ -84,15 +89,21 @@
     /**
      * 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()
      */
@@ -100,17 +111,10 @@
     {
         super.setUp();
 
-        DefaultConfigurableJdoFactory jdoFactory = (DefaultConfigurableJdoFactory) lookup(
JdoFactory.ROLE );
+        jdoFactory = createJdoFactory();
 
-        jdoFactory.setUrl( URL_TEST_DATABASE );
-
-        jdoFactory.setDriverName( DRIVER_TEST_DATABASE );
-
-        jdoFactory.setUserName( USERNAME_TEST_DATABASE );
-
-        jdoFactory.setPassword( PASSWORD_TEST_DATABASE );
-
-        persistenceManager = jdoFactory.getPersistenceManagerFactory().getPersistenceManager();
+        // persistenceManager =
+        // jdoFactory.getPersistenceManagerFactory().getPersistenceManager();
     }
 
     /**
@@ -131,6 +135,8 @@
      */
     protected void createBuildDatabase() throws Exception
     {
+        if ( null == persistenceManager || persistenceManager.isClosed() )
+            persistenceManager = jdoFactory.getPersistenceManagerFactory().getPersistenceManager();
 
         Connection connection = (Connection) persistenceManager.getDataStoreConnection().getNativeConnection();
 
@@ -158,23 +164,81 @@
      */
     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();
+            }
+        }
     }
 
     /**
@@ -188,6 +252,21 @@
     }
 
     /**
+     * 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.
      * 
@@ -232,7 +311,7 @@
         {
             String sql = (String) it.next();
 
-            System.out.println( sql );
+            // System.out.println( sql );
 
             try
             {
@@ -250,17 +329,22 @@
     }
 
     /**
-     * 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;
     }
 }

Modified: maven/continuum/branches/key-based-refactor/continuum-store/src/test/java/org/apache/maven/continuum/store/jdo/JdoProjectGroupTestCase.java
URL: http://svn.apache.org/viewvc/maven/continuum/branches/key-based-refactor/continuum-store/src/test/java/org/apache/maven/continuum/store/jdo/JdoProjectGroupTestCase.java?view=diff&rev=490836&r1=490835&r2=490836
==============================================================================
--- maven/continuum/branches/key-based-refactor/continuum-store/src/test/java/org/apache/maven/continuum/store/jdo/JdoProjectGroupTestCase.java
(original)
+++ maven/continuum/branches/key-based-refactor/continuum-store/src/test/java/org/apache/maven/continuum/store/jdo/JdoProjectGroupTestCase.java
Thu Dec 28 13:46:21 2006
@@ -31,7 +31,7 @@
         assertNotNull( store );
     }
 
-    public void testVerifyDatabase() throws Exception
+    public void testGetAllProjectGroups() throws Exception
     {
         ProjectGroupStore store = (ProjectGroupStore) lookup( ProjectGroupStore.ROLE, "jdo"
);
         List list = store.getAllProjectGroups();



Mime
View raw message