db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From krist...@apache.org
Subject svn commit: r774729 - in /db/derby/code/trunk/java/testing/org/apache/derbyTesting: functionTests/util/PrivilegedFileOpsForTests.java junit/BaseTestCase.java
Date Thu, 14 May 2009 11:14:08 GMT
Author: kristwaa
Date: Thu May 14 11:14:08 2009
New Revision: 774729

URL: http://svn.apache.org/viewvc?rev=774729&view=rev
Log:
DERBY-4199: Write exceptions to file in the fail directory as they occur with JUnit tests.
Write exceptions being thrown during JUnit testing to file as soon as they
happen. The output file is called 'error-stacktrace.out' and is located under
the fail-directory. If the output file already exists, the new output is
appended.
Added a new method for opening a file in append mode (in a privileged block).
Patch file: derby-4199-1a-writeExceptionsToFile.diff


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=774729&r1=774728&r2=774729&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 May 14 11:14:08 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/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=774729&r1=774728&r2=774729&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 Thu May
14 11:14:08 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