提交 16069460 编写于 作者: Z zyyang

[TD-2672]<test>: optimized the JDBC test cases

上级 c3755720
...@@ -100,7 +100,6 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat ...@@ -100,7 +100,6 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
* order to process those supported SQLs. * order to process those supported SQLs.
*/ */
private void preprocessSql() { private void preprocessSql() {
/***** For processing some of Spark SQLs*****/ /***** For processing some of Spark SQLs*****/
// should replace it first // should replace it first
this.rawSql = this.rawSql.replaceAll("or (.*) is null", ""); this.rawSql = this.rawSql.replaceAll("or (.*) is null", "");
...@@ -149,7 +148,6 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat ...@@ -149,7 +148,6 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
rawSql = rawSql.replace(matcher.group(1), tableFullName); rawSql = rawSql.replace(matcher.group(1), tableFullName);
} }
/***** for inner queries *****/ /***** for inner queries *****/
} }
/** /**
...@@ -196,7 +194,7 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat ...@@ -196,7 +194,7 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
@Override @Override
public void setNull(int parameterIndex, int sqlType) throws SQLException { public void setNull(int parameterIndex, int sqlType) throws SQLException {
setObject(parameterIndex, new String("NULL")); setObject(parameterIndex, "NULL");
} }
@Override @Override
......
...@@ -52,12 +52,18 @@ public class TSDBStatement implements Statement { ...@@ -52,12 +52,18 @@ public class TSDBStatement implements Statement {
this.isClosed = false; this.isClosed = false;
} }
@Override
public <T> T unwrap(Class<T> iface) throws SQLException { public <T> T unwrap(Class<T> 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 { 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 { public ResultSet executeQuery(String sql) throws SQLException {
......
package com.taosdata.jdbc; package com.taosdata.jdbc;
import org.junit.AfterClass; import org.junit.*;
import org.junit.BeforeClass; import org.junit.runners.MethodSorters;
import org.junit.FixMethodOrder;
import org.junit.Test;
import java.sql.*; import java.sql.*;
import java.util.Properties; import java.util.Properties;
...@@ -11,14 +9,13 @@ import java.util.Properties; ...@@ -11,14 +9,13 @@ import java.util.Properties;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@FixMethodOrder() @FixMethodOrder(value = MethodSorters.NAME_ASCENDING)
public class PreparedStatementTest extends BaseTest { public class PreparedStatementTest {
static Connection connection = null; static Connection connection;
static PreparedStatement statement = null; static TSDBPreparedStatement statement;
static String dbName = "test"; static String dbName = "test";
static String tName = "t0"; static String tName = "t0";
static String host = "localhost"; static String host = "localhost";
static ResultSet resSet = null;
@BeforeClass @BeforeClass
public static void createConnection() throws SQLException { public static void createConnection() throws SQLException {
...@@ -28,19 +25,16 @@ public class PreparedStatementTest extends BaseTest { ...@@ -28,19 +25,16 @@ public class PreparedStatementTest extends BaseTest {
return; return;
} }
Properties properties = new 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_CHARSET, "UTF-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8"); properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8"); properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
connection = DriverManager.getConnection("jdbc:TAOS://" + host + ":0/", properties); connection = DriverManager.getConnection("jdbc:TAOS://" + host + ":0/", properties);
String sql = "drop database if exists " + dbName; String sql = "drop database if exists " + dbName;
statement = (TSDBPreparedStatement) connection.prepareStatement(sql); statement = (TSDBPreparedStatement) connection.prepareStatement(sql);
} }
@Test @Test
public void createTableAndQuery() throws SQLException { public void case001_createTableAndQuery() throws SQLException {
long ts = System.currentTimeMillis(); long ts = System.currentTimeMillis();
statement.executeUpdate("create database if not exists " + dbName); statement.executeUpdate("create database if not exists " + dbName);
...@@ -48,47 +42,40 @@ public class PreparedStatementTest extends BaseTest { ...@@ -48,47 +42,40 @@ public class PreparedStatementTest extends BaseTest {
statement.executeUpdate("insert into " + dbName + "." + tName + " values (" + ts + ", 1)"); statement.executeUpdate("insert into " + dbName + "." + tName + " values (" + ts + ", 1)");
PreparedStatement selectStatement = connection.prepareStatement("select * from " + dbName + "." + tName); PreparedStatement selectStatement = connection.prepareStatement("select * from " + dbName + "." + tName);
ResultSet resultSet = selectStatement.executeQuery(); ResultSet resultSet = selectStatement.executeQuery();
assertTrue(null != resultSet); assertTrue(null != resultSet);
boolean isClosed = statement.isClosed(); boolean isClosed = statement.isClosed();
assertEquals(false, isClosed); assertEquals(false, isClosed);
selectStatement.close();
} }
@Test @Test
public void testPreparedStatement() throws SQLException { public void case002_testPreparedStatement() throws SQLException {
long ts = System.currentTimeMillis() + 20000; 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(); int affectedRows = saveStatement.executeUpdate();
assertTrue(1 == affectedRows); assertTrue(1 == affectedRows);
saveStatement.close();
} }
@Test @Test
public void testSavedPreparedStatement() throws SQLException { public void case003_testSavedPreparedStatement() throws SQLException {
long ts = System.currentTimeMillis(); 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(1, ts + 10000);
saveStatement.setObject(2, 3); saveStatement.setObject(2, 3);
int rows = saveStatement.executeUpdate(); int rows = saveStatement.executeUpdate();
assertEquals(1, rows); assertEquals(1, rows);
saveStatement.close();
} }
@Test @Test
public void testUnsupport() { public void case004_testUnsupport() throws SQLException {
// if(null == resSet) { TSDBPreparedStatement tsdbStatement = statement;
// return;
// } Assert.assertNotNull(tsdbStatement.unwrap(TSDBPreparedStatement.class));
TSDBPreparedStatement tsdbStatement = (TSDBPreparedStatement) statement;
try {
tsdbStatement.unwrap(null);
} catch (SQLException e) {
}
try { try {
tsdbStatement.isWrapperFor(null); tsdbStatement.isWrapperFor(null);
} catch (SQLException e) { } catch (SQLException e) {
...@@ -180,6 +167,7 @@ public class PreparedStatementTest extends BaseTest { ...@@ -180,6 +167,7 @@ public class PreparedStatementTest extends BaseTest {
try { try {
tsdbStatement.closeOnCompletion(); tsdbStatement.closeOnCompletion();
} catch (SQLException e) { } catch (SQLException e) {
} }
try { try {
tsdbStatement.isCloseOnCompletion(); tsdbStatement.isCloseOnCompletion();
......
...@@ -10,9 +10,9 @@ import java.sql.SQLException; ...@@ -10,9 +10,9 @@ import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.Properties; import java.util.Properties;
public class SubscribeTest extends BaseTest { public class SubscribeTest {
Connection connection = null; Connection connection;
Statement statement = null; Statement statement;
String dbName = "test"; String dbName = "test";
String tName = "t0"; String tName = "t0";
String host = "localhost"; String host = "localhost";
......
package com.taosdata.jdbc;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class TSDBPreparedStatementTest {
private static final String host = "127.0.0.1";
private static Connection conn;
@Test
public void executeQuery() {
}
@Test
public void executeUpdate() {
}
@Test
public void setNull() {
}
@Test
public void setBoolean() {
}
@Test
public void setByte() {
}
@Test
public void setShort() {
}
@Test
public void setInt() {
}
@Test
public void setLong() {
}
@Test
public void setFloat() {
}
@Test
public void setDouble() {
}
@Test
public void setBigDecimal() {
}
@Test
public void setString() {
}
@Test
public void setBytes() {
}
@Test
public void setDate() {
}
@Test
public void setTime() {
}
@Test
public void setTimestamp() {
}
@Test
public void setAsciiStream() {
}
@Test
public void setUnicodeStream() {
}
@Test
public void setBinaryStream() {
}
@Test
public void clearParameters() {
}
@Test
public void setObject() {
}
@Test
public void execute() {
}
@Test
public void addBatch() {
}
@Test
public void setCharacterStream() {
}
@Test
public void setRef() {
}
@Test
public void setBlob() {
}
@Test
public void setClob() {
}
@Test
public void setArray() {
}
@Test
public void getMetaData() {
}
@Test
public void setURL() {
}
@Test
public void getParameterMetaData() {
}
@Test
public void setRowId() {
}
@Test
public void setNString() {
}
@Test
public void setNCharacterStream() {
}
@Test
public void setNClob() {
}
@Test
public void setSQLXML() {
}
@BeforeClass
public static void beforeClass() {
try {
Class.forName("com.taosdata.jdbc.rs.RestfulDriver");
conn = DriverManager.getConnection("jdbc:TAOS://" + host + ":6030/jdbc_test?user=root&password=taosdata");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
@AfterClass
public static void afterClass() {
try {
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册