db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kmars...@apache.org
Subject svn commit: r1158949 - in /db/derby/code/trunk/java/testing/org/apache/derbyTesting: functionTests/tests/store/RecoveryTest.java functionTests/tests/store/_Suite.java functionTests/util/derby_tests.policy junit/BaseTestCase.java
Date Wed, 17 Aug 2011 22:28:39 GMT
Author: kmarsden
Date: Wed Aug 17 22:28:39 2011
New Revision: 1158949

URL: http://svn.apache.org/viewvc?rev=1158949&view=rev
Log:
DERBY-4249 Create a simple store recovery test in JUnit

Contributed by Siddharth Srivastava
Simple test to verify recovery.  
- Connect, create a table, commit and shutdown the database.
- fork a jvm, add one row, commit, add another row, exit the jvm.
- Reconnect with the first jvm and verify that the first row is there and the second is not.

Launches a junit method for forked jvm and reports any errors back.
Currently disabled for weme due to DERBY-4647. 


Added:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/store/RecoveryTest.java
  (with props)
Modified:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/store/_Suite.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/derby_tests.policy
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseTestCase.java

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/store/RecoveryTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/store/RecoveryTest.java?rev=1158949&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/store/RecoveryTest.java
(added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/store/RecoveryTest.java
Wed Aug 17 22:28:39 2011
@@ -0,0 +1,113 @@
+/*
+ *
+ * Derby - Class org.apache.derbyTesting.functionTests.tests.store.RecoveryTest
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, 
+ * software distributed under the License is distributed on an 
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
+ * either express or implied. See the License for the specific 
+ * language governing permissions and limitations under the License.
+ */
+
+package org.apache.derbyTesting.functionTests.tests.store;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.derbyTesting.junit.BaseJDBCTestCase;
+import org.apache.derbyTesting.junit.BaseTestCase;
+import org.apache.derbyTesting.junit.CleanDatabaseTestSetup;
+import org.apache.derbyTesting.junit.JDBC;
+import org.apache.derbyTesting.junit.SecurityManagerSetup;
+import org.apache.derbyTesting.junit.TestConfiguration;
+
+/**
+ * DERBY-4249 This class can be used as a framework to create junit Recovery
+ * Test or converting harness Recovery Tests to junit tests.
+ **/
+
+public final class RecoveryTest extends BaseJDBCTestCase
+{
+    public RecoveryTest(String name)
+    {
+        super(name);
+    }
+
+    public static Test suite()
+    {
+        // Add the test case into the test suite
+        TestSuite suite = new TestSuite("RecoveryTest");
+        //DERBY-4647 exec does not work on weme
+        if (BaseTestCase.isJ9Platform())
+            return suite;
+        suite.addTest(decorateTest());
+        return suite;
+    }
+
+    private static Test decorateTest()
+    {
+        Test test = new CleanDatabaseTestSetup(TestConfiguration.embeddedSuite(
+                RecoveryTest.class));
+        return test;
+    }
+
+    /**
+     * Tests the recovery of database. The test achieves its purpose 
+     * as follows:
+     * Connect, create a table, commit and shutdown the database.
+     * fork a jvm, add one row, commit, add another row, exit the jvm(killed).
+     * Reconnect with the first jvm and verify that the first row is there 
+     * and the second is not. 
+     * When a new JVM connects, the log entries are read one by one and if 
+     * then rolls back to the transaction boundaries, then the database is
+     * in a consistent state. 
+     * @throws Exception
+     */
+    public void testBasicRecovery() throws Exception
+    {
+        Connection c = getConnection();
+        c.setAutoCommit(false);
+        Statement st = createStatement();
+        st.executeUpdate("create table t( i int )");
+        c.commit();
+        TestConfiguration.getCurrent().shutdownDatabase();
+        st.close();
+        c.close();
+
+        //fork JVM
+        assertLaunchedJUnitTestMethod("org.apache.derbyTesting.functionTests.tests.store.RecoveryTest.launchRecoveryInsert");
+
+        st = createStatement();
+        ResultSet rs = st.executeQuery("select i from t");
+        JDBC.assertFullResultSet(rs, new String[][] { { "1956" } } );
+    }
+
+    /**
+     * This fixture is used by the forked JVM to add and commit rows to the
+     * database in the first JVM.
+     * @throws SQLException 
+     **/
+    public void launchRecoveryInsert() throws SQLException
+    {
+            Connection c = getConnection();
+            c.setAutoCommit(false);
+            Statement st = createStatement();
+            st.executeUpdate("insert into t(i) values (1956)");
+            c.commit();
+            st.executeUpdate("insert into t(i) values (2011)");
+    }
+}
\ No newline at end of file

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/store/RecoveryTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/store/_Suite.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/store/_Suite.java?rev=1158949&r1=1158948&r2=1158949&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/store/_Suite.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/store/_Suite.java
Wed Aug 17 22:28:39 2011
@@ -24,7 +24,7 @@ package org.apache.derbyTesting.function
 import org.apache.derbyTesting.junit.BaseTestCase;
 import org.apache.derbyTesting.junit.JDBC;
 
-import junit.framework.Test; 
+import junit.framework.Test;  
 import junit.framework.TestSuite;
 import org.apache.derby.iapi.services.sanity.SanityManager;
 
@@ -51,7 +51,7 @@ public class _Suite extends BaseTestCase
     public static Test suite() {
 
         TestSuite suite = new TestSuite("store");
-        
+
         suite.addTest(BootAllTest.suite());
         suite.addTest(ClassLoaderBootTest.suite());
         suite.addTest(StreamingColumnTest.suite());
@@ -76,7 +76,8 @@ public class _Suite extends BaseTestCase
         suite.addTest(MadhareTest.suite());
         suite.addTest(LongColumnTest.suite());
         suite.addTest(RowLockBasicTest.suite());
-        
+        suite.addTest(RecoveryTest.suite());
+
         /* Tests that only run in sane builds */
         if (SanityManager.DEBUG) {
             suite.addTest(HoldCursorExternalSortJDBC30Test.suite());
@@ -91,7 +92,7 @@ public class _Suite extends BaseTestCase
             suite.addTest(EncryptionKeyDESTest.suite());
             suite.addTest(EncryptionAESTest.suite());
         }
-        
+
         return suite;
     }
 }

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/derby_tests.policy
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/derby_tests.policy?rev=1158949&r1=1158948&r2=1158949&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/derby_tests.policy
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/derby_tests.policy
Wed Aug 17 22:28:39 2011
@@ -211,6 +211,7 @@ grant codeBase "${derbyTesting.codejar}d
   // ij needs permission to read the sql files in this jar
   permission java.io.FilePermission "${derbyTesting.testjarpath}", "read";
   
