提交 621b8687 authored 作者: noelgrandin's avatar noelgrandin

move the DatabaseEventLister into inner classes - makes it easier to see what…

move the DatabaseEventLister into inner classes - makes it easier to see what belongs to what, especially when some of the the overriden methods are very similar to methods in the superclass (TestBase)
上级 2084502c
/*
* Copyright 2004-2013 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
* Copyright 2004-2013 H2 Group. Multiple-Licensed under the H2 License, Version
* 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html). Initial Developer: H2 Group
*/
package org.h2.test.db;
......@@ -10,7 +9,6 @@ import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.h2.api.DatabaseEventListener;
import org.h2.test.TestBase;
import org.h2.util.Task;
......@@ -18,13 +16,13 @@ import org.h2.util.Task;
/**
* Multi-connection tests.
*/
public class TestMultiConn extends TestBase implements DatabaseEventListener {
public class TestMultiConn extends TestBase {
private static int wait;
/**
* Run just this test.
*
*
* @param a ignored
*/
public static void main(String... a) throws Exception {
......@@ -141,7 +139,7 @@ public class TestMultiConn extends TestBase implements DatabaseEventListener {
conn.createStatement().execute("INSERT INTO TEST VALUES(0, 'Hello'), (1, 'World')");
conn.createStatement().execute("SHUTDOWN");
conn.close();
final String listener = getClass().getName();
final String listener = MyDatabaseEventListener.class.getName();
Runnable r = new Runnable() {
@Override
public void run() {
......@@ -162,27 +160,6 @@ public class TestMultiConn extends TestBase implements DatabaseEventListener {
thread.join();
}
@Override
public void exceptionThrown(SQLException e, String sql) {
// do nothing
}
@Override
public void setProgress(int state, String name, int x, int max) {
if (wait > 0) {
try {
Thread.sleep(wait);
} catch (InterruptedException e) {
TestBase.logError("sleep", e);
}
}
}
@Override
public void closingDatabase() {
// do nothing
}
private void testCommitRollback() throws SQLException {
deleteDb("multiConn");
Connection c1 = getConnection("multiConn");
......@@ -222,14 +199,38 @@ public class TestMultiConn extends TestBase implements DatabaseEventListener {
}
@Override
public void init(String url) {
// do nothing
}
public static final class MyDatabaseEventListener implements DatabaseEventListener {
@Override
public void opened() {
// do nothing
@Override
public void exceptionThrown(SQLException e, String sql) {
// do nothing
}
@Override
public void setProgress(int state, String name, int x, int max) {
if (wait > 0) {
try {
Thread.sleep(wait);
} catch (InterruptedException e) {
TestBase.logError("sleep", e);
}
}
}
@Override
public void closingDatabase() {
// do nothing
}
@Override
public void init(String url) {
// do nothing
}
@Override
public void opened() {
// do nothing
}
}
}
/*
* Copyright 2004-2013 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
* Copyright 2004-2013 H2 Group. Multiple-Licensed under the H2 License, Version
* 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html). Initial Developer: H2 Group
*/
package org.h2.test.db;
......@@ -24,13 +23,13 @@ import org.h2.util.Task;
/**
* Tests opening and closing a database.
*/
public class TestOpenClose extends TestBase implements DatabaseEventListener {
public class TestOpenClose extends TestBase {
private int nextId = 10;
/**
* Run just this test.
*
*
* @param a ignored
*/
public static void main(String... a) throws Exception {
......@@ -55,8 +54,8 @@ public class TestOpenClose extends TestBase implements DatabaseEventListener {
deleteDb("openClose");
Connection conn;
conn = getConnection("jdbc:h2:" + getBaseDir() + "/openClose;FILE_LOCK=FS");
assertThrows(ErrorCode.DATABASE_ALREADY_OPEN_1, this).
getConnection("jdbc:h2:" + getBaseDir() + "/openClose;FILE_LOCK=FS;OPEN_NEW=TRUE");
assertThrows(ErrorCode.DATABASE_ALREADY_OPEN_1, this).getConnection(
"jdbc:h2:" + getBaseDir() + "/openClose;FILE_LOCK=FS;OPEN_NEW=TRUE");
conn.close();
}
......@@ -73,8 +72,7 @@ public class TestOpenClose extends TestBase implements DatabaseEventListener {
c.position(c.size() * 2 - 1);
c.write(ByteBuffer.wrap(new byte[1]));
c.close();
assertThrows(ErrorCode.IO_EXCEPTION_2, this).
getConnection("jdbc:h2:split:18:" + getBaseDir() + "/openClose2");
assertThrows(ErrorCode.IO_EXCEPTION_2, this).getConnection("jdbc:h2:split:18:" + getBaseDir() + "/openClose2");
FileUtils.delete("split:" + getBaseDir() + "/openClose2.h2.db");
}
......@@ -127,8 +125,7 @@ public class TestOpenClose extends TestBase implements DatabaseEventListener {
deleteDb("openClose");
String user = getUser(), password = getPassword();
String url = getURL("openClose;DATABASE_EVENT_LISTENER='" + TestOpenClose.class.getName()
+ "'", true);
String url = getURL("openClose;DATABASE_EVENT_LISTENER='" + MyDatabaseEventListener.class.getName() + "'", true);
Connection conn = DriverManager.getConnection(url, user, password);
Statement stat = conn.createStatement();
stat.execute("CREATE TABLE TEST(ID IDENTITY, NAME VARCHAR)");
......@@ -204,51 +201,54 @@ public class TestOpenClose extends TestBase implements DatabaseEventListener {
return nextId++;
}
@Override
public void exceptionThrown(SQLException e, String sql) {
throw new AssertionError("unexpected: " + e + " sql: " + sql);
}
public static final class MyDatabaseEventListener implements DatabaseEventListener {
@Override
public void setProgress(int state, String name, int current, int max) {
String stateName;
switch (state) {
case STATE_SCAN_FILE:
stateName = "Scan " + name + " " + current + "/" + max;
if (current > 0) {
throw new AssertionError("unexpected: " + stateName);
}
break;
case STATE_STATEMENT_START:
break;
case STATE_CREATE_INDEX:
stateName = "Create Index " + name + " " + current + "/" + max;
if (!"SYS:SYS_ID".equals(name)) {
throw new AssertionError("unexpected: " + stateName);
@Override
public void exceptionThrown(SQLException e, String sql) {
throw new AssertionError("unexpected: " + e + " sql: " + sql);
}
@Override
public void setProgress(int state, String name, int current, int max) {
String stateName;
switch (state) {
case STATE_SCAN_FILE:
stateName = "Scan " + name + " " + current + "/" + max;
if (current > 0) {
throw new AssertionError("unexpected: " + stateName);
}
break;
case STATE_STATEMENT_START:
break;
case STATE_CREATE_INDEX:
stateName = "Create Index " + name + " " + current + "/" + max;
if (!"SYS:SYS_ID".equals(name)) {
throw new AssertionError("unexpected: " + stateName);
}
break;
case STATE_RECOVER:
stateName = "Recover " + current + "/" + max;
break;
default:
stateName = "?";
}
break;
case STATE_RECOVER:
stateName = "Recover " + current + "/" + max;
break;
default:
stateName = "?";
// System.out.println(": " + stateName);
}
// System.out.println(": " + stateName);
}
@Override
public void closingDatabase() {
// nothing to do
}
@Override
public void closingDatabase() {
// nothing to do
}
@Override
public void init(String url) {
// nothing to do
}
@Override
public void init(String url) {
// nothing to do
}
@Override
public void opened() {
// nothing to do
@Override
public void opened() {
// nothing to do
}
}
}
......@@ -20,7 +20,7 @@ import org.h2.test.TestBase;
/**
* Tests the DatabaseEventListener interface.
*/
public class TestDatabaseEventListener extends TestBase implements DatabaseEventListener {
public class TestDatabaseEventListener extends TestBase {
private static boolean calledOpened, calledClosingDatabase, calledScan, calledCreateIndex;
private static boolean calledStatementStart, calledStatementEnd, calledStatementProgress;
......@@ -136,7 +136,7 @@ public class TestDatabaseEventListener extends TestBase implements DatabaseEvent
conn = DriverManager.getConnection(url, p);
conn.close();
calledCreateIndex = false;
p.put("DATABASE_EVENT_LISTENER", getClass().getName());
p.put("DATABASE_EVENT_LISTENER", MyDatabaseEventListener.class.getName());
conn = org.h2.Driver.load().connect(url, p);
conn.close();
assertTrue(!calledCreateIndex);
......@@ -168,7 +168,7 @@ public class TestDatabaseEventListener extends TestBase implements DatabaseEvent
stat.execute("insert into test select 1");
conn.close();
calledCreateIndex = false;
p.put("DATABASE_EVENT_LISTENER", getClass().getName());
p.put("DATABASE_EVENT_LISTENER", MyDatabaseEventListener.class.getName());
conn = org.h2.Driver.load().connect(url, p);
conn.close();
assertTrue(!calledCreateIndex);
......@@ -195,7 +195,7 @@ public class TestDatabaseEventListener extends TestBase implements DatabaseEvent
calledOpened = false;
calledScan = false;
p.put("DATABASE_EVENT_LISTENER", getClass().getName());
p.put("DATABASE_EVENT_LISTENER", MyDatabaseEventListener.class.getName());
conn = org.h2.Driver.load().connect(url, p);
conn.close();
if (calledOpened) {
......@@ -209,7 +209,7 @@ public class TestDatabaseEventListener extends TestBase implements DatabaseEvent
p.setProperty("password", "sa");
calledOpened = false;
calledClosingDatabase = false;
p.put("DATABASE_EVENT_LISTENER", getClass().getName());
p.put("DATABASE_EVENT_LISTENER", MyDatabaseEventListener.class.getName());
org.h2.Driver.load();
String url = "jdbc:h2:mem:databaseEventListener";
Connection conn = org.h2.Driver.load().connect(url, p);
......@@ -225,7 +225,7 @@ public class TestDatabaseEventListener extends TestBase implements DatabaseEvent
calledStatementStart = false;
calledStatementEnd = false;
calledStatementProgress = false;
p.put("DATABASE_EVENT_LISTENER", getClass().getName());
p.put("DATABASE_EVENT_LISTENER", MyDatabaseEventListener.class.getName());
org.h2.Driver.load();
String url = "jdbc:h2:mem:databaseEventListener";
Connection conn = org.h2.Driver.load().connect(url, p);
......@@ -238,51 +238,55 @@ public class TestDatabaseEventListener extends TestBase implements DatabaseEvent
assertTrue(calledStatementProgress);
}
@Override
public void closingDatabase() {
calledClosingDatabase = true;
}
@Override
public void exceptionThrown(SQLException e, String sql) {
// nothing to do
}
public static final class MyDatabaseEventListener implements DatabaseEventListener {
@Override
public void closingDatabase() {
calledClosingDatabase = true;
}
@Override
public void init(String url) {
// nothing to do
}
@Override
public void exceptionThrown(SQLException e, String sql) {
// nothing to do
}
@Override
public void opened() {
calledOpened = true;
}
@Override
public void init(String url) {
// nothing to do
}
@Override
public void setProgress(int state, String name, int x, int max) {
if (state == DatabaseEventListener.STATE_SCAN_FILE) {
calledScan = true;
@Override
public void opened() {
calledOpened = true;
}
if (state == DatabaseEventListener.STATE_CREATE_INDEX) {
if (!name.startsWith("SYS:")) {
calledCreateIndex = true;
@Override
public void setProgress(int state, String name, int x, int max) {
if (state == DatabaseEventListener.STATE_SCAN_FILE) {
calledScan = true;
}
}
if (state == STATE_STATEMENT_START) {
if (name.equals("select * from test")) {
calledStatementStart = true;
if (state == DatabaseEventListener.STATE_CREATE_INDEX) {
if (!name.startsWith("SYS:")) {
calledCreateIndex = true;
}
}
}
if (state == STATE_STATEMENT_END) {
if (name.equals("select * from test")) {
calledStatementEnd = true;
if (state == STATE_STATEMENT_START) {
if (name.equals("select * from test")) {
calledStatementStart = true;
}
}
}
if (state == STATE_STATEMENT_PROGRESS) {
if (name.equals("select * from test")) {
calledStatementProgress = true;
if (state == STATE_STATEMENT_END) {
if (name.equals("select * from test")) {
calledStatementEnd = true;
}
}
if (state == STATE_STATEMENT_PROGRESS) {
if (name.equals("select * from test")) {
calledStatementProgress = true;
}
}
}
}
}
......@@ -20,7 +20,7 @@ import org.h2.tools.Server;
/**
* Tests automatic embedded/server mode.
*/
public class TestAutoReconnect extends TestBase implements DatabaseEventListener {
public class TestAutoReconnect extends TestBase {
private String url;
private boolean autoServer;
......@@ -95,7 +95,7 @@ public class TestAutoReconnect extends TestBase implements DatabaseEventListener
}
// test the database event listener
conn = DriverManager.getConnection(url + ";DATABASE_EVENT_LISTENER='" + getClass().getName() + "'");
conn = DriverManager.getConnection(url + ";DATABASE_EVENT_LISTENER='" + MyDatabaseEventListener.class.getName() + "'");
conn.close();
Statement stat;
......@@ -176,29 +176,30 @@ public class TestAutoReconnect extends TestBase implements DatabaseEventListener
}
}
@Override
public void closingDatabase() {
// ignore
}
public static final class MyDatabaseEventListener implements DatabaseEventListener {
@Override
public void closingDatabase() {
// ignore
}
@Override
public void exceptionThrown(SQLException e, String sql) {
// ignore
}
@Override
public void exceptionThrown(SQLException e, String sql) {
// ignore
}
@Override
public void init(String u) {
// ignore
}
@Override
public void init(String u) {
// ignore
}
@Override
public void opened() {
// ignore
}
@Override
public void opened() {
// ignore
}
@Override
public void setProgress(int state, String name, int x, int max) {
// ignore
@Override
public void setProgress(int state, String name, int x, int max) {
// ignore
}
}
}
......@@ -20,7 +20,7 @@ import org.h2.test.utils.SelfDestructor;
* Tests the flag db_close_on_exit.
* A new process is started.
*/
public class TestExit extends TestBase implements DatabaseEventListener {
public class TestExit extends TestBase {
public static Connection conn;
......@@ -92,11 +92,11 @@ public class TestExit extends TestBase implements DatabaseEventListener {
String url = "";
switch (action) {
case OPEN_WITH_CLOSE_ON_EXIT:
url = "jdbc:h2:" + getBaseDir() + "/exit;database_event_listener='" + getClass().getName()
url = "jdbc:h2:" + getBaseDir() + "/exit;database_event_listener='" + MyDatabaseEventListener.class.getName()
+ "';db_close_on_exit=true";
break;
case OPEN_WITHOUT_CLOSE_ON_EXIT:
url = "jdbc:h2:" + getBaseDir() + "/exit;database_event_listener='" + getClass().getName()
url = "jdbc:h2:" + getBaseDir() + "/exit;database_event_listener='" + MyDatabaseEventListener.class.getName()
+ "';db_close_on_exit=false";
break;
default:
......@@ -111,37 +111,41 @@ public class TestExit extends TestBase implements DatabaseEventListener {
return DriverManager.getConnection(url, "sa", "");
}
@Override
public void exceptionThrown(SQLException e, String sql) {
// nothing to do
private static File getClosedFile() {
return new File(TestBase.BASE_TEST_DIR + "/closed.txt");
}
@Override
public void closingDatabase() {
try {
getClosedFile().createNewFile();
} catch (IOException e) {
TestBase.logError("error", e);
public static final class MyDatabaseEventListener implements DatabaseEventListener {
@Override
public void exceptionThrown(SQLException e, String sql) {
// nothing to do
}
}
private static File getClosedFile() {
return new File(TestBase.BASE_TEST_DIR + "/closed.txt");
}
@Override
public void closingDatabase() {
try {
getClosedFile().createNewFile();
} catch (IOException e) {
TestBase.logError("error", e);
}
}
@Override
public void setProgress(int state, String name, int x, int max) {
// nothing to do
}
@Override
public void setProgress(int state, String name, int x, int max) {
// nothing to do
}
@Override
public void init(String url) {
// nothing to do
}
@Override
public void init(String url) {
// nothing to do
}
@Override
public void opened() {
// nothing to do
@Override
public void opened() {
// nothing to do
}
}
}
/*
* Copyright 2004-2013 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
* Copyright 2004-2013 H2 Group. Multiple-Licensed under the H2 License, Version
* 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html). Initial Developer: H2 Group
*/
package org.h2.test.unit;
......@@ -31,13 +30,13 @@ import org.h2.util.New;
/**
* Test the page store.
*/
public class TestPageStore extends TestBase implements DatabaseEventListener {
public class TestPageStore extends TestBase {
static StringBuilder eventBuffer = new StringBuilder();
/**
* Run just this test.
*
*
* @param a ignored
*/
public static void main(String... a) throws Exception {
......@@ -145,11 +144,10 @@ public class TestPageStore extends TestBase implements DatabaseEventListener {
stat.execute("set max_log_size 1");
stat.execute("create table test(x varchar)");
for (int i = 0; i < 1000; ++i) {
stat.execute("insert into test values (space(2000))");
stat.execute("insert into test values (space(2000))");
}
stat.execute("checkpoint");
InputStream in = FileUtils.newInputStream(getBaseDir() +
"/pageStoreLogLimitFalsePositive.trace.db");
InputStream in = FileUtils.newInputStream(getBaseDir() + "/pageStoreLogLimitFalsePositive.trace.db");
String s = IOUtils.readStringAndClose(new InputStreamReader(in), -1);
assertFalse(s.indexOf("Transaction log could not be truncated") > 0);
conn.close();
......@@ -446,11 +444,10 @@ public class TestPageStore extends TestBase implements DatabaseEventListener {
Statement stat = conn.createStatement();
int size = 1500;
stat.execute("call rand(1)");
stat.execute("create table test(id int primary key, data varchar, test int) as " +
"select x, '', 123 from system_range(1, " + size + ")");
stat.execute("create table test(id int primary key, data varchar, test int) as "
+ "select x, '', 123 from system_range(1, " + size + ")");
Random random = new Random(1);
PreparedStatement prep = conn.prepareStatement(
"update test set data=space(?) where id=?");
PreparedStatement prep = conn.prepareStatement("update test set data=space(?) where id=?");
for (int i = 0; i < 2500; i++) {
int id = random.nextInt(size);
int newSize = random.nextInt(6000);
......@@ -502,7 +499,7 @@ public class TestPageStore extends TestBase implements DatabaseEventListener {
// ignore
}
eventBuffer.setLength(0);
conn = getConnection(url + ";DATABASE_EVENT_LISTENER='" + getClass().getName() + "'");
conn = getConnection(url + ";DATABASE_EVENT_LISTENER='" + MyDatabaseEventListener.class.getName() + "'");
assertEquals("init;opened;", eventBuffer.toString());
conn.close();
}
......@@ -520,8 +517,7 @@ public class TestPageStore extends TestBase implements DatabaseEventListener {
conn.createStatement().execute("INSERT INTO TEST VALUES(1, 'Hello')");
conn.close();
conn = getConnection(url);
assertThrows(ErrorCode.DUPLICATE_KEY_1, conn.createStatement()).
execute("INSERT INTO TEST VALUES(2, 'Hello')");
assertThrows(ErrorCode.DUPLICATE_KEY_1, conn.createStatement()).execute("INSERT INTO TEST VALUES(2, 'Hello')");
conn.close();
}
......@@ -742,8 +738,7 @@ public class TestPageStore extends TestBase implements DatabaseEventListener {
stat.execute("INSERT INTO TEST VALUES(1)");
conn.close();
conn = getConnection("pageStoreUniqueIndex");
assertThrows(ErrorCode.DUPLICATE_KEY_1, conn.createStatement()).
execute("INSERT INTO TEST VALUES(1)");
assertThrows(ErrorCode.DUPLICATE_KEY_1, conn.createStatement()).execute("INSERT INTO TEST VALUES(1)");
conn.close();
}
......@@ -761,7 +756,7 @@ public class TestPageStore extends TestBase implements DatabaseEventListener {
conn.close();
}
private void testFuzzOperations() throws SQLException {
private void testFuzzOperations() throws Exception {
int best = Integer.MAX_VALUE;
for (int i = 0; i < 10; i++) {
int x = testFuzzOperationsSeed(i, 10);
......@@ -835,43 +830,44 @@ public class TestPageStore extends TestBase implements DatabaseEventListener {
trace(" " + m);
}
@Override
public void closingDatabase() {
event("closing");
}
@Override
public void exceptionThrown(SQLException e, String sql) {
event("exceptionThrown " + e + " " + sql);
}
public static final class MyDatabaseEventListener implements DatabaseEventListener {
@Override
public void closingDatabase() {
event("closing");
}
@Override
public void init(String url) {
event("init");
}
@Override
public void exceptionThrown(SQLException e, String sql) {
event("exceptionThrown " + e + " " + sql);
}
@Override
public void opened() {
event("opened");
}
@Override
public void init(String url) {
event("init");
}
@Override
public void setProgress(int state, String name, int x, int max) {
if (name.startsWith("SYS:SYS_ID")) {
// ignore
return;
@Override
public void opened() {
event("opened");
}
switch (state) {
case DatabaseEventListener.STATE_STATEMENT_START:
case DatabaseEventListener.STATE_STATEMENT_END:
case DatabaseEventListener.STATE_STATEMENT_PROGRESS:
return;
@Override
public void setProgress(int state, String name, int x, int max) {
if (name.startsWith("SYS:SYS_ID")) {
// ignore
return;
}
switch (state) {
case DatabaseEventListener.STATE_STATEMENT_START:
case DatabaseEventListener.STATE_STATEMENT_END:
case DatabaseEventListener.STATE_STATEMENT_PROGRESS:
return;
}
event("setProgress " + state + " " + name + " " + x + " " + max);
}
event("setProgress " + state + " " + name + " " + x + " " + max);
}
private static void event(String s) {
eventBuffer.append(s).append(';');
private static void event(String s) {
eventBuffer.append(s).append(';');
}
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论