77import com .clickhouse .client .api .ClientConfigProperties ;
88import com .clickhouse .client .api .ServerException ;
99import com .clickhouse .client .api .internal .ServerSettings ;
10- import com .clickhouse .jdbc .internal .ClientInfoProperties ;
11- import com .clickhouse .jdbc .internal .DriverProperties ;
1210import com .github .tomakehurst .wiremock .WireMockServer ;
1311import com .github .tomakehurst .wiremock .client .WireMock ;
1412import com .github .tomakehurst .wiremock .common .ConsoleNotifier ;
1513import com .github .tomakehurst .wiremock .core .WireMockConfiguration ;
16-
1714import org .testng .Assert ;
1815import org .testng .annotations .DataProvider ;
1916import org .testng .annotations .Test ;
3229import java .util .Properties ;
3330import java .util .UUID ;
3431
32+ import static org .testng .Assert .assertEquals ;
33+ import static org .testng .Assert .assertNull ;
3534import static org .testng .Assert .assertThrows ;
3635import static org .testng .Assert .fail ;
3736
3837public class ConnectionTest extends JdbcIntegrationTest {
3938
40- @ Test (groups = { "integration" }, enabled = false )
39+ @ Test (groups = { "integration" })
4140 public void createAndCloseStatementTest () throws SQLException {
42- Connection localConnection = this .getJdbcConnection ();
43- Statement statement = localConnection .createStatement ();
44- Assert .assertNotNull (statement );
41+ Connection conn = getJdbcConnection ();
42+ Statement stmt = conn .createStatement ();
43+ PreparedStatement pStmt = conn .prepareStatement ("SELECT ? as v" );
44+ pStmt .setString (1 , "test string" );
45+ conn .close ();
46+ conn .close (); // check second attempt doesn't throw anything
47+ assertThrows (SQLException .class , conn ::createStatement );
48+
49+ try {
50+ stmt .executeQuery ("SELECT 1" );
51+ fail ("Exception expected" );
52+ } catch (SQLException e ) {
53+ Assert .assertTrue (e .getMessage ().contains ("closed" ));
54+ }
55+
56+ try {
57+ pStmt .executeQuery ();
58+ fail ("Exception expected" );
59+ } catch (SQLException e ) {
60+ Assert .assertTrue (e .getMessage ().contains ("closed" ));
4561
46- assertThrows (SQLFeatureNotSupportedException .class , () -> localConnection .createStatement (ResultSet .TYPE_FORWARD_ONLY , ResultSet .CONCUR_READ_ONLY ));
47- assertThrows (SQLFeatureNotSupportedException .class , () -> localConnection .createStatement (ResultSet .TYPE_FORWARD_ONLY , ResultSet .CONCUR_READ_ONLY , ResultSet .CLOSE_CURSORS_AT_COMMIT ));
62+ }
4863 }
4964
50- @ Test (groups = { "integration" }, enabled = false )
51- public void prepareStatementTest () throws SQLException {
52- Connection localConnection = this .getJdbcConnection ();
53- PreparedStatement statement = localConnection .prepareStatement ("SELECT 1" );
54- Assert .assertNotNull (statement );
55- assertThrows (SQLFeatureNotSupportedException .class , () -> localConnection .prepareStatement ("SELECT 1" , Statement .RETURN_GENERATED_KEYS ));
56- assertThrows (SQLFeatureNotSupportedException .class , () -> localConnection .prepareStatement ("SELECT 1" , new int [] { 1 }));
57- assertThrows (SQLFeatureNotSupportedException .class , () -> localConnection .prepareStatement ("SELECT 1" , ResultSet .TYPE_FORWARD_ONLY , ResultSet .CONCUR_READ_ONLY ));
58- assertThrows (SQLFeatureNotSupportedException .class , () -> localConnection .prepareStatement ("SELECT 1" , ResultSet .TYPE_FORWARD_ONLY , ResultSet .CONCUR_READ_ONLY , ResultSet .CLOSE_CURSORS_AT_COMMIT ));
59- assertThrows (SQLFeatureNotSupportedException .class , () -> localConnection .prepareStatement ("SELECT 1" , new String [] { "1" }));
65+ @ Test (groups = { "integration" })
66+ public void testCreateUnsupportedStatements () throws Throwable {
67+
68+ boolean [] throwUnsupportedException = new boolean [] {false , true };
69+
70+ for (boolean flag : throwUnsupportedException ) {
71+ Properties props = new Properties ();
72+ if (flag ) {
73+ props .setProperty (DriverProperties .IGNORE_UNSUPPORTED_VALUES .getKey (), "true" );
74+ }
75+
76+ try (Connection conn = this .getJdbcConnection (props )) {
77+ Assert .ThrowingRunnable [] createStatements = new Assert .ThrowingRunnable []{
78+ () -> conn .createStatement (ResultSet .TYPE_SCROLL_INSENSITIVE , ResultSet .CONCUR_READ_ONLY ),
79+ () -> conn .createStatement (ResultSet .TYPE_FORWARD_ONLY , ResultSet .CONCUR_UPDATABLE ),
80+ () -> conn .createStatement (ResultSet .TYPE_FORWARD_ONLY , ResultSet .CONCUR_READ_ONLY , ResultSet .HOLD_CURSORS_OVER_COMMIT ),
81+ () -> conn .prepareStatement ("SELECT 1" , Statement .RETURN_GENERATED_KEYS ),
82+ () -> conn .prepareStatement ("SELECT 1" , new int []{1 }),
83+ () -> conn .prepareStatement ("SELECT 1" , new String []{"1" }),
84+ () -> conn .prepareStatement ("SELECT 1" , ResultSet .TYPE_FORWARD_ONLY , ResultSet .CONCUR_UPDATABLE ),
85+ () -> conn .prepareStatement ("SELECT 1" , ResultSet .TYPE_SCROLL_INSENSITIVE , ResultSet .CONCUR_READ_ONLY ),
86+ () -> conn .prepareStatement ("SELECT 1" , ResultSet .TYPE_FORWARD_ONLY , ResultSet .CONCUR_READ_ONLY , ResultSet .HOLD_CURSORS_OVER_COMMIT ),
87+ conn ::setSavepoint ,
88+ () -> conn .setSavepoint ("save point" ),
89+ () -> conn .createStruct ("simple" , null ),
90+ };
91+
92+ for (Assert .ThrowingRunnable createStatement : createStatements ) {
93+ if (!flag ) {
94+ Assert .assertThrows (SQLFeatureNotSupportedException .class , createStatement );
95+ } else {
96+ createStatement .run ();
97+ }
98+ }
99+ }
100+ }
60101 }
61102
62103 @ Test (groups = { "integration" })
@@ -67,12 +108,16 @@ public void prepareCallTest() throws SQLException {
67108 assertThrows (SQLFeatureNotSupportedException .class , () -> localConnection .prepareCall ("SELECT 1" , ResultSet .TYPE_FORWARD_ONLY , ResultSet .CONCUR_READ_ONLY , ResultSet .CLOSE_CURSORS_AT_COMMIT ));
68109 }
69110
70- @ Test (groups = { "integration" }, enabled = false )
111+ @ Test (groups = { "integration" })
71112 public void nativeSQLTest () throws SQLException {
72- // TODO: implement
73- Connection localConnection = this .getJdbcConnection ();
74- String sql = "SELECT 1" ;
75- Assert .assertEquals (localConnection .nativeSQL (sql ), sql );
113+ try (Connection conn = this .getJdbcConnection ()) {
114+ String escapedSQL = "SELECT \n {ts '2024-01-02 02:01:01'} as v1,\n {d '2024-01-02 02:01:01'} as v2,\n {d ?} as v3" ;
115+ String nativeSQL = "SELECT \n timestamp('2024-01-02 02:01:01') as v1,\n toDate('2024-01-02 02:01:01') as v2,\n {d ?} as v3" ;
116+ Assert .assertEquals (conn .nativeSQL (escapedSQL ), nativeSQL );
117+
118+ Assert .expectThrows (IllegalArgumentException .class , () -> conn .nativeSQL (null ));
119+ Assert .assertEquals (conn .nativeSQL ("SELECT 1 as t" ), "SELECT 1 as t" );
120+ }
76121 }
77122
78123 @ Test (groups = { "integration" })
@@ -97,8 +142,8 @@ public void setAutoCommitTest() throws SQLException {
97142 @ Test (groups = { "integration" })
98143 public void testCommitRollback () throws SQLException {
99144 try (Connection localConnection = this .getJdbcConnection ()) {
100- assertThrows (SQLFeatureNotSupportedException .class , () -> localConnection . commit () );
101- assertThrows (SQLFeatureNotSupportedException .class , () -> localConnection . rollback () );
145+ assertThrows (SQLFeatureNotSupportedException .class , localConnection :: commit );
146+ assertThrows (SQLFeatureNotSupportedException .class , localConnection :: rollback );
102147 assertThrows (SQLFeatureNotSupportedException .class , () -> localConnection .rollback (null ));
103148 }
104149
@@ -183,7 +228,7 @@ public void clearWarningsTest() throws SQLException {
183228 @ Test (groups = { "integration" })
184229 public void getTypeMapTest () throws SQLException {
185230 Connection localConnection = this .getJdbcConnection ();
186- assertThrows (SQLFeatureNotSupportedException .class , () -> localConnection . getTypeMap () );
231+ assertThrows (SQLFeatureNotSupportedException .class , localConnection :: getTypeMap );
187232 }
188233
189234 @ Test (groups = { "integration" })
@@ -207,7 +252,7 @@ public void getHoldabilityTest() throws SQLException {
207252 @ Test (groups = { "integration" })
208253 public void setSavepointTest () throws SQLException {
209254 Connection localConnection = this .getJdbcConnection ();
210- assertThrows (SQLFeatureNotSupportedException .class , () -> localConnection . setSavepoint () );
255+ assertThrows (SQLFeatureNotSupportedException .class , localConnection :: setSavepoint );
211256 assertThrows (SQLFeatureNotSupportedException .class , () -> localConnection .setSavepoint ("savepoint-name" ));
212257 }
213258
@@ -268,7 +313,6 @@ public void setAndGetClientInfoTest(String clientName) throws SQLException {
268313 try (ResultSet rs = stmt .executeQuery (logQuery )) {
269314 Assert .assertTrue (rs .next ());
270315 String userAgent = rs .getString ("http_user_agent" );
271- System .out .println (userAgent );
272316 if (clientName != null && !clientName .isEmpty ()) {
273317 Assert .assertTrue (userAgent .startsWith (clientName ), "Expected to start with '" + clientName + "' but value was '" + userAgent + "'" );
274318 }
@@ -353,7 +397,7 @@ public void setNetworkTimeoutTest() throws SQLException {
353397 @ Test (groups = { "integration" })
354398 public void getNetworkTimeoutTest () throws SQLException {
355399 Connection localConnection = this .getJdbcConnection ();
356- assertThrows (SQLFeatureNotSupportedException .class , () -> localConnection . getNetworkTimeout () );
400+ assertThrows (SQLFeatureNotSupportedException .class , localConnection :: getNetworkTimeout );
357401 }
358402
359403 @ Test (groups = { "integration" })
@@ -606,4 +650,25 @@ private static Object[][] createValidDatabaseNames() {
606650 };
607651 }
608652
653+ @ Test (groups = {"integration" })
654+ public void testClientInfoProperties () throws Exception {
655+ try (Connection conn = this .getJdbcConnection ()) {
656+
657+ Properties properties = conn .getClientInfo ();
658+ assertEquals (properties .get (ClientInfoProperties .APPLICATION_NAME .getKey ()), "" );
659+
660+ properties .put (ClientInfoProperties .APPLICATION_NAME .getKey (), "test" );
661+ conn .setClientInfo (properties );
662+
663+ assertEquals (properties .get (ClientInfoProperties .APPLICATION_NAME .getKey ()), "test" );
664+
665+ conn .setClientInfo (new Properties ());
666+ assertNull (conn .getClientInfo (ClientInfoProperties .APPLICATION_NAME .getKey ()));
667+
668+ conn .setClientInfo (ClientInfoProperties .APPLICATION_NAME .getKey (), "test 2" );
669+ assertEquals (conn .getClientInfo (ClientInfoProperties .APPLICATION_NAME .getKey ()), "test 2" );
670+
671+ assertNull (conn .getClientInfo ("unknown" ));
672+ }
673+ }
609674}
0 commit comments