db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kmars...@apache.org
Subject svn commit: r704964 - in /db/derby/code/trunk/java/testing/org/apache/derbyTesting: functionTests/util/PrivilegedFileOpsForTests.java junit/BaseTestCase.java
Date Wed, 15 Oct 2008 17:24:39 GMT
Author: kmarsden
Date: Wed Oct 15 10:24:39 2008
New Revision: 704964

URL: http://svn.apache.org/viewvc?rev=704964&view=rev
Log:
DERBY-3905 Failed tests should save the database off to the fail directory


Modified:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/PrivilegedFileOpsForTests.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseTestCase.java

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=704964&r1=704963&r2=704964&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
Wed Oct 15 10:24:39 2008
@@ -25,10 +25,14 @@
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.FileReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
 import java.security.PrivilegedActionException;
 import java.security.PrivilegedExceptionAction;
+import java.sql.SQLException;
 
 /**
  * A set of operations on {@link java.io.File} that wraps the
@@ -64,6 +68,30 @@
     }
 
     /**
+     * Get the absolute path
+     *
+     * @param file File for absolute path
+     * @return Absolute path of the file.
+     * @throws SecurityException if the required permissions to access the file,
+     *      
+     * @see File#getAbsolutePath
+     */
+    public static String getAbsolutePath(final File file)
+            throws SecurityException {
+        if (file == null) {
+            throw new IllegalArgumentException("file cannot be <null>");
+        }
+        return (String)AccessController.doPrivileged(
+                new PrivilegedAction() {
+                    public Object run() throws SecurityException {
+                        return file.getAbsolutePath();
+                    }});
+    }
+      
+    
+    
+
+    /**
      * Returns a input stream for the specified file.
      *
      * @param file the file to open a stream for
@@ -137,6 +165,98 @@
         }
     }
 
+    
+    /**
+     * In a priv block, do a recursive copy from source to target.  
+     * If target exists it will be overwritten. Parent directory for 
+     * target will be created if it does not exist. 
+     * If source does not exist this will be a noop.
+     * 
+     * @param source  Source file or directory to copy
+     * @param target  Target file or directory to copy
+     * @throws IOException
+     * @throws SecurityException
+     */    
+    public static void copy(final File source, final File target) throws IOException {
+        try {
+            AccessController.doPrivileged(new PrivilegedExceptionAction() {
+                public Object run() throws IOException {
+                    recursiveCopy(source,target);
+                    return null;
+                }
+                });
+        } catch (PrivilegedActionException pae) {
+            throw (IOException) pae.getException();
+        
+        }
+        
+    }
+    /**
+     * Do a recursive copy from source to target.  If target exists it will 
+     * be overwritten. Parent directory for target will be created if it does
+     * not exist. If source does not exist this will be a noop.
+     * 
+     * @param source  Source file or directory to copy
+     * @param target  Target file or directory to copy
+     * @throws IOException
+     * @throws FileNotFoundException
+     */
+    private static void  recursiveCopy(File source, File target) throws IOException, FileNotFoundException{
+    
+        if (source.isFile()) {
+            copySingleFile(source,target);
+            return;
+        }
+            
+        String[] list = source.list();
+
+        // Some JVMs return null for File.list() when the
+        // directory is empty.
+        if (list != null) {
+            for (int i = 0; i < list.length; i++) {
+                File entry = new File(source, list[i]);
+                File targetEntry = new File(target, list[i]);
+                if (entry.isDirectory()) {
+                    copy(entry,targetEntry);
+                } else {
+                    copySingleFile(entry, targetEntry);
+                }
+            }
+
+        }
+    }
+
+    /**
+     * Copy a single file from source to target.  If target exists it will be 
+     * overwritten.  If source does not exist, this will be a noop.
+     * 
+     * @param source  Source file to copy
+     * @param target  Destination file for copy
+     * @throws IOException
+     * @throws FileNotFoundException
+     */
+    private static void copySingleFile (File source, File target) throws IOException, FileNotFoundException
{
+
+        File targetParent = target.getParentFile();
+        if (targetParent != null && ! targetParent.exists())
+            target.getParentFile().mkdirs();
+        
+                
+        InputStream in = new FileInputStream(source);
+        OutputStream out = new FileOutputStream(target);
+        byte[] buf = new byte[32 * 1024];
+        
+        for (;;) {
+            int read = in.read(buf);
+            if (read == -1)
+                break;
+            out.write(buf, 0, read);
+        }
+        in.close();
+        out.close();
+    }
+    
+
     /**
      * Returns a file output stream for the specified file.
      *

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=704964&r1=704963&r2=704964&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 Oct
15 10:24:39 2008
@@ -19,6 +19,7 @@
  */
 package org.apache.derbyTesting.junit;
 
+import org.apache.derbyTesting.functionTests.util.PrivilegedFileOpsForTests;
 import junit.framework.Assert;
 import junit.framework.TestCase;
 import junit.framework.AssertionFailedError;
@@ -103,36 +104,25 @@
         try {
             super.runBare();   
         }
-        //To save the derby.log of failed tests. 
+        //To save the derby.log  and database of failed tests.
         catch (Throwable running) {
             try{
-                AccessController.doPrivileged(new PrivilegedExceptionAction() {
-                    public Object run() throws SQLException, FileNotFoundException,
-                    IOException {
-
-                        File origLogDir = new File("system", "derby.log");
-                        if (origLogDir.exists()) {
-                            File failDir = getFailureFolder();
-                            InputStream in = new FileInputStream(origLogDir);
-                            OutputStream out = new FileOutputStream(new File(failDir,
-                            "derby.log"));
-                            byte[] buf = new byte[32 * 1024];
-
-                            for (;;) {
-                                int read = in.read(buf);
-                                if (read == -1)
-                                    break;
-                                out.write(buf, 0, read);
-                            }
-                            in.close();
-                            out.close();
-                        }
-
-                        return null;
-                    }
-                });
+                String failPath = PrivilegedFileOpsForTests.getAbsolutePath(getFailureFolder());
+                File origLog = new File("system", "derby.log");
+                File newLog = new File(failPath, "derby.log");
+                PrivilegedFileOpsForTests.copy(origLog, newLog);
+                String dbName = TestConfiguration.getCurrent().getDefaultDatabaseName();
+                File dbDir = new File("system", dbName );                        
+                File newDbDir = new File(failPath, dbName);
+                PrivilegedFileOpsForTests.copy(dbDir,newDbDir);
+           }
+            catch (IOException ioe) {
+                // We need to throw the original exception so if there
+                // is an exception saving the db or derby.log we will just
+                // print it.
+                BaseTestCase.printStackTrace(ioe);
             }
-            finally{        		
+            finally {
                 throw running;
             }
         }



Mime
View raw message