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

assertThrows

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