KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.store.LogDeviceTest
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 import java.sql.Connection JavaDoc;
24 import java.sql.Statement JavaDoc;
25 import java.sql.PreparedStatement JavaDoc;
26 import java.sql.ResultSet JavaDoc;
27 import java.sql.SQLException JavaDoc;
28 import org.apache.derby.tools.ij;
29 import org.apache.derbyTesting.functionTests.util.TestUtil;
30 import java.io.File JavaDoc;
31 import java.io.IOException JavaDoc;
32
33 /*
34  * This class tests create database with transaction
35  * log at non-default location specified as absolute path.
36  * @author <a HREF="mailto:suresh.thalamati@gmail.com">Suresh Thalamati</a>
37  * @version 1.0
38  */

39
40 public class LogDeviceTest {
41
42     private static final String JavaDoc TEST_DATABASE_NAME = "wombat" ;
43     private static final String JavaDoc TEST_DATABASE_NAME1 = "wombat1" ;
44     private static final String JavaDoc TEST_TABLE_NAME = "emp";
45     private static final String JavaDoc LOG_PATH = "extinout/logDeviceTest_c1";
46     private static final String JavaDoc LOG_PATH1 = "extinout/logDeviceTest_c2";
47
48     public static void main(String JavaDoc[] argv) throws Throwable JavaDoc {
49         
50         LogDeviceTest test = new LogDeviceTest();
51         ij.getPropertyArg(argv);
52
53         try {
54             test.runTest();
55         }
56         catch (SQLException JavaDoc sqle) {
57             dumpSQLException(sqle);
58         }
59     }
60
61     /*
62      * Returns the absolute path of the given path.
63      */

64     private String JavaDoc getFullPath(String JavaDoc path) throws IOException JavaDoc{
65         File JavaDoc f = new File JavaDoc(path);
66         return f.getCanonicalPath();
67     }
68
69
70     /*
71      * create a directory.
72      */

73     private boolean createDir(String JavaDoc path) {
74         File JavaDoc f = new File JavaDoc(path);
75         return f.mkdirs();
76     }
77
78
79
80     /*
81      * Test database creation with log in non-default location.
82      */

83     private void runTest() throws Exception JavaDoc {
84         logMessage("Begin Log Device Test");
85
86         // case 1: test logDevice property with absolute path
87

88         Connection JavaDoc conn;
89         String JavaDoc connAttr = "create=true;" + "logDevice=" +
90                            getFullPath(LOG_PATH);
91         conn = TestUtil.getConnection(TEST_DATABASE_NAME, connAttr);
92         conn.setAutoCommit(false);
93         createTable(conn, TEST_TABLE_NAME);
94         conn.commit();
95         // just insert few rows and rollback and commit
96
// to make sure tranaction log is working fine.
97
insert(conn, TEST_TABLE_NAME, 100);
98         conn.commit();
99         insert(conn, TEST_TABLE_NAME, 100);
100         conn.rollback();
101         // shutdown the test db
102
shutdown(TEST_DATABASE_NAME);
103         
104         // case 2: database creation on non-empty
105
// log dir location should fail.
106

107         
108         try {
109             // this database creation is specifying the same log
110
// location as the one above; so it should fail.
111
conn = TestUtil.getConnection(TEST_DATABASE_NAME1,
112                                           connAttr);
113         }catch (SQLException JavaDoc se) {
114             SQLException JavaDoc nse = se.getNextException();
115             if (nse != null) {
116                 // expect to fail with log dir exists error.
117
if (nse.getSQLState().equals("XSLAT"))
118                     System.out.println("Failed with Expected error:" +
119                                        nse.getSQLState());
120                 else
121                     dumpSQLException(se);
122             } else {
123                 dumpSQLException(se);
124             }
125         }
126             
127         // case 3: database creation on an empty log dir should pass.
128

129         // create a dummy log dir
130
createDir(getFullPath(LOG_PATH1) +
131                   File.separator + "log");
132         connAttr = "create=true;" + "logDevice=" +
133                    getFullPath(LOG_PATH1);
134         conn = TestUtil.getConnection(TEST_DATABASE_NAME1,
135                                       connAttr);
136         // just insert few rows and rollback and commit
137
// to make sure tranaction log is working fine.
138
conn.setAutoCommit(false);
139         createTable(conn, TEST_TABLE_NAME);
140         conn.commit();
141         insert(conn, TEST_TABLE_NAME, 100);
142         // shutdown the test db
143
shutdown(TEST_DATABASE_NAME1);
144         
145         // reconnect to the same database.
146
conn = TestUtil.getConnection(TEST_DATABASE_NAME1, null);
147         
148         logMessage("End log device Test");
149     }
150
151         
152     /**
153      * Shutdown the datbase
154      * @param dbName Name of the database to shutdown.
155      */

156     private void shutdown(String JavaDoc dbName) {
157
158         try{
159             //shutdown
160
TestUtil.getConnection(dbName, "shutdown=true");
161         }catch(SQLException JavaDoc se){
162             if (se.getSQLState() != null && se.getSQLState().equals("08006"))
163                 System.out.println("database shutdown properly");
164             else
165                 dumpSQLException(se);
166         }
167     }
168
169     /**
170      * Write message to the standard output.
171      */

172     private void logMessage(String JavaDoc str) {
173             System.out.println(str);
174     }
175
176     
177     /**
178      * dump the SQLException to the standard output.
179      */

180     static private void dumpSQLException(SQLException JavaDoc sqle) {
181         
182         org.apache.derby.tools.JDBCDisplayUtil. ShowSQLException(System.out, sqle);
183         sqle.printStackTrace(System.out);
184     }
185
186     /**
187      * Insert some rows into the specified table.
188      * @param conn connection to the database.
189      * @param tableName name of the table that rows are inserted.
190      * @param rowCount Number of rows to Insert.
191      * @exception SQLException if any database exception occurs.
192      */

193     private void insert(Connection JavaDoc conn,
194                         String JavaDoc tableName,
195                         int rowCount) throws SQLException JavaDoc {
196
197         PreparedStatement JavaDoc ps = conn.prepareStatement("INSERT INTO " +
198                                                      tableName +
199                                                      " VALUES(?,?,?)");
200         for (int i = 0; i < rowCount; i++) {
201             
202             ps.setInt(1, i); // ID
203
ps.setString(2 , "skywalker" + i);
204             ps.setFloat(3, (float)(i * 2000));
205             ps.executeUpdate();
206         }
207         ps.close();
208     }
209
210
211     /*
212      * create the tables that are used by this test.
213      * @param conn connection to the database.
214      * @param tableName Name of the table to create.
215      * @exception SQLException if any database exception occurs.
216      */

217     private void createTable(Connection JavaDoc conn,
218                              String JavaDoc tableName) throws SQLException JavaDoc {
219
220         Statement JavaDoc s = conn.createStatement();
221         s.executeUpdate("CREATE TABLE " + tableName +
222                         "(id INT," +
223                         "name CHAR(200),"+
224                         "salary float)");
225         s.executeUpdate("create index " + tableName + "_id_idx on " +
226                         tableName + "(id)");
227         s.close();
228     }
229 }
230
Popular Tags