Skip to content

Commit

Permalink
用@Before/@after替换@BeforeClass/@afterclass,方便创建和关闭不同的JDBC连接
Browse files Browse the repository at this point in the history
  • Loading branch information
codefollower committed Mar 8, 2017
1 parent 9fbf9ea commit 6676aea
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 60 deletions.
Expand Up @@ -18,7 +18,6 @@
package org.lealone.test.async;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Random;
Expand All @@ -31,10 +30,6 @@ public static void main(String[] args) throws Exception {
new AsyncTest().run();
}

static Connection getConn(String url) throws Exception {
return DriverManager.getConnection(url);
}

class MyThread extends Thread {
Connection connection;
Statement statement;
Expand Down Expand Up @@ -87,7 +82,6 @@ public void run() throws Exception {
sql = "select * from AsyncTest where pk = '01'";
// oneThread();
multiThreads();
tearDownAfterClass();
}

public void multiThreads() throws Exception {
Expand Down
Expand Up @@ -69,7 +69,5 @@ public void run() {

t1.join();
t2.join();

tearDownAfterClass();
}
}
Expand Up @@ -83,7 +83,6 @@ public void run() throws Exception {
sql = "select * from StatementPriorityTest where pk = '01'";
// oneThread();
multiThreads();
tearDownAfterClass();
}

public void multiThreads() throws Exception {
Expand Down
Expand Up @@ -22,6 +22,7 @@
import org.lealone.test.sql.SqlTestBase;

public class ShardingTest extends SqlTestBase {

public ShardingTest() {
super(LealoneDatabase.NAME); // 连到LealoneDatabase才能执行CREATE DATABASE
}
Expand All @@ -37,24 +38,36 @@ public void run() throws Exception {
stmt.executeUpdate("CREATE DATABASE IF NOT EXISTS ShardingTestDB1 RUN MODE sharding");
stmt.executeUpdate("CREATE DATABASE IF NOT EXISTS ShardingTestDB2 RUN MODE sharding PARAMETERS(hostIds='1,2')");

stmt.executeUpdate("drop table IF EXISTS ShardingTest");
stmt.executeUpdate("create table IF NOT EXISTS ShardingTest(f1 int SELECTIVITY 10, f2 int, f3 int)");

stmt.executeUpdate("create index IF NOT EXISTS ShardingTest_i1 on ShardingTest(f1)");

stmt.executeUpdate("insert into ShardingTest(f1, f2, f3) values(1,2,3)");
stmt.executeUpdate("insert into ShardingTest(f1, f2, f3) values(5,2,3)");
stmt.executeUpdate("insert into ShardingTest(f1, f2, f3) values(3,2,3)");
stmt.executeUpdate("insert into ShardingTest(f1, f2, f3) values(8,2,3)");
stmt.executeUpdate("insert into ShardingTest(f1, f2, f3) values(3,2,3)");
stmt.executeUpdate("insert into ShardingTest(f1, f2, f3) values(8,2,3)");
stmt.executeUpdate("insert into ShardingTest(f1, f2, f3) values(3,2,3)");
stmt.executeUpdate("insert into ShardingTest(f1, f2, f3) values(8,2,3)");
stmt.executeUpdate("insert into ShardingTest(f1, f2, f3) values(3,2,3)");
stmt.executeUpdate("insert into ShardingTest(f1, f2, f3) values(8,2,3)");

sql = "select distinct * from ShardingTest where f1 > 3";
sql = "select distinct f1 from ShardingTest";
printResultSet();
new ShardingCrudTest("ShardingTestDB1").runTest();
}

private class ShardingCrudTest extends SqlTestBase {

public ShardingCrudTest(String dbName) {
super(dbName);
}

@Override
protected void test() throws Exception {
stmt.executeUpdate("drop table IF EXISTS ShardingTest");
stmt.executeUpdate("create table IF NOT EXISTS ShardingTest(f1 int SELECTIVITY 10, f2 int, f3 int)");

stmt.executeUpdate("create index IF NOT EXISTS ShardingTest_i1 on ShardingTest(f1)");

stmt.executeUpdate("insert into ShardingTest(f1, f2, f3) values(1,2,3)");
stmt.executeUpdate("insert into ShardingTest(f1, f2, f3) values(5,2,3)");
stmt.executeUpdate("insert into ShardingTest(f1, f2, f3) values(3,2,3)");
stmt.executeUpdate("insert into ShardingTest(f1, f2, f3) values(8,2,3)");
stmt.executeUpdate("insert into ShardingTest(f1, f2, f3) values(3,2,3)");
stmt.executeUpdate("insert into ShardingTest(f1, f2, f3) values(8,2,3)");
stmt.executeUpdate("insert into ShardingTest(f1, f2, f3) values(3,2,3)");
stmt.executeUpdate("insert into ShardingTest(f1, f2, f3) values(8,2,3)");
stmt.executeUpdate("insert into ShardingTest(f1, f2, f3) values(3,2,3)");
stmt.executeUpdate("insert into ShardingTest(f1, f2, f3) values(8,2,3)");

sql = "select distinct * from ShardingTest where f1 > 3";
sql = "select distinct f1 from ShardingTest";
printResultSet();
}
}
}
68 changes: 41 additions & 27 deletions lealone-test/src/test/java/org/lealone/test/sql/SqlTestBase.java
Expand Up @@ -22,58 +22,72 @@
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.AfterClass;
import org.junit.After;
import org.junit.Before;
import org.lealone.common.util.JdbcUtils;
import org.lealone.test.TestBase;

public class SqlTestBase extends TestBase {
protected static Connection conn;
protected static Statement stmt;

// protected String user, password;
protected String dbName;
protected String user;
protected String password;

protected Connection conn;
protected Statement stmt;
protected ResultSet rs;
protected String sql;

protected SqlTestBase() {
try {
// addConnectionParameter("TRACE_LEVEL_FILE", TraceSystem.ADAPTER + "");
conn = getConnection();
stmt = conn.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
// addConnectionParameter("TRACE_LEVEL_FILE", TraceSystem.ADAPTER + "");
}

protected SqlTestBase(String dbName) {
this.dbName = dbName;
}

protected SqlTestBase(String user, String password) {
this.user = user;
this.password = password;
}

@Before
public void setUpBefore() {
try {
conn = getConnection(user, password);
if (dbName != null) {
conn = getConnection(dbName);
} else if (user != null) {
conn = getConnection(user, password);
} else {
conn = getConnection();
}
stmt = conn.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
}

protected SqlTestBase(String dbName) {
@After
public void tearDownAfter() {
JdbcUtils.closeSilently(rs);
JdbcUtils.closeSilently(stmt);
JdbcUtils.closeSilently(conn);
}

// 不用加@Test,子类可以手工运行,只要实现test方法即可
public void runTest() {
setUpBefore();
try {
conn = getConnection(dbName);
stmt = conn.createStatement();
test();
} catch (Exception e) {
e.printStackTrace();
} finally {
tearDownAfter();
}
}

// @BeforeClass
// public static void setUpBeforeClass() throws Exception {
// conn = new TestBase().getConnection();
// stmt = conn.createStatement();
// }
//
@AfterClass
public static void tearDownAfterClass() throws Exception {
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
protected void test() throws Exception {
// do nothing
}

public int executeUpdate(String sql) {
Expand Down
Expand Up @@ -126,7 +126,8 @@ void test0() throws Exception {
printResultSet();
}

void test() throws Exception {
@Override
protected void test() throws Exception {
// 以下有21个日期与时间函数,
// 在这个方法中org.lealone.expression.Function.getSimpleValue(Session, Value, Expression[], Value[])
// ----------------------------------------------------------------------------------------
Expand Down
Expand Up @@ -26,7 +26,8 @@ public void run() throws Exception {
test();
}

void test() throws Exception {
@Override
protected void test() throws Exception {
sql = "SELECT DECODE(RAND()>0.5, 0, 'Red', 1, 'Black')";

sql = "SELECT DECODE(RAND()>0.5, 0, 'Red1', 0, 'Red2', 1, 'Black1', 1, 'Black2')";
Expand Down
Expand Up @@ -34,7 +34,8 @@ void init() throws Exception {
stmt.executeUpdate("CREATE ALIAS IF NOT EXISTS MY_SQRT FOR \"java.lang.Math.sqrt\"");
}

void test() throws Exception {
@Override
protected void test() throws Exception {
sql = "?= CALL MY_SQRT(?)";
CallableStatement cs = conn.prepareCall(sql);
cs.registerOutParameter(1, Types.DOUBLE); // sqlType其实被忽略了,所以设什么都没用
Expand Down
Expand Up @@ -68,7 +68,8 @@ void insert() throws Exception {
executeUpdate("insert into JoinTest4(pk, id, name) values(4, 30, 'a1')");
}

void test() throws Exception {
@Override
protected void test() throws Exception {
sql = "select rownum, * from JoinTest1 LEFT OUTER JOIN JoinTest2";
sql = "select rownum, * from JoinTest1 RIGHT OUTER JOIN JoinTest2";
sql = "select rownum, * from JoinTest1 INNER JOIN JoinTest2";
Expand Down
Expand Up @@ -63,7 +63,8 @@ void init() throws Exception {
ps.close();
}

void test() throws Exception {
@Override
protected void test() throws Exception {
sql = "SELECT count(*) FROM PreparedStatementTest WHERE f2 >= ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, 30);
Expand Down

0 comments on commit 6676aea

Please sign in to comment.