From 16069460f1fbf09802072d6d958c29605307ed66 Mon Sep 17 00:00:00 2001 From: zyyang Date: Tue, 26 Jan 2021 14:03:06 +0800 Subject: [PATCH] [TD-2672]: optimized the JDBC test cases --- .../taosdata/jdbc/TSDBPreparedStatement.java | 4 +- .../java/com/taosdata/jdbc/TSDBStatement.java | 10 +- .../taosdata/jdbc/PreparedStatementTest.java | 50 ++--- .../java/com/taosdata/jdbc/SubscribeTest.java | 6 +- .../jdbc/TSDBPreparedStatementTest.java | 186 ++++++++++++++++++ 5 files changed, 217 insertions(+), 39 deletions(-) create mode 100644 src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBPreparedStatementTest.java diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBPreparedStatement.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBPreparedStatement.java index 230943fd53..c6b41ce004 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBPreparedStatement.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBPreparedStatement.java @@ -100,7 +100,6 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat * order to process those supported SQLs. */ private void preprocessSql() { - /***** For processing some of Spark SQLs*****/ // should replace it first this.rawSql = this.rawSql.replaceAll("or (.*) is null", ""); @@ -149,7 +148,6 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat rawSql = rawSql.replace(matcher.group(1), tableFullName); } /***** for inner queries *****/ - } /** @@ -196,7 +194,7 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat @Override public void setNull(int parameterIndex, int sqlType) throws SQLException { - setObject(parameterIndex, new String("NULL")); + setObject(parameterIndex, "NULL"); } @Override diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBStatement.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBStatement.java index 381f1d3622..4c900b5fdc 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBStatement.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBStatement.java @@ -52,12 +52,18 @@ public class TSDBStatement implements Statement { this.isClosed = false; } + @Override public T unwrap(Class iface) throws SQLException { - throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG); + try { + return iface.cast(this); + } catch (ClassCastException cce) { + throw new SQLException("Unable to unwrap to " + iface.toString()); + } } + @Override public boolean isWrapperFor(Class iface) throws SQLException { - throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG); + return iface.isInstance(this); } public ResultSet executeQuery(String sql) throws SQLException { diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/PreparedStatementTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/PreparedStatementTest.java index f52c50ff1a..a49ad05d05 100644 --- a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/PreparedStatementTest.java +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/PreparedStatementTest.java @@ -1,9 +1,7 @@ package com.taosdata.jdbc; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.FixMethodOrder; -import org.junit.Test; +import org.junit.*; +import org.junit.runners.MethodSorters; import java.sql.*; import java.util.Properties; @@ -11,14 +9,13 @@ import java.util.Properties; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -@FixMethodOrder() -public class PreparedStatementTest extends BaseTest { - static Connection connection = null; - static PreparedStatement statement = null; +@FixMethodOrder(value = MethodSorters.NAME_ASCENDING) +public class PreparedStatementTest { + static Connection connection; + static TSDBPreparedStatement statement; static String dbName = "test"; static String tName = "t0"; static String host = "localhost"; - static ResultSet resSet = null; @BeforeClass public static void createConnection() throws SQLException { @@ -28,19 +25,16 @@ public class PreparedStatementTest extends BaseTest { return; } Properties properties = new Properties(); - properties.setProperty(TSDBDriver.PROPERTY_KEY_HOST, host); properties.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8"); properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8"); properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8"); connection = DriverManager.getConnection("jdbc:TAOS://" + host + ":0/", properties); - String sql = "drop database if exists " + dbName; statement = (TSDBPreparedStatement) connection.prepareStatement(sql); - } @Test - public void createTableAndQuery() throws SQLException { + public void case001_createTableAndQuery() throws SQLException { long ts = System.currentTimeMillis(); statement.executeUpdate("create database if not exists " + dbName); @@ -48,47 +42,40 @@ public class PreparedStatementTest extends BaseTest { statement.executeUpdate("insert into " + dbName + "." + tName + " values (" + ts + ", 1)"); PreparedStatement selectStatement = connection.prepareStatement("select * from " + dbName + "." + tName); - ResultSet resultSet = selectStatement.executeQuery(); assertTrue(null != resultSet); boolean isClosed = statement.isClosed(); assertEquals(false, isClosed); + selectStatement.close(); } @Test - public void testPreparedStatement() throws SQLException { + public void case002_testPreparedStatement() throws SQLException { long ts = System.currentTimeMillis() + 20000; - PreparedStatement saveStatement = connection - .prepareStatement("insert into " + dbName + "." + tName + " values (" + ts + ", 1)"); + PreparedStatement saveStatement = connection.prepareStatement("insert into " + dbName + "." + tName + " values (" + ts + ", 1)"); int affectedRows = saveStatement.executeUpdate(); assertTrue(1 == affectedRows); + saveStatement.close(); } @Test - public void testSavedPreparedStatement() throws SQLException { + public void case003_testSavedPreparedStatement() throws SQLException { long ts = System.currentTimeMillis(); - - TSDBPreparedStatement saveStatement = (TSDBPreparedStatement) connection - .prepareStatement("insert into " + dbName + "." + tName + " values (?, ?)"); - + TSDBPreparedStatement saveStatement = (TSDBPreparedStatement) connection.prepareStatement("insert into " + dbName + "." + tName + " values (?, ?)"); saveStatement.setObject(1, ts + 10000); saveStatement.setObject(2, 3); int rows = saveStatement.executeUpdate(); assertEquals(1, rows); + saveStatement.close(); } @Test - public void testUnsupport() { - // if(null == resSet) { - // return; - // } - TSDBPreparedStatement tsdbStatement = (TSDBPreparedStatement) statement; - try { - tsdbStatement.unwrap(null); - } catch (SQLException e) { - } + public void case004_testUnsupport() throws SQLException { + TSDBPreparedStatement tsdbStatement = statement; + + Assert.assertNotNull(tsdbStatement.unwrap(TSDBPreparedStatement.class)); try { tsdbStatement.isWrapperFor(null); } catch (SQLException e) { @@ -180,6 +167,7 @@ public class PreparedStatementTest extends BaseTest { try { tsdbStatement.closeOnCompletion(); } catch (SQLException e) { + } try { tsdbStatement.isCloseOnCompletion(); diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/SubscribeTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/SubscribeTest.java index 0a71c77d1d..1b09f6d502 100644 --- a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/SubscribeTest.java +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/SubscribeTest.java @@ -10,9 +10,9 @@ import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; -public class SubscribeTest extends BaseTest { - Connection connection = null; - Statement statement = null; +public class SubscribeTest { + Connection connection; + Statement statement; String dbName = "test"; String tName = "t0"; String host = "localhost"; diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBPreparedStatementTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBPreparedStatementTest.java new file mode 100644 index 0000000000..0e4ed67bf9 --- /dev/null +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBPreparedStatementTest.java @@ -0,0 +1,186 @@ +package com.taosdata.jdbc; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +public class TSDBPreparedStatementTest { + private static final String host = "127.0.0.1"; + private static Connection conn; + + @Test + public void executeQuery() { + + } + + @Test + public void executeUpdate() { + + } + + @Test + public void setNull() { + } + + @Test + public void setBoolean() { + } + + @Test + public void setByte() { + } + + @Test + public void setShort() { + } + + @Test + public void setInt() { + } + + @Test + public void setLong() { + } + + @Test + public void setFloat() { + } + + @Test + public void setDouble() { + } + + @Test + public void setBigDecimal() { + } + + @Test + public void setString() { + } + + @Test + public void setBytes() { + } + + @Test + public void setDate() { + } + + @Test + public void setTime() { + } + + @Test + public void setTimestamp() { + } + + @Test + public void setAsciiStream() { + } + + @Test + public void setUnicodeStream() { + } + + @Test + public void setBinaryStream() { + } + + @Test + public void clearParameters() { + } + + @Test + public void setObject() { + + } + + @Test + public void execute() { + } + + @Test + public void addBatch() { + + } + + @Test + public void setCharacterStream() { + } + + @Test + public void setRef() { + } + + @Test + public void setBlob() { + } + + @Test + public void setClob() { + } + + @Test + public void setArray() { + } + + @Test + public void getMetaData() { + } + + @Test + public void setURL() { + } + + @Test + public void getParameterMetaData() { + } + + @Test + public void setRowId() { + } + + @Test + public void setNString() { + } + + @Test + public void setNCharacterStream() { + } + + @Test + public void setNClob() { + + } + + @Test + public void setSQLXML() { + + } + + + @BeforeClass + public static void beforeClass() { + try { + Class.forName("com.taosdata.jdbc.rs.RestfulDriver"); + conn = DriverManager.getConnection("jdbc:TAOS://" + host + ":6030/jdbc_test?user=root&password=taosdata"); + } catch (ClassNotFoundException | SQLException e) { + e.printStackTrace(); + } + } + + @AfterClass + public static void afterClass() { + try { + if (conn != null) + conn.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + +} \ No newline at end of file -- GitLab