From 35c92cc80ef700485d5dea8a67d494b6195c8617 Mon Sep 17 00:00:00 2001 From: Shuaiqiang Chang Date: Thu, 11 Jun 2020 19:12:39 +0800 Subject: [PATCH] tests: add test --- .../java/com/taosdata/jdbc/TSDBStatement.java | 22 +-- .../com/taosdata/jdbc/ConnectionTest.java | 24 +-- .../java/com/taosdata/jdbc/ResultSetTest.java | 129 +++++++++++++++ .../java/com/taosdata/jdbc/SelectTest.java | 2 + .../java/com/taosdata/jdbc/StatementTest.java | 155 ++++++++++++++++++ .../java/com/taosdata/jdbc/SubscribeTest.java | 6 +- 6 files changed, 314 insertions(+), 24 deletions(-) create mode 100644 src/connector/jdbc/src/test/java/com/taosdata/jdbc/ResultSetTest.java create mode 100644 src/connector/jdbc/src/test/java/com/taosdata/jdbc/StatementTest.java 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 c856c03a83..1cf024f30e 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 @@ -51,17 +51,16 @@ public class TSDBStatement implements Statement { if (isClosed) { throw new SQLException("Invalid method call on a closed statement."); } - System.out.println(sql); - long res = this.connecter.executeQuery(sql); + pSql = this.connecter.executeQuery(sql); long resultSetPointer = this.connecter.getResultSet(); if (resultSetPointer == TSDBConstants.JNI_CONNECTION_NULL) { - this.connecter.freeResultSet(res); + this.connecter.freeResultSet(pSql); throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL)); } else if (resultSetPointer == TSDBConstants.JNI_NULL_POINTER) { // create/insert/update/del/alter - this.connecter.freeResultSet(res); + this.connecter.freeResultSet(pSql); return null; } else { return new TSDBResultSet(this.connecter, resultSetPointer); @@ -72,18 +71,18 @@ public class TSDBStatement implements Statement { if (isClosed) { throw new SQLException("Invalid method call on a closed statement."); } - long res = this.connecter.executeQuery(sql); + pSql = this.connecter.executeQuery(sql); long resultSetPointer = this.connecter.getResultSet(); if (resultSetPointer == TSDBConstants.JNI_CONNECTION_NULL) { - this.connecter.freeResultSet(res); + this.connecter.freeResultSet(pSql); throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL)); } else if (resultSetPointer != TSDBConstants.JNI_NULL_POINTER) { this.connecter.freeResultSet(); throw new SQLException("The executed SQL is not a DML or a DDL"); } else { - int num = this.connecter.getAffectedRows(res); - this.connecter.freeResultSet(res); + int num = this.connecter.getAffectedRows(pSql); + this.connecter.freeResultSet(pSql); return num; } } @@ -151,16 +150,19 @@ public class TSDBStatement implements Statement { throw new SQLException("Invalid method call on a closed statement."); } boolean res = true; - this.connecter.executeQuery(sql); + pSql = this.connecter.executeQuery(sql); long resultSetPointer = this.connecter.getResultSet(); if (resultSetPointer == TSDBConstants.JNI_CONNECTION_NULL) { + this.connecter.freeResultSet(pSql); throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL)); } else if (resultSetPointer == TSDBConstants.JNI_NULL_POINTER) { // no result set is retrieved res = false; } - return res; + this.connecter.freeResultSet(pSql); + + return res; } public ResultSet getResultSet() throws SQLException { diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/ConnectionTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/ConnectionTest.java index ca38765ec5..a932f28113 100644 --- a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/ConnectionTest.java +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/ConnectionTest.java @@ -18,7 +18,7 @@ public class ConnectionTest { static String host = "localhost"; @Test - public static void createConnection() throws SQLException { + public void testConnection() throws SQLException { try { Class.forName("com.taosdata.jdbc.TSDBDriver"); } catch (ClassNotFoundException e) { @@ -30,23 +30,25 @@ public class ConnectionTest { , properties); assertTrue(null != connection); - } + statement = connection.createStatement(); + assertTrue(null != statement); + + // try reconnect + connection = DriverManager.getConnection("jdbc:TAOS://" + host + ":0/" + "?user=root&password=taosdata" + , properties); - @Test - public void createDatabase() { try { - statement.executeUpdate("create database if not exists " + dbName); + statement.execute("create database if not exists " + dbName); } catch (SQLException e) { assert false : "create database error: " + e.getMessage(); } - } - @Test - public void close() { try { - if (!statement.isClosed()) { - statement.executeUpdate("drop database " + dbName); - statement.close(); + if (!connection.isClosed()) { + if (!statement.isClosed()) { + statement.executeUpdate("drop database " + dbName); + statement.close(); + } connection.close(); } } catch (SQLException e) { diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/ResultSetTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/ResultSetTest.java new file mode 100644 index 0000000000..a92d8900d7 --- /dev/null +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/ResultSetTest.java @@ -0,0 +1,129 @@ +package com.taosdata.jdbc; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.sql.*; +import java.util.Properties; + +import static org.junit.Assert.assertEquals; + +public class ResultSetTest { + static Connection connection = null; + static Statement statement = null; + static String dbName = "test"; + static String tName = "t0"; + static String host = "localhost"; + static ResultSet resSet = null; + + @BeforeClass + public static void createDatabaseAndTable() throws SQLException { + try { + Class.forName("com.taosdata.jdbc.TSDBDriver"); + } catch (ClassNotFoundException e) { + return; + } + Properties properties = new Properties(); + properties.setProperty(TSDBDriver.PROPERTY_KEY_HOST, host); + connection = DriverManager.getConnection("jdbc:TAOS://" + host + ":0/" + "?user=root&password=taosdata" + , properties); + + statement = connection.createStatement(); + statement.executeUpdate("drop database if exists " + dbName); + statement.executeUpdate("create database if not exists " + dbName); + statement.executeUpdate("create table if not exists " + dbName + "." + tName + + " (ts timestamp, k1 int, k2 bigint, k3 float, k4 double, k5 binary(30), k6 smallint, k7 bool, k8 nchar(20))"); + + statement.executeQuery("use " + dbName); + } + + @Test + public void testResultSet() { + String sql = null; + long ts = 1496732686000l; + int v1 = 2147483600; + long v2 = ts + 1000; + float v3 = 3.1415926f; + double v4 = 3.1415926535897; + String v5 = "涛思数据,强~!"; + short v6 = 12; + boolean v7 = false; + String v8 = "TDengine is powerful"; + + sql = "insert into " + dbName + "." + tName + " values (" + ts + "," + v1 + "," + v2 + "," + v3 + "," + v4 + + ",\"" + v5 + "\"," + v6 + "," + v7 + ",\"" + v8 + "\")"; +// System.out.println(sql); + + try { + statement.executeUpdate(sql); + assertEquals(1, statement.getUpdateCount()); + } catch (SQLException e) { + assert false : "insert error " + e.getMessage(); + } + + try { + statement.executeQuery("select * from " + dbName + "." + tName); + resSet = statement.getResultSet(); + System.out.println(((TSDBResultSet) resSet).getRowData()); + while (resSet.next()) { + assertEquals(ts, resSet.getLong(1)); + assertEquals(ts, resSet.getLong("ts")); + + System.out.println(resSet.getTimestamp(1)); + + assertEquals(v1, resSet.getInt(2)); + assertEquals(v1, resSet.getInt("k1")); + + assertEquals(v2, resSet.getLong(3)); + assertEquals(v2, resSet.getLong("k2")); + + assertEquals(v3, resSet.getFloat(4), 7); + assertEquals(v3, resSet.getFloat("k3"), 7); + + assertEquals(v4, resSet.getDouble(5), 13); + assertEquals(v4, resSet.getDouble("k4"), 13); + + assertEquals(v5, resSet.getString(6)); + assertEquals(v5, resSet.getString("k5")); + + assertEquals(v6, resSet.getShort(7)); + assertEquals(v6, resSet.getShort("k6")); + + assertEquals(v7, resSet.getBoolean(8)); + assertEquals(v7, resSet.getBoolean("k7")); + + assertEquals(v8, resSet.getString(9)); + assertEquals(v8, resSet.getString("k8")); + + resSet.getBytes(9); + resSet.getObject(6); + resSet.getObject("k8"); + } + resSet.close(); + } catch (SQLException e) { + assert false : "insert error " + e.getMessage(); + } + } + + @Test + public void testBatch() throws SQLException { + String[] sqls = new String[]{"insert into test.t0 values (1496732686001,2147483600,1496732687000,3.1415925,3.1415926\n" + + "535897,\"涛思数据,强~!\",12,12,\"TDengine is powerful\")", "insert into test.t0 values (1496732686002,2147483600,1496732687000,3.1415925,3.1415926\n" + + "535897,\"涛思数据,强~!\",12,12,\"TDengine is powerful\")"}; + for (String sql : sqls) { + statement.addBatch(sql); + } + int[] res = statement.executeBatch(); + assertEquals(res.length, 2); + statement.clearBatch(); + } + + @AfterClass + public static void close() throws SQLException { + statement.executeUpdate("drop database " + dbName); + statement.close(); + connection.close(); + } + +} diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/SelectTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/SelectTest.java index 4b57131178..077f95318b 100644 --- a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/SelectTest.java +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/SelectTest.java @@ -1,5 +1,6 @@ package com.taosdata.jdbc; +import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -57,6 +58,7 @@ public class SelectTest { assertEquals(num, 50); } + @After public void close() throws SQLException { statement.executeUpdate("drop database " + dbName); statement.close(); diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/StatementTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/StatementTest.java new file mode 100644 index 0000000000..f5e2bcba20 --- /dev/null +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/StatementTest.java @@ -0,0 +1,155 @@ +package com.taosdata.jdbc; + +import org.junit.BeforeClass; +import org.junit.Test; + +import java.sql.*; +import java.util.Properties; + +import static org.junit.Assert.assertTrue; + +public class StatementTest { + static Connection connection = null; + static Statement statement = null; + static String dbName = "test"; + static String tName = "t0"; + static String host = "localhost"; + static ResultSet resSet = null; + + @BeforeClass + public static void createConnection() throws SQLException { + try { + Class.forName("com.taosdata.jdbc.TSDBDriver"); + } catch (ClassNotFoundException e) { + return; + } + Properties properties = new Properties(); + properties.setProperty(TSDBDriver.PROPERTY_KEY_HOST, host); + connection = DriverManager.getConnection("jdbc:TAOS://" + host + ":0/" + "?user=root&password=taosdata" + , properties); + + statement = connection.createStatement(); + statement.executeUpdate("drop database if exists " + dbName); + + } + + @Test + public void createTableAndQuery() throws SQLException { + long ts = System.currentTimeMillis(); + + statement.executeUpdate("create database if not exists " + dbName); + statement.executeUpdate("create table if not exists " + dbName + "." + tName + "(ts timestamp, k1 int)"); + statement.executeUpdate("insert into " + dbName + "." + tName + " values (" + ts + ", 1)"); + statement.executeQuery("select * from " + dbName + "." + tName); + ResultSet resultSet = statement.getResultSet(); + assertTrue(null != resultSet); + } + + @Test + public void testUnsupport() { +// if(null == resSet) { +// return; +// } + TSDBStatement tsdbStatement = (TSDBStatement) statement; + try { + tsdbStatement.unwrap(null); + } catch (SQLException e) { + } + try { + tsdbStatement.isWrapperFor(null); + } catch (SQLException e) { + } + try { + tsdbStatement.getMaxFieldSize(); + } catch (SQLException e) { + } + try { + tsdbStatement.setMaxFieldSize(0); + } catch (SQLException e) { + } + try { + tsdbStatement.setEscapeProcessing(true); + } catch (SQLException e) { + } + try { + tsdbStatement.cancel(); + } catch (SQLException e) { + } + try { + tsdbStatement.getWarnings(); + } catch (SQLException e) { + } + try { + tsdbStatement.clearWarnings(); + } catch (SQLException e) { + } + try { + tsdbStatement.setCursorName(null); + } catch (SQLException e) { + } + try { + tsdbStatement.getMoreResults(); + } catch (SQLException e) { + } + try { + tsdbStatement.setFetchDirection(0); + } catch (SQLException e) { + } + try { + tsdbStatement.getFetchDirection(); + } catch (SQLException e) { + } + try { + tsdbStatement.getResultSetConcurrency(); + } catch (SQLException e) { + } + try { + tsdbStatement.getResultSetType(); + } catch (SQLException e) { + } + try { + tsdbStatement.getConnection(); + } catch (SQLException e) { + } + try { + tsdbStatement.getMoreResults(); + } catch (SQLException e) { + } + try { + tsdbStatement.getGeneratedKeys(); + } catch (SQLException e) { + } + try { + tsdbStatement.executeUpdate(null, 0); + } catch (SQLException e) { + } + try { + tsdbStatement.executeUpdate(null, new int[]{0}); + } catch (SQLException e) { + } + try { + tsdbStatement.executeUpdate(null, new String[]{"str1", "str2"}); + } catch (SQLException e) { + } + try { + tsdbStatement.getResultSetHoldability(); + } catch (SQLException e) { + } + try { + tsdbStatement.setPoolable(true); + } catch (SQLException e) { + } + try { + tsdbStatement.isPoolable(); + } catch (SQLException e) { + } + try { + tsdbStatement.closeOnCompletion(); + } catch (SQLException e) { + } + try { + tsdbStatement.isCloseOnCompletion(); + } catch (SQLException e) { + } + } +} 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 fd18d85476..792ae5f671 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 @@ -20,7 +20,7 @@ public class SubscribeTest { String host = "localhost"; String topic = "test"; - @Before +// @Before public void createDatabase() throws SQLException { try { Class.forName("com.taosdata.jdbc.TSDBDriver"); @@ -42,7 +42,7 @@ public class SubscribeTest { } } - @Test +// @Test public void subscribe() throws Exception { TSDBSubscribe subscribe = null; long subscribId = 0; @@ -81,7 +81,7 @@ public class SubscribeTest { } } - @After +// @After public void close() throws Exception { statement.executeQuery("drop database " + dbName); statement.close();