001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.store.LogDeviceTest
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.sql.Connection;
025: import java.sql.Statement;
026: import java.sql.PreparedStatement;
027: import java.sql.ResultSet;
028: import java.sql.SQLException;
029: import org.apache.derby.tools.ij;
030: import org.apache.derbyTesting.functionTests.util.TestUtil;
031: import java.io.File;
032: import java.io.IOException;
033:
034: /*
035: * This class tests create database with transaction
036: * log at non-default location specified as absolute path.
037: * @author <a href="mailto:suresh.thalamati@gmail.com">Suresh Thalamati</a>
038: * @version 1.0
039: */
040:
041: public class LogDeviceTest {
042:
043: private static final String TEST_DATABASE_NAME = "wombat";
044: private static final String TEST_DATABASE_NAME1 = "wombat1";
045: private static final String TEST_TABLE_NAME = "emp";
046: private static final String LOG_PATH = "extinout/logDeviceTest_c1";
047: private static final String LOG_PATH1 = "extinout/logDeviceTest_c2";
048:
049: public static void main(String[] argv) throws Throwable {
050:
051: LogDeviceTest test = new LogDeviceTest();
052: ij.getPropertyArg(argv);
053:
054: try {
055: test.runTest();
056: } catch (SQLException sqle) {
057: dumpSQLException(sqle);
058: }
059: }
060:
061: /*
062: * Returns the absolute path of the given path.
063: */
064: private String getFullPath(String path) throws IOException {
065: File f = new File(path);
066: return f.getCanonicalPath();
067: }
068:
069: /*
070: * create a directory.
071: */
072: private boolean createDir(String path) {
073: File f = new File(path);
074: return f.mkdirs();
075: }
076:
077: /*
078: * Test database creation with log in non-default location.
079: */
080: private void runTest() throws Exception {
081: logMessage("Begin Log Device Test");
082:
083: // case 1: test logDevice property with absolute path
084:
085: Connection conn;
086: String connAttr = "create=true;" + "logDevice="
087: + getFullPath(LOG_PATH);
088: conn = TestUtil.getConnection(TEST_DATABASE_NAME, connAttr);
089: conn.setAutoCommit(false);
090: createTable(conn, TEST_TABLE_NAME);
091: conn.commit();
092: // just insert few rows and rollback and commit
093: // to make sure tranaction log is working fine.
094: insert(conn, TEST_TABLE_NAME, 100);
095: conn.commit();
096: insert(conn, TEST_TABLE_NAME, 100);
097: conn.rollback();
098: // shutdown the test db
099: shutdown(TEST_DATABASE_NAME);
100:
101: // case 2: database creation on non-empty
102: // log dir location should fail.
103:
104: try {
105: // this database creation is specifying the same log
106: // location as the one above; so it should fail.
107: conn = TestUtil
108: .getConnection(TEST_DATABASE_NAME1, connAttr);
109: } catch (SQLException se) {
110: SQLException nse = se.getNextException();
111: if (nse != null) {
112: // expect to fail with log dir exists error.
113: if (nse.getSQLState().equals("XSLAT"))
114: System.out.println("Failed with Expected error:"
115: + nse.getSQLState());
116: else
117: dumpSQLException(se);
118: } else {
119: dumpSQLException(se);
120: }
121: }
122:
123: // case 3: database creation on an empty log dir should pass.
124:
125: // create a dummy log dir
126: createDir(getFullPath(LOG_PATH1) + File.separator + "log");
127: connAttr = "create=true;" + "logDevice="
128: + getFullPath(LOG_PATH1);
129: conn = TestUtil.getConnection(TEST_DATABASE_NAME1, connAttr);
130: // just insert few rows and rollback and commit
131: // to make sure tranaction log is working fine.
132: conn.setAutoCommit(false);
133: createTable(conn, TEST_TABLE_NAME);
134: conn.commit();
135: insert(conn, TEST_TABLE_NAME, 100);
136: // shutdown the test db
137: shutdown(TEST_DATABASE_NAME1);
138:
139: // reconnect to the same database.
140: conn = TestUtil.getConnection(TEST_DATABASE_NAME1, null);
141:
142: logMessage("End log device Test");
143: }
144:
145: /**
146: * Shutdown the datbase
147: * @param dbName Name of the database to shutdown.
148: */
149: private void shutdown(String dbName) {
150:
151: try {
152: //shutdown
153: TestUtil.getConnection(dbName, "shutdown=true");
154: } catch (SQLException se) {
155: if (se.getSQLState() != null
156: && se.getSQLState().equals("08006"))
157: System.out.println("database shutdown properly");
158: else
159: dumpSQLException(se);
160: }
161: }
162:
163: /**
164: * Write message to the standard output.
165: */
166: private void logMessage(String str) {
167: System.out.println(str);
168: }
169:
170: /**
171: * dump the SQLException to the standard output.
172: */
173: static private void dumpSQLException(SQLException sqle) {
174:
175: org.apache.derby.tools.JDBCDisplayUtil.ShowSQLException(
176: System.out, sqle);
177: sqle.printStackTrace(System.out);
178: }
179:
180: /**
181: * Insert some rows into the specified table.
182: * @param conn connection to the database.
183: * @param tableName name of the table that rows are inserted.
184: * @param rowCount Number of rows to Insert.
185: * @exception SQLException if any database exception occurs.
186: */
187: private void insert(Connection conn, String tableName, int rowCount)
188: throws SQLException {
189:
190: PreparedStatement ps = conn.prepareStatement("INSERT INTO "
191: + tableName + " VALUES(?,?,?)");
192: for (int i = 0; i < rowCount; i++) {
193:
194: ps.setInt(1, i); // ID
195: ps.setString(2, "skywalker" + i);
196: ps.setFloat(3, (float) (i * 2000));
197: ps.executeUpdate();
198: }
199: ps.close();
200: }
201:
202: /*
203: * create the tables that are used by this test.
204: * @param conn connection to the database.
205: * @param tableName Name of the table to create.
206: * @exception SQLException if any database exception occurs.
207: */
208: private void createTable(Connection conn, String tableName)
209: throws SQLException {
210:
211: Statement s = conn.createStatement();
212: s.executeUpdate("CREATE TABLE " + tableName + "(id INT,"
213: + "name CHAR(200)," + "salary float)");
214: s.executeUpdate("create index " + tableName + "_id_idx on "
215: + tableName + "(id)");
216: s.close();
217: }
218: }
|