001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (license2)
004: * Initial Developer: H2 Group
005: */
006: package org.h2.test.unit;
007:
008: import java.io.File;
009: import java.io.IOException;
010: import java.sql.Connection;
011: import java.sql.DriverManager;
012: import java.sql.SQLException;
013:
014: import org.h2.api.DatabaseEventListener;
015: import org.h2.test.TestBase;
016:
017: /**
018: * Tests the flag db_close_on_exit.
019: * A new process is started.
020: */
021: public class TestExit extends TestBase implements DatabaseEventListener {
022:
023: public void test() throws Exception {
024: if (config.codeCoverage || config.networked) {
025: return;
026: }
027: String classPath = "bin" + File.pathSeparator + ".";
028:
029: deleteDb("exit");
030: String[] procDef;
031: String selfDestruct = SelfDestructor.getPropertyString(60);
032: procDef = new String[] { "java", selfDestruct, "-cp",
033: classPath, getClass().getName(),
034: "" + OPEN_WITH_CLOSE_ON_EXIT };
035: Process proc = Runtime.getRuntime().exec(procDef);
036: while (true) {
037: int ch = proc.getErrorStream().read();
038: if (ch < 0) {
039: break;
040: }
041: System.out.print((char) ch);
042: }
043: while (true) {
044: int ch = proc.getInputStream().read();
045: if (ch < 0) {
046: break;
047: }
048: System.out.print((char) ch);
049: }
050: proc.waitFor();
051: Thread.sleep(100);
052: if (!getClosedFile().exists()) {
053: error("did not close database");
054: }
055: procDef = new String[] { "java", "-cp", classPath,
056: getClass().getName(), "" + OPEN_WITHOUT_CLOSE_ON_EXIT };
057: proc = Runtime.getRuntime().exec(procDef);
058: proc.waitFor();
059: Thread.sleep(100);
060: if (getClosedFile().exists()) {
061: error("closed database");
062: }
063: }
064:
065: static final int OPEN_WITH_CLOSE_ON_EXIT = 1,
066: OPEN_WITHOUT_CLOSE_ON_EXIT = 2;
067:
068: public static Connection conn;
069:
070: public static void main(String[] args) throws Exception {
071: SelfDestructor.startCountdown(60);
072: if (args.length == 0) {
073: System.exit(1);
074: }
075: int action = Integer.parseInt(args[0]);
076: TestExit app = new TestExit();
077: app.execute(action);
078: }
079:
080: void execute(int action) throws Exception {
081: Class.forName("org.h2.Driver");
082: String url = "";
083: switch (action) {
084: case OPEN_WITH_CLOSE_ON_EXIT:
085: url = "jdbc:h2:" + baseDir
086: + "/exit;database_event_listener='"
087: + getClass().getName() + "';db_close_on_exit=true";
088: break;
089: case OPEN_WITHOUT_CLOSE_ON_EXIT:
090: url = "jdbc:h2:" + baseDir
091: + "/exit;database_event_listener='"
092: + getClass().getName() + "';db_close_on_exit=false";
093: break;
094: }
095: conn = open(url);
096: Connection conn2 = open(url);
097: conn2.close();
098: }
099:
100: private static Connection open(String url) throws Exception {
101: getClosedFile().delete();
102: return DriverManager.getConnection(url, "sa", "");
103: }
104:
105: public void diskSpaceIsLow(long stillAvailable) throws SQLException {
106: }
107:
108: public void exceptionThrown(SQLException e, String sql) {
109: }
110:
111: public void closingDatabase() {
112: try {
113: getClosedFile().createNewFile();
114: } catch (IOException e) {
115: TestBase.logError("error", e);
116: }
117: }
118:
119: private static File getClosedFile() {
120: return new File(baseDir + "/closed.txt");
121: }
122:
123: public void setProgress(int state, String name, int x, int max) {
124: }
125:
126: public void init(String url) {
127: }
128:
129: public void opened() {
130: }
131:
132: }
|