Skip to content

Commit

Permalink
added try-with-resources nest
Browse files Browse the repository at this point in the history
avoid manually closing statements, and safetly handles resources.
  • Loading branch information
rene-ye committed Jun 18, 2018
1 parent 4c85049 commit fc78177
Showing 1 changed file with 64 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,48 +190,48 @@ public void testDBSchemasForDashedCatalogName() throws SQLException {
UUID id = UUID.randomUUID();
String testCatalog = "dash-catalog" + id;
String testSchema = "some-schema" + id;
Statement stmt = connection.createStatement();

try (Connection dashConn = DriverManager.getConnection(connectionString); Statement dashStatement = dashConn.createStatement()) {

Utils.dropDatabaseIfExists(testCatalog, stmt);
stmt.execute(String.format("CREATE DATABASE [%s]", testCatalog));

dashStatement.execute(String.format("USE [%s]", testCatalog));
dashStatement.execute(String.format("CREATE SCHEMA [%s]", testSchema));

DatabaseMetaData databaseMetaData = connection.getMetaData();
ResultSet rs = databaseMetaData.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);
try (Statement stmt = connection.createStatement()) {
try (Connection dashConn = DriverManager.getConnection(connectionString); Statement dashStatement = dashConn.createStatement()) {

Utils.dropDatabaseIfExists(testCatalog, stmt);
stmt.execute(String.format("CREATE DATABASE [%s]", testCatalog));

dashStatement.execute(String.format("USE [%s]", testCatalog));
dashStatement.execute(String.format("CREATE SCHEMA [%s]", testSchema));

DatabaseMetaData databaseMetaData = connection.getMetaData();
ResultSet rs = databaseMetaData.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 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 {
Utils.dropDatabaseIfExists(testCatalog, stmt);
MessageFormat dashCatalogFormat = new MessageFormat(TestResource.getResource("R_atLeastOneFound"));
assertTrue(hasDashCatalogSchema, dashCatalogFormat.format(new Object[] {testSchema}));
}
finally {
Utils.dropDatabaseIfExists(testCatalog, stmt);
}
}
stmt.close();
}

/**
Expand All @@ -244,41 +244,41 @@ public void testDBSchemasForDashedCatalogNameWithPattern() throws SQLException {
UUID id = UUID.randomUUID();
String testCatalog = "dash-catalog" + id;
String testSchema = "some-schema" + id;
Statement stmt = connection.createStatement();

try (Connection dashConn = DriverManager.getConnection(connectionString); Statement dashStatement = dashConn.createStatement()) {
try (Statement stmt = connection.createStatement()) {
try (Connection dashConn = DriverManager.getConnection(connectionString); Statement dashStatement = dashConn.createStatement()) {

Utils.dropDatabaseIfExists(testCatalog, stmt);
stmt.execute(String.format("CREATE DATABASE [%s]", testCatalog));
Utils.dropDatabaseIfExists(testCatalog, stmt);
stmt.execute(String.format("CREATE DATABASE [%s]", testCatalog));

dashStatement.execute(String.format("USE [%s]", testCatalog));
dashStatement.execute(String.format("CREATE SCHEMA [%s]", testSchema));
dashStatement.execute(String.format("USE [%s]", testCatalog));
dashStatement.execute(String.format("CREATE SCHEMA [%s]", testSchema));

DatabaseMetaData databaseMetaData = connection.getMetaData();
ResultSet rs = databaseMetaData.getSchemas(testCatalog, "some-%");
DatabaseMetaData databaseMetaData = connection.getMetaData();
ResultSet rs = databaseMetaData.getSchemas(testCatalog, "some-%");

MessageFormat schemaEmptyFormat = new MessageFormat(TestResource.getResource("R_nameEmpty"));
Object[] schemaMsgArgs = {testSchema};
Object[] catalogMsgArgs = {testCatalog};
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]);
}
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 {
Utils.dropDatabaseIfExists(testCatalog, stmt);
MessageFormat atLeastOneFoundFormat = new MessageFormat(TestResource.getResource("R_atLeastOneFound"));
assertTrue(hasResults, atLeastOneFoundFormat.format(schemaMsgArgs));
}
finally {
Utils.dropDatabaseIfExists(testCatalog, stmt);
}
}
stmt.close();
}

/**
Expand Down

0 comments on commit fc78177

Please sign in to comment.