|
|
@ -2,12 +2,10 @@ |
|
|
|
import com.fanruan.AgentStarter; |
|
|
|
import com.fanruan.AgentStarter; |
|
|
|
import com.fanruan.ServerStater; |
|
|
|
import com.fanruan.ServerStater; |
|
|
|
|
|
|
|
|
|
|
|
import com.fanruan.servicejdbc.driver.MyDriver; |
|
|
|
import com.fanruan.service.jdbc.driver.ServiceDriver; |
|
|
|
import com.fanruan.proxy.ProxyFactory; |
|
|
|
import com.fanruan.proxy.ProxyFactory; |
|
|
|
import com.fanruan.utils.DBProperties; |
|
|
|
import com.fanruan.utils.DBProperties; |
|
|
|
import org.junit.jupiter.api.Assertions; |
|
|
|
import org.junit.jupiter.api.*; |
|
|
|
import org.junit.jupiter.api.BeforeAll; |
|
|
|
|
|
|
|
import org.junit.jupiter.api.Test; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.sql.*; |
|
|
|
import java.sql.*; |
|
|
|
import java.util.Properties; |
|
|
|
import java.util.Properties; |
|
|
@ -16,13 +14,34 @@ import java.util.Properties; |
|
|
|
* @author Yichen Dai |
|
|
|
* @author Yichen Dai |
|
|
|
* @date 2022/8/18 15:27 |
|
|
|
* @date 2022/8/18 15:27 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
public class TestUtil { |
|
|
|
public class TestUtil { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static Connection conn = null; |
|
|
|
|
|
|
|
static Statement st = null; |
|
|
|
|
|
|
|
static PreparedStatement pst = null; |
|
|
|
|
|
|
|
static ResultSet rs = null; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void configService(){ |
|
|
|
|
|
|
|
String[][] DBs = new String[][]{ |
|
|
|
|
|
|
|
DBProperties.HSQL, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
new ServerStater(DBs); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void configAgent(){ |
|
|
|
|
|
|
|
String[][] DBs = new String[][]{ |
|
|
|
|
|
|
|
DBProperties.HSQL, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
new AgentStarter(DBs); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@BeforeAll |
|
|
|
@BeforeAll |
|
|
|
static void autoConfig(){ |
|
|
|
static void autoConfig(){ |
|
|
|
configService(); |
|
|
|
configService(); |
|
|
|
configAgent(); |
|
|
|
configAgent(); |
|
|
|
try { |
|
|
|
try { |
|
|
|
|
|
|
|
// 等待socket连接
|
|
|
|
Thread.sleep(2000); |
|
|
|
Thread.sleep(2000); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
} catch (InterruptedException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
e.printStackTrace(); |
|
|
@ -30,81 +49,122 @@ public class TestUtil { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void testCURD(){ |
|
|
|
void testConnect() throws SQLException { |
|
|
|
|
|
|
|
// 建立连接
|
|
|
|
Properties info = new Properties(); |
|
|
|
Properties info = new Properties(); |
|
|
|
info.setProperty("user", "sa"); |
|
|
|
info.setProperty("user", "sa"); |
|
|
|
info.setProperty("password", ""); |
|
|
|
info.setProperty("password", ""); |
|
|
|
info.setProperty("agentID", "1001"); |
|
|
|
info.setProperty("agentID", "1001"); |
|
|
|
info.setProperty("agentDBName", DBProperties.HSQL[0]); |
|
|
|
info.setProperty("agentDBName", DBProperties.HSQL[0]); |
|
|
|
|
|
|
|
|
|
|
|
Connection conn = null; |
|
|
|
Driver driver = (ServiceDriver) ProxyFactory.getProxy(ServiceDriver.class, null); |
|
|
|
Statement st = null; |
|
|
|
conn = driver.connect("jdbc:hsqldb:mem:test", info); |
|
|
|
PreparedStatement pst = null; |
|
|
|
} |
|
|
|
ResultSet rs = null; |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 创建 连接
|
|
|
|
|
|
|
|
Driver driver = (MyDriver) ProxyFactory.getProxy(MyDriver.class, null); |
|
|
|
|
|
|
|
conn = driver.connect("jdbc:hsqldb:mem:test", info); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 创建 statement
|
|
|
|
|
|
|
|
st = conn.createStatement(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 创建表
|
|
|
|
|
|
|
|
st.executeUpdate("DROP TABLE student IF EXISTS;"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
st.executeUpdate("CREATE TABLE student (" + |
|
|
|
@Test |
|
|
|
"student_id INTEGER GENERATED BY DEFAULT AS IDENTITY " + |
|
|
|
void testCreateTable() throws SQLException { |
|
|
|
"(START WITH 1, INCREMENT BY 1) NOT NULL," + |
|
|
|
testConnect(); |
|
|
|
"student_name VARCHAR(100) NOT NULL," + |
|
|
|
// 创建 statement
|
|
|
|
"student_address VARCHAR(100) NOT NULL," + |
|
|
|
st = conn.createStatement(); |
|
|
|
"PRIMARY KEY (student_id)" + |
|
|
|
|
|
|
|
");"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 插入数据
|
|
|
|
// 创建表
|
|
|
|
st.executeUpdate("INSERT INTO student VALUES" + |
|
|
|
int num = st.executeUpdate("DROP TABLE student IF EXISTS;"); |
|
|
|
"(1, '张三', '上海')," + |
|
|
|
|
|
|
|
"(2, '李四', '北京')," + |
|
|
|
|
|
|
|
"(3, '王五', '成都');"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Assertions.assertEquals(0, num); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 预查询语句 删除指定 ID
|
|
|
|
num = st.executeUpdate("CREATE TABLE student (" + |
|
|
|
pst = conn.prepareStatement("delete from student where student_id = ?"); |
|
|
|
"student_id INTEGER GENERATED BY DEFAULT AS IDENTITY " + |
|
|
|
|
|
|
|
"(START WITH 1, INCREMENT BY 1) NOT NULL," + |
|
|
|
|
|
|
|
"student_name VARCHAR(100) NOT NULL," + |
|
|
|
|
|
|
|
"student_address VARCHAR(100) NOT NULL," + |
|
|
|
|
|
|
|
"PRIMARY KEY (student_id)" + |
|
|
|
|
|
|
|
");"); |
|
|
|
|
|
|
|
|
|
|
|
pst.setInt(1, 1); |
|
|
|
Assertions.assertEquals(0, num); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pst.executeUpdate(); |
|
|
|
@Test |
|
|
|
|
|
|
|
void testInsert() throws SQLException { |
|
|
|
|
|
|
|
testCreateTable(); |
|
|
|
|
|
|
|
// 插入数据
|
|
|
|
|
|
|
|
int num = st.executeUpdate("INSERT INTO student VALUES" + |
|
|
|
|
|
|
|
"(1, '张三', '上海')," + |
|
|
|
|
|
|
|
"(2, '李四', '北京')," + |
|
|
|
|
|
|
|
"(3, '王五', '成都');"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Assertions.assertEquals(3, num); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
rs = st.executeQuery("select * from student"); |
|
|
|
@Test |
|
|
|
|
|
|
|
void testUpdate() throws SQLException { |
|
|
|
|
|
|
|
testInsert(); |
|
|
|
|
|
|
|
// 预查询语句 删除指定 ID
|
|
|
|
|
|
|
|
pst = conn.prepareStatement("UPDATE student" + |
|
|
|
|
|
|
|
" SET student_name = '李华', student_address = '杭州'"+ |
|
|
|
|
|
|
|
"WHERE student_id = ?"); |
|
|
|
|
|
|
|
|
|
|
|
String[] nameStrings = new String[]{"张三", "李四", "王五"}; |
|
|
|
Assertions.assertNotNull(pst); |
|
|
|
String[] addressStrings = new String[]{"上海", "北京", "成都"}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 结果集断言
|
|
|
|
pst.setInt(1, 1); |
|
|
|
int num = 2; |
|
|
|
|
|
|
|
while(rs.next()) { |
|
|
|
int num = pst.executeUpdate(); |
|
|
|
Assertions.assertEquals(rs.getInt("student_id"), num); |
|
|
|
|
|
|
|
Assertions.assertEquals(rs.getString("student_name"), nameStrings[num-1]); |
|
|
|
Assertions.assertEquals(1, num); |
|
|
|
Assertions.assertEquals(rs.getString("student_address"), addressStrings[num-1]); |
|
|
|
|
|
|
|
num++; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static void configService(){ |
|
|
|
@Test |
|
|
|
String[][] DBs = new String[][]{ |
|
|
|
void testDelete() throws SQLException { |
|
|
|
DBProperties.HSQL, |
|
|
|
testInsert(); |
|
|
|
}; |
|
|
|
// 预查询语句 删除指定 ID
|
|
|
|
new ServerStater(DBs); |
|
|
|
pst = conn.prepareStatement("delete from student where student_id = ?"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Assertions.assertNotNull(pst); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pst.setInt(1, 1); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int num = pst.executeUpdate(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Assertions.assertEquals(1, num); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static void configAgent(){ |
|
|
|
@Test |
|
|
|
String[][] DBs = new String[][]{ |
|
|
|
void testSelect() throws SQLException { |
|
|
|
DBProperties.HSQL, |
|
|
|
testInsert(); |
|
|
|
}; |
|
|
|
rs = st.executeQuery("select * from student"); |
|
|
|
new AgentStarter(DBs); |
|
|
|
|
|
|
|
|
|
|
|
String[] nameStrings = new String[]{"张三", "李四", "王五"}; |
|
|
|
|
|
|
|
String[] addressStrings = new String[]{"上海", "北京", "成都"}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 结果集断言
|
|
|
|
|
|
|
|
int num = 1; |
|
|
|
|
|
|
|
while(rs.next()) { |
|
|
|
|
|
|
|
Assertions.assertEquals(rs.getInt("student_id"), num); |
|
|
|
|
|
|
|
Assertions.assertEquals(rs.getString("student_name"), nameStrings[num-1]); |
|
|
|
|
|
|
|
Assertions.assertEquals(rs.getString("student_address"), addressStrings[num-1]); |
|
|
|
|
|
|
|
num++; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@AfterAll |
|
|
|
|
|
|
|
static void close() throws SQLException { |
|
|
|
|
|
|
|
if(rs!= null){ |
|
|
|
|
|
|
|
rs.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(pst != null){ |
|
|
|
|
|
|
|
pst.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(st != null){ |
|
|
|
|
|
|
|
st.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(conn != null){ |
|
|
|
|
|
|
|
conn.close(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|