Browse Source

Rename the jdbc package to avoid the reflection call error

pull/1/head
yichen 2 years ago
parent
commit
b302d7f6c9
  1. 15
      agent/src/main/java/com/fanruan/AgentStarter.java
  2. 6
      agent/src/main/java/com/fanruan/agentjdbc/connection/MyConnection.java
  3. 4
      agent/src/main/java/com/fanruan/agentjdbc/driver/MyDriver.java
  4. 2
      agent/src/main/java/com/fanruan/agentjdbc/resultset/MyResultSet.java
  5. 4
      agent/src/main/java/com/fanruan/agentjdbc/statement/MyPreparedStatement.java
  6. 4
      agent/src/main/java/com/fanruan/agentjdbc/statement/MyStatement.java
  7. 17
      agent/src/main/java/com/fanruan/handler/DispatcherHelper.java
  8. 4
      agent/src/main/java/com/fanruan/handler/MyDispatcherImpl.java
  9. 2
      agent/src/main/java/com/fanruan/pojo/message/RpcRequest.java
  10. 19
      agent/src/main/java/com/fanruan/utils/DBProperties.java
  11. 2
      agent/src/test/java/HSQLTest.java
  12. 8
      agent/src/test/java/Test.java
  13. 1
      pom.xml
  14. 7
      service/src/main/java/com/fanruan/ServerStater.java
  15. 2
      service/src/main/java/com/fanruan/pojo/message/RpcRequest.java
  16. 5
      service/src/main/java/com/fanruan/proxy/interceptor/Interceptor.java
  17. 4
      service/src/main/java/com/fanruan/servicejdbc/MyDataBaseMetaData.java
  18. 8
      service/src/main/java/com/fanruan/servicejdbc/connection/MyConnection.java
  19. 4
      service/src/main/java/com/fanruan/servicejdbc/driver/MyDriver.java
  20. 2
      service/src/main/java/com/fanruan/servicejdbc/resultset/MyResultSet.java
  21. 4
      service/src/main/java/com/fanruan/servicejdbc/statement/MyPreparedStatement.java
  22. 4
      service/src/main/java/com/fanruan/servicejdbc/statement/MyStatement.java
  23. 19
      service/src/main/java/com/fanruan/utils/DBProperties.java
  24. 4
      service/src/test/java/AutoStarter.java
  25. 6
      service/src/test/java/ServiceTest.java
  26. 6
      service/src/test/java/Test.java
  27. 68
      test/pom.xml
  28. 110
      test/src/test/java/TestUtil.java

15
agent/src/main/java/com/fanruan/AgentStarter.java

