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 new file mode 100644 index 0000000000000000000000000000000000000000..ca38765ec5904ba9b3148949be446869453d8d3c --- /dev/null +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/ConnectionTest.java @@ -0,0 +1,56 @@ +package com.taosdata.jdbc; + +import org.junit.Test; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Properties; + +import static org.junit.Assert.assertTrue; + +public class ConnectionTest { + static Connection connection = null; + static Statement statement = null; + static String dbName = "test"; + static String stbName = "st"; + static String host = "localhost"; + + @Test + 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); + + assertTrue(null != connection); + } + + @Test + public void createDatabase() { + try { + statement.executeUpdate("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(); + connection.close(); + } + } catch (SQLException e) { + assert false : "close connection error: " + e.getMessage(); + } + } +} 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 cf44a78b97a153992098d987fd0762c7e32fb9df..b0c5862dec44f539d6c4fb0ab49997182fc63507 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 @@ -1,4 +1,111 @@ 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 StableTest { + static Connection connection = null; + static Statement statement = null; + static String dbName = "test"; + static String stbName = "st"; + static String host = "localhost"; + + @BeforeClass + public static void createDatabase() 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("create database if not exists " + dbName); + statement.executeQuery("use " + dbName); + } + + @Test + public void createStable() { + String sql = "create table " + stbName + " (ts timestamp, v1 int, v2 int) tags (tg nchar(20)) "; + + try { + statement.executeUpdate(sql); + } catch (SQLException e) { + assert false : "error create stable" + e.getMessage(); + } + } + + @Test + public void createTable() { + String sql = "create table t1 using " + stbName + " tags (\"beijing\")"; + + try { + statement.executeUpdate(sql); + } catch (SQLException e) { + assert false : "error create table" + e.getMessage(); + } + } + + @Test + public void describeSTable() { + String sql = "describe " + stbName; + int num = 0; + System.out.println("describe stable"); + try { + ResultSet res = statement.executeQuery(sql); + while (res.next()) { + for (int i = 1; i <= res.getMetaData().getColumnCount(); i++) { + System.out.printf("%d: %s\n", i, res.getString(i)); + } + num++; + } + assertEquals(4, num); + } catch (SQLException e) { + assert false : "error describe stable" + e.getMessage(); + } + } + + @Test + public void describeTable() { + String sql = "describe t1"; + int num = 0; + System.out.println("describe table"); + try { + ResultSet res = statement.executeQuery(sql); + while (res.next()) { + for (int i = 1; i <= res.getMetaData().getColumnCount(); i++) { + System.out.printf("%d: %s\n", i, res.getString(i)); + } + num++; + } + assertEquals(4, num); + } catch (SQLException e) { + assert false : "error describe stable" + e.getMessage(); + } + } + + @Test + public void validCreateSql() { + String sql = "create table t2 using " + stbName + " tags (\"beijing\")"; + boolean valid = ((TSDBConnection)connection).getConnection().validateCreateTableSql(sql); + assertEquals(true, valid); + } + + @AfterClass + public static void close() throws Exception { + if (!statement.isClosed()) { + statement.executeUpdate("drop database " + dbName); + statement.close(); + connection.close(); + } + } }