1 5 package org.h2.test.unit; 6 7 import java.io.File ; 8 import java.io.IOException ; 9 import java.sql.Connection ; 10 import java.sql.DriverManager ; 11 import java.sql.SQLException ; 12 13 import org.h2.api.DatabaseEventListener; 14 import org.h2.test.TestBase; 15 16 public class TestExit extends TestBase implements DatabaseEventListener { 17 18 public void test() throws Exception { 19 if(config.codeCoverage) { 20 return; 21 } 22 String classPath = "bin"+File.pathSeparator+"."; 23 24 deleteDb("exit"); 25 String [] procDef; 26 procDef = new String []{ 27 "java", "-cp", classPath, 28 getClass().getName(), 29 "" + OPEN_WITH_CLOSE_ON_EXIT 30 }; 31 Process proc = Runtime.getRuntime().exec(procDef); 32 while (true) { 33 int ch = proc.getErrorStream().read(); 34 if (ch < 0) { 35 break; 36 } 37 System.out.print((char) ch); 38 } 39 while (true) { 40 int ch = proc.getInputStream().read(); 41 if (ch < 0) { 42 break; 43 } 44 System.out.print((char) ch); 45 } 46 proc.waitFor(); 47 if(!getClosedFile().exists()) { 48 error("didnt close database"); 49 } 50 procDef = new String []{ 51 "java", "-cp", classPath, 52 getClass().getName(), 53 "" + OPEN_WITHOUT_CLOSE_ON_EXIT 54 }; 55 proc = Runtime.getRuntime().exec(procDef); 56 proc.waitFor(); 57 if(getClosedFile().exists()) { 58 error("closed database"); 59 } 60 } 61 62 static final int OPEN_WITH_CLOSE_ON_EXIT = 1, OPEN_WITHOUT_CLOSE_ON_EXIT = 2; 63 64 public static Connection conn; 65 66 public static void main(String [] args) throws Exception { 67 if(args.length==0) { 68 System.exit(1); 69 } 70 int action = Integer.parseInt(args[0]); 71 TestExit app = new TestExit(); 72 app.execute(action); 73 } 74 75 void execute(int action) throws Exception { 76 Class.forName("org.h2.Driver"); 77 String url = ""; 78 switch(action) { 79 case OPEN_WITH_CLOSE_ON_EXIT: 80 url = "jdbc:h2:"+BASE_DIR+"/exit;database_event_listener='" + getClass().getName() + "';db_close_on_exit=true"; 81 break; 82 case OPEN_WITHOUT_CLOSE_ON_EXIT: 83 url = "jdbc:h2:"+BASE_DIR+"/exit;database_event_listener='" + getClass().getName() + "';db_close_on_exit=false"; 84 break; 85 } 86 conn = open(url); 87 Connection conn2 = open(url); 88 conn2.close(); 89 } 90 91 private static Connection open(String url) throws Exception { 92 getClosedFile().delete(); 93 return DriverManager.getConnection(url, "sa", ""); 94 } 95 96 public void diskSpaceIsLow(long stillAvailable) throws SQLException { 97 } 98 99 public void exceptionThrown(SQLException e) { 100 } 101 102 public void closingDatabase() { 103 try { 104 getClosedFile().createNewFile(); 105 } catch(IOException e) { 106 TestBase.logError("error", e); 107 } 108 } 109 110 private static File getClosedFile() { 111 return new File (BASE_DIR + "/closed.txt"); 112 } 113 114 public void setProgress(int state, String name, int x, int max) { 115 } 116 117 public void init(String url) { 118 } 119 120 } 121 | Popular Tags |