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

Improve code coverage.

上级 b2b95ccd
......@@ -567,6 +567,10 @@ public abstract class TestBase {
* @throws AssertionError if the values are not equal
*/
public void assertEquals(byte[] expected, byte[] actual) {
if (expected == null || actual == null) {
assertTrue(expected == actual);
return;
}
assertEquals(expected.length, actual.length);
for (int i = 0; i < expected.length; i++) {
if (expected[i] != actual[i]) {
......
......@@ -10,18 +10,25 @@ import java.net.URL;
import java.net.URLClassLoader;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import org.h2.constant.ErrorCode;
import org.h2.test.TestBase;
import org.h2.tools.Server;
import org.hsqldb.types.Types;
/**
* Tests the compatibility with older versions
*/
public class TestOldVersion extends TestBase {
private ClassLoader cl;
private Driver driver;
/**
* Run just this test.
*
......@@ -32,20 +39,72 @@ public class TestOldVersion extends TestBase {
}
public void test() throws Exception {
URL[] urls = { new URL("file:ext/h2-1.2.127.jar") };
ClassLoader cl = new URLClassLoader(urls, null);
// cl = getClass().getClassLoader();
Class<?> driverClass;
try {
driverClass = cl.loadClass("org.h2.Driver");
} catch (ClassNotFoundException e) {
cl = getClassLoader("file:ext/h2-1.2.127.jar");
driver = getDriver(cl);
if (driver == null) {
println("not found: ext/h2-1.2.127.jar - test skipped");
return;
}
Method m = driverClass.getMethod("load");
Driver driver = (Driver) m.invoke(null);
Connection conn = driver.connect("jdbc:h2:mem:", null);
assertEquals("1.2.127 (2010-01-15)", conn.getMetaData().getDatabaseProductVersion());
conn.close();
testLobInFiles();
testOldClientNewServer();
}
private void testLobInFiles() throws Exception {
deleteDb("oldVersion");
Connection conn;
Statement stat;
conn = driver.connect("jdbc:h2:" + getBaseDir() + "/oldVersion", null);
stat = conn.createStatement();
stat.execute("create table test(id int primary key, b blob, c clob)");
PreparedStatement prep = conn.prepareStatement("insert into test values(?, ?, ?)");
prep.setInt(1, 0);
prep.setNull(2, Types.BLOB);
prep.setNull(3, Types.CLOB);
prep.execute();
prep.setInt(1, 1);
prep.setBytes(2, new byte[0]);
prep.setString(3, "");
prep.execute();
prep.setInt(1, 2);
prep.setBytes(2, new byte[5]);
prep.setString(3, "\u1234\u1234\u1234\u1234\u1234");
prep.execute();
prep.setInt(1, 3);
prep.setBytes(2, new byte[100000]);
prep.setString(3, new String(new char[100000]));
prep.execute();
conn.close();
conn = DriverManager.getConnection("jdbc:h2:" + getBaseDir() + "/oldVersion", new Properties());
stat = conn.createStatement();
checkResult(stat.executeQuery("select * from test order by id"));
stat.execute("create table test2 as select * from test");
checkResult(stat.executeQuery("select * from test2 order by id"));
stat.execute("delete from test");
conn.close();
}
private void checkResult(ResultSet rs) throws SQLException {
rs.next();
assertEquals(0, rs.getInt(1));
assertEquals(null, rs.getBytes(2));
assertEquals(null, rs.getString(3));
rs.next();
assertEquals(1, rs.getInt(1));
assertEquals(new byte[0], rs.getBytes(2));
assertEquals("", rs.getString(3));
rs.next();
assertEquals(2, rs.getInt(1));
assertEquals(new byte[5], rs.getBytes(2));
assertEquals("\u1234\u1234\u1234\u1234\u1234", rs.getString(3));
rs.next();
assertEquals(3, rs.getInt(1));
assertEquals(new byte[100000], rs.getBytes(2));
assertEquals(new String(new char[100000]), rs.getString(3));
}
private void testOldClientNewServer() throws Exception {
Server server = org.h2.tools.Server.createTcpServer("-tcpPort", "9001");
server.start();
try {
......@@ -56,10 +115,12 @@ public class TestOldVersion extends TestBase {
server.stop();
Class<?> serverClass = cl.loadClass("org.h2.tools.Server");
Method m;
m = serverClass.getMethod("createTcpServer", String[].class);
Object serverOld = m.invoke(null, new Object[]{new String[]{"-tcpPort", "9001"}});
m = serverOld.getClass().getMethod("start");
m.invoke(serverOld);
Connection conn;
conn = org.h2.Driver.load().connect("jdbc:h2:mem:", null);
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery("call 1");
......@@ -70,4 +131,21 @@ public class TestOldVersion extends TestBase {
m.invoke(serverOld);
}
private ClassLoader getClassLoader(String jarFile) throws Exception {
URL[] urls = { new URL(jarFile) };
return new URLClassLoader(urls, null);
}
private Driver getDriver(ClassLoader cl) throws Exception {
Class<?> driverClass;
try {
driverClass = cl.loadClass("org.h2.Driver");
} catch (ClassNotFoundException e) {
return null;
}
Method m = driverClass.getMethod("load");
Driver driver = (Driver) m.invoke(null);
return driver;
}
}
......@@ -9,15 +9,19 @@ package org.h2.test.unit;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.UUID;
import org.h2.test.TestBase;
import org.h2.tools.SimpleResultSet;
import org.h2.value.DataType;
import org.h2.value.Value;
import org.h2.value.ValueDouble;
import org.h2.value.ValueFloat;
import org.h2.value.ValueResultSet;
import org.h2.value.ValueUuid;
import org.hsqldb.types.Types;
/**
* Tests features of values.
......@@ -33,13 +37,36 @@ public class TestValue extends TestBase {
TestBase.createCaller().init().test();
}
public void test() {
public void test() throws SQLException {
testValueResultSet();
testDataType();
testUUID();
testDouble(false);
testDouble(true);
}
private void testValueResultSet() throws SQLException {
SimpleResultSet rs = new SimpleResultSet();
rs.addColumn("ID", Types.INTEGER, 0, 0);
rs.addColumn("NAME", Types.VARCHAR, 255, 0);
rs.addRow(1, "Hello");
rs.addRow(2, "World");
rs.addRow(3, "Peace");
ValueResultSet v = ValueResultSet.getCopy(rs, 2);
rs.beforeFirst();
ResultSet rs2 = v.getResultSet();
rs2.next();
rs.next();
assertEquals(rs.getInt(1), rs2.getInt(1));
assertEquals(rs.getString(2), rs2.getString(2));
rs2.next();
rs.next();
assertEquals(rs.getInt(1), rs2.getInt(1));
assertEquals(rs.getString(2), rs2.getString(2));
assertFalse(rs2.next());
assertTrue(rs.next());
}
private void testDataType() {
testDataType(Value.NULL, null);
testDataType(Value.NULL, Void.class);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论