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.db;
007:
008: import java.sql.Connection;
009: import java.sql.DriverManager;
010: import java.sql.PreparedStatement;
011: import java.sql.SQLException;
012: import java.sql.Statement;
013:
014: import org.h2.api.DatabaseEventListener;
015: import org.h2.test.TestBase;
016: import org.h2.util.JdbcUtils;
017:
018: /**
019: * Tests the DatabaseEventListener.
020: */
021: public class TestListener extends TestBase implements
022: DatabaseEventListener {
023:
024: private long last, start;
025: private String url;
026:
027: public TestListener() {
028: start = last = System.currentTimeMillis();
029: }
030:
031: public void test() throws Exception {
032: if (config.networked) {
033: return;
034: }
035: deleteDb("listener");
036: Connection conn;
037: conn = getConnection("listener");
038: Statement stat = conn.createStatement();
039: stat
040: .execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR)");
041: PreparedStatement prep = conn
042: .prepareStatement("INSERT INTO TEST VALUES(?, 'Test' || SPACE(100))");
043: int len = getSize(100, 100000);
044: for (int i = 0; i < len; i++) {
045: prep.setInt(1, i);
046: prep.execute();
047: }
048: crash(conn);
049:
050: conn = getConnection("listener;database_event_listener='"
051: + getClass().getName() + "'");
052: conn.close();
053:
054: }
055:
056: public void diskSpaceIsLow(long stillAvailable) throws SQLException {
057: printTime("diskSpaceIsLow stillAvailable=" + stillAvailable);
058: }
059:
060: public void exceptionThrown(SQLException e, String sql) {
061: TestBase.logError("exceptionThrown sql=" + sql, e);
062: }
063:
064: public void setProgress(int state, String name, int current, int max) {
065: long time = System.currentTimeMillis();
066: if (time < last + 1000) {
067: return;
068: }
069: last = time;
070: String stateName;
071: switch (state) {
072: case STATE_SCAN_FILE:
073: stateName = "Scan " + name;
074: break;
075: case STATE_CREATE_INDEX:
076: stateName = "Create Index " + name;
077: break;
078: case STATE_RECOVER:
079: stateName = "Recover";
080: break;
081: default:
082: TestBase.logError("unknown state: " + state, null);
083: stateName = "? " + name;
084: }
085: try {
086: Thread.sleep(1);
087: } catch (InterruptedException e) {
088: }
089: printTime("state: " + stateName + " " + (100 * current / max)
090: + " " + (time - start));
091: }
092:
093: public void closingDatabase() {
094: if (url.toUpperCase().indexOf("CIPHER") >= 0) {
095: return;
096: }
097: Connection conn = null;
098: try {
099: conn = DriverManager.getConnection(url, getUser(),
100: getPassword());
101: conn.createStatement().execute("DROP TABLE TEST2");
102: conn.close();
103: } catch (SQLException e) {
104: e.printStackTrace();
105: } finally {
106: JdbcUtils.closeSilently(conn);
107: }
108: }
109:
110: public void init(String url) {
111: this .url = url;
112: }
113:
114: public void opened() {
115: if (url.toUpperCase().indexOf("CIPHER") >= 0) {
116: return;
117: }
118: Connection conn = null;
119: try {
120: conn = DriverManager.getConnection(url, getUser(),
121: getPassword());
122: conn.createStatement().execute(
123: "CREATE TABLE IF NOT EXISTS TEST2(ID INT)");
124: conn.close();
125: } catch (SQLException e) {
126: e.printStackTrace();
127: } finally {
128: JdbcUtils.closeSilently(conn);
129: }
130: }
131:
132: }
|