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