From 55729a6e536225bdbbd52c15316d835ed0fe8a60 Mon Sep 17 00:00:00 2001 From: zyyang Date: Tue, 23 Feb 2021 15:54:57 +0800 Subject: [PATCH] add unit test cases for TSDBStatement --- .../com/taosdata/jdbc/AbstractStatement.java | 71 +++++-- .../com/taosdata/jdbc/TSDBStatementTest.java | 183 +++++++++++++++++- 2 files changed, 240 insertions(+), 14 deletions(-) diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractStatement.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractStatement.java index aac97c530d..e4eb03001e 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractStatement.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractStatement.java @@ -4,7 +4,6 @@ import java.sql.*; public abstract class AbstractStatement extends WrapperImpl implements Statement { - private volatile boolean closeOnCompletion; private int fetchSize; @Override @@ -45,13 +44,14 @@ public abstract class AbstractStatement extends WrapperImpl implements Statement throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); if (max < 0) throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_VARIABLE); - // nothing to do + // do nothing } @Override public void setEscapeProcessing(boolean enable) throws SQLException { if (isClosed()) throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + // do nothing } @Override @@ -71,6 +71,9 @@ public abstract class AbstractStatement extends WrapperImpl implements Statement @Override public void cancel() throws SQLException { + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @@ -92,6 +95,7 @@ public abstract class AbstractStatement extends WrapperImpl implements Statement public void setCursorName(String name) throws SQLException { if (isClosed()) throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @@ -113,6 +117,15 @@ public abstract class AbstractStatement extends WrapperImpl implements Statement public void setFetchDirection(int direction) throws SQLException { if (isClosed()) throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + + switch (direction) { + case ResultSet.FETCH_FORWARD: + case ResultSet.FETCH_REVERSE: + case ResultSet.FETCH_UNKNOWN: + break; + default: + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_VARIABLE); + } //nothing to do } @@ -170,42 +183,73 @@ public abstract class AbstractStatement extends WrapperImpl implements Statement public boolean getMoreResults(int current) throws SQLException { if (isClosed()) throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); - return false; + switch (current) { + case Statement.CLOSE_CURRENT_RESULT: + return false; + case Statement.KEEP_CURRENT_RESULT: + case Statement.CLOSE_ALL_RESULTS: + break; + default: + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_VARIABLE); + } + + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public ResultSet getGeneratedKeys() throws SQLException { - throw new SQLFeatureNotSupportedException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException { - throw new SQLFeatureNotSupportedException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public int executeUpdate(String sql, int[] columnIndexes) throws SQLException { - throw new SQLFeatureNotSupportedException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public int executeUpdate(String sql, String[] columnNames) throws SQLException { - throw new SQLFeatureNotSupportedException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public boolean execute(String sql, int autoGeneratedKeys) throws SQLException { - throw new SQLFeatureNotSupportedException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public boolean execute(String sql, int[] columnIndexes) throws SQLException { - throw new SQLFeatureNotSupportedException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public boolean execute(String sql, String[] columnNames) throws SQLException { - throw new SQLFeatureNotSupportedException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override @@ -222,7 +266,7 @@ public abstract class AbstractStatement extends WrapperImpl implements Statement public void setPoolable(boolean poolable) throws SQLException { if (isClosed()) throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); - //nothing to do + // do nothing } @Override @@ -236,14 +280,15 @@ public abstract class AbstractStatement extends WrapperImpl implements Statement public void closeOnCompletion() throws SQLException { if (isClosed()) throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); - this.closeOnCompletion = true; + // do nothing } @Override public boolean isCloseOnCompletion() throws SQLException { if (isClosed()) throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); - return this.closeOnCompletion; + + return false; } } diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBStatementTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBStatementTest.java index 4794fc61f1..ad33d44d62 100644 --- a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBStatementTest.java +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBStatementTest.java @@ -51,6 +51,68 @@ public class TSDBStatementTest { @Test public void close() { + // test in AfterClass method + } + + + @Test + public void getMaxFieldSize() throws SQLException { + Assert.assertEquals(16 * 1024, stmt.getMaxFieldSize()); + } + + @Test(expected = SQLException.class) + public void setMaxFieldSize() throws SQLException { + + stmt.setMaxFieldSize(0); + stmt.setMaxFieldSize(-1); + } + + @Test + public void getMaxRows() throws SQLException { + Assert.assertEquals(0, stmt.getMaxRows()); + } + + @Test(expected = SQLException.class) + public void setMaxRows() throws SQLException { + stmt.setMaxRows(0); + stmt.setMaxRows(-1); + } + + @Test + public void setEscapeProcessing() throws SQLException { + stmt.setEscapeProcessing(true); + stmt.setEscapeProcessing(false); + } + + @Test + public void getQueryTimeout() throws SQLException { + Assert.assertEquals(0, stmt.getQueryTimeout()); + } + + @Test + public void setQueryTimeout() throws SQLException { + stmt.setQueryTimeout(0); + stmt.setQueryTimeout(-1); + } + + @Test(expected = SQLFeatureNotSupportedException.class) + public void cancel() throws SQLException { + stmt.cancel(); + } + + @Test + public void getWarnings() throws SQLException { + Assert.assertNull(stmt.getWarnings()); + } + + @Test + public void clearWarnings() throws SQLException { + stmt.clearWarnings(); + } + + @Test(expected = SQLFeatureNotSupportedException.class) + public void setCursorName() throws SQLException { + stmt.setCursorName(""); } @Test @@ -130,7 +192,48 @@ public class TSDBStatementTest { @Test public void getUpdateCount() { - execute(); + // already test in execute method + } + + @Test + public void getMoreResults() throws SQLException { + Assert.assertEquals(false, stmt.getMoreResults()); + } + + @Test(expected = SQLException.class) + public void setFetchDirection() throws SQLException { + stmt.setFetchDirection(ResultSet.FETCH_FORWARD); + stmt.setFetchDirection(ResultSet.FETCH_REVERSE); + stmt.setFetchDirection(ResultSet.FETCH_UNKNOWN); + stmt.setFetchDirection(-1); + } + + @Test + public void getFetchDirection() throws SQLException { + Assert.assertEquals(ResultSet.FETCH_FORWARD, stmt.getFetchDirection()); + } + + @Test(expected = SQLException.class) + public void setFetchSize() throws SQLException { + stmt.setFetchSize(0); + stmt.setFetchSize(-1); + } + + @Test + public void getFetchSize() throws SQLException { + stmt.setFetchSize(0); + Assert.assertEquals(0, stmt.getFetchSize()); + stmt.setFetchSize(0); + } + + @Test + public void getResultSetConcurrency() throws SQLException { + Assert.assertEquals(ResultSet.CONCUR_READ_ONLY, stmt.getResultSetConcurrency()); + } + + @Test + public void getResultSetType() throws SQLException { + Assert.assertEquals(ResultSet.TYPE_FORWARD_ONLY, stmt.getResultSetType()); } @Test @@ -194,6 +297,52 @@ public class TSDBStatementTest { } } + @Test(expected = SQLFeatureNotSupportedException.class) + public void testGetMoreResults() throws SQLException { + Assert.assertEquals(false, stmt.getMoreResults(Statement.CLOSE_CURRENT_RESULT)); + stmt.getMoreResults(Statement.KEEP_CURRENT_RESULT); + } + + @Test(expected = SQLFeatureNotSupportedException.class) + public void getGeneratedKeys() throws SQLException { + stmt.getGeneratedKeys(); + } + + @Test(expected = SQLFeatureNotSupportedException.class) + public void testExecuteUpdate() throws SQLException { + stmt.executeUpdate("", 1); + } + + @Test(expected = SQLFeatureNotSupportedException.class) + public void testExecuteUpdate1() throws SQLException { + stmt.executeUpdate("", new int[]{}); + } + + @Test(expected = SQLFeatureNotSupportedException.class) + public void testExecuteUpdate2() throws SQLException { + stmt.executeUpdate("", new String[]{}); + } + + @Test(expected = SQLFeatureNotSupportedException.class) + public void testExecute() throws SQLException { + stmt.execute("", 1); + } + + @Test(expected = SQLFeatureNotSupportedException.class) + public void testExecute1() throws SQLException { + stmt.execute("", new int[]{}); + } + + @Test + public void testExecute2() throws SQLException { + stmt.execute("", new String[]{}); + } + + @Test + public void getResultSetHoldability() throws SQLException { + Assert.assertEquals(ResultSet.HOLD_CURSORS_OVER_COMMIT, stmt.getResultSetHoldability()); + } + @Test public void isClosed() { try { @@ -203,6 +352,38 @@ public class TSDBStatementTest { } } + @Test + public void setPoolable() throws SQLException { + stmt.setPoolable(true); + stmt.setPoolable(false); + } + + @Test + public void isPoolable() throws SQLException { + Assert.assertEquals(false, stmt.isPoolable()); + } + + @Test + public void closeOnCompletion() throws SQLException { + stmt.closeOnCompletion(); + } + + @Test + public void isCloseOnCompletion() throws SQLException { + Assert.assertFalse(stmt.isCloseOnCompletion()); + } + + @Test + public void unwrap() throws SQLException { + TSDBStatement unwrap = stmt.unwrap(TSDBStatement.class); + Assert.assertNotNull(unwrap); + } + + @Test + public void isWrapperFor() throws SQLException { + Assert.assertTrue(stmt.isWrapperFor(TSDBStatement.class)); + } + @BeforeClass public static void beforeClass() { try { -- GitLab