提交 5718e574 authored 作者: Thomas Mueller's avatar Thomas Mueller

assertThrows

上级 5dd0e44e
......@@ -505,7 +505,7 @@ public class LobStorage {
if (lobId != -1) {
removeLob(lobId);
}
throw DbException.convertIOException(e, "adding blob");
throw DbException.convertIOException(e, null);
}
} catch (SQLException e) {
throw DbException.convert(e);
......
......@@ -1298,7 +1298,12 @@ public abstract class TestBase {
fail("Expected: SQLException, got: " + e);
}
SQLException s = (SQLException) e;
assertEquals(errorCode, s.getErrorCode());
if (errorCode != s.getErrorCode()) {
AssertionError ae = new AssertionError(
"Expected an SQLException with error code " + errorCode);
ae.initCause(e);
throw ae;
}
}
}, "SQLException with error code " + errorCode, obj);
}
......@@ -1351,7 +1356,7 @@ public abstract class TestBase {
if (obj == this) {
// class proxies
try {
Class<?> pc = ProxyCodeGenerator.getClassProxy(TestBase.class);
Class<?> pc = ProxyCodeGenerator.getClassProxy(getClass());
Constructor cons = pc.getConstructor(new Class<?>[] { InvocationHandler.class });
return (T) cons.newInstance(new Object[] { ih });
} catch (Exception e) {
......
......@@ -76,15 +76,8 @@ public class TestOpenClose extends TestBase implements DatabaseEventListener {
FileObject f = FileSystem.getInstance(getBaseDir()).openFileObject(getBaseDir() + "/openClose2.h2.db.1.part", "rw");
f.setFileLength(f.length() * 2);
f.close();
int testing;
assertThrows(ErrorCode.IO_EXCEPTION_2, (TestBase) this).
assertThrows(ErrorCode.IO_EXCEPTION_2, this).
getConnection("jdbc:h2:split:18:" + getBaseDir() + "/openClose2");
// try {
// getConnection("jdbc:h2:split:18:" + getBaseDir() + "/openClose2");
// fail();
// } catch (SQLException e) {
// assertEquals(ErrorCode.IO_EXCEPTION_2, e.getErrorCode());
// }
FileSystem.getInstance("split:").delete("split:" + getBaseDir() + "/openClose2.h2.db");
}
......
......@@ -67,7 +67,7 @@ public class TestLobApi extends TestBase {
stat.execute("create table test(id identity, c clob, b blob)");
PreparedStatement prep = conn.prepareStatement("insert into test values(null, ?, ?)");
assertThrows(ErrorCode.IO_EXCEPTION_2, prep).
assertThrows(ErrorCode.IO_EXCEPTION_1, prep).
setCharacterStream(1, new Reader() {
int pos;
public int read(char[] buff, int off, int len) throws IOException {
......@@ -90,7 +90,7 @@ public class TestLobApi extends TestBase {
prep.execute();
prep.setString(1, "");
assertThrows(ErrorCode.IO_EXCEPTION_2, prep).
assertThrows(ErrorCode.IO_EXCEPTION_1, prep).
setBinaryStream(2, new InputStream() {
int pos;
public int read() throws IOException {
......
......@@ -11,10 +11,9 @@ import java.io.StringWriter;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.TreeMap;
import java.util.TreeSet;
import org.h2.test.TestBase;
import org.h2.util.New;
import org.h2.util.SourceCompiler;
......@@ -27,7 +26,7 @@ public class ProxyCodeGenerator {
static HashMap<Class<?>, Class<?>> proxyMap = New.hashMap();
private TreeSet<String> imports = new TreeSet<String>();
private ArrayList<Method> methods = new ArrayList<Method>();
private TreeMap<String, Method> methods = new TreeMap<String, Method>();
private String packageName;
private String className;
private Class<?> extendsClass;
......@@ -39,7 +38,7 @@ public class ProxyCodeGenerator {
}
ProxyCodeGenerator cg = new ProxyCodeGenerator();
cg.setPackageName("bytecode");
cg.generateClassProxy(TestBase.class);
cg.generateClassProxy(c);
StringWriter sw = new StringWriter();
cg.write(new PrintWriter(sw));
String code = sw.toString();
......@@ -76,16 +75,22 @@ public class ProxyCodeGenerator {
addImport(clazz);
className = getClassName(clazz) + "Proxy";
extendsClass = clazz;
for (Method m : clazz.getDeclaredMethods()) {
if (!Modifier.isStatic(m.getModifiers())) {
if (!Modifier.isPrivate(m.getModifiers())) {
int finalOrStaticOrPrivate = Modifier.FINAL | Modifier.STATIC | Modifier.PRIVATE;
while (clazz != null) {
for (Method m : clazz.getDeclaredMethods()) {
if ((m.getModifiers() & finalOrStaticOrPrivate) == 0) {
addMethod(m);
}
}
clazz = clazz.getSuperclass();
}
}
void addMethod(Method m) {
if (methods.containsKey(getMethodName(m))) {
// already declared in a subclass
return;
}
addImport(m.getReturnType());
for (Class<?> c : m.getParameterTypes()) {
addImport(c);
......@@ -93,9 +98,21 @@ public class ProxyCodeGenerator {
for (Class<?> c : m.getExceptionTypes()) {
addImport(c);
}
methods.add(m);
methods.put(getMethodName(m), m);
}
private String getMethodName(Method m) {
StringBuilder buff = new StringBuilder();
buff.append(m.getReturnType()).append(' ');
buff.append(m.getName());
for (Class<?> p : m.getParameterTypes()) {
buff.append(' ');
buff.append(p.getName());
}
return buff.toString();
}
void addImport(Class<?> c) {
while (c.isArray()) {
c = c.getComponentType();
......@@ -144,7 +161,7 @@ public class ProxyCodeGenerator {
writer.println(" private static <T extends RuntimeException> T convertException(Throwable e) {");
writer.println(" return (T) e;");
writer.println(" }");
for (Method m : methods) {
for (Method m : methods.values()) {
Class<?> retClass = m.getReturnType();
writer.print(" public " + getClassName(retClass) +
" " + m.getName() + "(");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论