From b302d7f6c9e1c4acd9427746bf4d720bda7b2a56 Mon Sep 17 00:00:00 2001 From: yichen Date: Fri, 19 Aug 2022 10:34:07 +0800 Subject: [PATCH] Rename the jdbc package to avoid the reflection call error --- .../main/java/com/fanruan/AgentStarter.java | 15 ++- .../connection/MyConnection.java | 6 +- .../{jdbc => agentjdbc}/driver/MyDriver.java | 4 +- .../resultset/MyResultSet.java | 2 +- .../statement/MyPreparedStatement.java | 4 +- .../statement/MyStatement.java | 4 +- .../com/fanruan/handler/DispatcherHelper.java | 17 ++- .../com/fanruan/handler/MyDispatcherImpl.java | 4 +- .../com/fanruan/pojo/message/RpcRequest.java | 2 +- .../java/com/fanruan/utils/DBProperties.java | 19 +-- agent/src/test/java/HSQLTest.java | 2 +- agent/src/test/java/Test.java | 8 +- pom.xml | 1 + .../main/java/com/fanruan/ServerStater.java | 7 +- .../com/fanruan/pojo/message/RpcRequest.java | 2 +- .../proxy/interceptor/Interceptor.java | 5 +- .../MyDataBaseMetaData.java | 4 +- .../connection/MyConnection.java | 8 +- .../driver/MyDriver.java | 4 +- .../resultset/MyResultSet.java | 2 +- .../statement/MyPreparedStatement.java | 4 +- .../statement/MyStatement.java | 4 +- .../java/com/fanruan/utils/DBProperties.java | 19 +-- service/src/test/java/AutoStarter.java | 4 +- service/src/test/java/ServiceTest.java | 6 +- service/src/test/java/Test.java | 6 +- test/pom.xml | 68 +++++++++++ test/src/test/java/TestUtil.java | 110 ++++++++++++++++++ 28 files changed, 257 insertions(+), 84 deletions(-) rename agent/src/main/java/com/fanruan/{jdbc => agentjdbc}/connection/MyConnection.java (98%) rename agent/src/main/java/com/fanruan/{jdbc => agentjdbc}/driver/MyDriver.java (94%) rename agent/src/main/java/com/fanruan/{jdbc => agentjdbc}/resultset/MyResultSet.java (99%) rename agent/src/main/java/com/fanruan/{jdbc => agentjdbc}/statement/MyPreparedStatement.java (99%) rename agent/src/main/java/com/fanruan/{jdbc => agentjdbc}/statement/MyStatement.java (98%) rename service/src/main/java/com/fanruan/{jdbc => servicejdbc}/MyDataBaseMetaData.java (99%) rename service/src/main/java/com/fanruan/{jdbc => servicejdbc}/connection/MyConnection.java (97%) rename service/src/main/java/com/fanruan/{jdbc => servicejdbc}/driver/MyDriver.java (95%) rename service/src/main/java/com/fanruan/{jdbc => servicejdbc}/resultset/MyResultSet.java (99%) rename service/src/main/java/com/fanruan/{jdbc => servicejdbc}/statement/MyPreparedStatement.java (99%) rename service/src/main/java/com/fanruan/{jdbc => servicejdbc}/statement/MyStatement.java (98%) create mode 100644 test/pom.xml create mode 100644 test/src/test/java/TestUtil.java diff --git a/agent/src/main/java/com/fanruan/AgentStarter.java b/agent/src/main/java/com/fanruan/AgentStarter.java index 5e73494..60b0cb2 100644 --- a/agent/src/main/java/com/fanruan/AgentStarter.java +++ b/agent/src/main/java/com/fanruan/AgentStarter.java @@ -33,16 +33,17 @@ public class AgentStarter { public static String AgentID; - public AgentStarter(String[] DBs) { + public AgentStarter(String[][] DBs) { myDispatcherImpl = new MyDispatcherImpl(); try { createSocket(DBs); + } catch (Exception e) { e.printStackTrace(); } } - private void createSocket(String[] DBs) throws IOException { + private void createSocket(String[][] DBs) throws IOException { logger.debug("加载配置"); IO.Options options = new IO.Options(); try{ @@ -80,10 +81,12 @@ public class AgentStarter { MyDispatcherImpl.CACHE.registerSocket("/", defaultSocket); configDefaultSocket(defaultSocket); - for(String dbName : DBs){ - Socket socket = IO.socket(URI.create(uri + "/" + dbName), options); - MyDispatcherImpl.CACHE.registerSocket(dbName, socket); - configSocket(socket, dbName); + for(String[] dbInfo : DBs){ + Socket socket = IO.socket(URI.create(uri + "/" + dbInfo[0]), options); + MyDispatcherImpl.CACHE.registerSocket(dbInfo[0], socket); + configSocket(socket, dbInfo[0]); + Class.forName(dbInfo[1]); + socket.connect(); } }catch (Exception e){ diff --git a/agent/src/main/java/com/fanruan/jdbc/connection/MyConnection.java b/agent/src/main/java/com/fanruan/agentjdbc/connection/MyConnection.java similarity index 98% rename from agent/src/main/java/com/fanruan/jdbc/connection/MyConnection.java rename to agent/src/main/java/com/fanruan/agentjdbc/connection/MyConnection.java index eba9f53..643c7de 100644 --- a/agent/src/main/java/com/fanruan/jdbc/connection/MyConnection.java +++ b/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.jdbc.statement.MyStatement; +import com.fanruan.agentjdbc.statement.MyPreparedStatement; +import com.fanruan.agentjdbc.statement.MyStatement; import java.sql.*; import java.util.Map; diff --git a/agent/src/main/java/com/fanruan/jdbc/driver/MyDriver.java b/agent/src/main/java/com/fanruan/agentjdbc/driver/MyDriver.java similarity index 94% rename from agent/src/main/java/com/fanruan/jdbc/driver/MyDriver.java rename to agent/src/main/java/com/fanruan/agentjdbc/driver/MyDriver.java index 61c262e..07c8c3c 100644 --- a/agent/src/main/java/com/fanruan/jdbc/driver/MyDriver.java +++ b/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.util.Enumeration; diff --git a/agent/src/main/java/com/fanruan/jdbc/resultset/MyResultSet.java b/agent/src/main/java/com/fanruan/agentjdbc/resultset/MyResultSet.java similarity index 99% rename from agent/src/main/java/com/fanruan/jdbc/resultset/MyResultSet.java rename to agent/src/main/java/com/fanruan/agentjdbc/resultset/MyResultSet.java index 1432acf..3093c2b 100644 --- a/agent/src/main/java/com/fanruan/jdbc/resultset/MyResultSet.java +++ b/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.Reader; diff --git a/agent/src/main/java/com/fanruan/jdbc/statement/MyPreparedStatement.java b/agent/src/main/java/com/fanruan/agentjdbc/statement/MyPreparedStatement.java similarity index 99% rename from agent/src/main/java/com/fanruan/jdbc/statement/MyPreparedStatement.java rename to agent/src/main/java/com/fanruan/agentjdbc/statement/MyPreparedStatement.java index a96e520..b9d218b 100644 --- a/agent/src/main/java/com/fanruan/jdbc/statement/MyPreparedStatement.java +++ b/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.Reader; diff --git a/agent/src/main/java/com/fanruan/jdbc/statement/MyStatement.java b/agent/src/main/java/com/fanruan/agentjdbc/statement/MyStatement.java similarity index 98% rename from agent/src/main/java/com/fanruan/jdbc/statement/MyStatement.java rename to agent/src/main/java/com/fanruan/agentjdbc/statement/MyStatement.java index 709428f..0e93b4b 100644 --- a/agent/src/main/java/com/fanruan/jdbc/statement/MyStatement.java +++ b/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.*; diff --git a/agent/src/main/java/com/fanruan/handler/DispatcherHelper.java b/agent/src/main/java/com/fanruan/handler/DispatcherHelper.java index 3668874..9f77d51 100644 --- a/agent/src/main/java/com/fanruan/handler/DispatcherHelper.java +++ b/agent/src/main/java/com/fanruan/handler/DispatcherHelper.java @@ -3,6 +3,7 @@ package com.fanruan.handler; import java.util.HashMap; import java.util.Map; +import java.util.regex.Pattern; /** * Some utils for MyDispatcher @@ -24,17 +25,17 @@ public class DispatcherHelper { }; public final static String[] CACHE_LIST = new String[]{ - "com.fanruan.jdbc.driver.MyDriver", - "com.fanruan.jdbc.connection.MyConnection", - "com.fanruan.jdbc.statement.MyStatement", - "com.fanruan.jdbc.statement.MyPreparedStatement", - "com.fanruan.jdbc.resultset.MyResultSet", + "MyDriver", + "MyConnection", + "MyStatement", + "MyPreparedStatement", + "MyResultSet", }; public static boolean isInCacheList(String className){ for(String s : CACHE_LIST){ - if(s.equals(className)){ + if(Pattern.matches(".*" + s, className)){ return true; } } @@ -57,4 +58,8 @@ public class DispatcherHelper { } return arr[n-1]; } + + public static String transformName(String name){ + return name.replace("servicejdbc", "agentjdbc"); + } } diff --git a/agent/src/main/java/com/fanruan/handler/MyDispatcherImpl.java b/agent/src/main/java/com/fanruan/handler/MyDispatcherImpl.java index d822ca3..389ce48 100644 --- a/agent/src/main/java/com/fanruan/handler/MyDispatcherImpl.java +++ b/agent/src/main/java/com/fanruan/handler/MyDispatcherImpl.java @@ -40,7 +40,8 @@ public class MyDispatcherImpl implements Dispatcher{ @Override 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(); Object[] args = rpcRequest.getArgs(); Class[] argTypes = rpcRequest.getArgTypes(); @@ -50,7 +51,6 @@ public class MyDispatcherImpl implements Dispatcher{ String IDToCache = rpcRequest.getID(); String IDToInvoke = rpcRequest.getIDToInvoke(); - String fullName = clazz.getName(); String className = DispatcherHelper.getClassName(fullName); // If BeanCache contains instance, get it; if not, create it. diff --git a/agent/src/main/java/com/fanruan/pojo/message/RpcRequest.java b/agent/src/main/java/com/fanruan/pojo/message/RpcRequest.java index afa4879..abc93c9 100644 --- a/agent/src/main/java/com/fanruan/pojo/message/RpcRequest.java +++ b/agent/src/main/java/com/fanruan/pojo/message/RpcRequest.java @@ -13,7 +13,7 @@ public class RpcRequest { private String ID; private boolean binding; private String IDToInvoke; - private Class serviceClass; + private String serviceClassName; private String methodName; private Object[] args; private Class[] argTypes; diff --git a/agent/src/main/java/com/fanruan/utils/DBProperties.java b/agent/src/main/java/com/fanruan/utils/DBProperties.java index e85399b..8e45592 100644 --- a/agent/src/main/java/com/fanruan/utils/DBProperties.java +++ b/agent/src/main/java/com/fanruan/utils/DBProperties.java @@ -4,17 +4,10 @@ package com.fanruan.utils; * @author Yichen Dai */ public class DBProperties { - public static final String MYSQL = "mysql"; - public static final String POSTGRESQL = "postgresql"; - public static final String ORACLE = "oracle"; - public static final String SQLSERVER = "sqlserver"; - public static final String DB2 = "db2"; - public static final String HSQL = "hsql"; - - 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"; + public static final String[] MYSQL = new String[]{"mysql", "com.mysql.cj.jdbc.Driver"}; + public static final String[] POSTGRESQL = new String[]{"postgresql", "org.postgresql.Driver"}; + public static final String[] ORACLE = new String[]{"oracle", "oracle.jdbc.driver.OracleDriver"}; + public static final String[] SQLSERVER = new String[]{"sqlserver", "com.microsoft.jdbc.sqlserver.SQLServerDriver"}; + public static final String[] DB2 = new String[]{"db2", "com.ibm.db2.jdbc.app.DB2Driver"}; + public static final String[] HSQL = new String[]{"hsql", "org.hsqldb.jdbcDriver"}; } diff --git a/agent/src/test/java/HSQLTest.java b/agent/src/test/java/HSQLTest.java index 9ee4c64..370efd6 100644 --- a/agent/src/test/java/HSQLTest.java +++ b/agent/src/test/java/HSQLTest.java @@ -14,7 +14,7 @@ public class HSQLTest { @BeforeEach void startHSQL(){ try { - Class.forName(DBProperties.HSQL_DRIVER_NAME); + Class.forName(DBProperties.HSQL[1]); Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); diff --git a/agent/src/test/java/Test.java b/agent/src/test/java/Test.java index b3af7c5..1f09520 100644 --- a/agent/src/test/java/Test.java +++ b/agent/src/test/java/Test.java @@ -18,8 +18,8 @@ public class Test { } static void testStart() throws IOException, ClassNotFoundException { - Class.forName(DBProperties.HSQL_DRIVER_NAME); - String[] DBs = new String[]{ + Class.forName(DBProperties.HSQL[1]); + String[][] DBs = new String[][]{ DBProperties.HSQL, }; @@ -28,11 +28,9 @@ public class Test { Socket mainSocket = AgentStarter.myDispatcherImpl.CACHE.getSocket("/"); mainSocket.connect(); - Socket socket = AgentStarter.myDispatcherImpl.CACHE.getSocket(DBProperties.HSQL); + Socket socket = AgentStarter.myDispatcherImpl.CACHE.getSocket(DBProperties.HSQL[0]); socket.connect(); - socket.open(); - } } diff --git a/pom.xml b/pom.xml index bd1a8f2..e7a0b99 100644 --- a/pom.xml +++ b/pom.xml @@ -10,6 +10,7 @@ agent service + test pom diff --git a/service/src/main/java/com/fanruan/ServerStater.java b/service/src/main/java/com/fanruan/ServerStater.java index b051f8e..f39e4aa 100644 --- a/service/src/main/java/com/fanruan/ServerStater.java +++ b/service/src/main/java/com/fanruan/ServerStater.java @@ -43,11 +43,12 @@ public class ServerStater{ - public ServerStater(String[] dbs){ + public ServerStater(String[][] dbs){ try{ + Class.forName("com.fanruan.servicejdbc.driver.MyDriver"); loadConfig(); - for(String dbName : dbs){ - SocketIONamespace namespace = server.addNamespace("/" + dbName); + for(String[] dbInfo : dbs){ + SocketIONamespace namespace = server.addNamespace("/" + dbInfo[0]); addEvent(namespace); } server.start(); diff --git a/service/src/main/java/com/fanruan/pojo/message/RpcRequest.java b/service/src/main/java/com/fanruan/pojo/message/RpcRequest.java index 1bfa565..70fb8d3 100644 --- a/service/src/main/java/com/fanruan/pojo/message/RpcRequest.java +++ b/service/src/main/java/com/fanruan/pojo/message/RpcRequest.java @@ -22,7 +22,7 @@ public class RpcRequest { private boolean binding; private String ID; private String IDToInvoke; - private Class serviceClass; + private String serviceClassName; private String methodName; private Object[] args; private Class[] argTypes; diff --git a/service/src/main/java/com/fanruan/proxy/interceptor/Interceptor.java b/service/src/main/java/com/fanruan/proxy/interceptor/Interceptor.java index 90eda3e..6cb2b90 100644 --- a/service/src/main/java/com/fanruan/proxy/interceptor/Interceptor.java +++ b/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.LockAndCondition; import com.fanruan.pojo.message.RpcRequest; +import com.fanruan.servicejdbc.resultset.MyResultSet; import com.fanruan.utils.Commons; import net.sf.cglib.proxy.MethodInterceptor; import net.sf.cglib.proxy.MethodProxy; @@ -61,13 +62,13 @@ public class Interceptor implements MethodInterceptor { rpcRequest.setReply(false) .setBinding(false) .setID(Commons.getID()) - .setServiceClass(clazz) + .setServiceClassName(clazz.getName()) .setMethodName(method.getName()) .setArgs(objects) .setArgTypes(getArgTypes(objects)); // 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()); if(flag) { rpcRequest.setReply(true); diff --git a/service/src/main/java/com/fanruan/jdbc/MyDataBaseMetaData.java b/service/src/main/java/com/fanruan/servicejdbc/MyDataBaseMetaData.java similarity index 99% rename from service/src/main/java/com/fanruan/jdbc/MyDataBaseMetaData.java rename to service/src/main/java/com/fanruan/servicejdbc/MyDataBaseMetaData.java index 169945e..5f24810 100644 --- a/service/src/main/java/com/fanruan/jdbc/MyDataBaseMetaData.java +++ b/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.ResultSet; diff --git a/service/src/main/java/com/fanruan/jdbc/connection/MyConnection.java b/service/src/main/java/com/fanruan/servicejdbc/connection/MyConnection.java similarity index 97% rename from service/src/main/java/com/fanruan/jdbc/connection/MyConnection.java rename to service/src/main/java/com/fanruan/servicejdbc/connection/MyConnection.java index b467558..90ac92b 100644 --- a/service/src/main/java/com/fanruan/jdbc/connection/MyConnection.java +++ b/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.fanruan.cache.ClientCache; -import com.fanruan.jdbc.MyDataBaseMetaData; -import com.fanruan.jdbc.statement.MyPreparedStatement; -import com.fanruan.jdbc.statement.MyStatement; +import com.fanruan.servicejdbc.MyDataBaseMetaData; +import com.fanruan.servicejdbc.statement.MyPreparedStatement; +import com.fanruan.servicejdbc.statement.MyStatement; import com.fanruan.proxy.ProxyFactory; import java.sql.*; diff --git a/service/src/main/java/com/fanruan/jdbc/driver/MyDriver.java b/service/src/main/java/com/fanruan/servicejdbc/driver/MyDriver.java similarity index 95% rename from service/src/main/java/com/fanruan/jdbc/driver/MyDriver.java rename to service/src/main/java/com/fanruan/servicejdbc/driver/MyDriver.java index af4d9c3..509eb2b 100644 --- a/service/src/main/java/com/fanruan/jdbc/driver/MyDriver.java +++ b/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 java.sql.*; diff --git a/service/src/main/java/com/fanruan/jdbc/resultset/MyResultSet.java b/service/src/main/java/com/fanruan/servicejdbc/resultset/MyResultSet.java similarity index 99% rename from service/src/main/java/com/fanruan/jdbc/resultset/MyResultSet.java rename to service/src/main/java/com/fanruan/servicejdbc/resultset/MyResultSet.java index 50efb3c..3ab5d8d 100644 --- a/service/src/main/java/com/fanruan/jdbc/resultset/MyResultSet.java +++ b/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; diff --git a/service/src/main/java/com/fanruan/jdbc/statement/MyPreparedStatement.java b/service/src/main/java/com/fanruan/servicejdbc/statement/MyPreparedStatement.java similarity index 99% rename from service/src/main/java/com/fanruan/jdbc/statement/MyPreparedStatement.java rename to service/src/main/java/com/fanruan/servicejdbc/statement/MyPreparedStatement.java index d732fd1..d841182 100644 --- a/service/src/main/java/com/fanruan/jdbc/statement/MyPreparedStatement.java +++ b/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 java.io.InputStream; diff --git a/service/src/main/java/com/fanruan/jdbc/statement/MyStatement.java b/service/src/main/java/com/fanruan/servicejdbc/statement/MyStatement.java similarity index 98% rename from service/src/main/java/com/fanruan/jdbc/statement/MyStatement.java rename to service/src/main/java/com/fanruan/servicejdbc/statement/MyStatement.java index b9e7495..d26b5ab 100644 --- a/service/src/main/java/com/fanruan/jdbc/statement/MyStatement.java +++ b/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 java.sql.*; diff --git a/service/src/main/java/com/fanruan/utils/DBProperties.java b/service/src/main/java/com/fanruan/utils/DBProperties.java index 1ce051e..19c4866 100644 --- a/service/src/main/java/com/fanruan/utils/DBProperties.java +++ b/service/src/main/java/com/fanruan/utils/DBProperties.java @@ -5,17 +5,10 @@ package com.fanruan.utils; * @author Yichen Dai */ public class DBProperties { - public static final String MYSQL = "mysql"; - public static final String POSTGRESQL = "postgresql"; - public static final String ORACLE = "oracle"; - public static final String SQLSERVER = "sqlserver"; - public static final String DB2 = "db2"; - public static final String HSQL = "hsql"; - - 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"; + public static final String[] MYSQL = new String[]{"mysql", "com.mysql.cj.jdbc.Driver"}; + public static final String[] POSTGRESQL = new String[]{"postgresql", "org.postgresql.Driver"}; + public static final String[] ORACLE = new String[]{"oracle", "oracle.jdbc.driver.OracleDriver"}; + public static final String[] SQLSERVER = new String[]{"sqlserver", "com.microsoft.jdbc.sqlserver.SQLServerDriver"}; + public static final String[] DB2 = new String[]{"db2", "com.ibm.db2.jdbc.app.DB2Driver"}; + public static final String[] HSQL = new String[]{"hsql", "org.hsqldb.jdbcDriver"}; } diff --git a/service/src/test/java/AutoStarter.java b/service/src/test/java/AutoStarter.java index 8108300..fc4b116 100644 --- a/service/src/test/java/AutoStarter.java +++ b/service/src/test/java/AutoStarter.java @@ -8,13 +8,13 @@ import com.fanruan.utils.DBProperties; public class AutoStarter { static { - String[] DBs = new String[]{ + String[][] DBs = new String[][]{ DBProperties.HSQL, }; new ServerStater(DBs); try { - Class.forName("com.fanruan.jdbc.driver.MyDriver"); + Class.forName("com.fanruan.servicejdbc.driver.MyDriver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } diff --git a/service/src/test/java/ServiceTest.java b/service/src/test/java/ServiceTest.java index 6293896..d395f64 100644 --- a/service/src/test/java/ServiceTest.java +++ b/service/src/test/java/ServiceTest.java @@ -21,14 +21,14 @@ public class ServiceTest extends AutoStarter{ void listen() throws ExecutionException, InterruptedException { FutureTask ft = new FutureTask<>( () -> { - while(ClientCache.getClient("1001", DBProperties.HSQL) == null){ + while(ClientCache.getClient("1001", DBProperties.HSQL[0]) == null){ try { Thread.sleep(100); }catch (Exception e){ e.printStackTrace(); } } - return ClientCache.getClient("1001", DBProperties.HSQL); + return ClientCache.getClient("1001", DBProperties.HSQL[0]); } ); ft.run(); @@ -41,7 +41,7 @@ public class ServiceTest extends AutoStarter{ info.setProperty("user", "sa"); info.setProperty("password", ""); info.setProperty("agentID", "1001"); - info.setProperty("agentDBName", DBProperties.HSQL); + info.setProperty("agentDBName", DBProperties.HSQL[0]); Connection conn = null; Statement st = null; diff --git a/service/src/test/java/Test.java b/service/src/test/java/Test.java index d475cab..d71ad09 100644 --- a/service/src/test/java/Test.java +++ b/service/src/test/java/Test.java @@ -13,7 +13,7 @@ public class Test { Test test = new Test(); - String[] DBs = new String[]{ + String[][] DBs = new String[][]{ DBProperties.MYSQL, DBProperties.POSTGRESQL, DBProperties.SQLSERVER, @@ -51,7 +51,7 @@ public class Test { ResultSet rs2 = null; ResultSet rs3 = null; 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); st = conn.createStatement(); rs1 = st.executeQuery("select * from `student`"); @@ -158,7 +158,7 @@ public class Test { ResultSet rs2 = null; ResultSet rs3 = null; 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); st = conn.createStatement(); rs1 = st.executeQuery("select * from student"); diff --git a/test/pom.xml b/test/pom.xml new file mode 100644 index 0000000..86cf656 --- /dev/null +++ b/test/pom.xml @@ -0,0 +1,68 @@ + + + + intranet + org.example + 1.0-SNAPSHOT + + 4.0.0 + + test + + + 8 + 8 + + + + + org.example + service + 1.0-SNAPSHOT + + + + org.example + agent + 1.0-SNAPSHOT + + + + + org.junit.jupiter + junit-jupiter-engine + 5.4.0 + test + + + + org.apache.logging.log4j + log4j-api + 2.17.2 + + + + org.apache.logging.log4j + log4j-core + 2.17.2 + + + + + org.hsqldb + hsqldb + debug + 2.5.2 + test + + + + org.apache.logging.log4j + log4j-slf4j-impl + 2.17.2 + test + + + \ No newline at end of file diff --git a/test/src/test/java/TestUtil.java b/test/src/test/java/TestUtil.java new file mode 100644 index 0000000..294133c --- /dev/null +++ b/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); + } +}