+
 };
 
 //
@@ -225,6 +226,9 @@ grant codeBase "${derbyTesting.testjar}d
   // Access all files under ${user.dir}to write the test directory structure
   permission java.io.FilePermission "${user.dir}${/}-", "read,write,delete"; 
 
+  // Tests need to be able to exec a java program.
+  permission java.io.FilePermission "${java.home}${/}-" "execute";	
+
   // When running with useprocess=false need to install and uninstall
   // the security manager and allow setIO to change the system err and out
   // streams. Currently the nist suite runs with useprocess=false.
@@ -297,7 +301,10 @@ grant codeBase "${derbyTesting.codeclass
   // Access all files under ${user.dir}to write the test directory structure
   // Also covers extin, extout and extinout locations
   permission java.io.FilePermission "${user.dir}${/}-", "read,write,delete"; 
-    
+   
+  // Tests need to be able to exec a java program.
+  permission java.io.FilePermission "${java.home}${/}-" "execute";
+ 
   // These permissions are needed to load the JCE for encryption with Sun and IBM JDK131.
   // JDK14 has the JCE  preloaded
   permission java.security.SecurityPermission "insertProvider.SunJCE";

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseTestCase.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseTestCase.java?rev=1158949&r1=1158948&r2=1158949&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseTestCase.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseTestCase.java Wed Aug
17 22:28:39 2011
@@ -506,9 +506,12 @@ public abstract class BaseTestCase
 	    Process pr = execJavaCmd(cmd);
 	    String output = readProcessOutput(pr);
 	    int exitValue = pr.exitValue();
+	    String expectedStrings = "";
+	    for (int i = 0; i < expectedString.length; i++) 
+	        expectedStrings += "\t[" +i + "]" + expectedString[i] +  "\n";
 	    Assert.assertEquals("expectedExitValue:" + expectedExitValue +
 	            " does not match exitValue:" + exitValue +"\n" +
-	            "expected output:" + expectedString + 
+	            "expected output strings:\n" + expectedStrings + 
 	            " actual output:" + output,
 	            expectedExitValue, exitValue);
 	    if (expectedString != null) {
@@ -802,4 +805,19 @@ public abstract class BaseTestCase
         ae.initCause(t);
         throw ae;
     }
+    
+    /**
+     * assert a method from an executing test
+     * 
+     * @param testLaunchMethod
+     *            complete pathname of the method to be executed
+     * @throws Exception
+     */
+    public static void assertLaunchedJUnitTestMethod(String testLaunchMethod)
+            throws Exception 
+    {
+        String[] cmd = new String[] { "junit.textui.TestRunner", "-m",
+        testLaunchMethod };
+        assertExecJavaCmdAsExpected(new String[] { "OK (1 test)" }, cmd, 0);
+    }
 } // End class BaseTestCase



Mime
View raw message