Return-Path: Delivered-To: apmail-db-derby-commits-archive@www.apache.org Received: (qmail 18345 invoked from network); 20 Sep 2010 09:29:05 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 20 Sep 2010 09:29:05 -0000 Received: (qmail 6420 invoked by uid 500); 20 Sep 2010 09:29:05 -0000 Delivered-To: apmail-db-derby-commits-archive@db.apache.org Received: (qmail 6343 invoked by uid 500); 20 Sep 2010 09:29:03 -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 6336 invoked by uid 99); 20 Sep 2010 09:29:02 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 20 Sep 2010 09:29:02 +0000 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; Mon, 20 Sep 2010 09:29:01 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 686C923889BB; Mon, 20 Sep 2010 09:28:41 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r998844 - /db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/store/OSReadOnlyTest.java Date: Mon, 20 Sep 2010 09:28:41 -0000 To: derby-commits@db.apache.org From: kristwaa@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20100920092841.686C923889BB@eris.apache.org> Author: kristwaa Date: Mon Sep 20 09:28:41 2010 New Revision: 998844 URL: http://svn.apache.org/viewvc?rev=998844&view=rev Log: DERBY-4804: Make database used in store.OSReadOnlyTest fully read-only Properly simulate a read-only media. Since Java cannot make a file writeable after first making it read-only until Java SE 6, the db.lck file (with invalid contents) was created and made read-only instead. Patch file: derby-4804-1a-test_change.diff (modified some comments) Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/store/OSReadOnlyTest.java Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/store/OSReadOnlyTest.java URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/store/OSReadOnlyTest.java?rev=998844&r1=998843&r2=998844&view=diff ============================================================================== --- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/store/OSReadOnlyTest.java (original) +++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/store/OSReadOnlyTest.java Mon Sep 20 09:28:41 2010 @@ -26,6 +26,7 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.security.AccessController; +import java.security.PrivilegedAction; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; @@ -41,6 +42,11 @@ import org.apache.derbyTesting.junit.JDB import org.apache.derbyTesting.junit.JDBCDataSource; import org.apache.derbyTesting.junit.TestConfiguration; +/** + * Simulates running Derby on a read-only media, and makes sure Derby gives a + * reasonable error message when the user tries to insert data into the + * read-only database. + */ public class OSReadOnlyTest extends BaseJDBCTestCase{ public OSReadOnlyTest(String name) { @@ -131,6 +137,7 @@ public class OSReadOnlyTest extends Base copyDatabaseOnOS(phDbName, "readOnly"); // change filePermissions on readOnly, to readonly. changeFilePermissions("readOnly"); + createDummyLockFile("readOnly"); DataSource ds = JDBCDataSource.getDataSource(); JDBCDataSource.setBeanProperty(ds, @@ -153,6 +160,7 @@ public class OSReadOnlyTest extends Base copyDatabaseOnOS("readWrite", "readOnly2"); // change filePermissions on readOnly, to readonly. changeFilePermissions("readOnly2"); + createDummyLockFile("readOnly2"); ds = JDBCDataSource.getDataSource(); JDBCDataSource.setBeanProperty(ds, @@ -260,7 +268,7 @@ public class OSReadOnlyTest extends Base insertIntValue + ", '" + insertStringValue + "')"); fail("expected an error indicating the db is readonly"); } catch (SQLException sqle) { - assertSQLState("40XD1", sqle); + assertSQLState("25502", sqle); } } stmt.close(); @@ -268,42 +276,62 @@ public class OSReadOnlyTest extends Base } private void copyDatabaseOnOS(String fromwhere, String todir) { - String from_dir; - String to_dir; + File from_dir = constructDbPath(fromwhere); + File to_dir = constructDbPath(todir); - String filesep=getSystemProperty("file.separator"); - - String testpath=new String( getSystemProperty("user.dir") + filesep + - "system" + filesep + "singleUse" + filesep); - - from_dir = testpath + fromwhere; - to_dir = testpath + todir; - assertTrue("Failed to copy directory from " + from_dir + " to " + to_dir, (copyDirectory(from_dir, to_dir))); assertTrue("Failed to remove directory: " + from_dir, (removeTemporaryDirectory(from_dir))); } + /** + * Creates a dummy database lock file if one doesn't exist, and sets the + * lock file to read-only. + *

+ * This method is a work-around for the problem that Java cannot make a file + * writable before Java 6. + * + * @param dbDir the database directory where the lock file belongs + */ + private void createDummyLockFile(String dbDir) { + final File f = new File(constructDbPath(dbDir), "db.lck"); + AccessController.doPrivileged(new PrivilegedAction() { + + public Object run() { + if (!f.exists()) { + try { + FileOutputStream fos = new FileOutputStream(f); + // Just write a dummy byte. + fos.write(12); + fos.close(); + } catch (IOException fnfe) { + // Ignore + } + } + f.setReadOnly(); + return null; + } + }); + } + public void changeFilePermissions(String dir) { - String filesep=getSystemProperty("file.separator"); - String dir_to_change = new String(getSystemProperty("user.dir") + filesep - + "system" + filesep + "singleUse" + filesep + dir); + File dir_to_change = constructDbPath(dir); assertTrue("Failed to change files in " + dir_to_change + " to ReadOnly", changeDirectoryToReadOnly(dir_to_change)); } - + /** - * Change all of the files in a directory and its subdirectories - * to read only (atleast not writeable, depending on system for execute - * permission). - * @param directory the string representation of the directory - * to start recursing from. - * @return true for success, false otherwise + * Constructs the path to the database base directory. + * + * @param relDbDirName the database name (relative) + * @return The path to the database. */ - public static boolean changeDirectoryToReadOnly( String directory ) - { - return changeDirectoryToReadOnly( new File(directory) ); + private File constructDbPath(String relDbDirName) { + // Example: "readOnly" -> "/system/singleUse/readOnly" + File f = new File(getSystemProperty("user.dir"), "system"); + f = new File(f, "singleUse"); + return new File(f, relDbDirName); } /** @@ -344,6 +372,10 @@ public class OSReadOnlyTest extends Base } } } + // Before Java 6 we cannot make the directory writable + // again, which means we cannot delete the directory and + // its content... + //success &= sdirectory.setReadOnly(); return new Boolean(success); } }); @@ -407,11 +439,6 @@ public class OSReadOnlyTest extends Base else return false; } - public static boolean removeTemporaryDirectory(String directory) - { - return removeTemporaryDirectory(new File(directory)); - } - /** Copy a directory and all of its contents. */ @@ -523,4 +550,4 @@ public class OSReadOnlyTest extends Base } return true; } -} \ No newline at end of file +}