You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@continuum.apache.org by ri...@apache.org on 2007/11/26 07:52:39 UTC

svn commit: r598146 - in /maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/maven/continuum/store: StoreTestCase.java jpa/JpaProjectStoreTest.java

Author: rinku
Date: Sun Nov 25 22:52:37 2007
New Revision: 598146

URL: http://svn.apache.org/viewvc?rev=598146&view=rev
Log:
o  Extracted an abstract test case to allow reuse in TestCase extensions that need to load test data from SQL files.

Added:
    maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/maven/continuum/store/StoreTestCase.java   (with props)
Modified:
    maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/maven/continuum/store/jpa/JpaProjectStoreTest.java

Added: maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/maven/continuum/store/StoreTestCase.java
URL: http://svn.apache.org/viewvc/maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/maven/continuum/store/StoreTestCase.java?rev=598146&view=auto
==============================================================================
--- maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/maven/continuum/store/StoreTestCase.java (added)
+++ maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/maven/continuum/store/StoreTestCase.java Sun Nov 25 22:52:37 2007
@@ -0,0 +1,109 @@
+/**
+ * 
+ */
+package org.apache.maven.continuum.store;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStreamReader;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.openjpa.persistence.test.SingleEMTestCase;
+
+/**
+ * Test Case support class that allows extensions to load test data from specified SQL files.
+ * 
+ * @author <a href='mailto:rahul.thakur.xdev@gmail.com'>Rahul Thakur</a>
+ * @since 1.2
+ * @version $Id$
+ */
+public abstract class StoreTestCase extends SingleEMTestCase
+{
+
+    /**
+     * Imports sql from the specified file.
+     * 
+     * @param sqlResource
+     *            Resource containing sql
+     */
+    public void setSqlSource( File sqlResource )
+    {
+        try
+        {
+            // TODO: Use Logger!
+            // System.out.println( "Loading sql: " + sqlResource.getAbsolutePath() );
+            List<String> statements = new ArrayList<String>( 20 );
+            BufferedReader br = new BufferedReader( new InputStreamReader( new FileInputStream( sqlResource ) ) );
+            String line = null;
+            StringBuffer currentStatement = new StringBuffer();
+            while ( ( line = br.readLine() ) != null )
+            {
+                if ( line.trim().length() == 0 )
+                    continue;
+                if ( line.trim().startsWith( "#" ) )
+                    continue;
+
+                currentStatement.append( line );
+                if ( line.endsWith( ";" ) )
+                {
+                    statements.add( currentStatement.toString() );
+                    currentStatement = new StringBuffer();
+                }
+            }
+            // append a line if missing a ';'
+            if ( currentStatement.length() > 0 )
+            {
+                statements.add( currentStatement.toString() );
+            }
+            runSQLStatements( statements );
+        }
+        catch ( Throwable e )
+        {
+            // TODO: User logger!
+            System.err.println( "Problem executing SQL!" );
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     * Run a bunch of SQL statements.
+     * 
+     * @param statements
+     *            Statements to run.
+     * @throws SQLException
+     */
+    public void runSQLStatements( final List<String> statements ) throws SQLException
+    {
+        for ( String qry : statements )
+        {
+            Connection con = (Connection) this.em.getConnection();
+            try
+            {
+                Statement stmt = con.createStatement();
+                System.out.println( qry );
+                stmt.execute( qry );
+                con.commit();
+            }
+            catch ( SQLException e )
+            {
+                try
+                {
+                    con.rollback();
+                }
+                catch ( SQLException e1 )
+                {
+                    // TODO: Use logger!
+                    System.err.println( "Unable to rollback transaction." );
+                    throw e1;
+                }
+                throw e;
+            }
+        }
+    }
+
+}

Propchange: maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/maven/continuum/store/StoreTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/maven/continuum/store/StoreTestCase.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Modified: maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/maven/continuum/store/jpa/JpaProjectStoreTest.java
URL: http://svn.apache.org/viewvc/maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/maven/continuum/store/jpa/JpaProjectStoreTest.java?rev=598146&r1=598145&r2=598146&view=diff
==============================================================================
--- maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/maven/continuum/store/jpa/JpaProjectStoreTest.java (original)
+++ maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/maven/continuum/store/jpa/JpaProjectStoreTest.java Sun Nov 25 22:52:37 2007
@@ -3,19 +3,12 @@
  */
 package org.apache.maven.continuum.store.jpa;
 
-import java.io.BufferedReader;
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.InputStreamReader;
-import java.sql.Connection;
-import java.sql.SQLException;
-import java.sql.Statement;
-import java.util.ArrayList;
 import java.util.List;
 import java.util.Properties;
 
+import org.apache.maven.continuum.store.StoreTestCase;
 import org.apache.openjpa.persistence.OpenJPAQuery;
-import org.apache.openjpa.persistence.test.SingleEMTestCase;
 
 /**
  * @author <a href='mailto:rahul.thakur.xdev@gmail.com'>Rahul Thakur</a>
@@ -26,7 +19,7 @@
  *      http://mail-archives.apache.org/mod_mbox/openjpa-users/200706.mbox/%3CBF2B99E3-7EF3-4E99-91E1-8AEB940524C7@apache.org%3E
  *      </a>
  */
-public class JpaProjectStoreTest extends SingleEMTestCase
+public class JpaProjectStoreTest extends StoreTestCase
 {
     private static final String PERSISTENT_UNIT_CONTINUUM_STORE = "continuum-store";
 
@@ -71,88 +64,6 @@
     {
         super.tearDown();
         // do nothing
-    }
-
-    /**
-     * Imports sql from the specified file.
-     * 
-     * @param sqlResource
-     *            Resource containing sql
-     */
-    public void setSqlSource( File sqlResource )
-    {
-        try
-        {
-            // TODO: Use Logger!
-            // System.out.println( "Loading sql: " + sqlResource.getAbsolutePath() );
-            List<String> statements = new ArrayList<String>( 20 );
-            BufferedReader br = new BufferedReader( new InputStreamReader( new FileInputStream( sqlResource ) ) );
-            String line = null;
-            StringBuffer currentStatement = new StringBuffer();
-            while ( ( line = br.readLine() ) != null )
-            {
-                if ( line.trim().length() == 0 )
-                    continue;
-                if ( line.trim().startsWith( "#" ) )
-                    continue;
-
-                currentStatement.append( line );
-                if ( line.endsWith( ";" ) )
-                {
-                    statements.add( currentStatement.toString() );
-                    currentStatement = new StringBuffer();
-                }
-            }
-            // append a line if missing a ';'
-            if ( currentStatement.length() > 0 )
-            {
-                statements.add( currentStatement.toString() );
-            }
-            runSQLStatements( statements );
-        }
-        catch ( Throwable e )
-        {
-            // TODO: User logger!
-            System.err.println( "Problem executing SQL!" );
-            e.printStackTrace();
-        }
-    }
-
-    /**
-     * Run a bunch of SQL statements.
-     * 
-     * @param statements
-     *            Statements to run.
-     * @throws SQLException
-     */
-
-    public void runSQLStatements( final List<String> statements ) throws SQLException
-    {
-        for ( String qry : statements )
-        {
-            Connection con = (Connection) this.em.getConnection();
-            try
-            {
-                Statement stmt = con.createStatement();
-                System.out.println( qry );
-                stmt.execute( qry );
-                con.commit();
-            }
-            catch ( SQLException e )
-            {
-                try
-                {
-                    con.rollback();
-                }
-                catch ( SQLException e1 )
-                {
-                    // TODO: Use logger!
-                    System.err.println( "Unable to rollback transaction." );
-                    throw e1;
-                }
-                throw e;
-            }
-        }
     }
 
 }