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.jdbc;
007:
008: import java.sql.Connection;
009: import java.sql.DriverManager;
010: import java.sql.SQLException;
011: import java.sql.Statement;
012: import java.util.Properties;
013:
014: import org.h2.api.DatabaseEventListener;
015: import org.h2.test.TestBase;
016:
017: /**
018: * Tests the DatabaseEventListener interface.
019: */
020: public class TestDatabaseEventListener extends TestBase implements
021: DatabaseEventListener {
022:
023: private boolean calledOpened, calledClosingDatabase, calledScan;
024:
025: public void test() throws Exception {
026: testCalled();
027: testCloseLog0(false);
028: testCloseLog0(true);
029: }
030:
031: private void testCloseLog0(boolean shutdown) throws Exception {
032: if (config.memory) {
033: return;
034: }
035: deleteDb("databaseEventListener");
036: String url = getURL("databaseEventListener", true);
037: String user = getUser(), password = getPassword();
038: Properties p = new Properties();
039: p.setProperty("user", user);
040: p.setProperty("password", password);
041: Connection conn = DriverManager.getConnection(url, p);
042: Statement stat = conn.createStatement();
043: stat.execute("set log 0");
044: stat
045: .execute("create table test(id int primary key, name varchar)");
046: stat
047: .execute("insert into test select x, space(1000) from system_range(1,1000)");
048: if (shutdown) {
049: stat.execute("shutdown");
050: }
051: conn.close();
052:
053: TestDatabaseEventListener l = new TestDatabaseEventListener();
054: p.put("DATABASE_EVENT_LISTENER_OBJECT", l);
055: org.h2.Driver.load();
056: conn = DriverManager.getConnection(url, p);
057: conn.close();
058: if (l.calledOpened) {
059: check(!l.calledScan);
060: }
061: }
062:
063: private void testCalled() throws Exception {
064: Properties p = new Properties();
065: p.setProperty("user", "sa");
066: p.setProperty("password", "sa");
067: TestDatabaseEventListener l = new TestDatabaseEventListener();
068: p.put("DATABASE_EVENT_LISTENER_OBJECT", l);
069: org.h2.Driver.load();
070: Connection conn = DriverManager.getConnection(
071: "jdbc:h2:mem:databaseEventListener", p);
072: conn.close();
073: check(l.calledOpened);
074: check(l.calledClosingDatabase);
075: }
076:
077: public void closingDatabase() {
078: calledClosingDatabase = true;
079: }
080:
081: public void diskSpaceIsLow(long stillAvailable) throws SQLException {
082: }
083:
084: public void exceptionThrown(SQLException e, String sql) {
085: }
086:
087: public void init(String url) {
088: }
089:
090: public void opened() {
091: calledOpened = true;
092: }
093:
094: public void setProgress(int state, String name, int x, int max) {
095: if (state == DatabaseEventListener.STATE_SCAN_FILE) {
096: calledScan = true;
097: }
098: }
099:
100: }
|