Browse Source

integration testing : Statement ResultSet

pull/1/head
yichen 2 years ago
parent
commit
621f5bf886
  1. 2
      agent/src/main/java/com/fanruan/agent/jdbc/AgentArray.java
  2. 2
      agent/src/main/java/com/fanruan/agent/jdbc/AgentBlob.java
  3. 2
      agent/src/main/java/com/fanruan/agent/jdbc/AgentClob.java
  4. 11
      agent/src/main/java/com/fanruan/agent/jdbc/statement/AgentCallableStatement.java
  5. 5
      agent/src/main/java/com/fanruan/agent/jdbc/statement/AgentStatement.java
  6. 3
      service/src/main/java/com/fanruan/service/jdbc/connection/ServiceConnection.java
  7. 24
      service/src/main/java/com/fanruan/service/jdbc/statement/ServiceStatement.java
  8. 1
      test/src/test/java/TestSuite.java
  9. 6
      test/src/test/java/com/fanruan/CallableStatementTest.java
  10. 44
      test/src/test/java/com/fanruan/PrepareStatementTest.java
  11. 57
      test/src/test/java/com/fanruan/ResultSetTest.java

2
agent/src/main/java/com/fanruan/agent/jdbc/AgentArray.java

@ -12,7 +12,7 @@ import java.util.Map;
public class AgentArray implements Array {
Array array;
AgentArray(Array array){
public AgentArray(Array array){
this.array = array;
}

2
agent/src/main/java/com/fanruan/agent/jdbc/AgentBlob.java

@ -12,7 +12,7 @@ import java.sql.SQLException;
public class AgentBlob implements Blob {
private Blob blob;
AgentBlob(Blob blob){
public AgentBlob(Blob blob){
this.blob = blob;
}

2
agent/src/main/java/com/fanruan/agent/jdbc/AgentClob.java

@ -14,7 +14,7 @@ import java.sql.SQLException;
public class AgentClob implements Clob {
private Clob clob;
AgentClob(Clob clob){
public AgentClob(Clob clob){
this.clob = clob;
}

11
agent/src/main/java/com/fanruan/agent/jdbc/statement/AgentCallableStatement.java

@ -1,5 +1,8 @@
package com.fanruan.agent.jdbc.statement;
import com.fanruan.agent.jdbc.AgentArray;
import com.fanruan.agent.jdbc.AgentBlob;
import com.fanruan.agent.jdbc.AgentClob;
import com.fanruan.agent.jdbc.AgentParameterMetaData;
import java.io.InputStream;
@ -124,17 +127,17 @@ public class AgentCallableStatement implements java.sql.CallableStatement {
@Override
public Blob getBlob(int parameterIndex) throws SQLException {
return cst.getBlob(parameterIndex);
return new AgentBlob(cst.getBlob(parameterIndex));
}
@Override
public Clob getClob(int parameterIndex) throws SQLException {
return cst.getClob(parameterIndex);
return new AgentClob(cst.getClob(parameterIndex));
}
@Override
public Array getArray(int parameterIndex) throws SQLException {
return cst.getArray(parameterIndex);
return new AgentArray(cst.getArray(parameterIndex));
}
@Override
@ -384,7 +387,7 @@ public class AgentCallableStatement implements java.sql.CallableStatement {
@Override
public Blob getBlob(String parameterName) throws SQLException {
return cst.getBlob(parameterName);
return new AgentBlob(cst.getBlob(parameterName));
}
@Override

5
agent/src/main/java/com/fanruan/agent/jdbc/statement/AgentStatement.java

@ -17,7 +17,6 @@ public class AgentStatement implements Statement {
}
//使用与 Service 同名的类保证数据库对应的 JDBC
@Override
public ResultSet executeQuery(String sql) throws SQLException {
return new AgentResultSet(st.executeQuery(sql));
@ -95,7 +94,7 @@ public class AgentStatement implements Statement {
@Override
public ResultSet getResultSet() throws SQLException {
return st.getResultSet();
return new AgentResultSet(st.getResultSet());
}
@Override
@ -165,7 +164,7 @@ public class AgentStatement implements Statement {
@Override
public ResultSet getGeneratedKeys() throws SQLException {
return st.getGeneratedKeys();
return new AgentResultSet(st.getGeneratedKeys());
}
@Override

3
service/src/main/java/com/fanruan/service/jdbc/connection/ServiceConnection.java

@ -29,6 +29,7 @@ public class ServiceConnection extends BasedBind implements Connection {
public Statement createStatement(){
ServiceStatement st = (ServiceStatement) ProxyFactory.getProxy(ServiceStatement.class, info);
st.setInfo(info);
st.setConnection(this);
return st;
}
@ -131,6 +132,7 @@ public class ServiceConnection extends BasedBind implements Connection {
public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
ServiceStatement st = (ServiceStatement) ProxyFactory.getProxy(ServiceStatement.class, info);
st.setInfo(info);
st.setConnection(this);
return st;
}
@ -192,6 +194,7 @@ public class ServiceConnection extends BasedBind implements Connection {
public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
ServiceStatement st = (ServiceStatement) ProxyFactory.getProxy(ServiceStatement.class, info);
st.setInfo(info);
st.setConnection(this);
return st;
}

24
service/src/main/java/com/fanruan/service/jdbc/statement/ServiceStatement.java

@ -1,5 +1,7 @@
package com.fanruan.service.jdbc.statement;
import com.fanruan.annotation.LocalMethod;
import com.fanruan.annotation.NotImplemented;
import com.fanruan.annotation.RemoteClass;
import com.fanruan.service.jdbc.AbstractBind;
import com.fanruan.service.jdbc.BasedBind;
@ -15,6 +17,13 @@ import java.util.Properties;
@RemoteClass(remoteClassName = "com.fanruan.agent.jdbc.statement.AgentStatement")
public class ServiceStatement extends BasedBind implements Statement {
private Connection connection;
@LocalMethod
public void setConnection(Connection connection){
this.connection = connection;
}
@Override
public ResultSet executeQuery(String sql) throws SQLException {
ServiceResultSet rs = (ServiceResultSet) ProxyFactory.getProxy(ServiceResultSet.class, info);
@ -94,7 +103,9 @@ public class ServiceStatement extends BasedBind implements Statement {
@Override
public ResultSet getResultSet() throws SQLException {
return null;
ServiceResultSet rs = (ServiceResultSet) ProxyFactory.getProxy(ServiceResultSet.class, info);
rs.setInfo(info);
return rs;
}
@Override
@ -153,8 +164,9 @@ public class ServiceStatement extends BasedBind implements Statement {
}
@Override
@LocalMethod
public Connection getConnection() throws SQLException {
return null;
return this.connection;
}
@Override
@ -164,7 +176,9 @@ public class ServiceStatement extends BasedBind implements Statement {
@Override
public ResultSet getGeneratedKeys() throws SQLException {
return null;
ServiceResultSet rs = (ServiceResultSet) ProxyFactory.getProxy(ServiceResultSet.class, info);
rs.setInfo(info);
return rs;
}
@Override
@ -228,8 +242,10 @@ public class ServiceStatement extends BasedBind implements Statement {
}
@Override
@LocalMethod
@NotImplemented
public <T> T unwrap(Class<T> iface) throws SQLException {
return null;
throw new SQLException("Not Implemented!");
}
@Override

1
test/src/test/java/TestSuite.java

@ -14,4 +14,3 @@ public class TestSuite {
}

6
test/src/test/java/com/fanruan/CallableStatementTest.java

@ -1162,8 +1162,12 @@ public class CallableStatementTest extends BaseJDBCTest{
beanCache);
}
);
}
@AfterAll
public void tearDown() throws SQLException{
Assertions.assertFalse(callableStatement.isClosed());
closeSQLObjects(statement, conn, callableStatement);
}
}

44
test/src/test/java/com/fanruan/PrepareStatementTest.java

@ -7,10 +7,7 @@ import com.fanruan.service.jdbc.ServiceArray;
import com.fanruan.service.jdbc.ServiceResultSetMetaData;
import com.fanruan.service.jdbc.statement.ServiceCallableStatement;
import com.fanruan.service.jdbc.statement.ServicePreparedStatement;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.*;
import java.math.BigDecimal;
import java.net.URL;
@ -471,21 +468,28 @@ public class PrepareStatementTest extends BaseJDBCTest{
pstm.setEscapeProcessing(false);
pstm.setCursorName("name");
pstm.setPoolable(false);
}
pstm.setCursorName("name");
pstm.setPoolable(false);
@Test
public void testQueryTimeout() throws SQLException{
preparedStatement.setQueryTimeout(2000);
Assertions.assertEquals(2000, preparedStatement.getQueryTimeout());
}
pstm.setQueryTimeout(2000);
@Test
public void testWarnings() throws SQLException{
preparedStatement.clearWarnings();
Assertions.assertNull(preparedStatement.getWarnings());
}
@Test
public void testFildSize() throws SQLException{
public void testFieldSize() throws SQLException{
preparedStatement.setMaxFieldSize(1000);
// HSQLDB always returns zero, meaning there is no limit.
Assertions.assertEquals(0, preparedStatement.getMaxFieldSize());
}
@ -499,7 +503,7 @@ public class PrepareStatementTest extends BaseJDBCTest{
}
@Test
public void testFectchSize() throws SQLException{
public void testFetchSize() throws SQLException{
preparedStatement.setFetchSize(0);
Assertions.assertEquals(0, preparedStatement.getFetchSize());
@ -512,16 +516,6 @@ public class PrepareStatementTest extends BaseJDBCTest{
Assertions.assertEquals(1000, preparedStatement.getMaxRows());
}
@Test
public void testGet() throws SQLException{
/**
* Retrieves the maximum number of rows that a ResultSet object produced by this Statement object can contain. If this limit is exceeded, the excess rows are silently dropped.
* This method should be used when the returned row limit may exceed Integer.MAX_VALUE.
*
* The default implementation will return 0
*/
Assertions.assertEquals(0, preparedStatement.getLargeMaxRows());
}
@Test
public void testExecuteBatch() throws SQLException{
@ -535,6 +529,8 @@ public class PrepareStatementTest extends BaseJDBCTest{
PreparedStatement psInsert = connection.prepareStatement(SQL_INSERT);
psInsert.clearBatch();
// Run list of insert commands
psInsert.setString(1, "mkyong");
psInsert.addBatch();
@ -643,4 +639,10 @@ public class PrepareStatementTest extends BaseJDBCTest{
Assertions.assertThrows(SQLException.class, () -> metaData.unwrap(null));
}
@AfterAll
void tearDown() throws SQLException {
Assertions.assertFalse(preparedStatement.isClosed());
closeSQLObjects(connection, preparedStatement);
}
}

57
test/src/test/java/com/fanruan/ResultSetTest.java

@ -0,0 +1,57 @@
package com.fanruan;
import com.fanruan.cache.BeanCacheImpl;
import com.fanruan.handler.DispatcherImpl;
import com.fanruan.pojo.message.RpcRequest;
import com.fanruan.service.jdbc.AbstractBind;
import com.fanruan.service.jdbc.statement.ServiceStatement;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import java.net.URL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* @author Yichen Dai
* @date 2022/9/7 10:58
*/
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class ResultSetTest extends BaseJDBCTest{
private Connection connection = null;
@BeforeAll
public void setUp() throws SQLException {
openSocket();
connection = getConnection();
Statement statement = connection.createStatement();
statement.execute("create table DemoTable\n" +
" (\n" +
" Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,\n" +
" TestTime TIME, \n" +
" TestDate DATE, \n" +
" TestStamp TIMESTAMP\n" +
" );");
statement.executeUpdate("insert into DEMOTABLE(TESTTIME,TESTDATE,TESTSTAMP) " +
"values('15:50:37', '2022-09-07', '2022-09-07 15:50:37');");
}
@Test
public void testDate() throws SQLException{
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("select TestTime, TestDate, TestStamp from DemoTable;");
while(resultSet.next()){
Assertions.assertEquals("15:50:37", resultSet.getTime(1).toString());
Assertions.assertEquals("2022-09-07", resultSet.getDate(2).toString());
Assertions.assertEquals("2022-09-07 15:50:37.0", resultSet.getTimestamp(3).toString());
}
}
}
Loading…
Cancel
Save