forked from microsoft/mssql-jdbc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSQLServerConnectionTest.java
602 lines (533 loc) · 26.1 KB
/
SQLServerConnectionTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
/*
* Microsoft JDBC Driver for SQL Server Copyright(c) Microsoft Corporation All rights reserved. This program is made
* available under the terms of the MIT License. See the LICENSE file in the project root for more information.
*/
package com.microsoft.sqlserver.jdbc;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.Statement;
import java.text.MessageFormat;
import java.util.UUID;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.logging.Logger;
import javax.sql.ConnectionEvent;
import javax.sql.PooledConnection;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import com.microsoft.sqlserver.testframework.AbstractSQLGenerator;
import com.microsoft.sqlserver.testframework.AbstractTest;
import com.microsoft.sqlserver.testframework.Constants;
import com.microsoft.sqlserver.testframework.PrepUtil;
@RunWith(JUnitPlatform.class)
public class SQLServerConnectionTest extends AbstractTest {
// If no retry is done, the function should at least exit in 5 seconds
static int threshHoldForNoRetryInMilliseconds = 5000;
static int loginTimeOutInSeconds = 10;
String randomServer = RandomUtil.getIdentifier("Server");
/**
* Test connection properties with SQLServerDataSource
*/
@Test
public void testDataSource() {
SQLServerDataSource ds = new SQLServerDataSource();
ds.setUser("User");
ds.setPassword("sUser");
ds.setApplicationName("User");
ds.setURL("jdbc:sqlserver://" + randomServer + ";packetSize=512");
String trustStore = "Store";
String trustStorePassword = "pwd";
ds.setTrustStore(trustStore);
ds.setEncrypt(true);
ds.setTrustStorePassword(trustStorePassword);
ds.setTrustServerCertificate(true);
assertEquals(trustStore, ds.getTrustStore(), TestResource.getResource("R_valuesAreDifferent"));
assertEquals(true, ds.getEncrypt(), TestResource.getResource("R_valuesAreDifferent"));
assertEquals(true, ds.getTrustServerCertificate(), TestResource.getResource("R_valuesAreDifferent"));
}
@Test
public void testEncryptedConnection() throws SQLException {
SQLServerDataSource ds = new SQLServerDataSource();
ds.setApplicationName("User");
ds.setURL(connectionString);
ds.setEncrypt(true);
ds.setTrustServerCertificate(true);
ds.setPacketSize(8192);
try (Connection con = ds.getConnection()) {}
}
@Test
public void testJdbcDataSourceMethod() throws SQLFeatureNotSupportedException {
SQLServerDataSource fxds = new SQLServerDataSource();
Logger logger = fxds.getParentLogger();
assertEquals(logger.getName(), Constants.MSSQL_JDBC_PACKAGE,
TestResource.getResource("R_parrentLoggerNameWrong"));
}
class MyEventListener implements javax.sql.ConnectionEventListener {
boolean connClosed = false;
boolean errorOccurred = false;
public MyEventListener() {}
public void connectionClosed(ConnectionEvent event) {
connClosed = true;
}
public void connectionErrorOccurred(ConnectionEvent event) {
errorOccurred = true;
}
}
/**
* Attach the Event listener and listen for connection events, fatal errors should not close the pooled connection
* objects
*
* @throws SQLException
*/
@Test
@Tag(Constants.xAzureSQLDW)
@Tag(Constants.xAzureSQLDB)
public void testConnectionEvents() throws SQLException {
SQLServerConnectionPoolDataSource mds = new SQLServerConnectionPoolDataSource();
mds.setURL(connectionString);
PooledConnection pooledConnection = mds.getPooledConnection();
// Attach the Event listener and listen for connection events.
MyEventListener myE = new MyEventListener();
pooledConnection.addConnectionEventListener(myE); // ConnectionListener implements ConnectionEventListener
try (Connection con = pooledConnection.getConnection();
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE)) {
boolean exceptionThrown = false;
try {
// raise a severe exception and make sure that the connection is not
// closed.
stmt.executeUpdate("RAISERROR ('foo', 20,1) WITH LOG");
} catch (Exception e) {
exceptionThrown = true;
}
assertTrue(exceptionThrown, TestResource.getResource("R_expectedExceptionNotThrown"));
// Check to see if error occurred.
assertTrue(myE.errorOccurred, TestResource.getResource("R_errorNotCalled"));
} finally {
// make sure that connection is closed.
if (null != pooledConnection)
pooledConnection.close();
}
}
@Test
@Tag(Constants.xAzureSQLDW)
@Tag(Constants.xAzureSQLDB)
public void testConnectionPoolGetTwice() throws SQLException {
SQLServerConnectionPoolDataSource mds = new SQLServerConnectionPoolDataSource();
mds.setURL(connectionString);
PooledConnection pooledConnection = mds.getPooledConnection();
// Attach the Event listener and listen for connection events.
MyEventListener myE = new MyEventListener();
pooledConnection.addConnectionEventListener(myE); // ConnectionListener implements ConnectionEventListener
try (Connection con = pooledConnection.getConnection();
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE)) {
// raise a non severe exception and make sure that the connection is not closed.
stmt.executeUpdate("RAISERROR ('foo', 3,1)");
// not a serious error there should not be any errors.
assertTrue(!myE.errorOccurred, TestResource.getResource("R_errorCalled"));
// check to make sure that connection is not closed.
assertTrue(!con.isClosed(), TestResource.getResource("R_connectionIsClosed"));
stmt.close();
con.close();
// check to make sure that connection is closed.
assertTrue(con.isClosed(), TestResource.getResource("R_connectionIsNotClosed"));
} finally {
// make sure that connection is closed.
if (null != pooledConnection)
pooledConnection.close();
}
}
@Test
@Tag(Constants.xAzureSQLDW)
@Tag(Constants.xAzureSQLDB)
public void testConnectionClosed() throws SQLException {
SQLServerDataSource mds = new SQLServerDataSource();
mds.setURL(connectionString);
try (Connection con = mds.getConnection();
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE)) {
boolean exceptionThrown = false;
try {
stmt.executeUpdate("RAISERROR ('foo', 20,1) WITH LOG");
} catch (Exception e) {
exceptionThrown = true;
}
assertTrue(exceptionThrown, TestResource.getResource("R_expectedExceptionNotThrown"));
// check to make sure that connection is closed.
assertTrue(con.isClosed(), TestResource.getResource("R_connectionIsNotClosed"));
} catch (Exception e) {
fail(TestResource.getResource("R_unexpectedErrorMessage") + e.getMessage());
}
}
@Test
public void testIsWrapperFor() throws SQLException, ClassNotFoundException {
try (Connection conn = getConnection(); SQLServerConnection ssconn = (SQLServerConnection) conn) {
boolean isWrapper;
isWrapper = ssconn.isWrapperFor(ssconn.getClass());
MessageFormat form = new MessageFormat(TestResource.getResource("R_supportUnwrapping"));
Object[] msgArgs1 = {"SQLServerConnection"};
assertTrue(isWrapper, form.format(msgArgs1));
assertEquals(ISQLServerConnection.TRANSACTION_SNAPSHOT, ISQLServerConnection.TRANSACTION_SNAPSHOT,
TestResource.getResource("R_cantAccessSnapshot"));
isWrapper = ssconn.isWrapperFor(Class.forName(Constants.MSSQL_JDBC_PACKAGE + ".ISQLServerConnection"));
Object[] msgArgs2 = {"ISQLServerConnection"};
assertTrue(isWrapper, form.format(msgArgs2));
ssconn.unwrap(Class.forName(Constants.MSSQL_JDBC_PACKAGE + ".ISQLServerConnection"));
assertEquals(ISQLServerConnection.TRANSACTION_SNAPSHOT, ISQLServerConnection.TRANSACTION_SNAPSHOT,
TestResource.getResource("R_cantAccessSnapshot"));
ssconn.unwrap(Class.forName("java.sql.Connection"));
}
}
@Test
public void testNewConnection() throws SQLException {
try (Connection conn = getConnection()) {
assertTrue(conn.isValid(0), TestResource.getResource("R_newConnectionShouldBeValid"));
}
}
@Test
public void testClosedConnection() throws SQLException {
try (Connection conn = getConnection()) {
conn.close();
assertTrue(!conn.isValid(0), TestResource.getResource("R_closedConnectionShouldBeInvalid"));
// getting shared timer on closed connection should fail
((SQLServerConnection) conn).getSharedTimer();
fail(TestResource.getResource("R_noExceptionClosedConnection"));
} catch (SQLServerException e) {
assertEquals(e.getMessage(), TestResource.getResource("R_connectionIsClosed"),
TestResource.getResource("R_wrongExceptionMessage"));
}
try (Connection conn = getConnection()) {
conn.close();
((SQLServerConnection) conn).checkClosed();
fail(TestResource.getResource("R_noExceptionClosedConnection"));
} catch (SQLServerException e) {
assertEquals(e.getMessage(), TestResource.getResource("R_connectionIsClosed"),
TestResource.getResource("R_wrongExceptionMessage"));
}
}
@Test
@Tag(Constants.xAzureSQLDW)
public void testSetTransactionIsolation() throws SQLException {
try (Connection conn = getConnection()) {
conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
assertTrue(conn.getTransactionIsolation() == Connection.TRANSACTION_READ_COMMITTED,
TestResource.getResource("R_expectedValue") + "Connection.TRANSACTION_READ_COMMITTED");
conn.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
assertTrue(conn.getTransactionIsolation() == Connection.TRANSACTION_READ_UNCOMMITTED,
TestResource.getResource("R_expectedValue") + "Connection.TRANSACTION_READ_UNCOMMITTED");
conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
assertTrue(conn.getTransactionIsolation() == Connection.TRANSACTION_REPEATABLE_READ,
TestResource.getResource("R_expectedValue") + "Connection.TRANSACTION_REPEATABLE_READ");
conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
assertTrue(conn.getTransactionIsolation() == Connection.TRANSACTION_SERIALIZABLE,
TestResource.getResource("R_expectedValue") + "Connection.TRANSACTION_SERIALIZABLE");
conn.setTransactionIsolation(SQLServerConnection.TRANSACTION_SNAPSHOT);
assertTrue(conn.getTransactionIsolation() == SQLServerConnection.TRANSACTION_SNAPSHOT,
TestResource.getResource("R_expectedValue") + "SQLServerConnection.TRANSACTION_SNAPSHOT");
}
}
@Test
public void testNativeSQL() throws SQLException {
try (Connection conn = getConnection()) {
String nativeSql = conn.nativeSQL("SELECT @@version");
assertTrue(nativeSql instanceof String);
} catch (Exception e) {
fail(TestResource.getResource("R_unexpectedErrorMessage") + e.getMessage());
}
}
@Test
public void testReadOnly() throws SQLException {
try (Connection conn = getConnection()) {
// not supported, will always return false
conn.setReadOnly(false);
assertTrue(!conn.isReadOnly(), TestResource.getResource("R_expectedValue") + Boolean.toString(false));
conn.setReadOnly(true);
assertTrue(!conn.isReadOnly(), TestResource.getResource("R_expectedValue") + Boolean.toString(false));
}
}
@Test
@Tag(Constants.xAzureSQLDW)
@Tag(Constants.xAzureSQLDB)
@Tag(Constants.xAzureSQLMI)
public void testCatalog() throws SQLException {
try (Connection conn = getConnection()) {
String catalog = "master";
conn.setCatalog(catalog);
assertTrue(conn.getCatalog().equals(catalog), TestResource.getResource("R_expectedValue") + catalog);
}
}
@Test
@SuppressWarnings({"rawtypes", "unchecked"})
public void testTypeMap() throws SQLException {
try (Connection conn = getConnection()) {
java.util.HashMap map = new java.util.HashMap();
conn.setTypeMap(map);
assertTrue(conn.getTypeMap().isEmpty(),
TestResource.getResource("R_expectedValue") + Boolean.toString(true));
}
}
@Test
public void testNegativeTimeout() throws Exception {
try (Connection conn = getConnection()) {
try {
conn.isValid(-42);
fail(TestResource.getResource("R_noExceptionNegativeTimeout"));
} catch (SQLException e) {
MessageFormat form = new MessageFormat(TestResource.getResource("R_invalidQueryTimeout"));
Object[] msgArgs = {"-42"};
assertEquals(e.getMessage(), form.format(msgArgs), TestResource.getResource("R_wrongExceptionMessage"));
}
}
}
@Test
@Tag(Constants.xAzureSQLDW)
@Tag(Constants.xAzureSQLDB)
public void testDeadConnection() throws SQLException {
String tableName = RandomUtil.getIdentifier("ConnectionTestTable");
try (Connection conn = PrepUtil.getConnection(connectionString + ";responseBuffering=adaptive");
Statement stmt = conn.createStatement()) {
conn.setAutoCommit(false);
stmt.executeUpdate(
"CREATE TABLE " + AbstractSQLGenerator.escapeIdentifier(tableName) + " (col1 int primary key)");
for (int i = 0; i < 80; i++) {
stmt.executeUpdate("INSERT INTO " + AbstractSQLGenerator.escapeIdentifier(tableName) + "(col1) values ("
+ i + ")");
}
conn.commit();
try {
stmt.execute("SELECT x1.col1 as foo, x2.col1 as bar, x1.col1 as eeep FROM "
+ AbstractSQLGenerator.escapeIdentifier(tableName) + " as x1, "
+ AbstractSQLGenerator.escapeIdentifier(tableName)
+ " as x2; RAISERROR ('Oops', 21, 42) WITH LOG");
} catch (SQLException e) {
assertEquals(e.getMessage(), TestResource.getResource("R_connectionReset"),
TestResource.getResource("R_unknownException"));
}
assertEquals(conn.isValid(5), false, TestResource.getResource("R_deadConnection"));
} catch (Exception e) {
fail(TestResource.getResource("R_unexpectedErrorMessage") + e.getMessage());
} finally {
if (null != tableName) {
try (Connection conn = PrepUtil.getConnection(connectionString + ";responseBuffering=adaptive");
Statement stmt = conn.createStatement()) {
stmt.execute("drop table " + AbstractSQLGenerator.escapeIdentifier(tableName));
}
}
}
}
@Test
public void testClientConnectionId() throws Exception {
try (ISQLServerConnection conn = (ISQLServerConnection) getConnection()) {
assertTrue(conn.getClientConnectionId() != null, TestResource.getResource("R_clientConnectionIdNull"));
conn.close();
try {
// Call getClientConnectionId on a closed connection, should raise exception
conn.getClientConnectionId();
fail(TestResource.getResource("R_noExceptionClosedConnection"));
} catch (SQLException e) {
assertEquals(e.getMessage(), TestResource.getResource("R_connectionIsClosed"),
TestResource.getResource("R_wrongExceptionMessage"));
}
}
// Wrong database, ClientConnectionId should be available in error message
try (Connection conn = PrepUtil.getConnection(connectionString + ";databaseName="
+ RandomUtil.getIdentifierForDB("DataBase") + Constants.SEMI_COLON)) {
conn.close();
} catch (SQLException e) {
assertTrue(e.getMessage().indexOf("ClientConnectionId") != -1,
TestResource.getResource("R_unexpectedWrongDB"));
}
// Non-existent host, ClientConnectionId should not be available in error message
try (Connection conn = PrepUtil.getConnection(
connectionString + ";instanceName=" + RandomUtil.getIdentifier("Instance") + ";logintimeout=5;")) {
conn.close();
} catch (SQLException e) {
assertEquals(false, e.getMessage().indexOf("ClientConnectionId") != -1,
TestResource.getResource("R_unexpectedWrongHost"));
}
}
@Test
public void testIncorrectDatabase() throws SQLException {
long timerStart = 0;
long timerEnd = 0;
final long milsecs = threshHoldForNoRetryInMilliseconds;
try {
SQLServerDataSource ds = new SQLServerDataSource();
ds.setURL(connectionString);
ds.setLoginTimeout(loginTimeOutInSeconds);
ds.setDatabaseName(RandomUtil.getIdentifier("DataBase"));
timerStart = System.currentTimeMillis();
try (Connection con = ds.getConnection()) {
long timeDiff = timerEnd - timerStart;
assertTrue(con == null, TestResource.getResource("R_shouldNotConnect"));
MessageFormat form = new MessageFormat(TestResource.getResource("R_exitedMoreSeconds"));
Object[] msgArgs = {milsecs / 1000};
assertTrue(timeDiff <= milsecs, form.format(msgArgs));
}
} catch (Exception e) {
assertTrue(e.getMessage().contains(TestResource.getResource("R_cannotOpenDatabase")));
timerEnd = System.currentTimeMillis();
}
}
@Test
public void testIncorrectUserName() throws SQLException {
long timerStart = 0;
long timerEnd = 0;
final long milsecs = threshHoldForNoRetryInMilliseconds;
try {
SQLServerDataSource ds = new SQLServerDataSource();
ds.setURL(connectionString);
ds.setLoginTimeout(loginTimeOutInSeconds);
ds.setUser(RandomUtil.getIdentifier("User"));
timerStart = System.currentTimeMillis();
try (Connection con = ds.getConnection()) {
long timeDiff = timerEnd - timerStart;
assertTrue(con == null, TestResource.getResource("R_shouldNotConnect"));
MessageFormat form = new MessageFormat(TestResource.getResource("R_exitedMoreSeconds"));
Object[] msgArgs = {milsecs / 1000};
assertTrue(timeDiff <= milsecs, form.format(msgArgs));
}
} catch (Exception e) {
assertTrue(e.getMessage().contains(TestResource.getResource("R_loginFailed")));
timerEnd = System.currentTimeMillis();
}
}
@Test
public void testIncorrectPassword() throws SQLException {
long timerStart = 0;
long timerEnd = 0;
final long milsecs = threshHoldForNoRetryInMilliseconds;
try {
SQLServerDataSource ds = new SQLServerDataSource();
ds.setURL(connectionString);
ds.setLoginTimeout(loginTimeOutInSeconds);
ds.setPassword(RandomUtil.getIdentifier("Password"));
timerStart = System.currentTimeMillis();
try (Connection con = ds.getConnection()) {
long timeDiff = timerEnd - timerStart;
assertTrue(con == null, TestResource.getResource("R_shouldNotConnect"));
MessageFormat form = new MessageFormat(TestResource.getResource("R_exitedMoreSeconds"));
Object[] msgArgs = {milsecs / 1000};
assertTrue(timeDiff <= milsecs, form.format(msgArgs));
}
} catch (Exception e) {
assertTrue(e.getMessage().contains(TestResource.getResource("R_loginFailed")));
timerEnd = System.currentTimeMillis();
}
}
@Test
public void testInvalidCombination() throws SQLException {
long timerStart = 0;
long timerEnd = 0;
final long milsecs = threshHoldForNoRetryInMilliseconds;
try {
SQLServerDataSource ds = new SQLServerDataSource();
ds.setURL(connectionString);
ds.setLoginTimeout(loginTimeOutInSeconds);
ds.setMultiSubnetFailover(true);
ds.setFailoverPartner(RandomUtil.getIdentifier("FailoverPartner"));
timerStart = System.currentTimeMillis();
try (Connection con = ds.getConnection()) {
long timeDiff = timerEnd - timerStart;
assertTrue(con == null, TestResource.getResource("R_shouldNotConnect"));
MessageFormat form = new MessageFormat(TestResource.getResource("R_exitedMoreSeconds"));
Object[] msgArgs = {milsecs / 1000};
assertTrue(timeDiff <= milsecs, form.format(msgArgs));
}
} catch (Exception e) {
assertTrue(e.getMessage().contains(TestResource.getResource("R_connectMirrored")));
timerEnd = System.currentTimeMillis();
}
}
@Test
@Tag("slow")
public void testIncorrectDatabaseWithFailoverPartner() throws SQLException {
long timerStart = 0;
long timerEnd = 0;
try {
SQLServerDataSource ds = new SQLServerDataSource();
ds.setURL(connectionString);
ds.setLoginTimeout(loginTimeOutInSeconds);
ds.setDatabaseName(RandomUtil.getIdentifierForDB("DB"));
ds.setFailoverPartner(RandomUtil.getIdentifier("FailoverPartner"));
timerStart = System.currentTimeMillis();
try (Connection con = ds.getConnection()) {
long timeDiff = timerEnd - timerStart;
assertTrue(con == null, TestResource.getResource("R_shouldNotConnect"));
MessageFormat form = new MessageFormat(TestResource.getResource("R_exitedLessSeconds"));
Object[] msgArgs = {loginTimeOutInSeconds - 1};
assertTrue(timeDiff >= ((loginTimeOutInSeconds - 1) * 1000), form.format(msgArgs));
}
} catch (Exception e) {
timerEnd = System.currentTimeMillis();
}
}
@Test
public void testAbortBadParam() throws SQLException {
try (Connection conn = getConnection()) {
try {
conn.abort(null);
} catch (SQLException e) {
assertTrue(e.getMessage().matches(TestUtils.formatErrorMsg("R_invalidArgument")));
}
}
}
@Test
public void testAbort() throws SQLException {
try (Connection conn = getConnection()) {
Executor executor = Executors.newFixedThreadPool(2);
conn.abort(executor);
assert (conn.isClosed());
// abort again on closed connection
conn.abort(executor);
}
}
@Test
public void testSetSchema() throws SQLException {
try (Connection conn = getConnection()) {
conn.setSchema(RandomUtil.getIdentifier("schema"));
}
}
@Test
public void testGetSchema() throws SQLException {
try (Connection conn = getConnection()) {
conn.getSchema();
}
}
static Boolean isInterrupted = false;
/**
* Test thread's interrupt status is not cleared.
*
* @throws InterruptedException
*/
@Test
@Tag("slow")
public void testThreadInterruptedStatus() throws InterruptedException {
Runnable runnable = new Runnable() {
public void run() {
SQLServerDataSource ds = new SQLServerDataSource();
ds.setURL(connectionString);
ds.setServerName("invalidServerName" + UUID.randomUUID());
ds.setLoginTimeout(5);
try (Connection con = ds.getConnection()) {} catch (SQLException e) {
isInterrupted = Thread.currentThread().isInterrupted();
}
}
};
ExecutorService executor = Executors.newFixedThreadPool(1);
Future<?> future = executor.submit(runnable);
Thread.sleep(1000);
// interrupt the thread in the Runnable
future.cancel(true);
Thread.sleep(8000);
executor.shutdownNow();
assertTrue(isInterrupted, TestResource.getResource("R_threadInterruptNotSet"));
}
}