提交 23adb2e7 authored 作者: Thomas Mueller's avatar Thomas Mueller

assertThrows

上级 c3e7d739
...@@ -16,6 +16,7 @@ import java.lang.reflect.Constructor; ...@@ -16,6 +16,7 @@ import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler; import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy; import java.lang.reflect.Proxy;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
...@@ -1353,18 +1354,23 @@ public abstract class TestBase { ...@@ -1353,18 +1354,23 @@ public abstract class TestBase {
} }
} }
}; };
if (obj == this) { Class<?>[] interfaces = c.getInterfaces();
// class proxies if (Modifier.isFinal(c.getModifiers())) {
try { // interface class proxies
Class<?> pc = ProxyCodeGenerator.getClassProxy(getClass()); if (interfaces.length == 0) {
Constructor cons = pc.getConstructor(new Class<?>[] { InvocationHandler.class }); throw new RuntimeException("Can not create a proxy for the class " +
return (T) cons.newInstance(new Object[] { ih }); c.getSimpleName() +
} catch (Exception e) { " because it doesn't implement any interfaces and is final");
throw new RuntimeException(e);
} }
return (T) Proxy.newProxyInstance(c.getClassLoader(), interfaces, ih);
}
try {
Class<?> pc = ProxyCodeGenerator.getClassProxy(c);
Constructor cons = pc.getConstructor(new Class<?>[] { InvocationHandler.class });
return (T) cons.newInstance(new Object[] { ih });
} catch (Exception e) {
throw new RuntimeException(e);
} }
return (T) Proxy.newProxyInstance(c.getClassLoader(),
c.getInterfaces(), ih);
} }
} }
...@@ -505,16 +505,11 @@ public class TestCases extends TestBase { ...@@ -505,16 +505,11 @@ public class TestCases extends TestBase {
conn.close(); conn.close();
} }
private void testInvalidDatabaseName() { private void testInvalidDatabaseName() throws SQLException {
if (config.memory) { if (config.memory) {
return; return;
} }
try { assertThrows(ErrorCode.INVALID_DATABASE_NAME_1, this).getConnection("cases/");
getConnection("cases/");
fail();
} catch (SQLException e) {
assertEquals(ErrorCode.INVALID_DATABASE_NAME_1, e.getErrorCode());
}
} }
private void testReuseSpace() throws SQLException { private void testReuseSpace() throws SQLException {
......
...@@ -322,18 +322,10 @@ public class TestCluster extends TestBase { ...@@ -322,18 +322,10 @@ public class TestCluster extends TestBase {
// try to connect in standalone mode - should fail // try to connect in standalone mode - should fail
// should not be able to connect in standalone mode // should not be able to connect in standalone mode
try { assertThrows(ErrorCode.CLUSTER_ERROR_DATABASE_RUNS_CLUSTERED_1, this).
getConnection("jdbc:h2:tcp://localhost:"+port1+"/test", user, password); getConnection("jdbc:h2:tcp://localhost:"+port1+"/test", user, password);
fail(); assertThrows(ErrorCode.CLUSTER_ERROR_DATABASE_RUNS_CLUSTERED_1, this).
} catch (SQLException e) { getConnection("jdbc:h2:tcp://localhost:"+port2+"/test", user, password);
assertKnownException(e);
}
try {
getConnection("jdbc:h2:tcp://localhost:"+port2+"/test", user, password);
fail();
} catch (SQLException e) {
assertKnownException(e);
}
// test a cluster connection // test a cluster connection
conn = DriverManager.getConnection("jdbc:h2:tcp://" + serverList + "/test", user, password); conn = DriverManager.getConnection("jdbc:h2:tcp://" + serverList + "/test", user, password);
......
...@@ -159,11 +159,21 @@ public class ProxyCodeGenerator { ...@@ -159,11 +159,21 @@ public class ProxyCodeGenerator {
writer.println(" }"); writer.println(" }");
writer.println(" @SuppressWarnings(\"unchecked\")"); writer.println(" @SuppressWarnings(\"unchecked\")");
writer.println(" private static <T extends RuntimeException> T convertException(Throwable e) {"); writer.println(" private static <T extends RuntimeException> T convertException(Throwable e) {");
writer.println(" if (e instanceof Error) {");
writer.println(" throw (Error) e;");
writer.println(" }");
writer.println(" return (T) e;"); writer.println(" return (T) e;");
writer.println(" }"); writer.println(" }");
for (Method m : methods.values()) { for (Method m : methods.values()) {
Class<?> retClass = m.getReturnType(); Class<?> retClass = m.getReturnType();
writer.print(" public " + getClassName(retClass) + writer.print(" ");
if (Modifier.isProtected(m.getModifiers())) {
// 'public' would also work
writer.print("protected ");
} else {
writer.print("public ");
}
writer.print(getClassName(retClass) +
" " + m.getName() + "("); " " + m.getName() + "(");
int i = 0; int i = 0;
for (Class<?> p : m.getParameterTypes()) { for (Class<?> p : m.getParameterTypes()) {
...@@ -192,7 +202,7 @@ public class ProxyCodeGenerator { ...@@ -192,7 +202,7 @@ public class ProxyCodeGenerator {
} else if (retClass == byte.class) { } else if (retClass == byte.class) {
writer.print("Byte"); writer.print("Byte");
} else if (retClass == char.class) { } else if (retClass == char.class) {
writer.print("Char"); writer.print("Character");
} else if (retClass == short.class) { } else if (retClass == short.class) {
writer.print("Short"); writer.print("Short");
} else if (retClass == int.class) { } else if (retClass == int.class) {
...@@ -210,7 +220,7 @@ public class ProxyCodeGenerator { ...@@ -210,7 +220,7 @@ public class ProxyCodeGenerator {
} }
writer.print("ih.invoke(this, "); writer.print("ih.invoke(this, ");
writer.println(getClassName(m.getDeclaringClass()) + writer.println(getClassName(m.getDeclaringClass()) +
".class.getMethod(\"" + m.getName() + ".class.getDeclaredMethod(\"" + m.getName() +
"\","); "\",");
writer.print(" new Class[] {"); writer.print(" new Class[] {");
i = 0; i = 0;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论