forked from microsoft/mssql-jdbc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDatabaseMetaDataTest.java
565 lines (494 loc) · 25.4 KB
/
DatabaseMetaDataTest.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
/*
* 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.databasemetadata;
import static org.junit.Assert.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.MessageFormat;
import java.util.UUID;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
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.jdbc.SQLServerDatabaseMetaData;
import com.microsoft.sqlserver.jdbc.StringUtils;
import com.microsoft.sqlserver.jdbc.TestResource;
import com.microsoft.sqlserver.jdbc.TestUtils;
import com.microsoft.sqlserver.testframework.AbstractTest;
import com.microsoft.sqlserver.testframework.Constants;
/**
* Test class for testing DatabaseMetaData.
*/
@RunWith(JUnitPlatform.class)
public class DatabaseMetaDataTest extends AbstractTest {
/**
* Verify DatabaseMetaData#isWrapperFor and DatabaseMetaData#unwrap.
*
* @throws SQLException
*/
@Test
public void testDatabaseMetaDataWrapper() throws SQLException {
try (Connection con = getConnection()) {
DatabaseMetaData databaseMetaData = con.getMetaData();
assertTrue(databaseMetaData.isWrapperFor(DatabaseMetaData.class));
assertSame(databaseMetaData, databaseMetaData.unwrap(DatabaseMetaData.class));
}
}
/**
* Testing if driver version is matching with manifest file or not. Will be useful while releasing preview / RTW
* release.
*
* //TODO: OSGI: Test for capability 1.7 for JDK 1.7 and 1.8 for 1.8 //Require-Capability:
* osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.8))" //String capability =
* attributes.getValue("Require-Capability");
*
* @throws SQLException
* SQL Exception
* @throws IOException
* IOExcption
*/
@Test
public void testDriverVersion() throws SQLException, IOException {
String manifestFile = TestUtils.getCurrentClassPath() + "META-INF/MANIFEST.MF";
manifestFile = manifestFile.replace("test-classes", "classes");
File f = new File(manifestFile);
try (InputStream in = new BufferedInputStream(new FileInputStream(f))) {
Manifest manifest = new Manifest(in);
Attributes attributes = manifest.getMainAttributes();
String buildVersion = attributes.getValue("Bundle-Version");
try (Connection conn = getConnection()) {
DatabaseMetaData dbmData = conn.getMetaData();
String driverVersion = dbmData.getDriverVersion();
// boolean isSnapshot = buildVersion.contains("SNAPSHOT");
// Removing all dots & chars easy for comparing.
driverVersion = driverVersion.replaceAll("[^0-9]", "");
buildVersion = buildVersion.replaceAll("[^0-9]", "");
// Not comparing last build number. We will compare only major.minor.patch
driverVersion = driverVersion.substring(0, 3);
buildVersion = buildVersion.substring(0, 3);
int intBuildVersion = Integer.valueOf(buildVersion);
int intDriverVersion = Integer.valueOf(driverVersion);
assertTrue(intDriverVersion == intBuildVersion, TestResource.getResource("R_buildVersionError"));
}
}
}
/**
* Your password should not be in getURL method.
*
* @throws SQLException
*/
@Test
public void testGetURL() throws SQLException {
try (Connection conn = getConnection()) {
DatabaseMetaData databaseMetaData = conn.getMetaData();
String url = databaseMetaData.getURL();
url = url.toLowerCase();
assertFalse(url.contains("password"), TestResource.getResource("R_getURLContainsPwd"));
} catch (Exception e) {
fail(TestResource.getResource("R_unexpectedErrorMessage") + e.getMessage());
}
}
/**
* Test getUsername.
*
* @throws SQLException
*/
@Test
@Tag(Constants.xAzureSQLDW)
@Tag(Constants.xAzureSQLDB)
public void testDBUserLogin() throws SQLException {
try (Connection conn = getConnection()) {
DatabaseMetaData databaseMetaData = conn.getMetaData();
String connectionString = getConnectionString();
connectionString = connectionString.toLowerCase();
int startIndex = 0;
int endIndex = 0;
if (connectionString.contains("username")) {
startIndex = connectionString.indexOf("username=");
endIndex = connectionString.indexOf(Constants.SEMI_COLON, startIndex);
startIndex = startIndex + "username=".length();
} else if (connectionString.contains("user")) {
startIndex = connectionString.indexOf("user=");
endIndex = connectionString.indexOf(Constants.SEMI_COLON, startIndex);
startIndex = startIndex + "user=".length();
}
String userFromConnectionString = connectionString.substring(startIndex, endIndex);
String userName = databaseMetaData.getUserName();
assertNotNull(userName, TestResource.getResource("R_userNameNull"));
assertTrue(userName.equalsIgnoreCase(userFromConnectionString),
TestResource.getResource("R_userNameNotMatch"));
} catch (Exception e) {
fail(TestResource.getResource("R_unexpectedErrorMessage") + e.getMessage());
}
}
/**
* Testing of {@link SQLServerDatabaseMetaData#getSchemas()}
*
* @throws SQLException
*/
@Test
public void testDBSchema() throws SQLException {
try (Connection conn = getConnection(); ResultSet rs = conn.getMetaData().getSchemas()) {
MessageFormat form = new MessageFormat(TestResource.getResource("R_nameEmpty"));
Object[] msgArgs = {"Schema"};
while (rs.next()) {
assertTrue(!StringUtils.isEmpty(rs.getString(1)), form.format(msgArgs));
}
} catch (Exception e) {
fail(TestResource.getResource("R_unexpectedErrorMessage") + e.getMessage());
}
}
/**
* Tests that the catalog parameter containing - is escaped by
* {@link SQLServerDatabaseMetaData#getSchemas(String catalog, String schemaPattern)}.
*
* @throws SQLException
*/
@Test
@Tag(Constants.xAzureSQLDW)
@Tag(Constants.xAzureSQLDB)
public void testDBSchemasForDashedCatalogName() throws SQLException {
UUID id = UUID.randomUUID();
String testCatalog = "dash-catalog" + id;
String testSchema = "some-schema" + id;
try (Connection conn = getConnection(); Statement stmt = connection.createStatement()) {
TestUtils.dropDatabaseIfExists(testCatalog, stmt);
stmt.execute(String.format("CREATE DATABASE [%s]", testCatalog));
stmt.execute(String.format("USE [%s]", testCatalog));
stmt.execute(String.format("CREATE SCHEMA [%s]", testSchema));
try (ResultSet rs = conn.getMetaData().getSchemas(testCatalog, null)) {
MessageFormat schemaEmptyFormat = new MessageFormat(TestResource.getResource("R_nameEmpty"));
Object[] schemaMsgArgs = {"Schema"};
boolean hasResults = false;
boolean hasDashCatalogSchema = false;
while (rs.next()) {
hasResults = true;
String schemaName = rs.getString(1);
assertTrue(!StringUtils.isEmpty(schemaName), schemaEmptyFormat.format(schemaMsgArgs));
String catalogName = rs.getString(2);
if (schemaName.equals(testSchema)) {
hasDashCatalogSchema = true;
assertEquals(catalogName, testCatalog);
} else {
assertNull(catalogName);
}
}
MessageFormat atLeastOneFoundFormat = new MessageFormat(TestResource.getResource("R_atLeastOneFound"));
assertTrue(hasResults, atLeastOneFoundFormat.format(schemaMsgArgs));
MessageFormat dashCatalogFormat = new MessageFormat(TestResource.getResource("R_atLeastOneFound"));
assertTrue(hasDashCatalogSchema, dashCatalogFormat.format(new Object[] {testSchema}));
} finally {
TestUtils.dropDatabaseIfExists(testCatalog, stmt);
}
} catch (Exception e) {
fail(TestResource.getResource("R_unexpectedErrorMessage") + e.getMessage());
}
}
/**
* Tests that the catalog parameter containing - is escaped by
* {@link SQLServerDatabaseMetaData#getSchemas(String catalog, String schemaPattern)}.
*
* @throws SQLException
*/
@Test
@Tag(Constants.xAzureSQLDW)
@Tag(Constants.xAzureSQLDB)
public void testDBSchemasForDashedCatalogNameWithPattern() throws SQLException {
UUID id = UUID.randomUUID();
String testCatalog = "dash-catalog" + id;
String testSchema = "some-schema" + id;
try (Connection conn = getConnection(); Statement stmt = conn.createStatement()) {
TestUtils.dropDatabaseIfExists(testCatalog, stmt);
stmt.execute(String.format("CREATE DATABASE [%s]", testCatalog));
stmt.execute(String.format("USE [%s]", testCatalog));
stmt.execute(String.format("CREATE SCHEMA [%s]", testSchema));
try (ResultSet rs = conn.getMetaData().getSchemas(testCatalog, "some-%")) {
MessageFormat schemaEmptyFormat = new MessageFormat(TestResource.getResource("R_nameEmpty"));
Object[] schemaMsgArgs = {testSchema};
Object[] catalogMsgArgs = {testCatalog};
boolean hasResults = false;
while (rs.next()) {
hasResults = true;
String schemaName = rs.getString(1);
String catalogName = rs.getString(2);
assertTrue(!StringUtils.isEmpty(schemaName), schemaEmptyFormat.format(schemaMsgArgs));
assertTrue(!StringUtils.isEmpty(catalogName), schemaEmptyFormat.format(catalogMsgArgs));
assertEquals(schemaName, schemaMsgArgs[0]);
assertEquals(catalogName, catalogMsgArgs[0]);
}
MessageFormat atLeastOneFoundFormat = new MessageFormat(TestResource.getResource("R_atLeastOneFound"));
assertTrue(hasResults, atLeastOneFoundFormat.format(schemaMsgArgs));
} finally {
TestUtils.dropDatabaseIfExists(testCatalog, stmt);
}
} catch (Exception e) {
fail(TestResource.getResource("R_unexpectedErrorMessage") + e.getMessage());
}
}
/**
* Get All Tables.
*
* @throws SQLException
*/
@Test
/*
* try (ResultSet rsCatalog = connection.getMetaData().getCatalogs(); ResultSet rs = connection.getMetaData()
* .getTables(rsCatalog.getString("TABLE_CAT"), null, "%", new String[] {"TABLE"})) {
*/
public void testDBTables() throws SQLException {
try (Connection con = getConnection()) {
DatabaseMetaData databaseMetaData = con.getMetaData();
try (ResultSet rsCatalog = databaseMetaData.getCatalogs()) {
MessageFormat form1 = new MessageFormat(TestResource.getResource("R_atLeastOneFound"));
Object[] msgArgs1 = {"catalog"};
assertTrue(rsCatalog.next(), form1.format(msgArgs1));
String[] types = {"TABLE"};
try (ResultSet rs = databaseMetaData.getTables(rsCatalog.getString("TABLE_CAT"), null, "%", types)) {
MessageFormat form2 = new MessageFormat(TestResource.getResource("R_nameEmpty"));
Object[] msgArgs2 = {"Table"};
while (rs.next()) {
assertTrue(!StringUtils.isEmpty(rs.getString("TABLE_NAME")), form2.format(msgArgs2));
}
}
}
}
}
/**
* Testing DB Columns.
* <p>
* We can Improve this test scenario by following way.
* <ul>
* <li>Create table with appropriate column size, data types,auto increment, NULLABLE etc.
* <li>Then get databasemetatadata.getColumns to see if there is any mismatch.
* </ul>
*
* @throws SQLException
*/
@Test
public void testGetDBColumn() throws SQLException {
try (Connection conn = getConnection()) {
DatabaseMetaData databaseMetaData = conn.getMetaData();
String[] types = {"TABLE"};
try (ResultSet rs = databaseMetaData.getTables(null, null, "%", types)) {
// Fetch one table
MessageFormat form1 = new MessageFormat(TestResource.getResource("R_atLeastOneFound"));
Object[] msgArgs1 = {"table"};
assertTrue(rs.next(), form1.format(msgArgs1));
// Go through all columns.
try (ResultSet rs1 = databaseMetaData.getColumns(null, null, rs.getString("TABLE_NAME"), "%")) {
MessageFormat form2 = new MessageFormat(TestResource.getResource("R_nameEmpty"));
Object[][] msgArgs2 = {{"Category"}, {"SCHEMA"}, {"Table"}, {"COLUMN"}, {"Data Type"}, {"Type"},
{"Column Size"}, {"Nullable value"}, {"IS_NULLABLE"}, {"IS_AUTOINCREMENT"}};
while (rs1.next()) {
assertTrue(!StringUtils.isEmpty(rs1.getString("TABLE_CAT")), form2.format(msgArgs2[0]));
assertTrue(!StringUtils.isEmpty(rs1.getString("TABLE_SCHEM")), form2.format(msgArgs2[1]));
assertTrue(!StringUtils.isEmpty(rs1.getString("TABLE_NAME")), form2.format(msgArgs2[2]));
assertTrue(!StringUtils.isEmpty(rs1.getString("COLUMN_NAME")), form2.format(msgArgs2[3]));
assertTrue(!StringUtils.isEmpty(rs1.getString("DATA_TYPE")), form2.format(msgArgs2[4]));
assertTrue(!StringUtils.isEmpty(rs1.getString("TYPE_NAME")), form2.format(msgArgs2[5]));
assertTrue(!StringUtils.isEmpty(rs1.getString("COLUMN_SIZE")), form2.format(msgArgs2[6]));
assertTrue(!StringUtils.isEmpty(rs1.getString("NULLABLE")), form2.format(msgArgs2[7])); // 11
assertTrue(!StringUtils.isEmpty(rs1.getString("IS_NULLABLE")), form2.format(msgArgs2[8])); // 18
assertTrue(!StringUtils.isEmpty(rs1.getString("IS_AUTOINCREMENT")), form2.format(msgArgs2[9])); // 22
}
}
}
} catch (Exception e) {
fail(TestResource.getResource("R_unexpectedErrorMessage") + e.getMessage());
}
}
/**
* We can improve this test case by following manner:
* <ul>
* <li>We can check if PRIVILEGE is in between CRUD / REFERENCES / SELECT / INSERT etc.
* <li>IS_GRANTABLE can have only 2 values YES / NO
* </ul>
*
* @throws SQLException
*/
@Test
@Tag(Constants.xAzureSQLDW)
public void testGetColumnPrivileges() throws SQLException {
try (Connection conn = getConnection()) {
DatabaseMetaData databaseMetaData = conn.getMetaData();
String[] types = {"TABLE"};
try (ResultSet rsTables = databaseMetaData.getTables(null, null, "%", types)) {
// Fetch one table
MessageFormat form1 = new MessageFormat(TestResource.getResource("R_atLeastOneFound"));
Object[] msgArgs1 = {"table"};
assertTrue(rsTables.next(), form1.format(msgArgs1));
try (ResultSet rs1 = databaseMetaData.getColumnPrivileges(null, null, rsTables.getString("TABLE_NAME"),
"%")) {
MessageFormat form2 = new MessageFormat(TestResource.getResource("R_nameEmpty"));
Object[][] msgArgs2 = {{"Category"}, {"SCHEMA"}, {"Table"}, {"COLUMN"}, {"GRANTOR"}, {"GRANTEE"},
{"PRIVILEGE"}, {"IS_GRANTABLE"}};
while (rs1.next()) {
assertTrue(!StringUtils.isEmpty(rs1.getString("TABLE_CAT")), form2.format(msgArgs2[0]));
assertTrue(!StringUtils.isEmpty(rs1.getString("TABLE_SCHEM")), form2.format(msgArgs2[1]));
assertTrue(!StringUtils.isEmpty(rs1.getString("TABLE_NAME")), form2.format(msgArgs2[2]));
assertTrue(!StringUtils.isEmpty(rs1.getString("COLUMN_NAME")), form2.format(msgArgs2[3]));
assertTrue(!StringUtils.isEmpty(rs1.getString("GRANTOR")), form2.format(msgArgs2[4]));
assertTrue(!StringUtils.isEmpty(rs1.getString("GRANTEE")), form2.format(msgArgs2[5]));
assertTrue(!StringUtils.isEmpty(rs1.getString("PRIVILEGE")), form2.format(msgArgs2[6]));
assertTrue(!StringUtils.isEmpty(rs1.getString("IS_GRANTABLE")), form2.format(msgArgs2[7]));
}
}
}
} catch (Exception e) {
fail(TestResource.getResource("R_unexpectedErrorMessage") + e.getMessage());
}
}
/**
* Testing {@link SQLServerDatabaseMetaData#getFunctions(String, String, String)} with sending wrong catalog.
*
* @throws SQLException
*/
@Test
public void testGetFunctionsWithWrongParams() throws SQLException {
try (Connection conn = getConnection()) {
conn.getMetaData().getFunctions("", null, "xp_%");
fail(TestResource.getResource("R_noSchemaShouldFail"));
} catch (SQLException e) {
assert (e.getMessage().contains(TestResource.getResource("R_invalidArgumentCatalog")));
}
}
/**
* Test {@link SQLServerDatabaseMetaData#getFunctions(String, String, String)}
*
* @throws SQLException
*/
@Test
public void testGetFunctions() throws SQLException {
try (Connection conn = getConnection(); ResultSet rs = conn.getMetaData().getFunctions(null, null, "xp_%")) {
MessageFormat form = new MessageFormat(TestResource.getResource("R_nameNull"));
Object[][] msgArgs = {{"FUNCTION_CAT"}, {"FUNCTION_SCHEM"}, {"FUNCTION_NAME"}, {"NUM_INPUT_PARAMS"},
{"NUM_OUTPUT_PARAMS"}, {"NUM_RESULT_SETS"}, {"FUNCTION_TYPE"}};
while (rs.next()) {
assertTrue(!StringUtils.isEmpty(rs.getString("FUNCTION_CAT")), form.format(msgArgs[0]));
assertTrue(!StringUtils.isEmpty(rs.getString("FUNCTION_SCHEM")), form.format(msgArgs[1]));
assertTrue(!StringUtils.isEmpty(rs.getString("FUNCTION_NAME")), form.format(msgArgs[2]));
assertTrue(!StringUtils.isEmpty(rs.getString("NUM_INPUT_PARAMS")), form.format(msgArgs[3]));
assertTrue(!StringUtils.isEmpty(rs.getString("NUM_OUTPUT_PARAMS")), form.format(msgArgs[4]));
assertTrue(!StringUtils.isEmpty(rs.getString("NUM_RESULT_SETS")), form.format(msgArgs[5]));
assertTrue(!StringUtils.isEmpty(rs.getString("FUNCTION_TYPE")), form.format(msgArgs[6]));
}
} catch (Exception e) {
fail(TestResource.getResource("R_unexpectedErrorMessage") + e.getMessage());
}
}
/**
*
* @throws SQLException
*/
@Test
public void testGetFunctionColumns() throws SQLException {
try (Connection conn = getConnection()) {
DatabaseMetaData databaseMetaData = conn.getMetaData();
try (ResultSet rsFunctions = databaseMetaData.getFunctions(null, null, "%")) {
// Fetch one Function
MessageFormat form1 = new MessageFormat(TestResource.getResource("R_atLeastOneFound"));
Object[] msgArgs1 = {"function"};
assertTrue(rsFunctions.next(), form1.format(msgArgs1));
try (ResultSet rs = databaseMetaData.getFunctionColumns(null, null,
rsFunctions.getString("FUNCTION_NAME"), "%")) {
MessageFormat form2 = new MessageFormat(TestResource.getResource("R_nameNull"));
Object[][] msgArgs2 = {{"FUNCTION_CAT"}, {"FUNCTION_SCHEM"}, {"FUNCTION_NAME"}, {"COLUMN_NAME"},
{"COLUMN_TYPE"}, {"DATA_TYPE"}, {"TYPE_NAME"}, {"NULLABLE"}, {"IS_NULLABLE"}};
while (rs.next()) {
assertTrue(!StringUtils.isEmpty(rs.getString("FUNCTION_CAT")), form2.format(msgArgs2[0]));
assertTrue(!StringUtils.isEmpty(rs.getString("FUNCTION_SCHEM")), form2.format(msgArgs2[1]));
assertTrue(!StringUtils.isEmpty(rs.getString("FUNCTION_NAME")), form2.format(msgArgs2[2]));
assertTrue(!StringUtils.isEmpty(rs.getString("COLUMN_NAME")), form2.format(msgArgs2[3]));
assertTrue(!StringUtils.isEmpty(rs.getString("COLUMN_TYPE")), form2.format(msgArgs2[4]));
assertTrue(!StringUtils.isEmpty(rs.getString("DATA_TYPE")), form2.format(msgArgs2[5]));
assertTrue(!StringUtils.isEmpty(rs.getString("TYPE_NAME")), form2.format(msgArgs2[6]));
assertTrue(!StringUtils.isEmpty(rs.getString("NULLABLE")), form2.format(msgArgs2[7])); // 12
assertTrue(!StringUtils.isEmpty(rs.getString("IS_NULLABLE")), form2.format(msgArgs2[8])); // 19
}
}
}
} catch (Exception e) {
fail(TestResource.getResource("R_unexpectedErrorMessage") + e.getMessage());
}
}
@Test
@Tag(Constants.xAzureSQLDW)
@Tag(Constants.xAzureSQLDB)
public void testPreparedStatementMetadataCaching() throws SQLException {
try (Connection connection = getConnection()) {
DatabaseMetaData databaseMetaData = connection.getMetaData();
String[] types = {"TABLE"};
Statement stmtNullCatalog;
Statement stmtMasterCatalog;
try (ResultSet rs = databaseMetaData.getTables(null, null, "%", types)) {
stmtNullCatalog = rs.getStatement();
}
try (ResultSet rs = databaseMetaData.getTables(null, null, "%", types)) {
assertSame(stmtNullCatalog, rs.getStatement());
rs.getStatement().close();
}
try (ResultSet rs = databaseMetaData.getTables(null, null, "%", types)) {
assertNotSame(stmtNullCatalog, rs.getStatement());
stmtNullCatalog = rs.getStatement();
}
try (ResultSet rs = databaseMetaData.getTables(null, null, "%", types)) {
assertSame(stmtNullCatalog, rs.getStatement());
}
try (ResultSet rs = databaseMetaData.getTables("master", null, "%", types)) {
stmtMasterCatalog = rs.getStatement();
}
try (ResultSet rs = databaseMetaData.getTables("master", null, "%", types)) {
assertSame(stmtMasterCatalog, rs.getStatement());
rs.getStatement().close();
}
try (ResultSet rs = databaseMetaData.getTables("master", null, "%", types)) {
assertNotSame(stmtMasterCatalog, rs.getStatement());
stmtMasterCatalog = rs.getStatement();
}
try (ResultSet rs = databaseMetaData.getTables("master", null, "%", types)) {
assertSame(stmtMasterCatalog, rs.getStatement());
}
try (ResultSet rs = databaseMetaData.getTables(null, null, "%", types)) {
assertSame(stmtNullCatalog, rs.getStatement());
rs.getStatement().close();
}
try (ResultSet rs = databaseMetaData.getTables("master", null, "%", types)) {
assertSame(stmtMasterCatalog, rs.getStatement());
rs.getStatement().close();
}
}
}
@Test
@Tag(Constants.xAzureSQLDW)
public void testGetMaxConnections() throws SQLException {
try (Statement stmt = connection.createStatement(); ResultSet rs = stmt
.executeQuery("select maximum from sys.configurations where name = 'user connections'")) {
assert (null != rs);
rs.next();
DatabaseMetaData databaseMetaData = connection.getMetaData();
int maxConn = databaseMetaData.getMaxConnections();
assertEquals(maxConn, rs.getInt(1));
} catch (SQLException e) {
fail(e.getMessage());
}
}
}