KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > store > TurnsReadOnly


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.store.LogChecksumSetup
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.functionTests.tests.store;
23
24 import java.io.File JavaDoc;
25 import java.sql.Connection JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.sql.Statement JavaDoc;
28 import java.util.Properties JavaDoc;
29
30 import javax.sql.DataSource JavaDoc;
31
32 import org.apache.derby.tools.ij;
33 import org.apache.derbyTesting.functionTests.util.TestUtil;
34
35 /*
36  * This class is a test where you are not able to create the lock file
37  * when booting an existing database. The database will then become
38  * read-only. The tests first creates a database and then shutdowns,
39  * turns off write access to the database directory and then boots the
40  * database again. A non-default log directory is used since that
41  * uncovered a bug (DERBY-555). (logDevice is set in the
42  * _app.properties file)
43  *
44  * NB! This test is not included in derbyall since it creates a
45  * read-only directory which will be annoying when trying to clean
46  * test directories. When Java 6 can be used, it will be possible to
47  * turn on write access at the end of the test.
48  *
49  * @author oystein.grovlen@sun.com
50  */

51
52 public class TurnsReadOnly
53 {
54     
55     public static void main(String JavaDoc[] argv) throws Throwable JavaDoc
56     {
57         try {
58             ij.getPropertyArg(argv);
59             Connection JavaDoc conn = ij.startJBMS();
60             conn.setAutoCommit(true);
61             System.out.println("Database has been booted.");
62
63             Statement JavaDoc s = conn.createStatement();
64             s.execute("CREATE TABLE t1(a INT)");
65             System.out.println("Table t1 created.");
66
67             // Shut down database
68
Properties JavaDoc shutdownAttrs = new Properties JavaDoc();
69             shutdownAttrs.setProperty("shutdownDatabase", "shutdown");
70             System.out.println("Shutting down database ...");
71             try {
72                 DataSource JavaDoc ds = TestUtil.getDataSource(shutdownAttrs);
73                 ds.getConnection();
74             } catch(SQLException JavaDoc se) {
75                 if (se.getSQLState() != null
76                     && se.getSQLState().equals("XJ015")) {
77                     System.out.println("Database shutdown completed");
78                 } else {
79                     throw se;
80                 }
81             }
82
83             // Make database directory read-only.
84
String JavaDoc derbyHome = System.getProperty("derby.system.home");
85             File JavaDoc dbDir = new File JavaDoc(derbyHome, "wombat");
86             dbDir.setReadOnly();
87             
88             // Boot database, check that it is read-only
89
conn = ij.startJBMS();
90             conn.setAutoCommit(true);
91             System.out.println("Database has been booted.");
92             s = conn.createStatement();
93             try {
94                 s.execute("INSERT INTO t1 VALUES(1)");
95             } catch(SQLException JavaDoc se) {
96                 if (se.getSQLState() != null
97                     && se.getSQLState().equals("25502")) {
98                     System.out.println("Database is read-only");
99                 } else {
100                     throw se;
101                 }
102             }
103
104         } catch (SQLException JavaDoc sqle) {
105             org.apache.derby.tools.JDBCDisplayUtil.ShowSQLException(System.out,
106                                                                     sqle);
107             sqle.printStackTrace(System.out);
108         }
109     }
110 }
111
Popular Tags