From derby-commits-return-10875-apmail-db-derby-commits-archive=db.apache.org@db.apache.org Wed Oct 15 17:29:03 2008 Return-Path: Delivered-To: apmail-db-derby-commits-archive@www.apache.org Received: (qmail 19619 invoked from network); 15 Oct 2008 17:29:03 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 15 Oct 2008 17:29:03 -0000 Received: (qmail 39937 invoked by uid 500); 15 Oct 2008 17:25:31 -0000 Delivered-To: apmail-db-derby-commits-archive@db.apache.org Received: (qmail 39917 invoked by uid 500); 15 Oct 2008 17:25:31 -0000 Mailing-List: contact derby-commits-help@db.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: Reply-To: "Derby Development" List-Id: Delivered-To: mailing list derby-commits@db.apache.org Received: (qmail 39907 invoked by uid 99); 15 Oct 2008 17:25:31 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 15 Oct 2008 10:25:31 -0700 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 15 Oct 2008 17:24:32 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id C490E238893B; Wed, 15 Oct 2008 10:24:39 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: derby-commits@db.apache.org From: kmarsden@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20081015172439.C490E238893B@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 "); + } + 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; } }