1 20 21 package org.apache.derbyTesting.functionTests.tests.derbynet; 22 23 import java.util.Properties ; 24 import org.apache.derbyTesting.functionTests.util.TestUtil; 25 import org.apache.derbyTesting.junit.BaseJDBCTestCase; 26 import org.apache.derbyTesting.junit.BaseTestCase; 27 import org.apache.derby.drda.NetworkServerControl; 28 29 import junit.framework.*; 30 import java.sql.*; 31 import java.io.PrintWriter ; 32 import java.io.File ; 33 import java.security.AccessController ; 34 35 43 public class ShutDownDBWhenNSShutsDownTest extends BaseJDBCTestCase { 44 45 46 NetworkServerControl server = null; 47 48 49 52 public ShutDownDBWhenNSShutsDownTest(String name) { 53 super(name); 54 } 55 56 61 public void testDatabasesShutDownWhenNSShutdown() 62 throws Exception 63 { 64 server = new NetworkServerControl(); 65 shutdownServerCheckDBShutDown(true); 69 70 server.start(null); 73 74 int i = 0; 76 while (!pingServer() && i < 10 ) { 77 Thread.sleep(1000); 78 i++; 79 } 80 81 shutdownServerCheckDBShutDown(false); 84 } 85 86 99 private void shutdownServerCheckDBShutDown(boolean dbShutDown) 100 throws Exception 101 { 102 createDatabase(); 104 105 shutdownServer(); 107 108 String fileName = getSystemProperty("derby.system.home") + 110 java.io.File.separator + "wombat" + 111 java.io.File.separator + "db.lck"; 112 113 boolean fileNotFound = false; 114 int i = 0; 115 do { 116 Thread.sleep(500); 117 fileNotFound = !fileExists(fileName); 118 i ++; 119 } while (fileNotFound != dbShutDown && i < 120); 120 121 assertEquals("Database is shut down", dbShutDown, fileNotFound); 122 } 123 124 private boolean fileExists (final String fileName) throws Exception { 125 Boolean b = (Boolean ) AccessController.doPrivileged 126 (new java.security.PrivilegedAction (){ 127 public Object run(){ 128 File file = new File (fileName); 129 return new Boolean (file.exists()); 130 } 131 }); 132 133 return b.booleanValue(); 134 } 135 136 private boolean pingServer() { 137 try { 138 server.ping(); 139 } 140 catch (Exception e) { 141 return false; 142 } 143 return true; 144 } 145 146 private void createDatabase() throws SQLException { 147 Connection conn = getConnection(); 148 conn.setAutoCommit(false); 149 Statement st = conn.createStatement(); 150 st.execute("CREATE TABLE T1 (a int)"); 151 st.execute("INSERT INTO T1 VALUES (1), (2), (3), (4), (5)"); 152 st.execute("DROP TABLE T1"); 153 conn.commit(); 154 conn.close(); 155 } 156 157 private void shutdownServer() throws Exception { 158 server.shutdown(); 159 } 160 161 } 162 | Popular Tags |