From ababaa75e390094a38f70c15c49753a31c063dc8 Mon Sep 17 00:00:00 2001 From: zyyang Date: Wed, 4 Nov 2020 10:23:33 +0800 Subject: [PATCH 01/19] [TD-1702]: add test cases for TSDBDriver --- .../java/com/taosdata/jdbc/TSDBDriver.java | 208 +++++++---------- .../com/taosdata/jdbc/TSDBDriverTest.java | 212 +++++++++++++++--- 2 files changed, 267 insertions(+), 153 deletions(-) diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java index 97d93fb0a1..cd45048a77 100755 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java @@ -14,13 +14,9 @@ *****************************************************************************/ package com.taosdata.jdbc; - import java.io.*; - import java.sql.*; -import java.util.ArrayList; -import java.util.List; -import java.util.Properties; +import java.util.*; import java.util.logging.Logger; /** @@ -44,76 +40,53 @@ import java.util.logging.Logger; */ public class TSDBDriver implements java.sql.Driver { - @Deprecated private static final String URL_PREFIX1 = "jdbc:TSDB://"; private static final String URL_PREFIX = "jdbc:TAOS://"; - /** - * Key used to retrieve the database value from the properties instance passed - * to the driver. - */ - public static final String PROPERTY_KEY_DBNAME = "dbname"; - /** * Key used to retrieve the host value from the properties instance passed to * the driver. */ public static final String PROPERTY_KEY_HOST = "host"; - /** - * Key used to retrieve the password value from the properties instance passed - * to the driver. - */ - public static final String PROPERTY_KEY_PASSWORD = "password"; - /** * Key used to retrieve the port number value from the properties instance * passed to the driver. */ public static final String PROPERTY_KEY_PORT = "port"; - + /** + * Key used to retrieve the database value from the properties instance passed + * to the driver. + */ + public static final String PROPERTY_KEY_DBNAME = "dbname"; /** * Key used to retrieve the user value from the properties instance passed to * the driver. */ public static final String PROPERTY_KEY_USER = "user"; - - + /** + * Key used to retrieve the password value from the properties instance passed + * to the driver. + */ + public static final String PROPERTY_KEY_PASSWORD = "password"; /** * Key for the configuration file directory of TSDB client in properties instance */ public static final String PROPERTY_KEY_CONFIG_DIR = "cfgdir"; - /** * Key for the timezone used by the TSDB client in properties instance */ public static final String PROPERTY_KEY_TIME_ZONE = "timezone"; - /** * Key for the locale used by the TSDB client in properties instance */ public static final String PROPERTY_KEY_LOCALE = "locale"; - - /** * Key for the char encoding used by the TSDB client in properties instance */ public static final String PROPERTY_KEY_CHARSET = "charset"; - public static final String PROPERTY_KEY_PROTOCOL = "protocol"; - - - /** - * Index for port coming out of parseHostPortPair(). - */ - public final static int PORT_NUMBER_INDEX = 1; - - /** - * Index for host coming out of parseHostPortPair(). - */ - public final static int HOST_NAME_INDEX = 0; - private TSDBDatabaseMetaData dbMetaData = null; static { @@ -169,9 +142,11 @@ public class TSDBDriver implements java.sql.Driver { } public Connection connect(String url, Properties info) throws SQLException { - if (url == null) { + if (url == null) throw new SQLException(TSDBConstants.WrapErrMsg("url is not set!")); - } + + if (!acceptsURL(url)) + return null; Properties props = null; if ((props = parseURL(url, info)) == null) { @@ -179,7 +154,10 @@ public class TSDBDriver implements java.sql.Driver { } //load taos.cfg start - if (info.getProperty(TSDBDriver.PROPERTY_KEY_HOST) == null && info.getProperty(TSDBDriver.PROPERTY_KEY_PORT) == null) { + if ((info.getProperty(TSDBDriver.PROPERTY_KEY_HOST) == null || + info.getProperty(TSDBDriver.PROPERTY_KEY_HOST).isEmpty()) && ( + info.getProperty(TSDBDriver.PROPERTY_KEY_PORT) == null || + info.getProperty(TSDBDriver.PROPERTY_KEY_PORT).isEmpty())) { File cfgDir = loadConfigDir(info.getProperty(TSDBDriver.PROPERTY_KEY_CONFIG_DIR)); File cfgFile = cfgDir.listFiles((dir, name) -> "taos.cfg".equalsIgnoreCase(name))[0]; List endpoints = loadConfigEndpoints(cfgFile); @@ -190,7 +168,9 @@ public class TSDBDriver implements java.sql.Driver { } try { - TSDBJNIConnector.init((String) props.get(PROPERTY_KEY_CONFIG_DIR), (String) props.get(PROPERTY_KEY_LOCALE), (String) props.get(PROPERTY_KEY_CHARSET), + TSDBJNIConnector.init((String) props.get(PROPERTY_KEY_CONFIG_DIR), + (String) props.get(PROPERTY_KEY_LOCALE), + (String) props.get(PROPERTY_KEY_CHARSET), (String) props.get(PROPERTY_KEY_TIME_ZONE)); Connection newConn = new TSDBConnection(props, this.dbMetaData); return newConn; @@ -208,43 +188,15 @@ public class TSDBDriver implements java.sql.Driver { } /** - * Parses hostPortPair in the form of [host][:port] into an array, with the - * element of index HOST_NAME_INDEX being the host (or null if not specified), - * and the element of index PORT_NUMBER_INDEX being the port (or null if not - * specified). - * - * @param hostPortPair host and port in form of of [host][:port] - * @return array containing host and port as Strings - * @throws SQLException if a parse error occurs + * @param url the URL of the database + * @return true if this driver understands the given URL; + * false otherwise + * @throws SQLException if a database access error occurs or the url is {@code null} */ - protected static String[] parseHostPortPair(String hostPortPair) throws SQLException { - String[] splitValues = new String[2]; - - int portIndex = hostPortPair.indexOf(":"); - - String hostname = null; - - if (portIndex != -1) { - if ((portIndex + 1) < hostPortPair.length()) { - String portAsString = hostPortPair.substring(portIndex + 1); - hostname = hostPortPair.substring(0, portIndex); - - splitValues[HOST_NAME_INDEX] = hostname; - - splitValues[PORT_NUMBER_INDEX] = portAsString; - } else { - throw new SQLException(TSDBConstants.WrapErrMsg("port is not proper!")); - } - } else { - splitValues[HOST_NAME_INDEX] = hostPortPair; - splitValues[PORT_NUMBER_INDEX] = null; - } - - return splitValues; - } - public boolean acceptsURL(String url) throws SQLException { - return (url != null && url.length() > 0 && url.trim().length() > 0) && url.startsWith(URL_PREFIX); + if (url == null) + throw new SQLException(TSDBConstants.WrapErrMsg("url is null")); + return (url != null && url.length() > 0 && url.trim().length() > 0) && (url.startsWith(URL_PREFIX) || url.startsWith(URL_PREFIX1)); } public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException { @@ -252,15 +204,17 @@ public class TSDBDriver implements java.sql.Driver { info = new Properties(); } - if ((url != null) && (url.startsWith(URL_PREFIX) || url.startsWith(URL_PREFIX1))) { + if (acceptsURL(url)) { info = parseURL(url, info); } DriverPropertyInfo hostProp = new DriverPropertyInfo(PROPERTY_KEY_HOST, info.getProperty(PROPERTY_KEY_HOST)); - hostProp.required = true; + hostProp.required = false; + hostProp.description = "Hostname"; DriverPropertyInfo portProp = new DriverPropertyInfo(PROPERTY_KEY_PORT, info.getProperty(PROPERTY_KEY_PORT, TSDBConstants.DEFAULT_PORT)); portProp.required = false; + portProp.description = "Port"; DriverPropertyInfo dbProp = new DriverPropertyInfo(PROPERTY_KEY_DBNAME, info.getProperty(PROPERTY_KEY_DBNAME)); dbProp.required = false; @@ -268,9 +222,11 @@ public class TSDBDriver implements java.sql.Driver { DriverPropertyInfo userProp = new DriverPropertyInfo(PROPERTY_KEY_USER, info.getProperty(PROPERTY_KEY_USER)); userProp.required = true; + userProp.description = "User"; DriverPropertyInfo passwordProp = new DriverPropertyInfo(PROPERTY_KEY_PASSWORD, info.getProperty(PROPERTY_KEY_PASSWORD)); passwordProp.required = true; + passwordProp.description = "Password"; DriverPropertyInfo[] propertyInfo = new DriverPropertyInfo[5]; propertyInfo[0] = hostProp; @@ -283,20 +239,60 @@ public class TSDBDriver implements java.sql.Driver { } /** - * example: jdbc:TSDB://127.0.0.1:0/db?user=root&password=your_password + * example: jdbc:TAOS://127.0.0.1:0/db?user=root&password=your_password */ - public Properties parseURL(String url, Properties defaults) throws java.sql.SQLException { + public Properties parseURL(String url, Properties defaults) { Properties urlProps = (defaults != null) ? defaults : new Properties(); - if (url == null) { + if (url == null || url.length() <= 0 || url.trim().length() <= 0) return null; - } - - if (!url.startsWith(URL_PREFIX) && !url.startsWith(URL_PREFIX1)) { + if (!url.startsWith(URL_PREFIX) && !url.startsWith(URL_PREFIX1)) return null; - } + // parse properties + int beginningOfSlashes = url.indexOf("//"); + int index = url.indexOf("?"); + if (index != -1) { + String paramString = url.substring(index + 1, url.length()); + url = url.substring(0, index); + StringTokenizer queryParams = new StringTokenizer(paramString, "&"); + while (queryParams.hasMoreElements()) { + String parameterValuePair = queryParams.nextToken(); + int indexOfEqual = parameterValuePair.indexOf("="); + String parameter = null; + String value = null; + if (indexOfEqual != -1) { + parameter = parameterValuePair.substring(0, indexOfEqual); + if (indexOfEqual + 1 < parameterValuePair.length()) { + value = parameterValuePair.substring(indexOfEqual + 1); + } + } + if ((value != null && value.length() > 0) && (parameter != null && parameter.length() > 0)) { + urlProps.setProperty(parameter, value); + } + } + } + // parse dbname + url = url.substring(beginningOfSlashes + 2); + int indexOfSlash = url.indexOf("/"); + if (indexOfSlash != -1) { + if (indexOfSlash + 1 < url.length()) { + urlProps.setProperty(TSDBDriver.PROPERTY_KEY_DBNAME, url.substring(indexOfSlash + 1)); + } + url = url.substring(0, indexOfSlash); + } + // parse port + int indexOfColon = url.indexOf(":"); + if (indexOfColon != -1) { + if (indexOfColon + 1 < url.length()) { + urlProps.setProperty(TSDBDriver.PROPERTY_KEY_PORT, url.substring(indexOfColon + 1)); + } + url = url.substring(0, indexOfColon); + } + if (url != null && url.length() > 0 && url.trim().length() > 0) { + urlProps.setProperty(TSDBDriver.PROPERTY_KEY_HOST, url); + } + /* String urlForMeta = url; - String dbProductName = url.substring(url.indexOf(":") + 1); dbProductName = dbProductName.substring(0, dbProductName.indexOf(":")); int beginningOfSlashes = url.indexOf("//"); @@ -345,11 +341,11 @@ public class TSDBDriver implements java.sql.Driver { user = urlProps.getProperty(PROPERTY_KEY_USER).toString(); this.dbMetaData = new TSDBDatabaseMetaData(dbProductName, urlForMeta, user); - +*/ return urlProps; } - public void setPropertyValue(Properties property, String[] keyValuePair) { + private void setPropertyValue(Properties property, String[] keyValuePair) { switch (keyValuePair[0].toLowerCase()) { case PROPERTY_KEY_USER: property.setProperty(PROPERTY_KEY_USER, keyValuePair[1]); @@ -372,13 +368,12 @@ public class TSDBDriver implements java.sql.Driver { } } - public int getMajorVersion() { - return 1; + return 2; } public int getMinorVersion() { - return 1; + return 0; } public boolean jdbcCompliant() { @@ -389,33 +384,4 @@ public class TSDBDriver implements java.sql.Driver { return null; } - /** - * Returns the host property - * - * @param props the java.util.Properties instance to retrieve the hostname from. - * @return the host - */ - public String host(Properties props) { - return props.getProperty(PROPERTY_KEY_HOST, "localhost"); - } - - /** - * Returns the port number property - * - * @param props the properties to get the port number from - * @return the port number - */ - public int port(Properties props) { - return Integer.parseInt(props.getProperty(PROPERTY_KEY_PORT, TSDBConstants.DEFAULT_PORT)); - } - - /** - * Returns the database property from props - * - * @param props the Properties to look for the database property. - * @return the database name. - */ - public String database(Properties props) { - return props.getProperty(PROPERTY_KEY_DBNAME); - } } 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 a0981063a5..dc52fdadb7 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 @@ -1,47 +1,195 @@ package com.taosdata.jdbc; +import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; -import java.sql.SQLException; +import java.sql.*; import java.util.Properties; -import static org.junit.Assert.assertEquals; +import static org.junit.Assert.*; public class TSDBDriverTest { + private static String[] validURLs = { + "jdbc:TAOS://localhost:0", + "jdbc:TAOS://localhost", + "jdbc:TAOS://localhost:6030/test", + "jdbc:TAOS://localhost:6030", + "jdbc:TAOS://localhost:6030/", + "jdbc:TSDB://localhost:6030", + "jdbc:TSDB://localhost:6030/", + "jdbc:TAOS://127.0.0.1:0/db?user=root&password=taosdata", + "jdbc:TAOS://:", + "jdbc:TAOS://:/", + "jdbc:TAOS://:/test", + "jdbc:TAOS://localhost:0/?user=root&password=taosdata" + }; + private static boolean islibLoaded; + + @BeforeClass + public static void before() { + String osName = System.getProperty("os.name").toLowerCase(); + String libPath = null; + switch (osName) { + case "linux": + libPath = "/usr/lib/libtaos.so"; + break; + case "windows": + libPath = "C:\\TDengine\\driver\\taos.dll"; + break; + default: + } + if (libPath == null) + return; + try { + System.loadLibrary(libPath); + } catch (UnsatisfiedLinkError error) { + islibLoaded = false; + } + islibLoaded = true; + } + @Test - public void urlParserTest() throws SQLException { + public void testParseURL() { TSDBDriver driver = new TSDBDriver(); - String url = "jdbc:TSDB://127.0.0.1:0/db"; - Properties properties = new Properties(); - driver.parseURL(url, properties); - assertEquals(properties.get("host"), "127.0.0.1"); - assertEquals(properties.get("port"), "0"); - assertEquals(properties.get("dbname"), "db"); - assertEquals(properties.get("user"), "root"); - assertEquals(properties.get("password"), "your_password"); + String url = "jdbc:TAOS://127.0.0.1:0/db?user=root&password=taosdata&charset=UTF-8"; + Properties config = new Properties(); + Properties actual = driver.parseURL(url, config); + assertEquals("failure - host should be 127.0.0.1", "127.0.0.1", actual.get("host")); + assertEquals("failure - port should be 0", "0", actual.get("port")); + assertEquals("failure - dbname should be db", "db", actual.get("dbname")); + assertEquals("failure - user should be root", "root", actual.get("user")); + assertEquals("failure - password should be taosdata", "taosdata", actual.get("password")); + assertEquals("failure - charset should be UTF-8", "UTF-8", actual.get("charset")); - url = "jdbc:TSDB://127.0.0.1:0/log?charset=UTF-8"; - properties = new Properties(); - driver.parseURL(url, properties); - assertEquals(properties.get("host"), "127.0.0.1"); - assertEquals(properties.get("port"), "0"); - assertEquals(properties.get("dbname"), "log"); - assertEquals(properties.get("charset"), "UTF-8"); - - url = "jdbc:TSDB://127.0.0.1:0/"; - properties = new Properties(); - driver.parseURL(url, properties); - assertEquals(properties.get("host"), "127.0.0.1"); - assertEquals(properties.get("port"), "0"); - assertEquals(properties.get("dbname"), null); - - url = "jdbc:TSDB://127.0.0.1:0/db"; - properties = new Properties(); - driver.parseURL(url, properties); - assertEquals(properties.get("host"), "127.0.0.1"); - assertEquals(properties.get("port"), "0"); - assertEquals(properties.get("dbname"), "db"); + url = "jdbc:TAOS://127.0.0.1:0"; + config = new Properties(); + actual = driver.parseURL(url, config); + assertEquals("failure - host should be 127.0.0.1", "127.0.0.1", actual.getProperty("host")); + assertEquals("failure - port should be 0", "0", actual.get("port")); + assertEquals("failure - dbname should be null", null, actual.get("dbname")); + + url = "jdbc:TAOS://127.0.0.1:0/db"; + config = new Properties(); + actual = driver.parseURL(url, config); + assertEquals("failure - host should be 127.0.0.1", "127.0.0.1", actual.getProperty("host")); + assertEquals("failure - port should be 0", "0", actual.get("port")); + assertEquals("failure - dbname should be db", "db", actual.get("dbname")); + + url = "jdbc:TAOS://:/?"; + config = new Properties(); + config.setProperty(TSDBDriver.PROPERTY_KEY_USER, "root"); + config.setProperty(TSDBDriver.PROPERTY_KEY_PASSWORD, "taosdata"); + actual = driver.parseURL(url, config); + assertEquals("failure - user should be root", "root", actual.getProperty("user")); + assertEquals("failure - password should be taosdata", "taosdata", actual.getProperty("password")); + assertEquals("failure - host should be null", null, actual.getProperty("host")); + assertEquals("failure - port should be null", null, actual.getProperty("port")); + assertEquals("failure - dbname should be null", null, actual.getProperty("dbname")); + } + + @Test + public void testConnectWithJdbcURL() { + + final String url = "jdbc:TAOS://localhost:3306/log?user=root&password=taosdata"; + try { + if (islibLoaded) { + Connection conn = DriverManager.getConnection(url); + assertNotNull("failure - connection should not be null", conn); + } + } catch (SQLException e) { + e.printStackTrace(); + } + } + + @Test + public void testConnectWithProperties() { + final String jdbcUrl = "jdbc:TAOS://localhost.com:6030/test?user=root&password=taosdata"; + Properties connProps = new Properties(); + connProps.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8"); + connProps.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8"); + connProps.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8"); + try { + if (islibLoaded) { + Connection conn = DriverManager.getConnection(jdbcUrl, connProps); + assertNotNull("failure - connection should not be null", conn); + } + } catch (SQLException e) { + e.printStackTrace(); + } + } + + @Test + public void testConnectWithConfigFile() { + String jdbcUrl = "jdbc:TAOS://:/test?user=root&password=taosdata"; + Properties connProps = new Properties(); + connProps.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8"); + connProps.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8"); + connProps.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8"); + try { + if (islibLoaded) { + Connection conn = DriverManager.getConnection(jdbcUrl, connProps); + assertNotNull("failure - connection should not be null", conn); + } + } catch (SQLException e) { + e.printStackTrace(); + } + } + + @Test(expected = SQLException.class) + public void testAcceptsURL() throws SQLException { + Driver driver = new TSDBDriver(); + for (String url : validURLs) { + assertTrue("failure - acceptsURL(\" " + url + " \") should be true", driver.acceptsURL(url)); + } + new TSDBDriver().acceptsURL(null); + fail("acceptsURL throws exception when parameter is null"); + } + + @Test + public void testGetPropertyInfo() throws SQLException { + Driver driver = new TSDBDriver(); + final String url = "jdbc:TAOS://localhost:6030/log?user=root&password=taosdata"; + Properties connProps = new Properties(); + connProps.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8"); + connProps.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8"); + connProps.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8"); + DriverPropertyInfo[] propertyInfo = driver.getPropertyInfo(url, connProps); + for (DriverPropertyInfo info : propertyInfo) { + if (info.name.equals(TSDBDriver.PROPERTY_KEY_HOST)) + assertEquals("failure - host should be localhost", "localhost", info.value); + if (info.name.equals(TSDBDriver.PROPERTY_KEY_PORT)) + assertEquals("failure - port should be 6030", "6030", info.value); + if (info.name.equals(TSDBDriver.PROPERTY_KEY_DBNAME)) + assertEquals("failure - dbname should be test", "log", info.value); + if (info.name.equals(TSDBDriver.PROPERTY_KEY_USER)) + assertEquals("failure - user should be root", "root", info.value); + if (info.name.equals(TSDBDriver.PROPERTY_KEY_PASSWORD)) + assertEquals("failure - password should be root", "taosdata", info.value); + } + } + + @Test + public void testGetMajorVersion() throws SQLException { + Driver driver = new TSDBDriver(); + assertEquals("failure - getMajorVersion should be 2", 2, driver.getMajorVersion()); + } + + @Test + public void testGetMinorVersion() { + Driver driver = new TSDBDriver(); + assertEquals("failure - getMinorVersion should be 0", 0, driver.getMinorVersion()); + } + + @Test + public void testJdbcCompliant() { +// assertFalse("failure - jdbcCompliant should be false", new TSDBDriver().jdbcCompliant()); + } + + @Test + public void testGetParentLogger() throws SQLFeatureNotSupportedException { + assertNull("failure - getParentLogger should be be null", new TSDBDriver().getParentLogger()); } } \ No newline at end of file From 8a1c2b240a53144c61bc6f277b4d65946fc1bb34 Mon Sep 17 00:00:00 2001 From: zyyang Date: Wed, 4 Nov 2020 10:34:15 +0800 Subject: [PATCH 02/19] change --- .../jdbc/src/test/java/com/taosdata/jdbc/TSDBDriverTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 dc52fdadb7..d5264e57d7 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 @@ -44,10 +44,10 @@ public class TSDBDriverTest { return; try { System.loadLibrary(libPath); + islibLoaded = true; } catch (UnsatisfiedLinkError error) { islibLoaded = false; } - islibLoaded = true; } @Test From e7bb764bd303855cc84e8b694205325a62a382e3 Mon Sep 17 00:00:00 2001 From: zyyang Date: Wed, 4 Nov 2020 14:23:42 +0800 Subject: [PATCH 03/19] change again --- .../com/taosdata/jdbc/TSDBDriverTest.java | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) 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 d5264e57d7..677ed8b7a2 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 @@ -1,6 +1,5 @@ package com.taosdata.jdbc; -import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; @@ -30,22 +29,27 @@ public class TSDBDriverTest { @BeforeClass public static void before() { String osName = System.getProperty("os.name").toLowerCase(); - String libPath = null; - switch (osName) { - case "linux": - libPath = "/usr/lib/libtaos.so"; - break; - case "windows": - libPath = "C:\\TDengine\\driver\\taos.dll"; - break; - default: - } - if (libPath == null) + if (!osName.equals("linux") && !osName.equals("windows")) { + islibLoaded = false; return; + } +// String libPath = null; +// switch (osName) { +// case "linux": +// libPath = "/usr/lib/libtaos.so"; +// break; +// case "windows": +// libPath = "C:\\TDengine\\driver\\taos.dll"; +// break; +// default: +// } +// if (libPath == null) +// return; try { - System.loadLibrary(libPath); + System.loadLibrary("taos"); islibLoaded = true; } catch (UnsatisfiedLinkError error) { + System.out.println("load tdengine lib failed."); islibLoaded = false; } } From ae26d9ac43b32cc390a3571be22b9458c9617c94 Mon Sep 17 00:00:00 2001 From: zyyang Date: Wed, 4 Nov 2020 14:48:26 +0800 Subject: [PATCH 04/19] change --- .../com/taosdata/jdbc/TSDBDriverTest.java | 35 ++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) 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 677ed8b7a2..f7a200b33d 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,6 +3,9 @@ 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; @@ -25,6 +28,7 @@ public class TSDBDriverTest { "jdbc:TAOS://localhost:0/?user=root&password=taosdata" }; private static boolean islibLoaded; + private static boolean isTaosdActived; @BeforeClass public static void before() { @@ -52,6 +56,21 @@ public class TSDBDriverTest { System.out.println("load tdengine lib failed."); islibLoaded = false; } + + try { + Process exec = Runtime.getRuntime().exec("ps -ef | grep taosd | grep -v \"grep\""); + BufferedReader reader = new BufferedReader(new InputStreamReader(exec.getInputStream())); + int lineCnt = 0; + while (reader.readLine() != null) { + lineCnt++; + } + if (lineCnt > 0) + isTaosdActived = true; + else + isTaosdActived = false; + } catch (IOException e) { + isTaosdActived = false; + } } @Test @@ -96,7 +115,6 @@ public class TSDBDriverTest { @Test public void testConnectWithJdbcURL() { - final String url = "jdbc:TAOS://localhost:3306/log?user=root&password=taosdata"; try { if (islibLoaded) { @@ -104,7 +122,10 @@ public class TSDBDriverTest { assertNotNull("failure - connection should not be null", conn); } } catch (SQLException e) { - e.printStackTrace(); + if (!isTaosdActived) + assertEquals("failure - should throw SQLException", "TDengine Error: Unable to establish connection", e.getMessage()); + else + fail("failure - should not throw Exception"); } } @@ -121,7 +142,10 @@ public class TSDBDriverTest { assertNotNull("failure - connection should not be null", conn); } } catch (SQLException e) { - e.printStackTrace(); + if (!isTaosdActived) + assertEquals("failure - should throw SQLException", "TDengine Error: Unable to establish connection", e.getMessage()); + else + fail("failure - should not throw Exception"); } } @@ -138,7 +162,10 @@ public class TSDBDriverTest { assertNotNull("failure - connection should not be null", conn); } } catch (SQLException e) { - e.printStackTrace(); + if (!isTaosdActived) + assertEquals("failure - should throw SQLException", "TDengine Error: Unable to establish connection", e.getMessage()); + else + fail("failure - should not throw Exception"); } } From 97945d8c688dfb9e3ad788cf0a39ca598479913d Mon Sep 17 00:00:00 2001 From: zyyang Date: Wed, 4 Nov 2020 14:51:29 +0800 Subject: [PATCH 05/19] change --- .../jdbc/src/test/java/com/taosdata/jdbc/TSDBDriverTest.java | 1 + 1 file changed, 1 insertion(+) 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 f7a200b33d..1b8f2f04d8 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 @@ -159,6 +159,7 @@ public class TSDBDriverTest { try { if (islibLoaded) { Connection conn = DriverManager.getConnection(jdbcUrl, connProps); + System.out.println(conn); assertNotNull("failure - connection should not be null", conn); } } catch (SQLException e) { From c6f6cd12bea98cf4cf921ef9447eacd272042e36 Mon Sep 17 00:00:00 2001 From: zyyang Date: Wed, 4 Nov 2020 18:57:10 +0800 Subject: [PATCH 06/19] change --- .../com/taosdata/jdbc/TSDBDriverTest.java | 36 ++++++++----------- 1 file changed, 15 insertions(+), 21 deletions(-) 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 1b8f2f04d8..8739c88e79 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 @@ -37,18 +37,6 @@ public class TSDBDriverTest { islibLoaded = false; return; } -// String libPath = null; -// switch (osName) { -// case "linux": -// libPath = "/usr/lib/libtaos.so"; -// break; -// case "windows": -// libPath = "C:\\TDengine\\driver\\taos.dll"; -// break; -// default: -// } -// if (libPath == null) -// return; try { System.loadLibrary("taos"); islibLoaded = true; @@ -58,16 +46,21 @@ public class TSDBDriverTest { } try { - Process exec = Runtime.getRuntime().exec("ps -ef | grep taosd | grep -v \"grep\""); - BufferedReader reader = new BufferedReader(new InputStreamReader(exec.getInputStream())); - int lineCnt = 0; - while (reader.readLine() != null) { - lineCnt++; - } - if (lineCnt > 0) - isTaosdActived = true; - else + if (osName.equals("linux")) { + 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; + else + isTaosdActived = false; + } else { isTaosdActived = false; + } } catch (IOException e) { isTaosdActived = false; } @@ -146,6 +139,7 @@ public class TSDBDriverTest { assertEquals("failure - should throw SQLException", "TDengine Error: Unable to establish connection", e.getMessage()); else fail("failure - should not throw Exception"); + e.printStackTrace(); } } From 8b2e3c82510b8ae3e98c7d45836493bd59ab1e28 Mon Sep 17 00:00:00 2001 From: zyyang Date: Wed, 4 Nov 2020 19:01:05 +0800 Subject: [PATCH 07/19] change --- .../jdbc/src/test/java/com/taosdata/jdbc/TSDBDriverTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 8739c88e79..69fdeba110 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 @@ -108,7 +108,7 @@ public class TSDBDriverTest { @Test public void testConnectWithJdbcURL() { - final String url = "jdbc:TAOS://localhost:3306/log?user=root&password=taosdata"; + final String url = "jdbc:TAOS://localhost:6030/log?user=root&password=taosdata"; try { if (islibLoaded) { Connection conn = DriverManager.getConnection(url); @@ -124,7 +124,7 @@ public class TSDBDriverTest { @Test public void testConnectWithProperties() { - final String jdbcUrl = "jdbc:TAOS://localhost.com:6030/test?user=root&password=taosdata"; + final String jdbcUrl = "jdbc:TAOS://localhost:6030/test?user=root&password=taosdata"; Properties connProps = new Properties(); connProps.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8"); connProps.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8"); From e21088d57d23dc13017f69fd1416018b456cf3ea Mon Sep 17 00:00:00 2001 From: zyyang Date: Wed, 4 Nov 2020 19:09:50 +0800 Subject: [PATCH 08/19] change --- .../test/java/com/taosdata/jdbc/TSDBDriverTest.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) 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 69fdeba110..e34531bddf 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 @@ -115,6 +115,7 @@ public class TSDBDriverTest { assertNotNull("failure - connection should not be null", conn); } } catch (SQLException e) { + e.printStackTrace(); if (!isTaosdActived) assertEquals("failure - should throw SQLException", "TDengine Error: Unable to establish connection", e.getMessage()); else @@ -135,11 +136,13 @@ public class TSDBDriverTest { assertNotNull("failure - connection should not be null", conn); } } catch (SQLException e) { - if (!isTaosdActived) - assertEquals("failure - should throw SQLException", "TDengine Error: Unable to establish connection", e.getMessage()); - else - fail("failure - should not throw Exception"); e.printStackTrace(); + if (!isTaosdActived) { + assertEquals("failure - should throw SQLException", "TDengine Error: Unable to establish connection", e.getMessage()); + } + else { + fail("failure - should not throw Exception"); + } } } @@ -157,6 +160,7 @@ public class TSDBDriverTest { assertNotNull("failure - connection should not be null", conn); } } catch (SQLException e) { + e.printStackTrace(); if (!isTaosdActived) assertEquals("failure - should throw SQLException", "TDengine Error: Unable to establish connection", e.getMessage()); else From 60d04f8121b0ac92b4040fca4bb12223e6f2fbc5 Mon Sep 17 00:00:00 2001 From: zyyang Date: Wed, 4 Nov 2020 19:12:57 +0800 Subject: [PATCH 09/19] change --- .../src/test/java/com/taosdata/jdbc/TSDBDriverTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 e34531bddf..db05c57b58 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 @@ -125,7 +125,7 @@ public class TSDBDriverTest { @Test public void testConnectWithProperties() { - final String jdbcUrl = "jdbc:TAOS://localhost:6030/test?user=root&password=taosdata"; + final String jdbcUrl = "jdbc:TAOS://localhost:6030/log?user=root&password=taosdata"; Properties connProps = new Properties(); connProps.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8"); connProps.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8"); @@ -138,9 +138,9 @@ public class TSDBDriverTest { } catch (SQLException e) { e.printStackTrace(); if (!isTaosdActived) { + System.out.println(e.getMessage()); assertEquals("failure - should throw SQLException", "TDengine Error: Unable to establish connection", e.getMessage()); - } - else { + } else { fail("failure - should not throw Exception"); } } @@ -148,7 +148,7 @@ public class TSDBDriverTest { @Test public void testConnectWithConfigFile() { - String jdbcUrl = "jdbc:TAOS://:/test?user=root&password=taosdata"; + String jdbcUrl = "jdbc:TAOS://:/log?user=root&password=taosdata"; Properties connProps = new Properties(); connProps.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8"); connProps.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8"); From dd2732ea68658996a7f0815ae79e0fc068d0a2c9 Mon Sep 17 00:00:00 2001 From: zyyang Date: Wed, 4 Nov 2020 19:15:27 +0800 Subject: [PATCH 10/19] change --- .../jdbc/src/test/java/com/taosdata/jdbc/TSDBDriverTest.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) 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 db05c57b58..8718c3e7da 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 @@ -136,10 +136,8 @@ public class TSDBDriverTest { assertNotNull("failure - connection should not be null", conn); } } catch (SQLException e) { - e.printStackTrace(); if (!isTaosdActived) { - System.out.println(e.getMessage()); - assertEquals("failure - should throw SQLException", "TDengine Error: Unable to establish connection", e.getMessage()); + assertEquals("failure - should throw SQLException", SQLException.class, e.getClass()); } else { fail("failure - should not throw Exception"); } From 62b857e4f8da9f4d0b8f2ae6d1a281e1051d6b25 Mon Sep 17 00:00:00 2001 From: zyyang Date: Thu, 5 Nov 2020 11:31:40 +0800 Subject: [PATCH 11/19] change --- .../com/taosdata/jdbc/TSDBDriverTest.java | 189 ++++++++---------- 1 file changed, 84 insertions(+), 105 deletions(-) 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 8718c3e7da..627daf76ac 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 @@ -13,7 +13,7 @@ import static org.junit.Assert.*; public class TSDBDriverTest { - private static String[] validURLs = { + private static final String[] validURLs = { "jdbc:TAOS://localhost:0", "jdbc:TAOS://localhost", "jdbc:TAOS://localhost:6030/test", @@ -27,45 +27,99 @@ public class TSDBDriverTest { "jdbc:TAOS://:/test", "jdbc:TAOS://localhost:0/?user=root&password=taosdata" }; - private static boolean islibLoaded; + private static boolean islibLoaded = false; private static boolean isTaosdActived; @BeforeClass public static void before() { String osName = System.getProperty("os.name").toLowerCase(); - if (!osName.equals("linux") && !osName.equals("windows")) { - islibLoaded = false; + 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."); - islibLoaded = false; + error.printStackTrace(); } - + // check taosd is activated try { - if (osName.equals("linux")) { - 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; - else - isTaosdActived = false; - } else { - isTaosdActived = false; + 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) { - isTaosdActived = false; + e.printStackTrace(); } } + + @Test + public void testConnectWithJdbcURL() { + final String url = "jdbc:TAOS://localhost:6030/log?user=root&password=taosdata"; + try { + if (islibLoaded && isTaosdActived) { + Connection conn = DriverManager.getConnection(url); + assertNotNull("failure - connection should not be null", conn); + } + } catch (SQLException e) { + e.printStackTrace(); + fail("failure - should not throw Exception"); + } + } + + @Test + public void testConnectWithProperties() { + final String jdbcUrl = "jdbc:TAOS://localhost:6030/log?user=root&password=taosdata"; + Properties connProps = new Properties(); + connProps.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8"); + connProps.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8"); + connProps.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8"); + try { + if (islibLoaded && isTaosdActived) { + Connection conn = DriverManager.getConnection(jdbcUrl, connProps); + assertNotNull("failure - connection should not be null", conn); + } + } catch (SQLException e) { + e.printStackTrace(); + fail("failure - should not throw Exception"); + } + } + + @Test + public void testConnectWithConfigFile() { + String jdbcUrl = "jdbc:TAOS://:/log?user=root&password=taosdata"; + Properties connProps = new Properties(); + connProps.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8"); + connProps.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8"); + connProps.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8"); + try { + if (islibLoaded && isTaosdActived) { + Connection conn = DriverManager.getConnection(jdbcUrl, connProps); + assertNotNull("failure - connection should not be null", conn); + } + } catch (SQLException e) { + e.printStackTrace(); + fail("failure - should not throw Exception"); + } + } + + @Test(expected = SQLException.class) + public void testAcceptsURL() throws SQLException { + Driver driver = new TSDBDriver(); + for (String url : validURLs) { + assertTrue("failure - acceptsURL(\" " + url + " \") should be true", driver.acceptsURL(url)); + } + driver.acceptsURL(null); + fail("acceptsURL throws exception when parameter is null"); + } + @Test public void testParseURL() { TSDBDriver driver = new TSDBDriver(); @@ -85,7 +139,7 @@ public class TSDBDriverTest { actual = driver.parseURL(url, config); assertEquals("failure - host should be 127.0.0.1", "127.0.0.1", actual.getProperty("host")); assertEquals("failure - port should be 0", "0", actual.get("port")); - assertEquals("failure - dbname should be null", null, actual.get("dbname")); + assertNull("failure - dbname should be null", actual.get("dbname")); url = "jdbc:TAOS://127.0.0.1:0/db"; config = new Properties(); @@ -101,79 +155,9 @@ public class TSDBDriverTest { actual = driver.parseURL(url, config); assertEquals("failure - user should be root", "root", actual.getProperty("user")); assertEquals("failure - password should be taosdata", "taosdata", actual.getProperty("password")); - assertEquals("failure - host should be null", null, actual.getProperty("host")); - assertEquals("failure - port should be null", null, actual.getProperty("port")); - assertEquals("failure - dbname should be null", null, actual.getProperty("dbname")); - } - - @Test - public void testConnectWithJdbcURL() { - final String url = "jdbc:TAOS://localhost:6030/log?user=root&password=taosdata"; - try { - if (islibLoaded) { - Connection conn = DriverManager.getConnection(url); - assertNotNull("failure - connection should not be null", conn); - } - } catch (SQLException e) { - e.printStackTrace(); - if (!isTaosdActived) - assertEquals("failure - should throw SQLException", "TDengine Error: Unable to establish connection", e.getMessage()); - else - fail("failure - should not throw Exception"); - } - } - - @Test - public void testConnectWithProperties() { - final String jdbcUrl = "jdbc:TAOS://localhost:6030/log?user=root&password=taosdata"; - Properties connProps = new Properties(); - connProps.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8"); - connProps.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8"); - connProps.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8"); - try { - if (islibLoaded) { - Connection conn = DriverManager.getConnection(jdbcUrl, connProps); - assertNotNull("failure - connection should not be null", conn); - } - } catch (SQLException e) { - if (!isTaosdActived) { - assertEquals("failure - should throw SQLException", SQLException.class, e.getClass()); - } else { - fail("failure - should not throw Exception"); - } - } - } - - @Test - public void testConnectWithConfigFile() { - String jdbcUrl = "jdbc:TAOS://:/log?user=root&password=taosdata"; - Properties connProps = new Properties(); - connProps.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8"); - connProps.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8"); - connProps.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8"); - try { - if (islibLoaded) { - Connection conn = DriverManager.getConnection(jdbcUrl, connProps); - System.out.println(conn); - assertNotNull("failure - connection should not be null", conn); - } - } catch (SQLException e) { - e.printStackTrace(); - if (!isTaosdActived) - assertEquals("failure - should throw SQLException", "TDengine Error: Unable to establish connection", e.getMessage()); - else - fail("failure - should not throw Exception"); - } - } - - @Test(expected = SQLException.class) - public void testAcceptsURL() throws SQLException { - Driver driver = new TSDBDriver(); - for (String url : validURLs) { - assertTrue("failure - acceptsURL(\" " + url + " \") should be true", driver.acceptsURL(url)); - } - new TSDBDriver().acceptsURL(null); - fail("acceptsURL throws exception when parameter is null"); + assertNull("failure - host should be null", actual.getProperty("host")); + assertNull("failure - port should be null", actual.getProperty("port")); + assertNull("failure - dbname should be null", actual.getProperty("dbname")); } @Test @@ -181,9 +165,6 @@ public class TSDBDriverTest { Driver driver = new TSDBDriver(); final String url = "jdbc:TAOS://localhost:6030/log?user=root&password=taosdata"; Properties connProps = new Properties(); - connProps.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8"); - connProps.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8"); - connProps.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8"); DriverPropertyInfo[] propertyInfo = driver.getPropertyInfo(url, connProps); for (DriverPropertyInfo info : propertyInfo) { if (info.name.equals(TSDBDriver.PROPERTY_KEY_HOST)) @@ -200,20 +181,18 @@ public class TSDBDriverTest { } @Test - public void testGetMajorVersion() throws SQLException { - Driver driver = new TSDBDriver(); - assertEquals("failure - getMajorVersion should be 2", 2, driver.getMajorVersion()); + public void testGetMajorVersion() { + assertEquals("failure - getMajorVersion should be 2", 2, new TSDBDriver().getMajorVersion()); } @Test public void testGetMinorVersion() { - Driver driver = new TSDBDriver(); - assertEquals("failure - getMinorVersion should be 0", 0, driver.getMinorVersion()); + assertEquals("failure - getMinorVersion should be 0", 0, new TSDBDriver().getMinorVersion()); } @Test public void testJdbcCompliant() { -// assertFalse("failure - jdbcCompliant should be false", new TSDBDriver().jdbcCompliant()); + assertFalse("failure - jdbcCompliant should be false", new TSDBDriver().jdbcCompliant()); } @Test From b49b361fdf302d9f6967bc63329c17c7d56e1c01 Mon Sep 17 00:00:00 2001 From: zyyang Date: Thu, 5 Nov 2020 13:47:55 +0800 Subject: [PATCH 12/19] change --- .../jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java | 8 ++++++++ .../src/test/java/com/taosdata/jdbc/TSDBDriverTest.java | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java index cd45048a77..63c42ca399 100755 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java @@ -249,6 +249,7 @@ public class TSDBDriver implements java.sql.Driver { return null; // parse properties + String urlForMeta = url; int beginningOfSlashes = url.indexOf("//"); int index = url.indexOf("?"); if (index != -1) { @@ -271,6 +272,10 @@ public class TSDBDriver implements java.sql.Driver { } } } + // parse Product Name + String dbProductName = url.substring(0, beginningOfSlashes); + dbProductName = dbProductName.substring(dbProductName.indexOf(":") + 1); + dbProductName = dbProductName.substring(0, dbProductName.indexOf(":")); // parse dbname url = url.substring(beginningOfSlashes + 2); int indexOfSlash = url.indexOf("/"); @@ -291,6 +296,9 @@ public class TSDBDriver implements java.sql.Driver { if (url != null && url.length() > 0 && url.trim().length() > 0) { urlProps.setProperty(TSDBDriver.PROPERTY_KEY_HOST, url); } + + this.dbMetaData = new TSDBDatabaseMetaData(dbProductName, urlForMeta, urlProps.getProperty(TSDBDriver.PROPERTY_KEY_USER)); + /* String urlForMeta = url; String dbProductName = url.substring(url.indexOf(":") + 1); 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 627daf76ac..b4a7af127a 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 @@ -57,6 +57,12 @@ public class TSDBDriverTest { } catch (IOException e) { e.printStackTrace(); } + + try { + Class.forName("com.taosdata.jdbc.TSDBDriver"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } } From c62dec736af3a9ab0ff95401f64b35613a1b9661 Mon Sep 17 00:00:00 2001 From: zyyang Date: Thu, 5 Nov 2020 13:56:32 +0800 Subject: [PATCH 13/19] change --- .../com/taosdata/jdbc/TSDBDriverTest.java | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) 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 b4a7af127a..f821266602 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 @@ -1,5 +1,6 @@ package com.taosdata.jdbc; +import org.junit.After; import org.junit.BeforeClass; import org.junit.Test; @@ -30,6 +31,8 @@ public class TSDBDriverTest { private static boolean islibLoaded = false; private static boolean isTaosdActived; + private Connection conn; + @BeforeClass public static void before() { String osName = System.getProperty("os.name").toLowerCase(); @@ -71,7 +74,7 @@ public class TSDBDriverTest { final String url = "jdbc:TAOS://localhost:6030/log?user=root&password=taosdata"; try { if (islibLoaded && isTaosdActived) { - Connection conn = DriverManager.getConnection(url); + conn = DriverManager.getConnection(url); assertNotNull("failure - connection should not be null", conn); } } catch (SQLException e) { @@ -89,7 +92,7 @@ public class TSDBDriverTest { connProps.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8"); try { if (islibLoaded && isTaosdActived) { - Connection conn = DriverManager.getConnection(jdbcUrl, connProps); + conn = DriverManager.getConnection(jdbcUrl, connProps); assertNotNull("failure - connection should not be null", conn); } } catch (SQLException e) { @@ -107,7 +110,7 @@ public class TSDBDriverTest { connProps.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8"); try { if (islibLoaded && isTaosdActived) { - Connection conn = DriverManager.getConnection(jdbcUrl, connProps); + conn = DriverManager.getConnection(jdbcUrl, connProps); assertNotNull("failure - connection should not be null", conn); } } catch (SQLException e) { @@ -205,4 +208,22 @@ public class TSDBDriverTest { public void testGetParentLogger() throws SQLFeatureNotSupportedException { assertNull("failure - getParentLogger should be be null", new TSDBDriver().getParentLogger()); } + + @After + public void after() { + Statement statement = null; + try { + statement = conn.createStatement(); + ResultSet resultSet = statement.executeQuery("show databases"); + ResultSetMetaData metaData = resultSet.getMetaData(); + while (resultSet.next()) { + for (int i = 1; i <= metaData.getColumnCount(); i++) { + System.out.print(metaData.getColumnLabel(i) + " : " + resultSet.getString(i) + "\t"); + } + System.out.println(); + } + } catch (SQLException e) { + e.printStackTrace(); + } + } } \ No newline at end of file From ce2287ef7fbf99c578e7caa9642f7cc7534bcb50 Mon Sep 17 00:00:00 2001 From: zyyang Date: Thu, 5 Nov 2020 13:58:28 +0800 Subject: [PATCH 14/19] change --- .../src/test/java/com/taosdata/jdbc/TSDBDriverTest.java | 9 +++++++++ 1 file changed, 9 insertions(+) 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 f821266602..dbfc68c100 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 @@ -224,6 +224,15 @@ public class TSDBDriverTest { } } catch (SQLException e) { e.printStackTrace(); + } finally { + try { + if (statement != null) + statement.close(); + if (conn != null) + conn.close(); + } catch (SQLException e) { + e.printStackTrace(); + } } } } \ No newline at end of file From be722bdf48b314ce65237765e5b5aad2d81c97c7 Mon Sep 17 00:00:00 2001 From: zyyang Date: Thu, 5 Nov 2020 14:00:40 +0800 Subject: [PATCH 15/19] change --- .../jdbc/src/test/java/com/taosdata/jdbc/TSDBDriverTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 dbfc68c100..cdda5ea894 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 @@ -113,6 +113,7 @@ public class TSDBDriverTest { conn = DriverManager.getConnection(jdbcUrl, connProps); assertNotNull("failure - connection should not be null", conn); } + printRs(); } catch (SQLException e) { e.printStackTrace(); fail("failure - should not throw Exception"); @@ -209,8 +210,7 @@ public class TSDBDriverTest { assertNull("failure - getParentLogger should be be null", new TSDBDriver().getParentLogger()); } - @After - public void after() { + public void printRs() { Statement statement = null; try { statement = conn.createStatement(); From 7cbbc0733e0a9e6a8e5af4397aaafcfa9a6598dc Mon Sep 17 00:00:00 2001 From: zyyang Date: Thu, 5 Nov 2020 14:22:19 +0800 Subject: [PATCH 16/19] change --- .../src/main/java/com/taosdata/jdbc/TSDBConnection.java | 3 ++- .../main/java/com/taosdata/jdbc/TSDBDatabaseMetaData.java | 2 +- .../src/test/java/com/taosdata/jdbc/TSDBDriverTest.java | 8 +++++++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBConnection.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBConnection.java index ac0e4eb84a..57230ee65b 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBConnection.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBConnection.java @@ -197,12 +197,13 @@ public class TSDBConnection implements Connection { } public SQLWarning getWarnings() throws SQLException { + //todo: implement getWarnings according to the warning messages returned from TDengine throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG); } public void clearWarnings() throws SQLException { // left blank to support HikariCP connection - //todo: implement getWarnings according to the warning messages returned from TDengine + //todo: implement clearWarnings according to the warning messages returned from TDengine } public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException { diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDatabaseMetaData.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDatabaseMetaData.java index c1d9d2af8e..f4dee67adf 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDatabaseMetaData.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDatabaseMetaData.java @@ -96,7 +96,7 @@ public class TSDBDatabaseMetaData implements java.sql.DatabaseMetaData { } public int getDriverMajorVersion() { - return 0; + return 2; } public int getDriverMinorVersion() { 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 cdda5ea894..d254b242af 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 @@ -8,6 +8,7 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.sql.*; +import java.util.Enumeration; import java.util.Properties; import static org.junit.Assert.*; @@ -68,7 +69,6 @@ public class TSDBDriverTest { } } - @Test public void testConnectWithJdbcURL() { final String url = "jdbc:TAOS://localhost:6030/log?user=root&password=taosdata"; @@ -114,6 +114,12 @@ public class TSDBDriverTest { assertNotNull("failure - connection should not be null", conn); } printRs(); + Properties clientInfo = conn.getClientInfo(); + Enumeration propertyNames = clientInfo.propertyNames(); + while (propertyNames.hasMoreElements()) { + String name = (String) propertyNames.nextElement(); + System.out.println(name + " : " + clientInfo.getProperty(name)); + } } catch (SQLException e) { e.printStackTrace(); fail("failure - should not throw Exception"); From bc94bec5c3be69e75c2a34ebc00ba2f5dc0e3192 Mon Sep 17 00:00:00 2001 From: zyyang Date: Thu, 5 Nov 2020 14:26:16 +0800 Subject: [PATCH 17/19] change --- .../jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java index 63c42ca399..d5f8510320 100755 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java @@ -168,6 +168,13 @@ public class TSDBDriver implements java.sql.Driver { } try { + + Enumeration propertyNames = props.propertyNames(); + while (propertyNames.hasMoreElements()) { + Object name = propertyNames.nextElement(); + System.out.println(name + " : " + props.get(name)); + } + TSDBJNIConnector.init((String) props.get(PROPERTY_KEY_CONFIG_DIR), (String) props.get(PROPERTY_KEY_LOCALE), (String) props.get(PROPERTY_KEY_CHARSET), From 5e4b45728055f8e2e179810a07d120bd2c3d2eb6 Mon Sep 17 00:00:00 2001 From: zyyang Date: Thu, 5 Nov 2020 14:40:47 +0800 Subject: [PATCH 18/19] change --- .../src/main/java/com/taosdata/jdbc/TSDBConnection.java | 1 - .../jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java | 7 ------- .../src/test/java/com/taosdata/jdbc/TSDBDriverTest.java | 7 ------- 3 files changed, 15 deletions(-) diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBConnection.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBConnection.java index 57230ee65b..294544ed47 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBConnection.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBConnection.java @@ -52,7 +52,6 @@ public class TSDBConnection implements Connection { public TSDBConnection(Properties info, TSDBDatabaseMetaData meta) throws SQLException { this.dbMetaData = meta; - connect(info.getProperty(TSDBDriver.PROPERTY_KEY_HOST), Integer.parseInt(info.getProperty(TSDBDriver.PROPERTY_KEY_PORT, "0")), info.getProperty(TSDBDriver.PROPERTY_KEY_DBNAME), info.getProperty(TSDBDriver.PROPERTY_KEY_USER), diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java index d5f8510320..63c42ca399 100755 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java @@ -168,13 +168,6 @@ public class TSDBDriver implements java.sql.Driver { } try { - - Enumeration propertyNames = props.propertyNames(); - while (propertyNames.hasMoreElements()) { - Object name = propertyNames.nextElement(); - System.out.println(name + " : " + props.get(name)); - } - TSDBJNIConnector.init((String) props.get(PROPERTY_KEY_CONFIG_DIR), (String) props.get(PROPERTY_KEY_LOCALE), (String) props.get(PROPERTY_KEY_CHARSET), 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 d254b242af..e7425a1569 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 @@ -113,13 +113,6 @@ public class TSDBDriverTest { conn = DriverManager.getConnection(jdbcUrl, connProps); assertNotNull("failure - connection should not be null", conn); } - printRs(); - Properties clientInfo = conn.getClientInfo(); - Enumeration propertyNames = clientInfo.propertyNames(); - while (propertyNames.hasMoreElements()) { - String name = (String) propertyNames.nextElement(); - System.out.println(name + " : " + clientInfo.getProperty(name)); - } } catch (SQLException e) { e.printStackTrace(); fail("failure - should not throw Exception"); From 42add31acd4355510e48b8404abe841fff4d62d7 Mon Sep 17 00:00:00 2001 From: zyyang Date: Thu, 5 Nov 2020 14:44:01 +0800 Subject: [PATCH 19/19] change --- .../com/taosdata/jdbc/TSDBDriverTest.java | 27 ------------------- 1 file changed, 27 deletions(-) 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 e7425a1569..8adcdefb29 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 @@ -1,6 +1,5 @@ package com.taosdata.jdbc; -import org.junit.After; import org.junit.BeforeClass; import org.junit.Test; @@ -8,7 +7,6 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.sql.*; -import java.util.Enumeration; import java.util.Properties; import static org.junit.Assert.*; @@ -209,29 +207,4 @@ public class TSDBDriverTest { assertNull("failure - getParentLogger should be be null", new TSDBDriver().getParentLogger()); } - public void printRs() { - Statement statement = null; - try { - statement = conn.createStatement(); - ResultSet resultSet = statement.executeQuery("show databases"); - ResultSetMetaData metaData = resultSet.getMetaData(); - while (resultSet.next()) { - for (int i = 1; i <= metaData.getColumnCount(); i++) { - System.out.print(metaData.getColumnLabel(i) + " : " + resultSet.getString(i) + "\t"); - } - System.out.println(); - } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - try { - if (statement != null) - statement.close(); - if (conn != null) - conn.close(); - } catch (SQLException e) { - e.printStackTrace(); - } - } - } } \ No newline at end of file