KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > test > unit > TestExit


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.test.unit;
6
7 import java.io.File JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.sql.Connection JavaDoc;
10 import java.sql.DriverManager JavaDoc;
11 import java.sql.SQLException JavaDoc;
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 JavaDoc {
19         if(config.codeCoverage) {
20             return;
21         }
22         String JavaDoc classPath = "bin"+File.pathSeparator+".";
23         
24         deleteDb("exit");
25         String JavaDoc[] procDef;
26         procDef = new String JavaDoc[]{
27                 "java", "-cp", classPath,
28                 getClass().getName(),
29                 "" + OPEN_WITH_CLOSE_ON_EXIT
30         };
31         Process JavaDoc 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 JavaDoc[]{
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 JavaDoc conn;
65     
66     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
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 JavaDoc {
76         Class.forName("org.h2.Driver");
77         String JavaDoc 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 JavaDoc conn2 = open(url);
88         conn2.close();
89     }
90     
91     private static Connection JavaDoc open(String JavaDoc url) throws Exception JavaDoc {
92         getClosedFile().delete();
93         return DriverManager.getConnection(url, "sa", "");
94     }
95
96     public void diskSpaceIsLow(long stillAvailable) throws SQLException JavaDoc {
97     }
98
99     public void exceptionThrown(SQLException JavaDoc e) {
100     }
101
102     public void closingDatabase() {
103         try {
104             getClosedFile().createNewFile();
105         } catch(IOException JavaDoc e) {
106             TestBase.logError("error", e);
107         }
108     }
109     
110     private static File JavaDoc getClosedFile() {
111         return new File JavaDoc(BASE_DIR + "/closed.txt");
112     }
113
114     public void setProgress(int state, String JavaDoc name, int x, int max) {
115     }
116
117     public void init(String JavaDoc url) {
118     }
119
120 }
121
Popular Tags