1 21 22 package org.apache.derbyTesting.functionTests.tests.store; 23 import java.sql.Connection ; 24 import java.sql.Statement ; 25 import java.sql.PreparedStatement ; 26 import java.sql.ResultSet ; 27 import java.sql.SQLException ; 28 import org.apache.derby.tools.ij; 29 import org.apache.derbyTesting.functionTests.util.TestUtil; 30 import java.io.File ; 31 import java.io.IOException ; 32 33 39 40 public class LogDeviceTest { 41 42 private static final String TEST_DATABASE_NAME = "wombat" ; 43 private static final String TEST_DATABASE_NAME1 = "wombat1" ; 44 private static final String TEST_TABLE_NAME = "emp"; 45 private static final String LOG_PATH = "extinout/logDeviceTest_c1"; 46 private static final String LOG_PATH1 = "extinout/logDeviceTest_c2"; 47 48 public static void main(String [] argv) throws Throwable { 49 50 LogDeviceTest test = new LogDeviceTest(); 51 ij.getPropertyArg(argv); 52 53 try { 54 test.runTest(); 55 } 56 catch (SQLException sqle) { 57 dumpSQLException(sqle); 58 } 59 } 60 61 64 private String getFullPath(String path) throws IOException { 65 File f = new File (path); 66 return f.getCanonicalPath(); 67 } 68 69 70 73 private boolean createDir(String path) { 74 File f = new File (path); 75 return f.mkdirs(); 76 } 77 78 79 80 83 private void runTest() throws Exception { 84 logMessage("Begin Log Device Test"); 85 86 88 Connection conn; 89 String 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 insert(conn, TEST_TABLE_NAME, 100); 98 conn.commit(); 99 insert(conn, TEST_TABLE_NAME, 100); 100 conn.rollback(); 101 shutdown(TEST_DATABASE_NAME); 103 104 107 108 try { 109 conn = TestUtil.getConnection(TEST_DATABASE_NAME1, 112 connAttr); 113 }catch (SQLException se) { 114 SQLException nse = se.getNextException(); 115 if (nse != null) { 116 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 129 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 conn.setAutoCommit(false); 139 createTable(conn, TEST_TABLE_NAME); 140 conn.commit(); 141 insert(conn, TEST_TABLE_NAME, 100); 142 shutdown(TEST_DATABASE_NAME1); 144 145 conn = TestUtil.getConnection(TEST_DATABASE_NAME1, null); 147 148 logMessage("End log device Test"); 149 } 150 151 152 156 private void shutdown(String dbName) { 157 158 try{ 159 TestUtil.getConnection(dbName, "shutdown=true"); 161 }catch(SQLException 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 172 private void logMessage(String str) { 173 System.out.println(str); 174 } 175 176 177 180 static private void dumpSQLException(SQLException sqle) { 181 182 org.apache.derby.tools.JDBCDisplayUtil. ShowSQLException(System.out, sqle); 183 sqle.printStackTrace(System.out); 184 } 185 186 193 private void insert(Connection conn, 194 String tableName, 195 int rowCount) throws SQLException { 196 197 PreparedStatement ps = conn.prepareStatement("INSERT INTO " + 198 tableName + 199 " VALUES(?,?,?)"); 200 for (int i = 0; i < rowCount; i++) { 201 202 ps.setInt(1, i); ps.setString(2 , "skywalker" + i); 204 ps.setFloat(3, (float)(i * 2000)); 205 ps.executeUpdate(); 206 } 207 ps.close(); 208 } 209 210 211 217 private void createTable(Connection conn, 218 String tableName) throws SQLException { 219 220 Statement 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 |