提交 69097413 authored 作者: Thomas Mueller's avatar Thomas Mueller

Improved test case.

上级 67b3f30f
...@@ -731,7 +731,7 @@ kill -9 `jps -l | grep "org.h2.test." | cut -d " " -f 1` ...@@ -731,7 +731,7 @@ kill -9 `jps -l | grep "org.h2.test." | cut -d " " -f 1`
* old database files in the test directory and trace files. It also starts * old database files in the test directory and trace files. It also starts
* a TCP server if the test uses remote connections. * a TCP server if the test uses remote connections.
*/ */
void beforeTest() throws SQLException { public void beforeTest() throws SQLException {
Driver.load(); Driver.load();
FileUtils.deleteRecursive(TestBase.BASE_TEST_DIR, true); FileUtils.deleteRecursive(TestBase.BASE_TEST_DIR, true);
DeleteDbFiles.execute(TestBase.BASE_TEST_DIR, null, true); DeleteDbFiles.execute(TestBase.BASE_TEST_DIR, null, true);
...@@ -749,7 +749,10 @@ kill -9 `jps -l | grep "org.h2.test." | cut -d " " -f 1` ...@@ -749,7 +749,10 @@ kill -9 `jps -l | grep "org.h2.test." | cut -d " " -f 1`
} }
} }
private void afterTest() { /**
* Stop the server if it was started.
*/
public void afterTest() {
FileUtils.deleteRecursive("trace.db", true); FileUtils.deleteRecursive("trace.db", true);
if (networked && server != null) { if (networked && server != null) {
server.stop(); server.stop();
......
...@@ -1421,7 +1421,7 @@ public abstract class TestBase { ...@@ -1421,7 +1421,7 @@ public abstract class TestBase {
} }
try { try {
Class<?> pc = ProxyCodeGenerator.getClassProxy(c); Class<?> pc = ProxyCodeGenerator.getClassProxy(c);
Constructor cons = pc.getConstructor(new Class<?>[] { InvocationHandler.class }); Constructor<?> cons = pc.getConstructor(new Class<?>[] { InvocationHandler.class });
return (T) cons.newInstance(new Object[] { ih }); return (T) cons.newInstance(new Object[] { ih });
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
*/ */
package org.h2.test.jdbc; package org.h2.test.jdbc;
import java.io.Serializable;
import java.sql.Connection; import java.sql.Connection;
import java.sql.Date; import java.sql.Date;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
...@@ -18,7 +19,6 @@ import java.sql.Types; ...@@ -18,7 +19,6 @@ import java.sql.Types;
import java.util.Arrays; import java.util.Arrays;
import java.util.UUID; import java.util.UUID;
import org.h2.constant.SysProperties; import org.h2.constant.SysProperties;
import org.h2.test.TestAll;
import org.h2.test.TestBase; import org.h2.test.TestBase;
/** /**
...@@ -35,13 +35,13 @@ public class TestJavaObject extends TestBase { ...@@ -35,13 +35,13 @@ public class TestJavaObject extends TestBase {
* @param a ignored * @param a ignored
*/ */
public static void main(String... a) throws Exception { public static void main(String... a) throws Exception {
// System.setProperty("h2.serializeJavaObject", "false"); TestBase test = createCaller().init();
test.config.traceTest = true;
TestAll conf = new TestAll(); test.config.memory = true;
conf.traceTest = true; test.config.networked = true;
conf.memory = true; test.config.beforeTest();
// conf.networked = true; test.test();
TestBase.createCaller().init(conf).test(); test.config.afterTest();
} }
@Override @Override
...@@ -49,7 +49,7 @@ public class TestJavaObject extends TestBase { ...@@ -49,7 +49,7 @@ public class TestJavaObject extends TestBase {
SysProperties.SERIALIZE_JAVA_OBJECT = false; SysProperties.SERIALIZE_JAVA_OBJECT = false;
try { try {
trace("Test Java Object"); trace("Test Java Object");
startServerIfRequired(); doTest(new MyObj(1), new MyObj(2), false);
doTest(Arrays.asList(UUID.randomUUID(), null), Arrays.asList(UUID.randomUUID(), UUID.randomUUID()), true); doTest(Arrays.asList(UUID.randomUUID(), null), Arrays.asList(UUID.randomUUID(), UUID.randomUUID()), true);
doTest(new Timestamp(System.currentTimeMillis()), new Timestamp(System.currentTimeMillis() + 10000), false); doTest(new Timestamp(System.currentTimeMillis()), new Timestamp(System.currentTimeMillis() + 10000), false);
doTest(200, 100, false); doTest(200, 100, false);
...@@ -66,8 +66,8 @@ public class TestJavaObject extends TestBase { ...@@ -66,8 +66,8 @@ public class TestJavaObject extends TestBase {
private void doTest(Object o1, Object o2, boolean hash) throws SQLException { private void doTest(Object o1, Object o2, boolean hash) throws SQLException {
deleteDb("javaObject"); deleteDb("javaObject");
Connection conn = getConnection("javaObject"); Connection conn = getConnection("javaObject");
Statement stmt = conn.createStatement(); Statement stat = conn.createStatement();
stmt.execute("create table t(id identity, val other)"); stat.execute("create table t(id identity, val other)");
PreparedStatement ins = conn.prepareStatement("insert into t(val) values(?)"); PreparedStatement ins = conn.prepareStatement("insert into t(val) values(?)");
...@@ -77,30 +77,30 @@ public class TestJavaObject extends TestBase { ...@@ -77,30 +77,30 @@ public class TestJavaObject extends TestBase {
ins.setObject(1, o2, Types.JAVA_OBJECT); ins.setObject(1, o2, Types.JAVA_OBJECT);
assertEquals(1, ins.executeUpdate()); assertEquals(1, ins.executeUpdate());
ResultSet rs = stmt.executeQuery("select val from t order by val limit 1"); ResultSet rs = stat.executeQuery("select val from t order by val limit 1");
assertTrue(rs.next()); assertTrue(rs.next());
Object x; Object smallest;
if (hash) { if (hash) {
if (o1.getClass() != o2.getClass()) { if (o1.getClass() != o2.getClass()) {
x = o1.getClass().getName().compareTo(o2.getClass().getName()) < 0 ? o1 : o2; smallest = o1.getClass().getName().compareTo(o2.getClass().getName()) < 0 ? o1 : o2;
} else { } else {
assertFalse(o1.hashCode() == o2.hashCode()); assertFalse(o1.hashCode() == o2.hashCode());
x = o1.hashCode() < o2.hashCode() ? o1 : o2; smallest = o1.hashCode() < o2.hashCode() ? o1 : o2;
} }
} else { } else {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
int compare = ((Comparable<Object>) o1).compareTo(o2); int compare = ((Comparable<Object>) o1).compareTo(o2);
assertFalse(compare == 0); assertFalse(compare == 0);
x = compare < 0 ? o1 : o2; smallest = compare < 0 ? o1 : o2;
} }
assertEquals(x.toString(), rs.getString(1)); assertEquals(smallest.toString(), rs.getString(1));
Object y = rs.getObject(1); Object y = rs.getObject(1);
assertTrue(x.equals(y)); assertTrue(smallest.equals(y));
assertFalse(rs.next()); assertFalse(rs.next());
rs.close(); rs.close();
...@@ -120,11 +120,41 @@ public class TestJavaObject extends TestBase { ...@@ -120,11 +120,41 @@ public class TestJavaObject extends TestBase {
assertFalse(rs.next()); assertFalse(rs.next());
rs.close(); rs.close();
stmt.close(); stat.close();
prep.close(); prep.close();
conn.close(); conn.close();
deleteDb("javaObject"); deleteDb("javaObject");
trace("ok: " + o1.getClass().getName() + " vs " + o2.getClass().getName()); // trace("ok: " + o1.getClass().getName() + " vs " + o2.getClass().getName());
}
/**
* A test class.
*/
public static class MyObj implements Comparable<MyObj>, Serializable {
private static final long serialVersionUID = 1L;
private final int value;
MyObj(int value) {
this.value = value;
}
public String toString() {
return "" + value;
}
public int compareTo(MyObj o) {
return value - o.value;
}
public boolean equals(Object o) {
return value == ((MyObj) o).value;
}
public int hashCode() {
return -value;
}
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论