|
| 1 | +package com.clickhouse.jdbc; |
| 2 | + |
| 3 | +import java.sql.DriverManager; |
| 4 | +import java.sql.SQLException; |
| 5 | +import java.sql.Statement; |
| 6 | +import java.util.Properties; |
| 7 | + |
| 8 | +import org.testng.Assert; |
| 9 | +import org.testng.annotations.Test; |
| 10 | + |
| 11 | +import com.clickhouse.client.ClickHouseProtocol; |
| 12 | +import com.clickhouse.client.config.ClickHouseClientOption; |
| 13 | +import com.clickhouse.client.config.ClickHouseDefaults; |
| 14 | + |
| 15 | +public class ClickHouseDataSourceTest extends JdbcIntegrationTest { |
| 16 | + @Test(groups = "integration") |
| 17 | + public void testGetConnection() throws SQLException { |
| 18 | + String url = "jdbc:ch://" + getServerAddress(DEFAULT_PROTOCOL); |
| 19 | + String urlWithCredentials = "jdbc:ch://default@" + getServerAddress(ClickHouseProtocol.HTTP); |
| 20 | + String clientName = "client1"; |
| 21 | + int maxExecuteTime = 1234; |
| 22 | + boolean continueBatchOnError = true; |
| 23 | + |
| 24 | + Properties properties = new Properties(); |
| 25 | + properties.setProperty(ClickHouseDefaults.USER.getKey(), "default"); |
| 26 | + properties.setProperty(ClickHouseDefaults.PASSWORD.getKey(), ""); |
| 27 | + properties.setProperty(ClickHouseClientOption.CLIENT_NAME.getKey(), clientName); |
| 28 | + properties.setProperty(ClickHouseClientOption.MAX_EXECUTION_TIME.getKey(), Integer.toString(maxExecuteTime)); |
| 29 | + properties.setProperty(JdbcConfig.PROP_CONTINUE_BATCH, Boolean.toString(continueBatchOnError)); |
| 30 | + String params = String.format("?%s=%s&%s=%d&%s", ClickHouseClientOption.CLIENT_NAME.getKey(), clientName, |
| 31 | + ClickHouseClientOption.MAX_EXECUTION_TIME.getKey(), maxExecuteTime, JdbcConfig.PROP_CONTINUE_BATCH); |
| 32 | + |
| 33 | + for (ClickHouseDataSource ds : new ClickHouseDataSource[] { |
| 34 | + new ClickHouseDataSource(url, properties), |
| 35 | + new ClickHouseDataSource(urlWithCredentials, properties), |
| 36 | + new ClickHouseDataSource(url + params), |
| 37 | + new ClickHouseDataSource(urlWithCredentials + params), |
| 38 | + }) { |
| 39 | + for (ClickHouseConnection connection : new ClickHouseConnection[] { |
| 40 | + ds.getConnection(), |
| 41 | + ds.getConnection("default", ""), |
| 42 | + new ClickHouseDriver().connect(url, properties), |
| 43 | + new ClickHouseDriver().connect(urlWithCredentials, properties), |
| 44 | + new ClickHouseDriver().connect(url + params, new Properties()), |
| 45 | + new ClickHouseDriver().connect(urlWithCredentials + params, new Properties()), |
| 46 | + (ClickHouseConnection) DriverManager.getConnection(url, properties), |
| 47 | + (ClickHouseConnection) DriverManager.getConnection(urlWithCredentials, properties), |
| 48 | + (ClickHouseConnection) DriverManager.getConnection(url + params), |
| 49 | + (ClickHouseConnection) DriverManager.getConnection(urlWithCredentials + params), |
| 50 | + (ClickHouseConnection) DriverManager.getConnection(url + params, "default", ""), |
| 51 | + (ClickHouseConnection) DriverManager.getConnection(urlWithCredentials + params, "default", ""), |
| 52 | + }) { |
| 53 | + try (ClickHouseConnection conn = connection; Statement stmt = conn.createStatement()) { |
| 54 | + Assert.assertEquals(conn.getClientInfo(ClickHouseConnection.PROP_APPLICATION_NAME), clientName); |
| 55 | + Assert.assertEquals(stmt.getQueryTimeout(), maxExecuteTime); |
| 56 | + Assert.assertEquals(conn.getJdbcConfig().isContinueBatchOnError(), continueBatchOnError); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | +} |
0 commit comments