db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From myrn...@apache.org
Subject svn commit: r703246 - in /db/derby/code/trunk/java: engine/org/apache/derby/impl/store/raw/data/ testing/org/apache/derbyTesting/functionTests/tests/store/ testing/org/apache/derbyTesting/functionTests/util/ testing/org/apache/derbyTesting/junit/
Date Thu, 09 Oct 2008 20:20:32 GMT
Author: myrnavl
Date: Thu Oct  9 13:20:32 2008
New Revision: 703246

URL: http://svn.apache.org/viewvc?rev=703246&view=rev
Log:
DERBY-3875; closing containers after a problem has been found allows restoreFrom to work correctly.
  Patch contributions by Jason McLaurin, Kristian Waagan and Myrna van Lunteren.

Added:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/store/BackupRestoreTest.java
  (with props)
Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/RAFContainer.java
    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/PrivilegedFileOpsForTests.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/Utilities.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/RAFContainer.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/RAFContainer.java?rev=703246&r1=703245&r2=703246&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/RAFContainer.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/RAFContainer.java
Thu Oct  9 13:20:32 2008
@@ -852,8 +852,18 @@
         {
             return AccessController.doPrivileged( this) != null;
         }
-        catch( PrivilegedActionException pae){ throw (StandardException) pae.getException();}
-        finally{ actionIdentity = null; }
+        catch( PrivilegedActionException pae) { 
+            closeContainer();
+            throw (StandardException) pae.getException();
+        }
+        catch (RuntimeException e) {
+            closeContainer();
+            throw e;
+        }
+        finally
+        { 
+            actionIdentity = null; 
+        }
     }
 
 	private synchronized void stubbify(LogInstant instant)

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/store/BackupRestoreTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/store/BackupRestoreTest.java?rev=703246&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/store/BackupRestoreTest.java
(added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/store/BackupRestoreTest.java
Thu Oct  9 13:20:32 2008
@@ -0,0 +1,120 @@
+/*
+
+   Derby - Class org.apache.derbyTesting.functionTests.tests.store.BackupRestoreTest
+
+   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.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.sql.CallableStatement;
+import java.sql.SQLException;
+import javax.sql.DataSource;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import org.apache.derbyTesting.functionTests.util.PrivilegedFileOpsForTests;
+import org.apache.derbyTesting.junit.BaseJDBCTestCase;
+import org.apache.derbyTesting.junit.JDBCDataSource;
+import org.apache.derbyTesting.junit.SupportFilesSetup;
+import org.apache.derbyTesting.junit.TestConfiguration;
+import org.apache.derbyTesting.junit.Utilities;
+
+public class BackupRestoreTest
+    extends BaseJDBCTestCase {
+
+    public BackupRestoreTest(String name) {
+        super(name);
+    }
+
+    /**
+     * See DERBY-3875.
+     * <p>
+     * Steps in the test:
+     *  1) Create a database and perform a backup.
+     *  2) Shutdown the Derby engine.
+     *  3) Corrupt one of the database files.
+     *  4) Boot corrupted database.
+     *  5) Restore backup.
+     * <p>
+     * With the bug present, the test failed in step 5.
+     * Note that the test did fail only on Windows platforms, which is probably
+     * because of differences in the file system code.
+     */
+    public void testDerby3875()
+            throws SQLException, IOException {
+        // Create the database.
+        println("Creating database");
+        getConnection();
+        // Backup the database.
+        println("Backing up database");
+        String dbBackup = SupportFilesSetup.getReadWrite("dbbackup").getPath();
+        CallableStatement cs = prepareCall(
+                "CALL SYSCS_UTIL.SYSCS_BACKUP_DATABASE(?)");
+        cs.setString(1, dbBackup);
+        cs.execute();
+        cs.close();
+        // Shutdown the database.
+        getTestConfiguration().shutdownEngine();
+
+        // Corrupt one of the database files.
+        File dataDir = new File("system/" +
+                getTestConfiguration().getDefaultDatabaseName(), "seg0");
+        File df = new File(dataDir, "c10.dat");
+        assertTrue("File to corrupt doesn't exist: " + df.getPath(),
+                PrivilegedFileOpsForTests.exists(df));
+        println("Corrupting data file");
+        byte[] zeros = new byte[(int)PrivilegedFileOpsForTests.length(df)];
+        FileOutputStream fout =
+                PrivilegedFileOpsForTests.getFileOutputStream(df);
+        fout.write(zeros);
+        fout.flush();
+        fout.close();
+
+        // Reboot the database, which should fail.
+        try {
+            println("Rebooting corrupted database");
+            getConnection();
+            fail("Reboot of currupted database should have failed");
+        } catch (SQLException sqle) {
+            assertSQLState("XJ040", sqle);
+        }
+
+        // Now try to restore database.
+        println("Restoring database");
+        String tmp[] = Utilities.split(
+                getTestConfiguration().getDefaultDatabaseName(), '/');
+        final String dbName = tmp[tmp.length -1];
+        DataSource ds = JDBCDataSource.getDataSource();
+        JDBCDataSource.setBeanProperty(ds, "connectionAttributes", 
+                ("restoreFrom=" + dbBackup + "/" + dbName ));
+        assertNotNull(ds.getConnection());
+    }
+
+    /**
+     * Returns a suite running with a single use database with the embedded
+     * driver only.
+     *
+     * @return A test suite.
+     */
+    public static Test suite() {
+        TestSuite suite = new TestSuite(BackupRestoreTest.class);
+        return new SupportFilesSetup(
+                TestConfiguration.singleUseDatabaseDecorator(suite));
+    }
+}

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/store/BackupRestoreTest.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=703246&r1=703245&r2=703246&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
Thu Oct  9 13:20:32 2008
@@ -56,6 +56,7 @@
         suite.addTest(Derby3625Test.suite());
         suite.addTest(PositionedStoreStreamTest.suite());
         suite.addTest(OSReadOnlyTest.suite());