@ -33,16 +33,17 @@ public class AgentStarter {
public static String AgentID; public static String AgentID;
public AgentStarter(String[] DBs) { public AgentStarter(String[][] DBs) {
myDispatcherImpl = new MyDispatcherImpl(); myDispatcherImpl = new MyDispatcherImpl();
try { try {
createSocket(DBs); createSocket(DBs);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
private void createSocket(String[] DBs) throws IOException { private void createSocket(String[][] DBs) throws IOException {
logger.debug("加载配置"); logger.debug("加载配置");
IO.Options options = new IO.Options(); IO.Options options = new IO.Options();
try{ try{
@ -80,10 +81,12 @@ public class AgentStarter {
MyDispatcherImpl.CACHE.registerSocket("/", defaultSocket); MyDispatcherImpl.CACHE.registerSocket("/", defaultSocket);
configDefaultSocket(defaultSocket); configDefaultSocket(defaultSocket);
for(String dbName : DBs){ for(String[] dbInfo : DBs){
Socket socket = IO.socket(URI.create(uri + "/" + dbName), options); Socket socket = IO.socket(URI.create(uri + "/" + dbInfo[0]), options);
MyDispatcherImpl.CACHE.registerSocket(dbName, socket); MyDispatcherImpl.CACHE.registerSocket(dbInfo[0], socket);
configSocket(socket, dbName); configSocket(socket, dbInfo[0]);
Class.forName(dbInfo[1]);
socket.connect();
} }
}catch (Exception e){ }catch (Exception e){

6
agent/src/main/java/com/fanruan/jdbc/connection/MyConnection.java → agent/src/main/java/com/fanruan/agentjdbc/connection/MyConnection.java

@ -1,7 +1,7 @@
package com.fanruan.jdbc.connection; package com.fanruan.agentjdbc.connection;
import com.fanruan.jdbc.statement.MyPreparedStatement; import com.fanruan.agentjdbc.statement.MyPreparedStatement;
import com.fanruan.jdbc.statement.MyStatement; import com.fanruan.agentjdbc.statement.MyStatement;
import java.sql.*; import java.sql.*;
import java.util.Map; import java.util.Map;

4
agent/src/main/java/com/fanruan/jdbc/driver/MyDriver.java → agent/src/main/java/com/fanruan/agentjdbc/driver/MyDriver.java

@ -1,6 +1,6 @@
package com.fanruan.jdbc.driver; package com.fanruan.agentjdbc.driver;
import com.fanruan.jdbc.connection.MyConnection; import com.fanruan.agentjdbc.connection.MyConnection;
import java.sql.*; import java.sql.*;
import java.util.Enumeration; import java.util.Enumeration;

2
agent/src/main/java/com/fanruan/jdbc/resultset/MyResultSet.java → agent/src/main/java/com/fanruan/agentjdbc/resultset/MyResultSet.java

@ -1,4 +1,4 @@
package com.fanruan.jdbc.resultset; package com.fanruan.agentjdbc.resultset;
import java.io.InputStream; import java.io.InputStream;
import java.io.Reader; import java.io.Reader;

4
agent/src/main/java/com/fanruan/jdbc/statement/MyPreparedStatement.java → agent/src/main/java/com/fanruan/agentjdbc/statement/MyPreparedStatement.java

@ -1,7 +1,7 @@
package com.fanruan.jdbc.statement; package com.fanruan.agentjdbc.statement;
import com.fanruan.jdbc.resultset.MyResultSet; import com.fanruan.agentjdbc.resultset.MyResultSet;
import java.io.InputStream; import java.io.InputStream;
import java.io.Reader; import java.io.Reader;

4
agent/src/main/java/com/fanruan/jdbc/statement/MyStatement.java → agent/src/main/java/com/fanruan/agentjdbc/statement/MyStatement.java

@ -1,6 +1,6 @@
package com.fanruan.jdbc.statement; package com.fanruan.agentjdbc.statement;
import com.fanruan.jdbc.resultset.MyResultSet; import com.fanruan.agentjdbc.resultset.MyResultSet;
import java.sql.*; import java.sql.*;

17
agent/src/main/java/com/fanruan/handler/DispatcherHelper.java

@ -3,6 +3,7 @@ package com.fanruan.handler;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.regex.Pattern;
/** /**
* Some utils for MyDispatcher * Some utils for MyDispatcher
@ -24,17 +25,17 @@ public class DispatcherHelper {
}; };
public final static String[] CACHE_LIST = new String[]{ public final static String[] CACHE_LIST = new String[]{
"com.fanruan.jdbc.driver.MyDriver", "MyDriver",
"com.fanruan.jdbc.connection.MyConnection", "MyConnection",
"com.fanruan.jdbc.statement.MyStatement", "MyStatement",
"com.fanruan.jdbc.statement.MyPreparedStatement", "MyPreparedStatement",
"com.fanruan.jdbc.resultset.MyResultSet", "MyResultSet",
}; };
public static boolean isInCacheList(String className){ public static boolean isInCacheList(String className){
for(String s : CACHE_LIST){ for(String s : CACHE_LIST){
if(s.equals(className)){ if(Pattern.matches(".*" + s, className)){
return true; return true;
} }
} }
@ -57,4 +58,8 @@ public class DispatcherHelper {
} }
return arr[n-1]; return arr[n-1];
} }
public static String transformName(String name){
return name.replace("servicejdbc", "agentjdbc");
}
} }

4
agent/src/main/java/com/fanruan/handler/MyDispatcherImpl.java

@ -40,7 +40,8 @@ public class MyDispatcherImpl implements Dispatcher{
@Override @Override
public Object invokeAsRequest(RpcRequest rpcRequest, BeanCacheImpl beanCache) throws Exception{ public Object invokeAsRequest(RpcRequest rpcRequest, BeanCacheImpl beanCache) throws Exception{
Class<?> clazz = rpcRequest.getServiceClass(); String fullName = DispatcherHelper.transformName(rpcRequest.getServiceClassName());
Class<?> clazz = Class.forName(fullName);
String methodName = rpcRequest.getMethodName(); String methodName = rpcRequest.getMethodName();
Object[] args = rpcRequest.getArgs(); Object[] args = rpcRequest.getArgs();
Class<?>[] argTypes = rpcRequest.getArgTypes(); Class<?>[] argTypes = rpcRequest.getArgTypes();
@ -50,7 +51,6 @@ public class MyDispatcherImpl implements Dispatcher{
String IDToCache = rpcRequest.getID(); String IDToCache = rpcRequest.getID();
String IDToInvoke = rpcRequest.getIDToInvoke(); String IDToInvoke = rpcRequest.getIDToInvoke();
String fullName = clazz.getName();
String className = DispatcherHelper.getClassName(fullName); String className = DispatcherHelper.getClassName(fullName);
// If BeanCache contains instance, get it; if not, create it. // If BeanCache contains instance, get it; if not, create it.

2
agent/src/main/java/com/fanruan/pojo/message/RpcRequest.java

@ -13,7 +13,7 @@ public class RpcRequest {
private String ID; private String ID;
private boolean binding; private boolean binding;
private String IDToInvoke; private String IDToInvoke;
private Class<?> serviceClass; private String serviceClassName;
private String methodName; private String methodName;
private Object[] args; private Object[] args;
private Class<?>[] argTypes; private Class<?>[] argTypes;

19
agent/src/main/java/com/fanruan/utils/DBProperties.java

@ -4,17 +4,10 @@ package com.fanruan.utils;
* @author Yichen Dai * @author Yichen Dai
*/ */
public class DBProperties { public class DBProperties {
public static final String MYSQL = "mysql"; public static final String[] MYSQL = new String[]{"mysql", "com.mysql.cj.jdbc.Driver"};
public static final String POSTGRESQL = "postgresql"; public static final String[] POSTGRESQL = new String[]{"postgresql", "org.postgresql.Driver"};
public static final String ORACLE = "oracle"; public static final String[] ORACLE = new String[]{"oracle", "oracle.jdbc.driver.OracleDriver"};
public static final String SQLSERVER = "sqlserver"; public static final String[] SQLSERVER = new String[]{"sqlserver", "com.microsoft.jdbc.sqlserver.SQLServerDriver"};
public static final String DB2 = "db2"; public static final String[] DB2 = new String[]{"db2", "com.ibm.db2.jdbc.app.DB2Driver"};
public static final String HSQL = "hsql"; public static final String[] HSQL = new String[]{"hsql", "org.hsqldb.jdbcDriver"};
public static final String MYSQL_DRIVER_NAME = "com.mysql.cj.jdbc.Driver";
public static final String POSTGRESQL_DRIVER_NAME = "org.postgresql.Driver";
public static final String ORACLE_DRIVER_NAME = "oracle.jdbc.driver.OracleDriver";
public static final String SQLSERVER_DRIVER_NAME = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
public static final String DB2_DRIVER_NAME = "com.ibm.db2.jdbc.app.DB2Driver";
public static final String HSQL_DRIVER_NAME = "org.hsqldb.jdbcDriver";
} }

2
agent/src/test/java/HSQLTest.java

@ -14,7 +14,7 @@ public class HSQLTest {
@BeforeEach @BeforeEach
void startHSQL(){ void startHSQL(){
try { try {
Class.forName(DBProperties.HSQL_DRIVER_NAME); Class.forName(DBProperties.HSQL[1]);
Thread.sleep(1000); Thread.sleep(1000);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();

8
agent/src/test/java/Test.java

@ -18,8 +18,8 @@ public class Test {
} }
static void testStart() throws IOException, ClassNotFoundException { static void testStart() throws IOException, ClassNotFoundException {
Class.forName(DBProperties.HSQL_DRIVER_NAME); Class.forName(DBProperties.HSQL[1]);
String[] DBs = new String[]{ String[][] DBs = new String[][]{
DBProperties.HSQL, DBProperties.HSQL,
}; };
@ -28,11 +28,9 @@ public class Test {
Socket mainSocket = AgentStarter.myDispatcherImpl.CACHE.getSocket("/"); Socket mainSocket = AgentStarter.myDispatcherImpl.CACHE.getSocket("/");
mainSocket.connect(); mainSocket.connect();
Socket socket = AgentStarter.myDispatcherImpl.CACHE.getSocket(DBProperties.HSQL); Socket socket = AgentStarter.myDispatcherImpl.CACHE.getSocket(DBProperties.HSQL[0]);
socket.connect(); socket.connect();
socket.open();
} }
} }

1
pom.xml

@ -10,6 +10,7 @@
<modules> <modules>
<module>agent</module> <module>agent</module>
<module>service</module> <module>service</module>
<module>test</module>
</modules> </modules>
<packaging>pom</packaging> <packaging>pom</packaging>

7
service/src/main/java/com/fanruan/ServerStater.java

@ -43,11 +43,12 @@ public class ServerStater{
public ServerStater(String[] dbs){ public ServerStater(String[][] dbs){
try{ try{
Class.forName("com.fanruan.servicejdbc.driver.MyDriver");
loadConfig(); loadConfig();
for(String dbName : dbs){ for(String[] dbInfo : dbs){
SocketIONamespace namespace = server.addNamespace("/" + dbName); SocketIONamespace namespace = server.addNamespace("/" + dbInfo[0]);
addEvent(namespace); addEvent(namespace);
} }
server.start(); server.start();

2
service/src/main/java/com/fanruan/pojo/message/RpcRequest.java

@ -22,7 +22,7 @@ public class RpcRequest {
private boolean binding; private boolean binding;
private String ID; private String ID;
private String IDToInvoke; private String IDToInvoke;
private Class serviceClass; private String serviceClassName;
private String methodName; private String methodName;
private Object[] args; private Object[] args;
private Class[] argTypes; private Class[] argTypes;

5
service/src/main/java/com/fanruan/proxy/interceptor/Interceptor.java

@ -7,6 +7,7 @@ import com.fanruan.cache.ClientCache;
import com.fanruan.cache.ClientWrapper; import com.fanruan.cache.ClientWrapper;
import com.fanruan.cache.LockAndCondition; import com.fanruan.cache.LockAndCondition;
import com.fanruan.pojo.message.RpcRequest; import com.fanruan.pojo.message.RpcRequest;
import com.fanruan.servicejdbc.resultset.MyResultSet;
import com.fanruan.utils.Commons; import com.fanruan.utils.Commons;
import net.sf.cglib.proxy.MethodInterceptor; import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy; import net.sf.cglib.proxy.MethodProxy;
@ -61,13 +62,13 @@ public class Interceptor implements MethodInterceptor {
rpcRequest.setReply(false) rpcRequest.setReply(false)
.setBinding(false) .setBinding(false)
.setID(Commons.getID()) .setID(Commons.getID())
.setServiceClass(clazz) .setServiceClassName(clazz.getName())
.setMethodName(method.getName()) .setMethodName(method.getName())
.setArgs(objects) .setArgs(objects)
.setArgTypes(getArgTypes(objects)); .setArgTypes(getArgTypes(objects));
// Set whether the rpcResponses of this rpcRequest need to carry return value // Set whether the rpcResponses of this rpcRequest need to carry return value
if(o instanceof com.fanruan.jdbc.resultset.MyResultSet){ if(o instanceof MyResultSet){
boolean flag = InterceptorUtils.isInReplyList(method.getName()); boolean flag = InterceptorUtils.isInReplyList(method.getName());
if(flag) { if(flag) {
rpcRequest.setReply(true); rpcRequest.setReply(true);

4
service/src/main/java/com/fanruan/jdbc/MyDataBaseMetaData.java → service/src/main/java/com/fanruan/servicejdbc/MyDataBaseMetaData.java

@ -1,7 +1,7 @@
package com.fanruan.jdbc; package com.fanruan.servicejdbc;
import com.fanruan.jdbc.driver.MyDriver; import com.fanruan.servicejdbc.driver.MyDriver;
import java.sql.Connection; import java.sql.Connection;
import java.sql.ResultSet; import java.sql.ResultSet;

8
service/src/main/java/com/fanruan/jdbc/connection/MyConnection.java → service/src/main/java/com/fanruan/servicejdbc/connection/MyConnection.java

@ -1,11 +1,11 @@
package com.fanruan.jdbc.connection; package com.fanruan.servicejdbc.connection;
import com.corundumstudio.socketio.SocketIOClient; import com.corundumstudio.socketio.SocketIOClient;
import com.fanruan.cache.ClientCache; import com.fanruan.cache.ClientCache;
import com.fanruan.jdbc.MyDataBaseMetaData; import com.fanruan.servicejdbc.MyDataBaseMetaData;
import com.fanruan.jdbc.statement.MyPreparedStatement; import com.fanruan.servicejdbc.statement.MyPreparedStatement;
import com.fanruan.jdbc.statement.MyStatement; import com.fanruan.servicejdbc.statement.MyStatement;
import com.fanruan.proxy.ProxyFactory; import com.fanruan.proxy.ProxyFactory;
import java.sql.*; import java.sql.*;

4
service/src/main/java/com/fanruan/jdbc/driver/MyDriver.java → service/src/main/java/com/fanruan/servicejdbc/driver/MyDriver.java

@ -1,6 +1,6 @@
package com.fanruan.jdbc.driver; package com.fanruan.servicejdbc.driver;
import com.fanruan.jdbc.connection.MyConnection; import com.fanruan.servicejdbc.connection.MyConnection;
import com.fanruan.proxy.ProxyFactory; import com.fanruan.proxy.ProxyFactory;
import java.sql.*; import java.sql.*;

2
service/src/main/java/com/fanruan/jdbc/resultset/MyResultSet.java → service/src/main/java/com/fanruan/servicejdbc/resultset/MyResultSet.java

@ -1,4 +1,4 @@
package com.fanruan.jdbc.resultset; package com.fanruan.servicejdbc.resultset;
import java.io.InputStream; import java.io.InputStream;

4
service/src/main/java/com/fanruan/jdbc/statement/MyPreparedStatement.java → service/src/main/java/com/fanruan/servicejdbc/statement/MyPreparedStatement.java

@ -1,6 +1,6 @@
package com.fanruan.jdbc.statement; package com.fanruan.servicejdbc.statement;
import com.fanruan.jdbc.resultset.MyResultSet; import com.fanruan.servicejdbc.resultset.MyResultSet;
import com.fanruan.proxy.ProxyFactory; import com.fanruan.proxy.ProxyFactory;
import java.io.InputStream; import java.io.InputStream;

4
service/src/main/java/com/fanruan/jdbc/statement/MyStatement.java → service/src/main/java/com/fanruan/servicejdbc/statement/MyStatement.java

@ -1,6 +1,6 @@
package com.fanruan.jdbc.statement; package com.fanruan.servicejdbc.statement;
import com.fanruan.jdbc.resultset.MyResultSet; import com.fanruan.servicejdbc.resultset.MyResultSet;
import com.fanruan.proxy.ProxyFactory; import com.fanruan.proxy.ProxyFactory;
import java.sql.*; import java.sql.*;

19
service/src/main/java/com/fanruan/utils/DBProperties.java

@ -5,17 +5,10 @@ package com.fanruan.utils;
* @author Yichen Dai * @author Yichen Dai
*/ */
public class DBProperties { public class DBProperties {
public static final String MYSQL = "mysql"; public static final String[] MYSQL = new String[]{"mysql", "com.mysql.cj.jdbc.Driver"};
public static final String POSTGRESQL = "postgresql"; public static final String[] POSTGRESQL = new String[]{"postgresql", "org.postgresql.Driver"};
public static final String ORACLE = "oracle"; public static final String[] ORACLE = new String[]{"oracle", "oracle.jdbc.driver.OracleDriver"};
public static final String SQLSERVER = "sqlserver"; public static final String[] SQLSERVER = new String[]{"sqlserver", "com.microsoft.jdbc.sqlserver.SQLServerDriver"};
public static final String DB2 = "db2"; public static final String[] DB2 = new String[]{"db2", "com.ibm.db2.jdbc.app.DB2Driver"};
public static final String HSQL = "hsql"; public static final String[] HSQL = new String[]{"hsql", "org.hsqldb.jdbcDriver"};
public static final String MYSQL_DRIVER_NAME = "com.mysql.cj.jdbc.Driver";
public static final String POSTGRESQL_DRIVER_NAME = "org.postgresql.Driver";
public static final String ORACLE_DRIVER_NAME = "oracle.jdbc.driver.OracleDriver";
public static final String SQLSERVER_DRIVER_NAME = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
public static final String DB2_DRIVER_NAME = "com.ibm.db2.jdbc.app.DB2Driver";
public static final String HSQL_DRIVER_NAME = "org.hsqldb.jdbcDriver";
} }

4
service/src/test/java/AutoStarter.java

@ -8,13 +8,13 @@ import com.fanruan.utils.DBProperties;
public class AutoStarter { public class AutoStarter {
static { static {
String[] DBs = new String[]{ String[][] DBs = new String[][]{
DBProperties.HSQL, DBProperties.HSQL,
}; };
new ServerStater(DBs); new ServerStater(DBs);
try { try {
Class.forName("com.fanruan.jdbc.driver.MyDriver"); Class.forName("com.fanruan.servicejdbc.driver.MyDriver");
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} }

6
service/src/test/java/ServiceTest.java

@ -21,14 +21,14 @@ public class ServiceTest extends AutoStarter{
void listen() throws ExecutionException, InterruptedException { void listen() throws ExecutionException, InterruptedException {
FutureTask<SocketIOClient> ft = new FutureTask<>( FutureTask<SocketIOClient> ft = new FutureTask<>(
() -> { () -> {
while(ClientCache.getClient("1001", DBProperties.HSQL) == null){ while(ClientCache.getClient("1001", DBProperties.HSQL[0]) == null){
try { try {
Thread.sleep(100); Thread.sleep(100);
}catch (Exception e){ }catch (Exception e){
e.printStackTrace(); e.printStackTrace();
} }
} }
return ClientCache.getClient("1001", DBProperties.HSQL); return ClientCache.getClient("1001", DBProperties.HSQL[0]);
} }
); );
ft.run(); ft.run();
@ -41,7 +41,7 @@ public class ServiceTest extends AutoStarter{
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); info.setProperty("agentDBName", DBProperties.HSQL[0]);
Connection conn = null; Connection conn = null;
Statement st = null; Statement st = null;

6
service/src/test/java/Test.java

@ -13,7 +13,7 @@ public class Test {
Test test = new Test(); Test test = new Test();
String[] DBs = new String[]{ String[][] DBs = new String[][]{
DBProperties.MYSQL, DBProperties.MYSQL,
DBProperties.POSTGRESQL, DBProperties.POSTGRESQL,
DBProperties.SQLSERVER, DBProperties.SQLSERVER,
@ -51,7 +51,7 @@ public class Test {
ResultSet rs2 = null; ResultSet rs2 = null;
ResultSet rs3 = null; ResultSet rs3 = null;
try { try {
Class.forName("com.fanruan.jdbc.driver.MyDriver"); Class.forName("com.fanruan.servicejdbc.driver.MyDriver");
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", info); conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", info);
st = conn.createStatement(); st = conn.createStatement();
rs1 = st.executeQuery("select * from `student`"); rs1 = st.executeQuery("select * from `student`");
@ -158,7 +158,7 @@ public class Test {
ResultSet rs2 = null; ResultSet rs2 = null;
ResultSet rs3 = null; ResultSet rs3 = null;
try { try {
Class.forName("com.fanruan.jdbc.driver.MyDriver"); Class.forName("com.fanruan.servicejdbc.driver.MyDriver");
conn = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/test", info); conn = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/test", info);
st = conn.createStatement(); st = conn.createStatement();
rs1 = st.executeQuery("select * from student"); rs1 = st.executeQuery("select * from student");

68
test/pom.xml

@ -0,0 +1,68 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>intranet</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>test</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>agent</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hsqldb/hsqldb -->
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<classifier>debug</classifier>
<version>2.5.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.17.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

110
test/src/test/java/TestUtil.java

@ -0,0 +1,110 @@
import com.fanruan.AgentStarter;
import com.fanruan.ServerStater;
import com.fanruan.servicejdbc.driver.MyDriver;
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 java.sql.*;
import java.util.Properties;
/**
* @author Yichen Dai
* @date 2022/8/18 15:27
*/
public class TestUtil {
@BeforeAll
static void autoConfig(){
configService();
configAgent();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Test
void testCURD(){
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 = (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)" +
");");
// 插入数据
st.executeUpdate("INSERT INTO student VALUES" +
"(1, '张三', '上海')," +
"(2, '李四', '北京')," +
"(3, '王五', '成都');");
// 预查询语句 删除指定 ID
pst = conn.prepareStatement("delete from student where student_id = ?");
pst.setInt(1, 1);
pst.executeUpdate();
rs = st.executeQuery("select * from student");
String[] nameStrings = new String[]{"张三", "李四", "王五"};
String[] addressStrings = new String[]{"上海", "北京", "成都"};
// 结果集断言
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();
}
}
static void configService(){
String[][] DBs = new String[][]{
DBProperties.HSQL,
};
new ServerStater(DBs);
}
static void configAgent(){
String[][] DBs = new String[][]{
DBProperties.HSQL,
};
new AgentStarter(DBs);
}
}
Loading…
Cancel
Save