001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.store.LogChecksumSetup
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.functionTests.tests.store;
023:
024: import java.io.File;
025: import java.sql.Connection;
026: import java.sql.SQLException;
027: import java.sql.Statement;
028: import java.util.Properties;
029:
030: import javax.sql.DataSource;
031:
032: import org.apache.derby.tools.ij;
033: import org.apache.derbyTesting.functionTests.util.TestUtil;
034:
035: /*
036: * This class is a test where you are not able to create the lock file
037: * when booting an existing database. The database will then become
038: * read-only. The tests first creates a database and then shutdowns,
039: * turns off write access to the database directory and then boots the
040: * database again. A non-default log directory is used since that
041: * uncovered a bug (DERBY-555). (logDevice is set in the
042: * _app.properties file)
043: *
044: * NB! This test is not included in derbyall since it creates a
045: * read-only directory which will be annoying when trying to clean
046: * test directories. When Java 6 can be used, it will be possible to
047: * turn on write access at the end of the test.
048: *
049: * @author oystein.grovlen@sun.com
050: */
051:
052: public class TurnsReadOnly {
053:
054: public static void main(String[] argv) throws Throwable {
055: try {
056: ij.getPropertyArg(argv);
057: Connection conn = ij.startJBMS();
058: conn.setAutoCommit(true);
059: System.out.println("Database has been booted.");
060:
061: Statement s = conn.createStatement();
062: s.execute("CREATE TABLE t1(a INT)");
063: System.out.println("Table t1 created.");
064:
065: // Shut down database
066: Properties shutdownAttrs = new Properties();
067: shutdownAttrs.setProperty("shutdownDatabase", "shutdown");
068: System.out.println("Shutting down database ...");
069: try {
070: DataSource ds = TestUtil.getDataSource(shutdownAttrs);
071: ds.getConnection();
072: } catch (SQLException se) {
073: if (se.getSQLState() != null
074: && se.getSQLState().equals("XJ015")) {
075: System.out.println("Database shutdown completed");
076: } else {
077: throw se;
078: }
079: }
080:
081: // Make database directory read-only.
082: String derbyHome = System.getProperty("derby.system.home");
083: File dbDir = new File(derbyHome, "wombat");
084: dbDir.setReadOnly();
085:
086: // Boot database, check that it is read-only
087: conn = ij.startJBMS();
088: conn.setAutoCommit(true);
089: System.out.println("Database has been booted.");
090: s = conn.createStatement();
091: try {
092: s.execute("INSERT INTO t1 VALUES(1)");
093: } catch (SQLException se) {
094: if (se.getSQLState() != null
095: && se.getSQLState().equals("25502")) {
096: System.out.println("Database is read-only");
097: } else {
098: throw se;
099: }
100: }
101:
102: } catch (SQLException sqle) {
103: org.apache.derby.tools.JDBCDisplayUtil.ShowSQLException(
104: System.out, sqle);
105: sqle.printStackTrace(System.out);
106: }
107: }
108: }
|