db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From krist...@apache.org
Subject svn commit: r775907 - in /db/derby/code/branches/10.5/java/testing/org/apache/derbyTesting: functionTests/util/PrivilegedFileOpsForTests.java junit/BaseTestCase.java
Date Mon, 18 May 2009 11:48:44 GMT
Author: kristwaa
Date: Mon May 18 11:48:44 2009
New Revision: 775907

URL: http://svn.apache.org/viewvc?rev=775907&view=rev
Log:
DERBY-4199: Write exceptions to file in the fail directory as they occur with JUnit tests.
Merged fix from trunk (revision 774729).

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

Modified: db/derby/code/branches/10.5/java/testing/org/apache/derbyTesting/functionTests/util/PrivilegedFileOpsForTests.java
URL: http://svn.apache.org/viewvc/db/derby/code/branches/10.5/java/testing/org/apache/derbyTesting/functionTests/util/PrivilegedFileOpsForTests.java?rev=775907&r1=775906&r2=775907&view=diff
==============================================================================
--- db/derby/code/branches/10.5/java/testing/org/apache/derbyTesting/functionTests/util/PrivilegedFileOpsForTests.java
(original)
+++ db/derby/code/branches/10.5/java/testing/org/apache/derbyTesting/functionTests/util/PrivilegedFileOpsForTests.java
Mon May 18 11:48:44 2009
@@ -259,6 +259,8 @@
 
     /**
      * Returns a file output stream for the specified file.
+     * <p>
+     * If the file already exists and is writable, it will be overwritten.
      *
      * @param file the file to create a stream for
      * @return An output stream.
@@ -268,6 +270,22 @@
      */
     public static FileOutputStream getFileOutputStream(final File file)
             throws FileNotFoundException {
+        return getFileOutputStream(file, false);
+    }
+
+    /**
+     * Returns a file output stream for the specified file.
+     *
+     * @param file the file to create a stream for
+     * @param append whether to append or overwrite an existing file
+     * @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,
+                                                       final boolean append)
+            throws FileNotFoundException {
         if (file == null) {
             throw new IllegalArgumentException("file cannot be <null>");
         }
@@ -276,7 +294,7 @@
                     new PrivilegedExceptionAction() {
                         public Object run()
                                 throws FileNotFoundException {
-                            return new FileOutputStream(file);
+                            return new FileOutputStream(file, append);
                         }
                     });
         } catch (PrivilegedActionException pae) {

Modified: db/derby/code/branches/10.5/java/testing/org/apache/derbyTesting/junit/BaseTestCase.java
URL: http://svn.apache.org/viewvc/db/derby/code/branches/10.5/java/testing/org/apache/derbyTesting/junit/BaseTestCase.java?rev=775907&r1=775906&r2=775907&view=diff
==============================================================================
--- db/derby/code/branches/10.5/java/testing/org/apache/derbyTesting/junit/BaseTestCase.java
(original)
+++ db/derby/code/branches/10.5/java/testing/org/apache/derbyTesting/junit/BaseTestCase.java
Mon May 18 11:48:44 2009
@@ -35,6 +35,7 @@
 import java.io.OutputStream;
 import java.io.Reader;
 import java.io.PrintStream;
+import java.io.PrintWriter;
 import java.net.URL;
 import java.sql.SQLException;
 import java.security.AccessController;
@@ -104,13 +105,25 @@
         try {
             super.runBare();   
         }
-        //To save the derby.log  and database of failed tests.
+        // To log the exception to file, copy the derby.log file and copy
+        // the database of the failed test.
         catch (Throwable running) {
+            PrintWriter stackOut = null;
             try{
                 String failPath = PrivilegedFileOpsForTests.getAbsolutePath(getFailureFolder());
+                // Write the stack trace of the error/failure to file.
+                stackOut = new PrintWriter(
+                        PrivilegedFileOpsForTests.getFileOutputStream(
+                            new File(failPath, "error-stacktrace.out"), true));
+                stackOut.println("[Error/failure logged at " +
+                        new java.util.Date() + "]");
+                running.printStackTrace(stackOut);
+                stackOut.println(); // Add an extra blank line.
+                // Copy the derby.log file.
                 File origLog = new File("system", "derby.log");
                 File newLog = new File(failPath, "derby.log");
                 PrivilegedFileOpsForTests.copy(origLog, newLog);
+                // Copy the database.
                 String dbName = TestConfiguration.getCurrent().getDefaultDatabaseName();
                 File dbDir = new File("system", dbName );                        
                 File newDbDir = new File(failPath, dbName);
@@ -118,11 +131,19 @@
            }
             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.
+                // is an exception saving the db or derby.log we will print it
+                // and additionally try to log it to file.
                 BaseTestCase.printStackTrace(ioe);
+                if (stackOut != null) {
+                    stackOut.println("Copying derby.log or database failed:");
+                    ioe.printStackTrace(stackOut);
+                    stackOut.println();
+                }
             }
             finally {
+                if (stackOut != null) {
+                    stackOut.close();
+                }
                 throw running;
             }
         }



Mime
View raw message