diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/databasemetadata/DatabaseMetaDataTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/databasemetadata/DatabaseMetaDataTest.java index afb0547b2..403ccfd79 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/databasemetadata/DatabaseMetaDataTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/databasemetadata/DatabaseMetaDataTest.java @@ -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(); } /** @@ -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(); } /**