提交 1173ab9b authored 作者: Thomas Mueller's avatar Thomas Mueller

More tests

上级 5b5a787a
...@@ -86,6 +86,7 @@ import org.h2.test.server.TestPgServer; ...@@ -86,6 +86,7 @@ import org.h2.test.server.TestPgServer;
import org.h2.test.server.TestWeb; import org.h2.test.server.TestWeb;
import org.h2.test.synth.TestBtreeIndex; import org.h2.test.synth.TestBtreeIndex;
import org.h2.test.synth.TestCrashAPI; import org.h2.test.synth.TestCrashAPI;
import org.h2.test.synth.TestFuzzOptimizations;
import org.h2.test.synth.TestHaltApp; import org.h2.test.synth.TestHaltApp;
import org.h2.test.synth.TestJoin; import org.h2.test.synth.TestJoin;
import org.h2.test.synth.TestKill; import org.h2.test.synth.TestKill;
...@@ -570,6 +571,7 @@ http://www.w3schools.com/sql/ ...@@ -570,6 +571,7 @@ http://www.w3schools.com/sql/
// synth // synth
new TestCrashAPI().runTest(this); new TestCrashAPI().runTest(this);
new TestFuzzOptimizations().runTest(this);
new TestRandomSQL().runTest(this); new TestRandomSQL().runTest(this);
new TestKillRestart().runTest(this); new TestKillRestart().runTest(this);
new TestKillRestartMulti().runTest(this); new TestKillRestartMulti().runTest(this);
......
...@@ -12,6 +12,7 @@ import java.sql.DriverManager; ...@@ -12,6 +12,7 @@ import java.sql.DriverManager;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.ResultSetMetaData; import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
...@@ -98,6 +99,20 @@ public class Db { ...@@ -98,6 +99,20 @@ public class Db {
} }
} }
static List query(ResultSet rs) throws SQLException {
List list = new ArrayList();
ResultSetMetaData meta = rs.getMetaData();
int columnCount = meta.getColumnCount();
while (rs.next()) {
HashMap map = new HashMap();
for (int i = 0; i < columnCount; i++) {
map.put(meta.getColumnLabel(i+1), rs.getObject(i+1));
}
list.add(map);
}
return list;
}
/** /**
* Execute a SQL statement. * Execute a SQL statement.
* *
...@@ -106,18 +121,7 @@ public class Db { ...@@ -106,18 +121,7 @@ public class Db {
*/ */
public List query(String sql) { public List query(String sql) {
try { try {
List list = new ArrayList(); return query(stat.executeQuery(sql));
ResultSet rs = stat.executeQuery(sql);
ResultSetMetaData meta = rs.getMetaData();
int columnCount = meta.getColumnCount();
while (rs.next()) {
HashMap map = new HashMap();
for (int i = 0; i < columnCount; i++) {
map.put(meta.getColumnLabel(i+1), rs.getObject(i+1));
}
list.add(map);
}
return list;
} catch (Exception e) { } catch (Exception e) {
throw convert(e); throw convert(e);
} }
...@@ -211,6 +215,17 @@ public class Db { ...@@ -211,6 +215,17 @@ public class Db {
throw convert(e); throw convert(e);
} }
} }
/**
* Execute the prepared query.
*/
public List query() {
try {
return Db.query(prep.executeQuery());
} catch (Exception e) {
throw convert(e);
}
}
} }
/** /**
......
...@@ -11,6 +11,7 @@ import java.sql.SQLException; ...@@ -11,6 +11,7 @@ import java.sql.SQLException;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
import org.h2.constant.SysProperties;
import org.h2.test.TestBase; import org.h2.test.TestBase;
import org.h2.test.db.Db; import org.h2.test.db.Db;
import org.h2.test.db.Db.Prepared; import org.h2.test.db.Db.Prepared;
...@@ -19,7 +20,7 @@ import org.h2.test.db.Db.Prepared; ...@@ -19,7 +20,7 @@ import org.h2.test.db.Db.Prepared;
* This test executes random SQL statements to test if optimizations are working * This test executes random SQL statements to test if optimizations are working
* correctly. * correctly.
*/ */
public class TestOptimizations extends TestBase { public class TestFuzzOptimizations extends TestBase {
private Connection conn; private Connection conn;
...@@ -36,8 +37,39 @@ public class TestOptimizations extends TestBase { ...@@ -36,8 +37,39 @@ public class TestOptimizations extends TestBase {
deleteDb("optimizations"); deleteDb("optimizations");
conn = getConnection("optimizations"); conn = getConnection("optimizations");
testGroupSorted(); testGroupSorted();
testInSelect();
conn.close(); conn.close();
} }
private void testInSelect() throws SQLException {
boolean old = SysProperties.optimizeInJoin;
Db db = new Db(conn);
db.execute("CREATE TABLE TEST(A INT, B INT)");
db.execute("CREATE INDEX IDX ON TEST(A)");
db.execute("INSERT INTO TEST SELECT X/4, MOD(X, 4) FROM SYSTEM_RANGE(1, 16)");
db.execute("UPDATE TEST SET A = NULL WHERE A = 0");
db.execute("UPDATE TEST SET B = NULL WHERE B = 0");
Random random = new Random();
long seed = random.nextLong();
println("seed: " + seed);
for (int i = 0; i < 100; i++) {
String sql = "SELECT * FROM TEST T WHERE ";
sql += random.nextBoolean() ? "A" : "B";
sql += " IN(SELECT ";
sql += new String[] { "NULL", "0", "A", "B" }[random.nextInt(4)];
sql += " FROM TEST I WHERE I.";
sql += random.nextBoolean() ? "A" : "B";
sql += "=?) ORDER BY 1, 2";
int v = random.nextInt(3);
SysProperties.optimizeInJoin = false;
List a = db.prepare(sql).set(v).query();
SysProperties.optimizeInJoin = true;
List b = db.prepare(sql).set(v).query();
assertTrue(a.equals(b));
}
db.execute("DROP TABLE TEST");
SysProperties.optimizeInJoin = old;
}
private void testGroupSorted() throws SQLException { private void testGroupSorted() throws SQLException {
Db db = new Db(conn); Db db = new Db(conn);
...@@ -92,6 +124,7 @@ public class TestOptimizations extends TestBase { ...@@ -92,6 +124,7 @@ public class TestOptimizations extends TestBase {
} }
db.execute("DROP TABLE TEST_INDEXED"); db.execute("DROP TABLE TEST_INDEXED");
} }
db.execute("DROP TABLE TEST");
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论