提交 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,7 +16,7 @@ import org.h2.util.Task;
/**
* Multi-connection tests.
*/
public class TestMultiConn extends TestBase implements DatabaseEventListener {
public class TestMultiConn extends TestBase {
private static int wait;
......@@ -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,6 +199,29 @@ public class TestMultiConn extends TestBase implements DatabaseEventListener {
}
public static final class MyDatabaseEventListener implements DatabaseEventListener {
@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
......@@ -231,5 +231,6 @@ public class TestMultiConn extends TestBase implements DatabaseEventListener {
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,7 +23,7 @@ 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;
......@@ -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,6 +201,8 @@ public class TestOpenClose extends TestBase implements DatabaseEventListener {
return nextId++;
}
public static final class MyDatabaseEventListener implements DatabaseEventListener {
@Override
public void exceptionThrown(SQLException e, String sql) {
throw new AssertionError("unexpected: " + e + " sql: " + sql);
......@@ -250,5 +249,6 @@ public class TestOpenClose extends TestBase implements DatabaseEventListener {
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,6 +238,8 @@ public class TestDatabaseEventListener extends TestBase implements DatabaseEvent
assertTrue(calledStatementProgress);
}
public static final class MyDatabaseEventListener implements DatabaseEventListener {
@Override
public void closingDatabase() {
calledClosingDatabase = true;
......@@ -285,4 +287,6 @@ public class TestDatabaseEventListener extends TestBase implements DatabaseEvent
}
}
}
}
......@@ -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,6 +176,7 @@ public class TestAutoReconnect extends TestBase implements DatabaseEventListener
}
}
public static final class MyDatabaseEventListener implements DatabaseEventListener {
@Override
public void closingDatabase() {
// ignore
......@@ -200,5 +201,5 @@ public class TestAutoReconnect extends TestBase implements DatabaseEventListener
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,6 +111,12 @@ public class TestExit extends TestBase implements DatabaseEventListener {
return DriverManager.getConnection(url, "sa", "");
}
private static File getClosedFile() {
return new File(TestBase.BASE_TEST_DIR + "/closed.txt");
}
public static final class MyDatabaseEventListener implements DatabaseEventListener {
@Override
public void exceptionThrown(SQLException e, String sql) {
// nothing to do
......@@ -125,10 +131,6 @@ public class TestExit extends TestBase implements DatabaseEventListener {
}
}
private static File getClosedFile() {
return new File(TestBase.BASE_TEST_DIR + "/closed.txt");
}
@Override
public void setProgress(int state, String name, int x, int max) {
// nothing to do
......@@ -144,4 +146,6 @@ public class TestExit extends TestBase implements DatabaseEventListener {
// 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,7 +30,7 @@ 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();
......@@ -148,8 +147,7 @@ public class TestPageStore extends TestBase implements DatabaseEventListener {
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,6 +830,7 @@ public class TestPageStore extends TestBase implements DatabaseEventListener {
trace(" " + m);
}
public static final class MyDatabaseEventListener implements DatabaseEventListener {
@Override
public void closingDatabase() {
event("closing");
......@@ -873,5 +869,5 @@ public class TestPageStore extends TestBase implements DatabaseEventListener {
private static void event(String s) {
eventBuffer.append(s).append(';');
}
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论