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

assertThrows

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