diff --git a/databend-client/src/test/java/com/databend/client/TestClientIT.java b/databend-client/src/test/java/com/databend/client/TestClientIT.java index 25b6df5c..7bdeaaec 100644 --- a/databend-client/src/test/java/com/databend/client/TestClientIT.java +++ b/databend-client/src/test/java/com/databend/client/TestClientIT.java @@ -28,8 +28,10 @@ @Test(timeOut = 10000) public class TestClientIT { + static String port = System.getenv("DATABEND_CONN_PORT") != null ? System.getenv("DATABEND_CONN_PORT") : "8000"; + // please setup a local databend cluster before running this test, and create databend - private static final String DATABEND_HOST = "http://databend:databend@127.0.0.1:8000"; + private static final String DATABEND_HOST = "http://databend:databend@127.0.0.1:" + port; private static final String DATABASE = "default"; @Test(groups = {"it"}) diff --git a/databend-jdbc/src/test/java/com/databend/jdbc/TestBasicDriver.java b/databend-jdbc/src/test/java/com/databend/jdbc/TestBasicDriver.java index b8e71167..dbda69a5 100644 --- a/databend-jdbc/src/test/java/com/databend/jdbc/TestBasicDriver.java +++ b/databend-jdbc/src/test/java/com/databend/jdbc/TestBasicDriver.java @@ -12,7 +12,6 @@ import java.sql.Connection; import java.sql.Date; -import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; @@ -25,27 +24,11 @@ @Test(timeOut = 10000) public class TestBasicDriver { - private Connection createConnection() - throws SQLException { - String url = "jdbc:databend://localhost:8000"; - return DriverManager.getConnection(url, "databend", "databend"); - } - - private Connection createConnection(String database) throws SQLException { - String url = "jdbc:databend://localhost:8000/" + database; - return DriverManager.getConnection(url, "databend", "databend"); - } - - private Connection createConnection(String database, Properties p) throws SQLException { - String url = "jdbc:databend://localhost:8000/" + database; - return DriverManager.getConnection(url, p); - } - @BeforeTest public void setUp() throws SQLException { // create table - Connection c = createConnection(); + Connection c = Utils.createConnection(); c.createStatement().execute("drop database if exists test_basic_driver"); c.createStatement().execute("drop database if exists test_basic_driver_2"); c.createStatement().execute("create database test_basic_driver"); @@ -61,7 +44,7 @@ public void setUp() @Test(groups = {"IT"}) public void testBasic() throws SQLException { - try (Connection connection = createConnection()) { + try (Connection connection = Utils.createConnection()) { PaginationOptions p = connection.unwrap(DatabendConnection.class).getPaginationOptions(); Assert.assertEquals(p.getWaitTimeSecs(), PaginationOptions.getDefaultWaitTimeSec()); Assert.assertEquals(p.getMaxRowsInBuffer(), PaginationOptions.getDefaultMaxRowsInBuffer()); @@ -83,7 +66,7 @@ public void testBasic() @Test(groups = {"IT"}) public void testExecuteInvalidSql() { assertThrows(SQLException.class, () -> { - try (Connection connection = createConnection(); + try (Connection connection = Utils.createConnection(); Statement statement = connection.createStatement()) { statement.execute("create tabl xxx (a int, b varchar)"); } @@ -91,7 +74,7 @@ public void testExecuteInvalidSql() { } public void testSchema() { - try (Connection connection = createConnection()) { + try (Connection connection = Utils.createConnection()) { PaginationOptions p = connection.unwrap(DatabendConnection.class).getPaginationOptions(); Assert.assertEquals(p.getWaitTimeSecs(), PaginationOptions.getDefaultWaitTimeSec()); Assert.assertEquals(p.getMaxRowsInBuffer(), PaginationOptions.getDefaultMaxRowsInBuffer()); @@ -122,13 +105,13 @@ public void testCreateUserFunction() throws SQLException { " return i+k;\n" + "}\n" + "$$;"; - try (Connection connection = createConnection()) { + try (Connection connection = Utils.createConnection()) { DatabendStatement statement = (DatabendStatement) connection.createStatement(); statement.execute(s); } catch (SQLException throwables) { throwables.printStackTrace(); } - try (Connection connection = createConnection()) { + try (Connection connection = Utils.createConnection()) { DatabendStatement statement = (DatabendStatement) connection.createStatement(); statement.execute("select add_plus(1,2)"); ResultSet r = statement.getResultSet(); @@ -141,7 +124,7 @@ public void testCreateUserFunction() throws SQLException { @Test public void TestMergeinto() throws SQLException { - try (Connection connection = createConnection()) { + try (Connection connection = Utils.createConnection()) { DatabendStatement statement = (DatabendStatement) connection.createStatement(); statement.execute("CREATE TABLE IF NOT EXISTS test_basic_driver.target_table (\n" + " ID INT,\n" + @@ -184,7 +167,7 @@ public void TestMergeinto() throws SQLException { @Test public void testWriteDouble() throws SQLException { - try (Connection connection = createConnection()) { + try (Connection connection = Utils.createConnection()) { DatabendStatement statement = (DatabendStatement) connection.createStatement(); statement.execute("CREATE TABLE IF NOT EXISTS test_basic_driver.table_double (\n" + " ID INT,\n" + @@ -216,7 +199,7 @@ public void testWriteDouble() throws SQLException { @Test public void testDefaultSelectNullValue() throws SQLException { - try (Connection connection = createConnection()) { + try (Connection connection = Utils.createConnection()) { DatabendStatement statement = (DatabendStatement) connection.createStatement(); statement.executeQuery("SELECT a,b from test_basic_driver.table_with_null"); ResultSet r = statement.getResultSet(); @@ -231,7 +214,7 @@ public void testDefaultSelectNullValue() throws SQLException { @Test(groups = {"IT"}) public void testQueryUpdateCount() throws SQLException { - try (Connection connection = createConnection()) { + try (Connection connection = Utils.createConnection()) { DatabendStatement statement = (DatabendStatement) connection.createStatement(); statement.execute("SELECT version()"); ResultSet r = statement.getResultSet(); @@ -247,11 +230,11 @@ public void testBasicWithProperties() throws SQLException { p.setProperty("wait_time_secs", "10"); p.setProperty("max_rows_in_buffer", "100"); p.setProperty("max_rows_per_page", "100"); - p.setProperty("user", "databend"); - p.setProperty("password", "databend"); + p.setProperty("user", Utils.getUsername()); + p.setProperty("password", Utils.getPassword()); //INFO databend_query::servers::http::v1::http_query_handlers: receive http query: HttpQueryRequest { session_id: None, session: Some(HttpSessionConf { database: Some("test_basic_driver"), keep_server_session_secs: None, settings: None }), sql: "SELECT 1", pagination: PaginationConf { wait_time_secs: 10, max_rows_in_buffer: 100, max_rows_per_page: 100 }, string_fields: true, stage_attachment: None } - try (Connection connection = createConnection("test_basic_driver", p)) { + try (Connection connection = Utils.createConnection("test_basic_driver", p)) { PaginationOptions options = connection.unwrap(DatabendConnection.class).getPaginationOptions(); Assert.assertEquals(options.getWaitTimeSecs(), 10); Assert.assertEquals(options.getMaxRowsInBuffer(), 100); @@ -267,7 +250,7 @@ public void testBasicWithProperties() throws SQLException { @Test public void testPrepareStatementQuery() throws SQLException { String sql = "SELECT number from numbers(100) where number = ? or number = ?"; - Connection conn = createConnection("test_basic_driver"); + Connection conn = Utils.createConnection("test_basic_driver"); try (PreparedStatement statement = conn.prepareStatement(sql)) { statement.setInt(1, 1); statement.setInt(2, 2); @@ -281,7 +264,7 @@ public void testPrepareStatementQuery() throws SQLException { @Test(groups = {"IT"}) public void testBasicWithDatabase() throws SQLException { - try (Connection connection = createConnection("test_basic_driver")) { + try (Connection connection = Utils.createConnection("test_basic_driver")) { Statement statement = connection.createStatement(); statement.execute("SELECT i from table1"); ResultSet r = statement.getResultSet(); @@ -305,7 +288,7 @@ public void testBasicWithDatabase() @Test(groups = {"IT"}) public void testUpdateSession() throws SQLException { - try (Connection connection = createConnection("test_basic_driver")) { + try (Connection connection = Utils.createConnection("test_basic_driver")) { connection.createStatement().execute("set max_threads=1"); connection.createStatement().execute("use test_basic_driver_2"); DatabendSession session = connection.unwrap(DatabendConnection.class).getSession(); @@ -316,7 +299,7 @@ public void testUpdateSession() @Test(groups = {"IT"}) public void testResultException() { - try (Connection connection = createConnection()) { + try (Connection connection = Utils.createConnection()) { Statement statement = connection.createStatement(); ResultSet r = statement.executeQuery("SELECT 1e189he 198h"); @@ -329,7 +312,7 @@ public void testResultException() { @Test(groups = {"IT"}) public void testSelectWithPreparement() throws SQLException { - try (Connection connection = createConnection()) { + try (Connection connection = Utils.createConnection()) { connection.createStatement().execute("create or replace table test_basic_driver.table_time(t timestamp, d date, ts timestamp)"); connection.createStatement().execute("insert into test_basic_driver.table_time values('2021-01-01 00:00:00', '2021-01-01', '2021-01-01 00:00:00')"); PreparedStatement statement = connection.prepareStatement("SELECT * from test_basic_driver.table_time where t < ? and d < ? and ts < ?"); @@ -346,7 +329,7 @@ public void testSelectWithPreparement() public void testSelectGeometry() throws SQLException, ParseException { // skip due to failed cluster tests - try (Connection connection = createConnection()) { + try (Connection connection = Utils.createConnection()) { connection.createStatement().execute("set enable_geo_create_table=1"); connection.createStatement().execute("CREATE or replace table cities ( id INT, name VARCHAR NOT NULL, location GEOMETRY);"); connection.createStatement().execute("INSERT INTO cities (id, name, location) VALUES (1, 'New York', 'POINT (-73.935242 40.73061))');"); diff --git a/databend-jdbc/src/test/java/com/databend/jdbc/TestDatabendDatabaseMetaData.java b/databend-jdbc/src/test/java/com/databend/jdbc/TestDatabendDatabaseMetaData.java index 33cbeea7..eaf10a22 100644 --- a/databend-jdbc/src/test/java/com/databend/jdbc/TestDatabendDatabaseMetaData.java +++ b/databend-jdbc/src/test/java/com/databend/jdbc/TestDatabendDatabaseMetaData.java @@ -55,7 +55,7 @@ private static void assertTableMetadata(ResultSet rs) public void setUp() throws SQLException { // create table - Connection c = createConnection(); + Connection c = Utils.createConnection(); c.createStatement().execute("drop table if exists test_column_meta"); c.createStatement().execute("drop table if exists decimal_test"); c.createStatement().execute("drop table if exists test_comment"); @@ -65,25 +65,19 @@ public void setUp() // json data } - private Connection createConnection() - throws SQLException { - String url = "jdbc:databend://localhost:8000"; - return DriverManager.getConnection(url, "databend", "databend"); - } - @Test(groups = {"IT"}) public void testGetUrl() throws SQLException { - try (Connection c = createConnection()) { + try (Connection c = Utils.createConnection()) { DatabaseMetaData metaData = c.getMetaData(); String url = metaData.getURL(); - Assert.assertEquals(url, "jdbc:databend://http://localhost:8000"); + Assert.assertEquals(url, Utils.baseURL()); } } @Test(groups = {"IT"}) public void testGetDatabaseProductName() throws Exception { - try (Connection connection = createConnection()) { + try (Connection connection = Utils.createConnection()) { DatabaseMetaData metaData = connection.getMetaData(); assertEquals(metaData.getDatabaseProductName(), "Databend"); } @@ -92,7 +86,7 @@ public void testGetDatabaseProductName() @Test(groups = {"IT"}) public void testGetDatabaseProductVersion() throws Exception { - try (Connection connection = createConnection()) { + try (Connection connection = Utils.createConnection()) { DatabaseMetaData metaData = connection.getMetaData(); float majorVersion = (float) metaData.getDatabaseMajorVersion() / 10; int minorVersion = metaData.getDatabaseMinorVersion(); @@ -104,7 +98,7 @@ public void testGetDatabaseProductVersion() @Test(groups = {"IT"}) public void testGetUserName() throws Exception { - try (Connection connection = createConnection()) { + try (Connection connection = Utils.createConnection()) { DatabaseMetaData metaData = connection.getMetaData(); Assert.assertTrue(metaData.getUserName().contains("databend")); } @@ -112,7 +106,7 @@ public void testGetUserName() @Test(groups = {"IT"}) public void testGetTables() throws Exception { - try (Connection connection = createConnection()) { + try (Connection connection = Utils.createConnection()) { DatabaseMetaData metaData = connection.getMetaData(); try (ResultSet rs = connection.getMetaData().getTables(null, null, null, null)) { assertTableMetadata(rs); @@ -122,7 +116,7 @@ public void testGetTables() throws Exception { @Test(groups = {"IT"}) public void testGetSchemas() throws Exception { - try (Connection connection = createConnection()) { + try (Connection connection = Utils.createConnection()) { DatabaseMetaData metaData = connection.getMetaData(); try (ResultSet rs = connection.getMetaData().getSchemas()) { ResultSetMetaData metaData1 = rs.getMetaData(); @@ -137,7 +131,7 @@ public void testGetSchemas() throws Exception { @Test(groups = {"IT"}) public void testGetColumns() throws Exception { - try (Connection connection = createConnection()) { + try (Connection connection = Utils.createConnection()) { DatabaseMetaData metaData = connection.getMetaData(); try (ResultSet rs = connection.getMetaData().getColumns(null, null, null, null)) { assertEquals(rs.getMetaData().getColumnCount(), 24); @@ -147,7 +141,7 @@ public void testGetColumns() throws Exception { @Test(groups = {"IT"}) public void testComment() throws SQLException { - try (Connection connection = createConnection()) { + try (Connection connection = Utils.createConnection()) { DatabaseMetaData metaData = connection.getMetaData(); try (ResultSet rs = connection.getMetaData().getColumns("default", "default", "test_comment", null)) { while (rs.next()) { @@ -164,7 +158,7 @@ public void testComment() throws SQLException { @Test(groups = {"IT"}) public void testColumnsMeta() throws Exception { - try (Connection connection = createConnection()) { + try (Connection connection = Utils.createConnection()) { try (ResultSet rs = connection.getMetaData().getColumns("default", "default", "trino_sjh", null)) { while (rs.next()) { String tableCat = rs.getString("table_cat"); @@ -192,7 +186,7 @@ public void testColumnsMeta() throws Exception { @Test(groups = {"IT"}) public void testGetColumnTypesBySelectEmpty() throws Exception { - try (Connection connection = createConnection()) { + try (Connection connection = Utils.createConnection()) { ResultSet rs = connection.createStatement().executeQuery("select * from test_column_meta where 1=2"); ResultSetMetaData metaData = rs.getMetaData(); assertEquals(metaData.getColumnCount(), 17); @@ -204,7 +198,7 @@ public void testGetColumnTypesBySelectEmpty() throws Exception { @Test(groups = {"IT"}) public void testGetColumnTypeWithDecimal() throws Exception { - try (Connection connection = createConnection()) { + try (Connection connection = Utils.createConnection()) { ResultSet rs = connection.createStatement().executeQuery("select * from decimal_test"); ResultSetMetaData metaData = rs.getMetaData(); @@ -221,7 +215,7 @@ public void testGetColumnTypeWithDecimal() throws Exception { @Test(groups = {"IT"}) public void testGetObjectWithDecimal() throws Exception { - try (Connection connection = createConnection()) { + try (Connection connection = Utils.createConnection()) { connection.createStatement().execute("insert into decimal_test values(1.2)"); ResultSet rs = connection.createStatement().executeQuery("select * from decimal_test"); while (rs.next()) { @@ -232,7 +226,7 @@ public void testGetObjectWithDecimal() throws Exception { @Test(groups = {"IT"}) public void testGetPrimaryKeys() throws Exception { - try (Connection connection = createConnection()) { + try (Connection connection = Utils.createConnection()) { DatabaseMetaData metaData = connection.getMetaData(); try (ResultSet rs = connection.getMetaData().getPrimaryKeys(null, null, null)) { assertEquals(rs.getMetaData().getColumnCount(), 6); @@ -242,7 +236,7 @@ public void testGetPrimaryKeys() throws Exception { @Test(groups = {"IT"}) public void testTableTypes() throws Exception { - try (Connection connection = createConnection()) { + try (Connection connection = Utils.createConnection()) { DatabaseMetaData metaData = connection.getMetaData(); try (ResultSet rs = metaData.getTableTypes()) { assertEquals(rs.getMetaData().getColumnCount(), 1); @@ -260,7 +254,7 @@ public void testTableTypes() throws Exception { @Test(groups = {"IT"}) public void testGetFunctions() throws Exception { - try (Connection connection = createConnection()) { + try (Connection connection = Utils.createConnection()) { try (ResultSet rs = connection.getMetaData().getFunctions(null, null, "abs")) { ResultSetMetaData metadata = rs.getMetaData(); assertEquals(metadata.getColumnCount(), 6); diff --git a/databend-jdbc/src/test/java/com/databend/jdbc/TestDatabendDriverUri.java b/databend-jdbc/src/test/java/com/databend/jdbc/TestDatabendDriverUri.java index 4477dc1c..fc758d72 100644 --- a/databend-jdbc/src/test/java/com/databend/jdbc/TestDatabendDriverUri.java +++ b/databend-jdbc/src/test/java/com/databend/jdbc/TestDatabendDriverUri.java @@ -4,7 +4,6 @@ import org.testng.Assert; import org.testng.annotations.Test; -import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties; @@ -236,9 +235,7 @@ public void testFull() throws SQLException { @Test public void TestSetSchema() throws SQLException { - String url = "jdbc:databend://databend:databend@localhost:8000/"; - Properties p = new Properties(); - DatabendConnection connection = (DatabendConnection) DriverManager.getConnection(url, p); + DatabendConnection connection = (DatabendConnection) Utils.createConnection(); try { connection.createStatement().execute("create or replace database test2"); connection.createStatement().execute("create or replace table test2.test2(id int)"); diff --git a/databend-jdbc/src/test/java/com/databend/jdbc/TestDatabendParameterMetaData.java b/databend-jdbc/src/test/java/com/databend/jdbc/TestDatabendParameterMetaData.java index caeb4e04..9271dee2 100644 --- a/databend-jdbc/src/test/java/com/databend/jdbc/TestDatabendParameterMetaData.java +++ b/databend-jdbc/src/test/java/com/databend/jdbc/TestDatabendParameterMetaData.java @@ -7,7 +7,6 @@ import java.sql.Connection; -import java.sql.DriverManager; import java.sql.ParameterMetaData; import java.sql.PreparedStatement; import java.sql.SQLException; @@ -16,22 +15,11 @@ public class TestDatabendParameterMetaData { - private Connection createConnection() - throws SQLException { - String url = "jdbc:databend://localhost:8000"; - return DriverManager.getConnection(url, "databend", "databend"); - } - - private Connection createConnection(boolean presignDisabled) throws SQLException { - String url = "jdbc:databend://localhost:8000?presigned_url_disabled=" + presignDisabled; - return DriverManager.getConnection(url, "databend", "databend"); - } - @BeforeTest public void setUp() throws SQLException { // create table - Connection c = createConnection(); + Connection c = Utils.createConnection(); System.out.println("-----------------"); System.out.println("drop all existing test table"); } @@ -39,7 +27,7 @@ public void setUp() @Test(groups = "integration") public void testGetParameterMetaData() throws SQLException { - try (Connection conn = createConnection(); + try (Connection conn = Utils.createConnection(); PreparedStatement emptyPs = conn.prepareStatement("select 1"); // If you want to use ps.getParameterMetaData().* methods, you need to use a valid sql such as // insert into table_name (col1 type1, col2 typ2, col3 type3) values (?, ?, ?) @@ -62,7 +50,7 @@ public void testGetParameterMetaData() throws SQLException { } } - try (Connection conn = createConnection(); + try (Connection conn = Utils.createConnection(); PreparedStatement ps = conn.prepareStatement("insert into test_table (a int, b int) values (?,?)");) { Assert.assertEquals(ps.getParameterMetaData().getParameterCount(), 2); Assert.assertEquals(ps.getParameterMetaData().getParameterMode(2), ParameterMetaData.parameterModeIn); @@ -73,7 +61,7 @@ public void testGetParameterMetaData() throws SQLException { Assert.assertEquals(ps.getParameterMetaData().getParameterTypeName(2), DatabendDataType.INT_32.getDisplayName().toLowerCase()); } - try (Connection conn = createConnection(); + try (Connection conn = Utils.createConnection(); PreparedStatement ps = conn.prepareStatement("insert into test_table (a int, b VARIANT) values (?,?)");) { Assert.assertEquals(ps.getParameterMetaData().getParameterCount(), 2); Assert.assertEquals(ps.getParameterMetaData().getParameterMode(2), ParameterMetaData.parameterModeIn); diff --git a/databend-jdbc/src/test/java/com/databend/jdbc/TestFileTransfer.java b/databend-jdbc/src/test/java/com/databend/jdbc/TestFileTransfer.java index 3a4daf29..277970a1 100644 --- a/databend-jdbc/src/test/java/com/databend/jdbc/TestFileTransfer.java +++ b/databend-jdbc/src/test/java/com/databend/jdbc/TestFileTransfer.java @@ -13,7 +13,6 @@ import java.nio.file.Files; import java.sql.Connection; -import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; @@ -41,23 +40,12 @@ private static byte[] streamToByteArray(InputStream stream) throws IOException { public void setUp() throws SQLException { // create table - Connection c = createConnection(); + Connection c = Utils.createConnection(); c.createStatement().execute("drop table if exists copy_into"); c.createStatement().execute("CREATE TABLE IF NOT EXISTS copy_into (i int, a Variant, b string) ENGINE = FUSE"); } - private Connection createConnection() - throws SQLException { - String url = "jdbc:databend://localhost:8000/default"; - return DriverManager.getConnection(url, "databend", "databend"); - } - - private Connection createConnection(boolean presignDisabled) throws SQLException { - String url = "jdbc:databend://localhost:8000/default?presigned_url_disabled=" + presignDisabled; - return DriverManager.getConnection(url, "databend", "databend"); - } - // generate a csv file in a temp directory with given lines, return absolute path of the generated csv private String generateRandomCSV(int lines) { if (lines <= 0) { @@ -126,7 +114,7 @@ public void testFileTransfer() File f = new File(filePath); InputStream downloaded = null; try (FileInputStream fileInputStream = new FileInputStream(f)) { - Connection connection = createConnection(); + Connection connection = Utils.createConnection(); String stageName = "test_stage"; DatabendConnection databendConnection = connection.unwrap(DatabendConnection.class); PresignContext.createStageIfNotExists(databendConnection, stageName); @@ -149,7 +137,8 @@ public void testFileTransferThroughAPI() { File f = new File(filePath); try (InputStream fileInputStream = Files.newInputStream(f.toPath())) { Logger.getLogger(OkHttpClient.class.getName()).setLevel(Level.ALL); - Connection connection = createConnection(true); + + Connection connection = Utils.createConnectionWithPresignedUrlDisable(); String stageName = "test_stage"; DatabendConnection databendConnection = connection.unwrap(DatabendConnection.class); PresignContext.createStageIfNotExists(databendConnection, stageName); @@ -169,7 +158,7 @@ public void testCopyInto() { String filePath = generateRandomCSVComplex(10); File f = new File(filePath); try (FileInputStream fileInputStream = new FileInputStream(f)) { - Connection connection = createConnection(); + Connection connection = Utils.createConnection(); String stageName = "test_stage"; DatabendConnection databendConnection = connection.unwrap(DatabendConnection.class); PresignContext.createStageIfNotExists(databendConnection, stageName); diff --git a/databend-jdbc/src/test/java/com/databend/jdbc/TestPrepareStatement.java b/databend-jdbc/src/test/java/com/databend/jdbc/TestPrepareStatement.java index 4e25b0fa..6a04dcde 100644 --- a/databend-jdbc/src/test/java/com/databend/jdbc/TestPrepareStatement.java +++ b/databend-jdbc/src/test/java/com/databend/jdbc/TestPrepareStatement.java @@ -8,7 +8,6 @@ import java.sql.Connection; import java.sql.Date; -import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; @@ -17,24 +16,14 @@ import java.sql.Types; import java.util.ArrayList; import java.util.List; +import java.util.Properties; public class TestPrepareStatement { - private Connection createConnection() - throws SQLException { - String url = "jdbc:databend://localhost:8000?debug=true"; - return DriverManager.getConnection(url, "databend", "databend"); - } - - private Connection createConnection(boolean presignDisabled) throws SQLException { - String url = "jdbc:databend://localhost:8000?presigned_url_disabled=" + presignDisabled; - return DriverManager.getConnection(url, "databend", "databend"); - } - @BeforeTest public void setUp() throws SQLException { // create table - Connection c = createConnection(); + Connection c = Utils.createConnection(); System.out.println("-----------------"); System.out.println("drop all existing test table"); c.createStatement().execute("drop table if exists test_prepare_statement"); @@ -53,7 +42,7 @@ public void setUp() @Test(groups = "IT") public void TestBatchInsert() throws SQLException { - Connection c = createConnection(); + Connection c = Utils.createConnection(); c.setAutoCommit(false); PreparedStatement ps = c.prepareStatement("insert into test_prepare_statement values"); @@ -82,7 +71,7 @@ public void TestBatchInsert() throws SQLException { @Test(groups = "IT") public void TestBatchInsertWithNULL() throws SQLException { - Connection c = createConnection(); + Connection c = Utils.createConnection(); c.setAutoCommit(false); PreparedStatement ps = c.prepareStatement("insert into test_prepare_statement_null values"); @@ -139,7 +128,7 @@ public void TestConvertSQLWithBatchValues() throws SQLException { @Test(groups = "IT") public void TestBatchDelete() throws SQLException { - Connection c = createConnection(); + Connection c = Utils.createConnection(); c.setAutoCommit(false); PreparedStatement ps = c.prepareStatement("insert into test_prepare_statement values"); @@ -184,7 +173,7 @@ public void TestBatchDelete() throws SQLException { @Test(groups = "IT") public void TestBatchInsertWithTime() throws SQLException { - Connection c = createConnection(); + Connection c = Utils.createConnection(); c.setAutoCommit(false); PreparedStatement ps = c.prepareStatement("insert into test_prepare_time values"); ps.setDate(1, Date.valueOf("2020-01-10")); @@ -210,7 +199,7 @@ public void TestBatchInsertWithTime() throws SQLException { @Test(groups = "IT") public void TestBatchInsertWithComplexDataType() throws SQLException { - Connection c = createConnection(); + Connection c = Utils.createConnection(); c.setAutoCommit(false); PreparedStatement ps = c.prepareStatement("insert into objects_test1 values"); ps.setInt(1, 1); @@ -237,7 +226,7 @@ public void TestBatchInsertWithComplexDataType() throws SQLException { @Test(groups = "IT") public void TestBatchInsertWithComplexDataTypeWithPresignAPI() throws SQLException { - Connection c = createConnection(true); + Connection c = Utils.createConnection(); c.setAutoCommit(false); PreparedStatement ps = c.prepareStatement("insert into objects_test1 values"); ps.setInt(1, 1); @@ -264,7 +253,7 @@ public void TestBatchInsertWithComplexDataTypeWithPresignAPI() throws SQLExcepti @Test(groups = "IT") public void TestBatchInsertWithComplexDataTypeWithPresignAPIPlaceHolder() throws SQLException { - Connection c = createConnection(true); + Connection c = Utils.createConnection(); c.setAutoCommit(false); PreparedStatement ps = c.prepareStatement("insert into objects_test1 values(?,?,?,?,?)"); for (int i = 0; i < 500000; i++) { @@ -291,7 +280,7 @@ public void TestBatchInsertWithComplexDataTypeWithPresignAPIPlaceHolder() throws @Test(groups = "IT") public void TestBatchReplaceInto() throws SQLException { - Connection c = createConnection(); + Connection c = Utils.createConnection(); c.setAutoCommit(false); PreparedStatement ps1 = c.prepareStatement("insert into test_prepare_statement values"); ps1.setInt(1, 1); @@ -325,7 +314,7 @@ public void TestBatchReplaceInto() throws SQLException { @Test public void testPrepareStatementExecute() throws SQLException { - Connection conn = createConnection(); + Connection conn = Utils.createConnection(); conn.createStatement().execute("delete from test_prepare_statement"); String insertSql = "insert into test_prepare_statement values (?,?)"; try (PreparedStatement statement = conn.prepareStatement(insertSql)) { @@ -368,7 +357,7 @@ public void testPrepareStatementExecute() throws SQLException { @Test public void testUpdateSetNull() throws SQLException { - Connection conn = createConnection(); + Connection conn = Utils.createConnection(); String sql = "insert into test_prepare_statement values (?,?)"; try (PreparedStatement statement = conn.prepareStatement(sql)) { statement.setInt(1, 1); @@ -404,7 +393,7 @@ public void testUpdateSetNull() throws SQLException { @Test public void testUpdateStatement() throws SQLException { - Connection conn = createConnection(); + Connection conn = Utils.createConnection(); String sql = "insert into test_prepare_statement values (?,?)"; try (PreparedStatement statement = conn.prepareStatement(sql)) { statement.setInt(1, 1); @@ -435,7 +424,7 @@ public void testUpdateStatement() throws SQLException { @Test public void testAllPreparedStatement() throws SQLException { String sql = "insert into test_prepare_statement values (?,?)"; - Connection conn = createConnection(); + Connection conn = Utils.createConnection(); try (PreparedStatement statement = conn.prepareStatement(sql)) { statement.setInt(1, 1); statement.setString(2, "b"); @@ -500,7 +489,7 @@ public void testAllPreparedStatement() throws SQLException { // @Test // public void testSetBlobNotNull() throws SQLException { // String sql = "insert into binary1 values (?)"; -// Connection conn = createConnection(); +// Connection conn = Utils.createConnection(); // // Create a Blob // String blobData = "blob data"; // InputStream blobInputStream = new ByteArrayInputStream(blobData.getBytes()); @@ -515,7 +504,7 @@ public void testAllPreparedStatement() throws SQLException { @Test public void shouldBuildStageAttachmentWithFileFormatOptions() throws SQLException { - Connection conn = createConnection(); + Connection conn = Utils.createConnection(); Assertions.assertEquals("", conn.unwrap(DatabendConnection.class).binaryFormat()); StageAttachment stageAttachment = DatabendPreparedStatement.buildStateAttachment((DatabendConnection) conn, "stagePath"); @@ -527,7 +516,7 @@ public void shouldBuildStageAttachmentWithFileFormatOptions() throws SQLExceptio @Test public void testSelectWithClusterKey() throws SQLException { - Connection conn = createConnection(); + Connection conn = Utils.createConnection(); conn.createStatement().execute("drop table if exists default.test_clusterkey"); conn.createStatement().execute("create table default.test_clusterkey (a int, b string)"); String insertSql = "insert into default.test_clusterkey values (?,?)"; @@ -554,11 +543,14 @@ public void testSelectWithClusterKey() throws SQLException { @Test public void testEncodePass() throws SQLException { - Connection conn = createConnection(); + Connection conn = Utils.createConnection(); conn.createStatement().execute("create user if not exists 'u01' identified by 'mS%aFRZW*GW';"); conn.createStatement().execute("GRANT ALL PRIVILEGES ON default.* TO 'u01'@'%'"); + Properties p = new Properties(); + p.setProperty("user", "u01"); + p.setProperty("password", "mS%aFRZW*GW"); - Connection conn2 = DriverManager.getConnection("jdbc:databend://localhost:8000", "u01", "mS%aFRZW*GW"); + Connection conn2 = Utils.createConnection(); conn2.createStatement().execute("select 1"); conn.createStatement().execute("drop user if exists 'u01'"); } diff --git a/databend-jdbc/src/test/java/com/databend/jdbc/TestPresignContext.java b/databend-jdbc/src/test/java/com/databend/jdbc/TestPresignContext.java index f1f653c3..3d11acb2 100644 --- a/databend-jdbc/src/test/java/com/databend/jdbc/TestPresignContext.java +++ b/databend-jdbc/src/test/java/com/databend/jdbc/TestPresignContext.java @@ -3,17 +3,7 @@ import org.testng.Assert; import org.testng.annotations.Test; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.SQLException; - public class TestPresignContext { - private Connection createConnection() - throws SQLException { - String url = "jdbc:databend://localhost:8000"; - return DriverManager.getConnection(url, "databend", "databend"); - } - @Test(groups = {"Unit"}) public void TestPreisgnUrlBuild() { String presignSql = PresignContext.buildRequestSQL(PresignContext.PresignMethod.UPLOAD, "test_bucket", "test.csv"); @@ -27,7 +17,7 @@ public void TestPreisgnUrlBuild() { @Test(groups = {"IT"}) public void TestGetPresignUrl() { try { - DatabendConnection connection = (DatabendConnection) createConnection(); + DatabendConnection connection = (DatabendConnection) Utils.createConnection(); PresignContext ctx = PresignContext.getPresignContext(connection, PresignContext.PresignMethod.UPLOAD, null, "test.csv"); Assert.assertNotNull(ctx); Assert.assertNotNull(ctx.getUrl()); @@ -40,7 +30,7 @@ public void TestGetPresignUrl() { @Test(groups = {"IT"}) public void TestGetPresignUrlCase2() { try { - DatabendConnection connection = (DatabendConnection) createConnection(); + DatabendConnection connection = (DatabendConnection) Utils.createConnection(); String stageName = "test_stage"; PresignContext.createStageIfNotExists(connection, stageName); PresignContext ctx = PresignContext.getPresignContext(connection, PresignContext.PresignMethod.UPLOAD, stageName, "a/b/d/test.csv"); diff --git a/databend-jdbc/src/test/java/com/databend/jdbc/TestTransaction.java b/databend-jdbc/src/test/java/com/databend/jdbc/TestTransaction.java index ce956b66..cd18228e 100644 --- a/databend-jdbc/src/test/java/com/databend/jdbc/TestTransaction.java +++ b/databend-jdbc/src/test/java/com/databend/jdbc/TestTransaction.java @@ -5,24 +5,17 @@ import org.testng.annotations.Test; import java.sql.Connection; -import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class TestTransaction { - private Connection createConnection() - throws SQLException { - String url = "jdbc:databend://localhost:8000?presigned_url_disabled=true"; - return DriverManager.getConnection(url, "databend", "databend"); - } - @BeforeTest public void setUp() throws SQLException { // create table - Connection c = createConnection(); + Connection c = Utils.createConnection(); c.createStatement().execute("drop database if exists test_txn"); c.createStatement().execute("create database test_txn"); c.createStatement().execute("create table test_txn.table1(i int)"); @@ -31,7 +24,7 @@ public void setUp() @Test public void testRollback() throws SQLException { - Connection c = createConnection(); + Connection c = Utils.createConnection(); c.createStatement().execute("delete from test_txn.table1"); try (Statement statemte = c.createStatement()) { statemte.execute("begin"); @@ -66,8 +59,8 @@ public void testRollback() @Test public void testCommit() throws SQLException { - Connection c1 = createConnection(); - Connection c2 = createConnection(); + Connection c1 = Utils.createConnection(); + Connection c2 = Utils.createConnection(); c1.createStatement().execute("delete from test_txn.table1"); try (Statement statement = c1.createStatement()) { statement.execute("create or replace table test_txn.table1(i int)"); @@ -90,7 +83,7 @@ public void testCommit() throws SQLException { } } c1.commit(); - Connection c3 = createConnection(); + Connection c3 = Utils.createConnection(); try (Statement statement = c3.createStatement()) { statement.execute("select * from test_txn.table1"); ResultSet rs = statement.getResultSet(); diff --git a/databend-jdbc/src/test/java/com/databend/jdbc/Utils.java b/databend-jdbc/src/test/java/com/databend/jdbc/Utils.java new file mode 100644 index 00000000..5429c084 --- /dev/null +++ b/databend-jdbc/src/test/java/com/databend/jdbc/Utils.java @@ -0,0 +1,46 @@ +package com.databend.jdbc; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.util.Properties; + +public class Utils { + + static String port = System.getenv("DATABEND_CONN_PORT") != null ? System.getenv("DATABEND_CONN_PORT") : "8000"; + + static String username = "databend"; + static String password = "databend"; + public static String baseURL() { + return "jdbc:databend://localhost:" + port; + } + + public static String getUsername() { + return username; + } + public static String getPassword() { + return password; + } + + + public static Connection createConnection() + throws SQLException { + return DriverManager.getConnection(baseURL(), username, password); + } + + public static Connection createConnection(String database) throws SQLException { + String url = baseURL() + "/" + database; + return DriverManager.getConnection(url, username, password); + } + + public static Connection createConnection(String database, Properties p) throws SQLException { + String url = baseURL() + "/" + database; + return DriverManager.getConnection(url, p); + } + + + public static Connection createConnectionWithPresignedUrlDisable() throws SQLException { + String url = baseURL() + "?presigned_url_disabled=true"; + return DriverManager.getConnection(url, "databend", "databend"); + } +} diff --git a/databend-jdbc/src/test/java/com/databend/jdbc/cloud/TestDatabendPresignClient.java b/databend-jdbc/src/test/java/com/databend/jdbc/cloud/TestDatabendPresignClient.java index f0b82682..2a05b3ee 100644 --- a/databend-jdbc/src/test/java/com/databend/jdbc/cloud/TestDatabendPresignClient.java +++ b/databend-jdbc/src/test/java/com/databend/jdbc/cloud/TestDatabendPresignClient.java @@ -1,6 +1,7 @@ package com.databend.jdbc.cloud; import com.databend.jdbc.DatabendConnection; +import com.databend.jdbc.Utils; import okhttp3.OkHttpClient; import org.testng.annotations.Test; @@ -8,16 +9,9 @@ import java.io.FileInputStream; import java.io.FileWriter; import java.io.InputStream; -import java.sql.Connection; -import java.sql.DriverManager; import java.sql.SQLException; public class TestDatabendPresignClient { - private Connection createConnection() - throws SQLException { - String url = "jdbc:databend://localhost:8000"; - return DriverManager.getConnection(url, "databend", "databend"); - } private String generateRandomCSV(int lines) { if (lines <= 0) { @@ -41,7 +35,7 @@ private String generateRandomCSV(int lines) { @Test(groups = {"Local"}) public void uploadFileAPI() { String filePath = null; - try (DatabendConnection connection = createConnection().unwrap(DatabendConnection.class)) { + try (DatabendConnection connection = Utils.createConnection().unwrap(DatabendConnection.class)) { OkHttpClient client = connection.getHttpClient(); DatabendPresignClient presignClient = new DatabendPresignClientV1(client, connection.getURI().toString()); filePath = generateRandomCSV(10);