7
7
import com .clickhouse .client .api .ClientConfigProperties ;
8
8
import com .clickhouse .client .api .ServerException ;
9
9
import com .clickhouse .client .api .internal .ServerSettings ;
10
- import com .clickhouse .jdbc .internal .ClientInfoProperties ;
11
- import com .clickhouse .jdbc .internal .DriverProperties ;
12
10
import com .github .tomakehurst .wiremock .WireMockServer ;
13
11
import com .github .tomakehurst .wiremock .client .WireMock ;
14
12
import com .github .tomakehurst .wiremock .common .ConsoleNotifier ;
15
13
import com .github .tomakehurst .wiremock .core .WireMockConfiguration ;
16
-
17
14
import org .testng .Assert ;
18
15
import org .testng .annotations .DataProvider ;
19
16
import org .testng .annotations .Test ;
32
29
import java .util .Properties ;
33
30
import java .util .UUID ;
34
31
32
+ import static org .testng .Assert .assertEquals ;
33
+ import static org .testng .Assert .assertNull ;
35
34
import static org .testng .Assert .assertThrows ;
36
35
import static org .testng .Assert .fail ;
37
36
38
37
public class ConnectionTest extends JdbcIntegrationTest {
39
38
40
- @ Test (groups = { "integration" }, enabled = false )
39
+ @ Test (groups = { "integration" })
41
40
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" ));
45
61
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
+ }
48
63
}
49
64
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
+ }
60
101
}
61
102
62
103
@ Test (groups = { "integration" })
@@ -67,12 +108,16 @@ public void prepareCallTest() throws SQLException {
67
108
assertThrows (SQLFeatureNotSupportedException .class , () -> localConnection .prepareCall ("SELECT 1" , ResultSet .TYPE_FORWARD_ONLY , ResultSet .CONCUR_READ_ONLY , ResultSet .CLOSE_CURSORS_AT_COMMIT ));
68
109
}
69
110
70
- @ Test (groups = { "integration" }, enabled = false )
111
+ @ Test (groups = { "integration" })
71
112
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
+ }
76
121
}
77
122
78
123
@ Test (groups = { "integration" })
@@ -97,8 +142,8 @@ public void setAutoCommitTest() throws SQLException {
97
142
@ Test (groups = { "integration" })
98
143
public void testCommitRollback () throws SQLException {
99
144
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 );
102
147
assertThrows (SQLFeatureNotSupportedException .class , () -> localConnection .rollback (null ));
103
148
}
104
149
@@ -183,7 +228,7 @@ public void clearWarningsTest() throws SQLException {
183
228
@ Test (groups = { "integration" })
184
229
public void getTypeMapTest () throws SQLException {
185
230
Connection localConnection = this .getJdbcConnection ();
186
- assertThrows (SQLFeatureNotSupportedException .class , () -> localConnection . getTypeMap () );
231
+ assertThrows (SQLFeatureNotSupportedException .class , localConnection :: getTypeMap );
187
232
}
188
233
189
234
@ Test (groups = { "integration" })
@@ -207,7 +252,7 @@ public void getHoldabilityTest() throws SQLException {
207
252
@ Test (groups = { "integration" })
208
253
public void setSavepointTest () throws SQLException {
209
254
Connection localConnection = this .getJdbcConnection ();
210
- assertThrows (SQLFeatureNotSupportedException .class , () -> localConnection . setSavepoint () );
255
+ assertThrows (SQLFeatureNotSupportedException .class , localConnection :: setSavepoint );
211
256
assertThrows (SQLFeatureNotSupportedException .class , () -> localConnection .setSavepoint ("savepoint-name" ));
212
257
}
213
258
@@ -268,7 +313,6 @@ public void setAndGetClientInfoTest(String clientName) throws SQLException {
268
313
try (ResultSet rs = stmt .executeQuery (logQuery )) {
269
314
Assert .assertTrue (rs .next ());
270
315
String userAgent = rs .getString ("http_user_agent" );
271
- System .out .println (userAgent );
272
316
if (clientName != null && !clientName .isEmpty ()) {
273
317
Assert .assertTrue (userAgent .startsWith (clientName ), "Expected to start with '" + clientName + "' but value was '" + userAgent + "'" );
274
318
}
@@ -353,7 +397,7 @@ public void setNetworkTimeoutTest() throws SQLException {
353
397
@ Test (groups = { "integration" })
354
398
public void getNetworkTimeoutTest () throws SQLException {
355
399
Connection localConnection = this .getJdbcConnection ();
356
- assertThrows (SQLFeatureNotSupportedException .class , () -> localConnection . getNetworkTimeout () );
400
+ assertThrows (SQLFeatureNotSupportedException .class , localConnection :: getNetworkTimeout );
357
401
}
358
402
359
403
@ Test (groups = { "integration" })
@@ -606,4 +650,25 @@ private static Object[][] createValidDatabaseNames() {
606
650
};
607
651
}
608
652
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
+ }
609
674
}
0 commit comments