+        suite.addTest(BackupRestoreTest.suite());
         // Encryption only supported for Derby in J2SE/J2EE environments.
         // J2ME (JSR169) does not support encryption.
         if (JDBC.vmSupportsJDBC3()) {

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/PrivilegedFileOpsForTests.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/PrivilegedFileOpsForTests.java?rev=703246&r1=703245&r2=703246&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/PrivilegedFileOpsForTests.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/PrivilegedFileOpsForTests.java
Thu Oct  9 13:20:32 2008
@@ -23,6 +23,7 @@
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
 import java.io.FileReader;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
@@ -135,4 +136,31 @@
             throw (FileNotFoundException)pae.getCause();
         }
     }
+
+    /**
+     * Returns a file output stream for the specified file.
+     *
+     * @param file the file to create a stream for
+     * @return An output stream.
+     * @throws FileNotFoundException if the specified file does not exist
+     * @throws SecurityException if the required permissions to write the file,
+     *      or the path it is in, are missing
+     */
+    public static FileOutputStream getFileOutputStream(final File file)
+            throws FileNotFoundException {
+        if (file == null) {
+            throw new IllegalArgumentException("file cannot be <null>");
+        }
+        try {
+            return (FileOutputStream)AccessController.doPrivileged(
+                    new PrivilegedExceptionAction() {
+                        public Object run()
+                                throws FileNotFoundException {
+                            return new FileOutputStream(file);
+                        }
+                    });
+        } catch (PrivilegedActionException pae) {
+            throw (FileNotFoundException)pae.getCause();
+        }
+    }
 }

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/Utilities.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/Utilities.java?rev=703246&r1=703245&r2=703246&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/Utilities.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/Utilities.java Thu Oct
 9 13:20:32 2008
@@ -33,6 +33,7 @@
 import java.sql.ResultSet;
 import java.sql.ResultSetMetaData;
 import java.sql.SQLException;
+import java.util.StringTokenizer;
 
 
 /**
@@ -195,5 +196,39 @@
         return new BufferedReader(new StringReader(
                 NetworkServerTestSetup.getNetworkServerControl().getSysinfo()));
     }
+    
+    /**
+     * Splits a string around matches of the given delimiter character.
+     * Copied from org.apache.derby.iapi.util.StringUtil
+     *
+     * Where applicable, this method can be used as a substitute for
+     * <code>String.split(String regex)</code>, which is not available
+     * on a JSR169/Java ME platform.
+     *
+     * @param str the string to be split
+     * @param delim the delimiter
+     * @throws NullPointerException if str is null
+     */
+    static public String[] split(String str, char delim)
+    {
+        if (str == null) {
+            throw new NullPointerException("str can't be null");
+        }
+
+        // Note the javadoc on StringTokenizer:
+        //     StringTokenizer is a legacy class that is retained for
+        //     compatibility reasons although its use is discouraged in
+        //     new code.
+        // In other words, if StringTokenizer is ever removed from the JDK,
+        // we need to have a look at String.split() (or java.util.regex)
+        // if it is supported on a JSR169/Java ME platform by then.
+        StringTokenizer st = new StringTokenizer(str, String.valueOf(delim));
+        int n = st.countTokens();
+        String[] s = new String[n];
+        for (int i = 0; i < n; i++) {
+            s[i] = st.nextToken();
+        }
+        return s;
+    }
 
 }



Mime
View raw message