diff --git a/src/connector/jdbc/pom.xml b/src/connector/jdbc/pom.xml index bc90cae0dbe88e4f573991fe6f6635613d981053..4756ac555fa3b32f1e261b3376d811f5e24bcc70 100755 --- a/src/connector/jdbc/pom.xml +++ b/src/connector/jdbc/pom.xml @@ -126,7 +126,7 @@ **/*Test.java - **/BatchInsertTest.java + **/AppMemoryLeakTest.java **/FailOverTest.java true diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBConstants.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBConstants.java index 8ffdd7b97afb8662e63ad7acb49cc203f303f45a..4fb172ceb59040e2f531fd91b83c776cd7d89067 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBConstants.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBConstants.java @@ -19,6 +19,7 @@ import java.util.Map; public abstract class TSDBConstants { + public static final String STATEMENT_CLOSED = "Statement already closed."; public static final String DEFAULT_PORT = "6200"; public static final String UNSUPPORT_METHOD_EXCEPTIONZ_MSG = "this operation is NOT supported currently!"; public static final String INVALID_VARIABLES = "invalid variables"; 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 230943fd53ae8fa36b9de40f851a5c263c052f0d..c6b41ce004d739a94c00e87f141b9180efa18e57 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 381f1d3622a393cd47361c6e367e310d29fc5d72..e7317b8e1d6eb2744c50a5bf1d106c7b687cc965 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 { @@ -130,10 +136,15 @@ public class TSDBStatement implements Statement { } public void setMaxFieldSize(int max) throws SQLException { + if (isClosed()) + throw new SQLException(TSDBConstants.STATEMENT_CLOSED); + throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG); } public int getMaxRows() throws SQLException { + if (isClosed()) + throw new SQLException(TSDBConstants.STATEMENT_CLOSED); // always set maxRows to zero, meaning unlimitted rows in a resultSet return 0; } diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulStatement.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulStatement.java index 56d4318fb4b8919414c533571c75e807f597bcc2..14ea3180924a8b23055ff610a8f7db5a70522039 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulStatement.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulStatement.java @@ -14,7 +14,6 @@ import java.util.stream.Collectors; public class RestfulStatement implements Statement { - private static final String STATEMENT_CLOSED = "Statement already closed."; private boolean closed; private String database; private final RestfulConnection conn; @@ -108,14 +107,14 @@ public class RestfulStatement implements Statement { @Override public int getMaxFieldSize() throws SQLException { if (isClosed()) - throw new SQLException(STATEMENT_CLOSED); + throw new SQLException(TSDBConstants.STATEMENT_CLOSED); return TSDBConstants.maxFieldSize; } @Override public void setMaxFieldSize(int max) throws SQLException { if (isClosed()) - throw new SQLException(STATEMENT_CLOSED); + throw new SQLException(TSDBConstants.STATEMENT_CLOSED); if (max < 0) throw new SQLException(TSDBConstants.INVALID_VARIABLES); // nothing to do @@ -124,14 +123,14 @@ public class RestfulStatement implements Statement { @Override public int getMaxRows() throws SQLException { if (isClosed()) - throw new SQLException(STATEMENT_CLOSED); + throw new SQLException(TSDBConstants.STATEMENT_CLOSED); return 0; } @Override public void setMaxRows(int max) throws SQLException { if (isClosed()) - throw new SQLException(STATEMENT_CLOSED); + throw new SQLException(TSDBConstants.STATEMENT_CLOSED); if (max < 0) throw new SQLException(TSDBConstants.INVALID_VARIABLES); // nothing to do @@ -140,20 +139,20 @@ public class RestfulStatement implements Statement { @Override public void setEscapeProcessing(boolean enable) throws SQLException { if (isClosed()) - throw new SQLException(RestfulStatement.STATEMENT_CLOSED); + throw new SQLException(TSDBConstants.STATEMENT_CLOSED); } @Override public int getQueryTimeout() throws SQLException { if (isClosed()) - throw new SQLException(STATEMENT_CLOSED); + throw new SQLException(TSDBConstants.STATEMENT_CLOSED); return 0; } @Override public void setQueryTimeout(int seconds) throws SQLException { if (isClosed()) - throw new SQLException(STATEMENT_CLOSED); + throw new SQLException(TSDBConstants.STATEMENT_CLOSED); if (seconds < 0) throw new SQLException(TSDBConstants.INVALID_VARIABLES); } @@ -166,7 +165,7 @@ public class RestfulStatement implements Statement { @Override public SQLWarning getWarnings() throws SQLException { if (isClosed()) - throw new SQLException(STATEMENT_CLOSED); + throw new SQLException(TSDBConstants.STATEMENT_CLOSED); return null; } @@ -174,13 +173,13 @@ public class RestfulStatement implements Statement { public void clearWarnings() throws SQLException { // nothing to do if (isClosed()) - throw new SQLException(STATEMENT_CLOSED); + throw new SQLException(TSDBConstants.STATEMENT_CLOSED); } @Override public void setCursorName(String name) throws SQLException { if (isClosed()) - throw new SQLException(RestfulStatement.STATEMENT_CLOSED); + throw new SQLException(TSDBConstants.STATEMENT_CLOSED); throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG); } @@ -260,7 +259,7 @@ public class RestfulStatement implements Statement { @Override public ResultSet getResultSet() throws SQLException { if (isClosed()) - throw new SQLException(STATEMENT_CLOSED); + throw new SQLException(TSDBConstants.STATEMENT_CLOSED); return resultSet; } @@ -292,7 +291,7 @@ public class RestfulStatement implements Statement { @Override public void setFetchSize(int rows) throws SQLException { if (isClosed()) - throw new SQLException(STATEMENT_CLOSED); + throw new SQLException(TSDBConstants.STATEMENT_CLOSED); if (rows < 0) throw new SQLException(TSDBConstants.INVALID_VARIABLES); //nothing to do @@ -301,28 +300,28 @@ public class RestfulStatement implements Statement { @Override public int getFetchSize() throws SQLException { if (isClosed()) - throw new SQLException(STATEMENT_CLOSED); + throw new SQLException(TSDBConstants.STATEMENT_CLOSED); return 0; } @Override public int getResultSetConcurrency() throws SQLException { if (isClosed()) - throw new SQLException(STATEMENT_CLOSED); + throw new SQLException(TSDBConstants.STATEMENT_CLOSED); return this.resultSet.getConcurrency(); } @Override public int getResultSetType() throws SQLException { if (isClosed()) - throw new SQLException(STATEMENT_CLOSED); + throw new SQLException(TSDBConstants.STATEMENT_CLOSED); return this.resultSet.getType(); } @Override public void addBatch(String sql) throws SQLException { if (isClosed()) - throw new SQLException(STATEMENT_CLOSED); + throw new SQLException(TSDBConstants.STATEMENT_CLOSED); //TODO: } @@ -340,14 +339,14 @@ public class RestfulStatement implements Statement { @Override public Connection getConnection() throws SQLException { if (isClosed()) - throw new SQLException(STATEMENT_CLOSED); + throw new SQLException(TSDBConstants.STATEMENT_CLOSED); return this.conn; } @Override public boolean getMoreResults(int current) throws SQLException { if (isClosed()) - throw new SQLException(STATEMENT_CLOSED); + throw new SQLException(TSDBConstants.STATEMENT_CLOSED); if (resultSet == null) return false; @@ -405,7 +404,7 @@ public class RestfulStatement implements Statement { @Override public int getResultSetHoldability() throws SQLException { if (isClosed()) - throw new SQLException(STATEMENT_CLOSED); + throw new SQLException(TSDBConstants.STATEMENT_CLOSED); return this.resultSet.getHoldability(); } @@ -417,28 +416,28 @@ public class RestfulStatement implements Statement { @Override public void setPoolable(boolean poolable) throws SQLException { if (isClosed()) - throw new SQLException(STATEMENT_CLOSED); + throw new SQLException(TSDBConstants.STATEMENT_CLOSED); //nothing to do } @Override public boolean isPoolable() throws SQLException { if (isClosed()) - throw new SQLException(STATEMENT_CLOSED); + throw new SQLException(TSDBConstants.STATEMENT_CLOSED); return false; } @Override public void closeOnCompletion() throws SQLException { if (isClosed()) - throw new SQLException(STATEMENT_CLOSED); + throw new SQLException(TSDBConstants.STATEMENT_CLOSED); this.closeOnCompletion = true; } @Override public boolean isCloseOnCompletion() throws SQLException { if (isClosed()) - throw new SQLException(STATEMENT_CLOSED); + throw new SQLException(TSDBConstants.STATEMENT_CLOSED); return this.closeOnCompletion; } diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/BaseTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/BaseTest.java deleted file mode 100644 index ce3735c12894807efadd1f5673fc34eee43ae01b..0000000000000000000000000000000000000000 --- a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/BaseTest.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.taosdata.jdbc; - -import com.taosdata.jdbc.utils.TDNodes; - -import org.junit.AfterClass; -import org.junit.BeforeClass; - -public abstract class BaseTest { - - private static boolean testCluster = false; - private static TDNodes nodes = new TDNodes(); - - @BeforeClass - public static void setupEnv() { - try { - if (nodes.getTDNode(1).getTaosdPid() != null) { - System.out.println("Kill taosd before running JDBC test"); - nodes.getTDNode(1).setRunning(1); - nodes.stop(1); - } - nodes.setTestCluster(testCluster); - nodes.deploy(1); - nodes.start(1); - } catch (Exception e) { - e.printStackTrace(); - } - } - - @AfterClass - public static void cleanUpEnv() { - nodes.stop(1); - } -} \ No newline at end of file diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/BatchInsertTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/BatchInsertTest.java deleted file mode 100644 index 4046e3edf6e1d9d62ceddaf99f73212dd1dd02c2..0000000000000000000000000000000000000000 --- a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/BatchInsertTest.java +++ /dev/null @@ -1,115 +0,0 @@ -package com.taosdata.jdbc; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import java.sql.*; -import java.util.Properties; -import java.util.Random; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; - -import static org.junit.Assert.assertEquals; - -public class BatchInsertTest { - - private Connection connection; - - private static String dbName = "test"; - private static String stbName = "meters"; - private static String host = "127.0.0.1"; - private static int numOfTables = 30; - private static int numOfRecordsPerTable = 1000; - private static long ts = 1496732686000l; - private static String tablePrefix = "t"; - - @Before - public void createDatabase() throws SQLException { - try { - Class.forName("com.taosdata.jdbc.TSDBDriver"); - } catch (ClassNotFoundException e) { - return; - } - - Properties properties = new Properties(); - 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); - - Statement stmt = connection.createStatement(); - stmt.execute("drop database if exists " + dbName); - stmt.execute("create database if not exists " + dbName); - stmt.execute("use " + dbName); - - String createTableSql = "create table " + stbName + "(ts timestamp, f1 int, f2 int, f3 int) tags(areaid int, loc binary(20))"; - stmt.execute(createTableSql); - - for (int i = 0; i < numOfTables; i++) { - String loc = i % 2 == 0 ? "beijing" : "shanghai"; - String createSubTalbesSql = "create table " + tablePrefix + i + " using " + stbName + " tags(" + i + ", '" + loc + "')"; - stmt.execute(createSubTalbesSql); - } - stmt.close(); - } - - @Test - public void testBatchInsert() throws SQLException { - ExecutorService executorService = Executors.newFixedThreadPool(numOfTables); - for (int i = 0; i < numOfTables; i++) { - final int index = i; - executorService.execute(() -> { - try { - long startTime = System.currentTimeMillis(); - Statement statement = connection.createStatement(); // get statement - StringBuilder sb = new StringBuilder(); - sb.append("INSERT INTO " + tablePrefix + index + " VALUES"); - Random rand = new Random(); - for (int j = 1; j <= numOfRecordsPerTable; j++) { - sb.append("(" + (ts + j) + ", "); - sb.append(rand.nextInt(100) + ", "); - sb.append(rand.nextInt(100) + ", "); - sb.append(rand.nextInt(100) + ")"); - } - statement.addBatch(sb.toString()); - statement.executeBatch(); - long endTime = System.currentTimeMillis(); - System.out.println("Thread " + index + " takes " + (endTime - startTime) + " microseconds"); - connection.commit(); - statement.close(); - } catch (Exception e) { - e.printStackTrace(); - } - }); - } - executorService.shutdown(); - - try { - executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - Statement statement = connection.createStatement(); - ResultSet rs = statement.executeQuery("select * from meters"); - int num = 0; - while (rs.next()) { - num++; - } - assertEquals(num, numOfTables * numOfRecordsPerTable); - rs.close(); - } - - @After - public void close() { - try { - if (connection != null) - connection.close(); - } catch (SQLException e) { - e.printStackTrace(); - } - } - -} \ No newline at end of file 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 f52c50ff1a6dde4709a99a0172eef2d13877d3d0..8c9b258dcdf8d4e5bb2f6cfc91fe945f3425b4d2 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,141 +42,132 @@ 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) { - } - try { - tsdbStatement.isWrapperFor(null); - } catch (SQLException e) { - } + public void case004_testUnsupport() throws SQLException { + + Assert.assertNotNull(statement.unwrap(TSDBPreparedStatement.class)); + Assert.assertTrue(statement.isWrapperFor(TSDBPreparedStatement.class)); + try { - tsdbStatement.getMaxFieldSize(); + statement.getMaxFieldSize(); } catch (SQLException e) { } try { - tsdbStatement.setMaxFieldSize(0); + statement.setMaxFieldSize(0); } catch (SQLException e) { } try { - tsdbStatement.setEscapeProcessing(true); + statement.setEscapeProcessing(true); } catch (SQLException e) { } try { - tsdbStatement.cancel(); + statement.cancel(); } catch (SQLException e) { } try { - tsdbStatement.getWarnings(); + statement.getWarnings(); } catch (SQLException e) { } try { - tsdbStatement.clearWarnings(); + statement.clearWarnings(); } catch (SQLException e) { } try { - tsdbStatement.setCursorName(null); + statement.setCursorName(null); } catch (SQLException e) { } try { - tsdbStatement.getMoreResults(); + statement.getMoreResults(); } catch (SQLException e) { } try { - tsdbStatement.setFetchDirection(0); + statement.setFetchDirection(0); } catch (SQLException e) { } try { - tsdbStatement.getFetchDirection(); + statement.getFetchDirection(); } catch (SQLException e) { } try { - tsdbStatement.getResultSetConcurrency(); + statement.getResultSetConcurrency(); } catch (SQLException e) { } try { - tsdbStatement.getResultSetType(); + statement.getResultSetType(); } catch (SQLException e) { } try { - tsdbStatement.getConnection(); + statement.getConnection(); } catch (SQLException e) { } try { - tsdbStatement.getMoreResults(); + statement.getMoreResults(); } catch (SQLException e) { } try { - tsdbStatement.getGeneratedKeys(); + statement.getGeneratedKeys(); } catch (SQLException e) { } try { - tsdbStatement.executeUpdate(null, 0); + statement.executeUpdate(null, 0); } catch (SQLException e) { } try { - tsdbStatement.executeUpdate(null, new int[]{0}); + statement.executeUpdate(null, new int[]{0}); } catch (SQLException e) { } try { - tsdbStatement.executeUpdate(null, new String[]{"str1", "str2"}); + statement.executeUpdate(null, new String[]{"str1", "str2"}); } catch (SQLException e) { } try { - tsdbStatement.getResultSetHoldability(); + statement.getResultSetHoldability(); } catch (SQLException e) { } try { - tsdbStatement.setPoolable(true); + statement.setPoolable(true); } catch (SQLException e) { } try { - tsdbStatement.isPoolable(); + statement.isPoolable(); } catch (SQLException e) { } try { - tsdbStatement.closeOnCompletion(); + statement.closeOnCompletion(); } catch (SQLException e) { + } try { - tsdbStatement.isCloseOnCompletion(); + statement.isCloseOnCompletion(); } catch (SQLException e) { } } diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/QueryDataTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/QueryDataTest.java index 611e21e887b1988a66019d437133d6c3f7926961..37fbc284877dbb27ecb8c9da1b752cfaff029023 100644 --- a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/QueryDataTest.java +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/QueryDataTest.java @@ -6,76 +6,67 @@ import org.junit.Test; import java.sql.*; import java.util.Properties; -import java.util.Random; import static org.junit.Assert.assertEquals; -import java.util.Properties; -import java.util.concurrent.Executors; -import java.util.concurrent.*; - -import static org.junit.Assert.assertTrue; -public class QueryDataTest extends BaseTest { +public class QueryDataTest { - static Connection connection = null; - static Statement statement = null; + static Connection connection; + static Statement statement; static String dbName = "test"; static String stbName = "meters"; - static String host = "localhost"; - static int numOfTables = 30; - final static int numOfRecordsPerTable = 1000; - static long ts = 1496732686000l; - final static String tablePrefix = "t"; + static String host = "127.0.0.1"; @Before - public void createDatabase() throws SQLException { + public void createDatabase() { try { Class.forName("com.taosdata.jdbc.TSDBDriver"); - } catch (ClassNotFoundException e) { + Properties properties = new Properties(); + 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); + + statement = connection.createStatement(); + statement.executeUpdate("drop database if exists " + dbName); + statement.executeUpdate("create database if not exists " + dbName); + statement.executeUpdate("use " + dbName); + + String createTableSql = "create table " + stbName + "(ts timestamp, name binary(64))"; + statement.executeUpdate(createTableSql); + + } catch (ClassNotFoundException | SQLException e) { 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); - - statement = connection.createStatement(); - statement.executeUpdate("drop database if exists " + dbName); - statement.executeUpdate("create database if not exists " + dbName); - statement.executeUpdate("use " + dbName); - - String createTableSql = "create table " + stbName + "(ts timestamp, name binary(6))"; - statement.executeUpdate(createTableSql); } - - @Test - public void testQueryBinaryData() throws SQLException{ - - String insertSql = "insert into " + stbName + " values(now, 'taosda')"; - System.out.println(insertSql); + @Test + public void testQueryBinaryData() throws SQLException { + String insertSql = "insert into " + stbName + " values(now, 'taosdata')"; + System.out.println(insertSql); statement.executeUpdate(insertSql); String querySql = "select * from " + stbName; - ResultSet rs = statement.executeQuery(querySql); + ResultSet rs = statement.executeQuery(querySql); - while(rs.next()) { - String name = rs.getString(2) + "001"; + while (rs.next()) { + String name = rs.getString(2); System.out.println("name = " + name); - assertEquals(name, "taosda001"); + assertEquals("taosdata", name); } - rs.close(); + rs.close(); } - @After - public void close() throws Exception { - statement.close(); - connection.close(); - Thread.sleep(10); + public void close() { + try { + if (statement != null) + statement.close(); + if (connection != null) + connection.close(); + } catch (SQLException e) { + e.printStackTrace(); + } } - + } \ No newline at end of file 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 index 8067c547df148587766d87e204f5abad2bfb659d..3d80ff066cacfdf71f087942f11e2ee5e86b65ee 100644 --- a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/ResultSetTest.java +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/ResultSetTest.java @@ -1,6 +1,7 @@ package com.taosdata.jdbc; import org.junit.AfterClass; +import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; @@ -13,42 +14,37 @@ import java.util.Properties; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -public class ResultSetTest extends BaseTest { - static Connection connection = null; - static Statement statement = null; +public class ResultSetTest { + static Connection connection; + static Statement statement; static String dbName = "test"; static String tName = "t0"; static String host = "localhost"; - static ResultSet resSet = null; + static ResultSet resSet; @BeforeClass - public static void createDatabaseAndTable() throws SQLException { + public static void createDatabaseAndTable() { try { Class.forName("com.taosdata.jdbc.TSDBDriver"); - } catch (ClassNotFoundException e) { + Properties properties = new Properties(); + properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8"); + properties.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8"); + properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8"); + connection = DriverManager.getConnection("jdbc:TAOS://" + host + ":0/", properties); + statement = connection.createStatement(); + statement.executeUpdate("drop database if exists " + dbName); + statement.executeUpdate("create database if not exists " + dbName); + statement.execute("use " + 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))"); + } catch (ClassNotFoundException | SQLException e) { return; } - Properties properties = new Properties(); - properties.setProperty(TSDBDriver.PROPERTY_KEY_HOST, host); - properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8"); - 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); - - 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; + String sql; long ts = 1496732686000l; int v1 = 2147483600; long v2 = ts + 1000; @@ -119,16 +115,8 @@ public class ResultSetTest extends BaseTest { public void testUnsupport() throws SQLException { statement.executeQuery("show databases"); resSet = statement.getResultSet(); - try { - resSet.unwrap(null); - } catch (SQLException e) { - assertTrue(e.getMessage().contains("this operation is NOT supported currently!")); - } - try { - resSet.isWrapperFor(null); - } catch (SQLException e) { - assertTrue(e.getMessage().contains("this operation is NOT supported currently!")); - } + Assert.assertNotNull(resSet.unwrap(TSDBResultSet.class)); + Assert.assertTrue(resSet.isWrapperFor(TSDBResultSet.class)); try { resSet.getAsciiStream(0); } catch (SQLException e) { @@ -815,13 +803,18 @@ public class ResultSetTest extends BaseTest { assertEquals(res.length, 2); statement.clearBatch(); } - @AfterClass - public static void close() throws Exception { - statement.executeUpdate("drop database " + dbName); - statement.close(); - connection.close(); - Thread.sleep(10); + @AfterClass + public static void close() { + try { + statement.executeUpdate("drop database " + dbName); + if (statement != null) + statement.close(); + if (connection != null) + connection.close(); + } catch (SQLException e) { + e.printStackTrace(); + } } } diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/StableTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/StableTest.java index c86a0b4c6aeeab5def76d758268b2b9e0d84b01f..0a1c548baa330edada8c393f79cc89583bea7b18 100644 --- a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/StableTest.java +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/StableTest.java @@ -29,6 +29,7 @@ public class StableTest { properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8"); connection = DriverManager.getConnection("jdbc:TAOS://" + host + ":0/", properties); Statement statement = connection.createStatement(); + statement.execute("drop database if exists " + dbName); statement.execute("create database if not exists " + dbName); statement.execute("use " + 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 index a79512984e6d78fda30b1281fefe91ddea7c2434..b09482fb03f643cd183468b18bf36e50c50bf0b4 100644 --- a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/StatementTest.java +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/StatementTest.java @@ -1,6 +1,7 @@ package com.taosdata.jdbc; import org.junit.AfterClass; +import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; @@ -16,23 +17,22 @@ public class StatementTest { 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"); + Properties properties = new Properties(); + 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/?user=root&password=taosdata", properties); + statement = connection.createStatement(); + statement.executeUpdate("drop database if exists " + dbName); + } catch (ClassNotFoundException e) { return; } - Properties properties = new Properties(); - 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/?user=root&password=taosdata", properties); - - statement = connection.createStatement(); - statement.executeUpdate("drop database if exists " + dbName); } @Test @@ -49,7 +49,6 @@ public class StatementTest { } catch (SQLException e) { e.printStackTrace(); } - } @Test @@ -67,118 +66,46 @@ public class StatementTest { assertEquals(false, isClosed); } - @Test - public void testUnsupport() { - 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) { - } + @Test(expected = SQLException.class) + public void testUnsupport() throws SQLException { + Assert.assertNotNull(statement.unwrap(TSDBStatement.class)); + Assert.assertTrue(statement.isWrapperFor(TSDBStatement.class)); + + statement.getMaxFieldSize(); + statement.setMaxFieldSize(0); + statement.setEscapeProcessing(true); + statement.cancel(); + statement.getWarnings(); + statement.clearWarnings(); + statement.setCursorName(null); + statement.getMoreResults(); + statement.setFetchDirection(0); + statement.getFetchDirection(); + statement.getResultSetConcurrency(); + statement.getResultSetType(); + statement.getConnection(); + statement.getMoreResults(); + statement.getGeneratedKeys(); + statement.executeUpdate(null, 0); + statement.executeUpdate(null, new int[]{0}); + statement.executeUpdate(null, new String[]{"str1", "str2"}); + statement.getResultSetHoldability(); + statement.setPoolable(true); + statement.isPoolable(); + statement.closeOnCompletion(); + statement.isCloseOnCompletion(); } @AfterClass - public static void close() throws Exception { - if (!statement.isClosed()) { - statement.executeUpdate("drop database if exists " + dbName); - statement.close(); - connection.close(); - Thread.sleep(10); + public static void close() { + try { + statement.execute("drop database if exists " + dbName); + if (statement != null) + statement.close(); + if (connection != null) + connection.close(); + } catch (SQLException e) { + e.printStackTrace(); } } } 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 0a71c77d1d9dc718cc029c6a0b11ed63cd3034c1..1d8ff08db6d5f177fe74de579e1b6bb26ee35750 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,36 +10,37 @@ 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"; String topic = "test"; @Before - public void createDatabase() throws SQLException { + public void createDatabase() { try { Class.forName("com.taosdata.jdbc.TSDBDriver"); - } catch (ClassNotFoundException e) { - 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); + 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); - statement = connection.createStatement(); - statement.executeUpdate("create database if not exists " + dbName); - statement.executeUpdate("create table if not exists " + dbName + "." + tName + " (ts timestamp, k int, v int)"); - long ts = System.currentTimeMillis(); - for (int i = 0; i < 2; i++) { - ts += i; - String sql = "insert into " + dbName + "." + tName + " values (" + ts + ", " + (100 + i) + ", " + i + ")"; - statement.executeUpdate(sql); + statement = connection.createStatement(); + statement.executeUpdate("create database if not exists " + dbName); + statement.executeUpdate("create table if not exists " + dbName + "." + tName + " (ts timestamp, k int, v int)"); + long ts = System.currentTimeMillis(); + for (int i = 0; i < 2; i++) { + ts += i; + String sql = "insert into " + dbName + "." + tName + " values (" + ts + ", " + (100 + i) + ", " + i + ")"; + statement.executeUpdate(sql); + } + + } catch (ClassNotFoundException | SQLException e) { + return; } } @@ -79,10 +80,16 @@ public class SubscribeTest extends BaseTest { } @After - public void close() throws Exception { - statement.executeQuery("drop database " + dbName); - statement.close(); - connection.close(); - Thread.sleep(10); + public void close() { + try { + statement.executeQuery("drop database " + dbName); + if (statement != null) + statement.close(); + if (connection != null) + connection.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } } \ No newline at end of file diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBDatabaseMetaDataTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBDatabaseMetaDataTest.java index 8066b38573b87d9d5b91a628b56ab5700e379113..a7657fa3e581c96cf85555b16fca5f8b4c7f8ebb 100644 --- a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBDatabaseMetaDataTest.java +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBDatabaseMetaDataTest.java @@ -6,37 +6,9 @@ import java.sql.*; import java.util.Properties; public class TSDBDatabaseMetaDataTest { - private TSDBDatabaseMetaData metaData; private static final String host = "127.0.0.1"; - private Connection connection; - - @BeforeClass - public void before() { - try { - Class.forName("com.taosdata.jdbc.TSDBDriver"); - 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 + ":6030/?user=root&password=taosdata", properties); - metaData = connection.getMetaData().unwrap(TSDBDatabaseMetaData.class); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } catch (SQLException e) { - e.printStackTrace(); - } - } - - @AfterClass - public void after() { - try { - if (connection != null) - connection.close(); - } catch (SQLException e) { - e.printStackTrace(); - } - } + private static Connection connection; + private static TSDBDatabaseMetaData metaData; @Test public void unwrap() throws SQLException { @@ -61,7 +33,7 @@ public class TSDBDatabaseMetaDataTest { @Test public void getURL() throws SQLException { - Assert.assertEquals("jdbc:TAOS://localhost:6030/?user=root&password=taosdata", metaData.getURL()); + Assert.assertEquals("jdbc:TAOS://" + host + ":6030/?user=root&password=taosdata", metaData.getURL()); } @Test @@ -975,4 +947,32 @@ public class TSDBDatabaseMetaDataTest { public void generatedKeyAlwaysReturned() throws SQLException { Assert.assertFalse(metaData.generatedKeyAlwaysReturned()); } + + @BeforeClass + public static void beforeClass() { + try { + Class.forName("com.taosdata.jdbc.TSDBDriver"); + Properties properties = new Properties(); + 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 + ":6030/?user=root&password=taosdata", properties); + metaData = connection.getMetaData().unwrap(TSDBDatabaseMetaData.class); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + @AfterClass + public static void afterClass() { + try { + if (connection != null) + connection.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } \ No newline at end of file diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBDriverTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBDriverTest.java index 8adcdefb2974d5817793297091eee1c2f40a52b9..445723f501a85f7c697afffb59b273bcf6a1a630 100644 --- a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBDriverTest.java +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBDriverTest.java @@ -3,9 +3,6 @@ package com.taosdata.jdbc; import org.junit.BeforeClass; import org.junit.Test; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; import java.sql.*; import java.util.Properties; @@ -27,54 +24,15 @@ public class TSDBDriverTest { "jdbc:TAOS://:/test", "jdbc:TAOS://localhost:0/?user=root&password=taosdata" }; - private static boolean islibLoaded = false; - private static boolean isTaosdActived; private Connection conn; - @BeforeClass - public static void before() { - String osName = System.getProperty("os.name").toLowerCase(); - if (!osName.equals("linux")) - return; - // try to load taos lib - try { - System.loadLibrary("taos"); - islibLoaded = true; - } catch (UnsatisfiedLinkError error) { - System.out.println("load tdengine lib failed."); - error.printStackTrace(); - } - // check taosd is activated - try { - String[] cmd = {"/bin/bash", "-c", "ps -ef | grep taosd | grep -v \"grep\""}; - Process exec = Runtime.getRuntime().exec(cmd); - BufferedReader reader = new BufferedReader(new InputStreamReader(exec.getInputStream())); - int lineCnt = 0; - while (reader.readLine() != null) { - lineCnt++; - } - if (lineCnt > 0) - isTaosdActived = true; - } catch (IOException e) { - e.printStackTrace(); - } - - try { - Class.forName("com.taosdata.jdbc.TSDBDriver"); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } - } - @Test public void testConnectWithJdbcURL() { final String url = "jdbc:TAOS://localhost:6030/log?user=root&password=taosdata"; try { - if (islibLoaded && isTaosdActived) { - conn = DriverManager.getConnection(url); - assertNotNull("failure - connection should not be null", conn); - } + conn = DriverManager.getConnection(url); + assertNotNull("failure - connection should not be null", conn); } catch (SQLException e) { e.printStackTrace(); fail("failure - should not throw Exception"); @@ -89,10 +47,8 @@ public class TSDBDriverTest { connProps.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8"); connProps.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8"); try { - if (islibLoaded && isTaosdActived) { - conn = DriverManager.getConnection(jdbcUrl, connProps); - assertNotNull("failure - connection should not be null", conn); - } + conn = DriverManager.getConnection(jdbcUrl, connProps); + assertNotNull("failure - connection should not be null", conn); } catch (SQLException e) { e.printStackTrace(); fail("failure - should not throw Exception"); @@ -107,10 +63,8 @@ public class TSDBDriverTest { connProps.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8"); connProps.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8"); try { - if (islibLoaded && isTaosdActived) { - conn = DriverManager.getConnection(jdbcUrl, connProps); - assertNotNull("failure - connection should not be null", conn); - } + conn = DriverManager.getConnection(jdbcUrl, connProps); + assertNotNull("failure - connection should not be null", conn); } catch (SQLException e) { e.printStackTrace(); fail("failure - should not throw Exception"); @@ -207,4 +161,14 @@ public class TSDBDriverTest { assertNull("failure - getParentLogger should be be null", new TSDBDriver().getParentLogger()); } + @BeforeClass + public static void before() { + try { + Class.forName("com.taosdata.jdbc.TSDBDriver"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } + + } \ No newline at end of file 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 0000000000000000000000000000000000000000..ebacf6af6b480d7d8b9364256551f4cdb0034608 --- /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/?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 diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/cases/BatchInsertTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/cases/BatchInsertTest.java index 9608c4985db15312e8e2966badfe4db8241bc306..472da34980d98cbe694783b120bc5533a908fa5b 100644 --- a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/cases/BatchInsertTest.java +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/cases/BatchInsertTest.java @@ -1,14 +1,12 @@ package com.taosdata.jdbc.cases; -import com.taosdata.jdbc.lib.TSDBCommon; +import com.taosdata.jdbc.TSDBDriver; import org.junit.After; import org.junit.Before; import org.junit.Test; -import java.sql.Connection; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; +import java.sql.*; +import java.util.Properties; import java.util.Random; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -18,57 +16,71 @@ import static org.junit.Assert.assertEquals; public class BatchInsertTest { - static String host = "localhost"; + static String host = "127.0.0.1"; static String dbName = "test"; static String stbName = "meters"; static int numOfTables = 30; final static int numOfRecordsPerTable = 1000; static long ts = 1496732686000l; final static String tablePrefix = "t"; - private Connection connection; @Before public void before() { try { - connection = TSDBCommon.getConn(host); - TSDBCommon.createDatabase(connection, dbName); - TSDBCommon.createStable(connection, stbName); - TSDBCommon.createTables(connection, numOfTables, stbName, tablePrefix); + Class.forName("com.taosdata.jdbc.TSDBDriver"); + 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); + + Statement statement = connection.createStatement(); + statement.executeUpdate("drop database if exists " + dbName); + statement.executeUpdate("create database if not exists " + dbName); + statement.executeUpdate("use " + dbName); + // create stable + String createTableSql = "create table " + stbName + "(ts timestamp, f1 int, f2 int, f3 int) tags(areaid int, loc binary(20))"; + statement.executeUpdate(createTableSql); + // create tables + for(int i = 0; i < numOfTables; i++) { + String loc = i % 2 == 0 ? "beijing" : "shanghai"; + String createSubTalbesSql = "create table " + tablePrefix + i + " using " + stbName + " tags(" + i + ", '" + loc + "')"; + statement.executeUpdate(createSubTalbesSql); + } + statement.close(); } catch (Exception e) { e.printStackTrace(); } } @Test - public void testBatchInsert(){ + public void testBatchInsert() { ExecutorService executorService = Executors.newFixedThreadPool(numOfTables); for (int i = 0; i < numOfTables; i++) { final int index = i; - executorService.execute(new Runnable() { - @Override - public void run() { - try { - long startTime = System.currentTimeMillis(); - Statement statement = connection.createStatement(); // get statement - StringBuilder sb = new StringBuilder(); - sb.append("INSERT INTO " + tablePrefix + index + " VALUES"); - Random rand = new Random(); - for (int j = 1; j <= numOfRecordsPerTable; j++) { - sb.append("(" + (ts + j) + ", "); - sb.append(rand.nextInt(100) + ", "); - sb.append(rand.nextInt(100) + ", "); - sb.append(rand.nextInt(100) + ")"); - } - statement.addBatch(sb.toString()); - statement.executeBatch(); - long endTime = System.currentTimeMillis(); - System.out.println("Thread " + index + " takes " + (endTime - startTime) + " microseconds"); - connection.commit(); - statement.close(); - } catch (Exception e) { - e.printStackTrace(); + executorService.execute(() -> { + try { + long startTime = System.currentTimeMillis(); + Statement statement = connection.createStatement(); // get statement + StringBuilder sb = new StringBuilder(); + sb.append("INSERT INTO " + tablePrefix + index + " VALUES"); + Random rand = new Random(); + for (int j = 1; j <= numOfRecordsPerTable; j++) { + sb.append("(" + (ts + j) + ", "); + sb.append(rand.nextInt(100) + ", "); + sb.append(rand.nextInt(100) + ", "); + sb.append(rand.nextInt(100) + ")"); } + statement.addBatch(sb.toString()); + statement.executeBatch(); + long endTime = System.currentTimeMillis(); + System.out.println("Thread " + index + " takes " + (endTime - startTime) + " microseconds"); + connection.commit(); + statement.close(); + } catch (Exception e) { + e.printStackTrace(); } }); } @@ -80,7 +92,7 @@ public class BatchInsertTest { e.printStackTrace(); } - try{ + try { Statement statement = connection.createStatement(); ResultSet rs = statement.executeQuery("select * from meters"); int num = 0; @@ -89,7 +101,7 @@ public class BatchInsertTest { } assertEquals(num, numOfTables * numOfRecordsPerTable); rs.close(); - }catch (Exception e){ + } catch (Exception e) { e.printStackTrace(); } } @@ -102,7 +114,6 @@ public class BatchInsertTest { } catch (SQLException e) { e.printStackTrace(); } - } } diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/lib/TSDBCommon.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/lib/TSDBCommon.java deleted file mode 100644 index 0e2613d617074571c86c39ef253d68af9a0f6922..0000000000000000000000000000000000000000 --- a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/lib/TSDBCommon.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.taosdata.jdbc.lib; - -import com.taosdata.jdbc.TSDBDriver; - -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.Properties; - -public class TSDBCommon { - - public static Connection getConn(String host) throws SQLException, ClassNotFoundException { - Class.forName("com.taosdata.jdbc.TSDBDriver"); - 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"); - return DriverManager.getConnection("jdbc:TAOS://" + host + ":0/", properties); - } - - public static void createDatabase(Connection connection, String dbName) throws SQLException { - Statement statement = connection.createStatement(); - statement.executeUpdate("drop database if exists " + dbName); - statement.executeUpdate("create database if not exists " + dbName); - statement.executeUpdate("use " + dbName); - statement.close(); - } - - public static void createStable(Connection connection, String stbName) throws SQLException { - Statement statement = connection.createStatement(); - String createTableSql = "create table " + stbName + "(ts timestamp, f1 int, f2 int, f3 int) tags(areaid int, loc binary(20))"; - statement.executeUpdate(createTableSql); - statement.close(); - } - - public static void createTables(Connection connection, int numOfTables, String stbName,String tablePrefix) throws SQLException { - Statement statement = connection.createStatement(); - for(int i = 0; i < numOfTables; i++) { - String loc = i % 2 == 0 ? "beijing" : "shanghai"; - String createSubTalbesSql = "create table " + tablePrefix + i + " using " + stbName + " tags(" + i + ", '" + loc + "')"; - statement.executeUpdate(createSubTalbesSql); - } - statement.close(); - } -} diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/utils/SqlSyntaxValidatorTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/utils/SqlSyntaxValidatorTest.java index c38958c6ceb50af5d3925003f34e5cc16c272b97..ccb0941da00e2755eaf8fde2553b8a8e6b33cd25 100644 --- a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/utils/SqlSyntaxValidatorTest.java +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/utils/SqlSyntaxValidatorTest.java @@ -16,10 +16,6 @@ public class SqlSyntaxValidatorTest { @Test public void isUseSQL() { Assert.assertTrue(SqlSyntaxValidator.isUseSql("use database test")); - Assert.assertTrue(SqlSyntaxValidator.isUseSql("create database test")); - Assert.assertTrue(SqlSyntaxValidator.isUseSql("create database if not exist test")); - Assert.assertTrue(SqlSyntaxValidator.isUseSql("drop database test")); - Assert.assertTrue(SqlSyntaxValidator.isUseSql("drop database if exist test")); } } \ No newline at end of file