Skip to content

Commit

Permalink
Added test
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffery-Wasty committed Aug 18, 2023
1 parent 89e6094 commit 3394474
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1518,7 +1518,7 @@ public java.sql.ResultSet getSchemas(String catalog, String schemaPattern) throw
if (loggerExternal.isLoggable(Level.FINER) && Util.isActivityTraceOn()) {
loggerExternal.finer(toString() + ACTIVITY_ID + ActivityCorrelator.getCurrent().toString());
}
return getSchemasInternal(catalog, schemaPattern);
return getSchemasInternal(catalog, escapeIDName(schemaPattern));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,57 @@ public void testDBSchemasForDashedCatalogNameWithPattern() throws SQLException {
}
}

/**
* Tests that the schemaPattern parameter containing _ and % are escaped by
* {@link SQLServerDatabaseMetaData#getSchemas(String catalog, String schemaPattern)}.
*
* @throws SQLException
*/
@Test
@Tag(Constants.xAzureSQLDW)
@Tag(Constants.xAzureSQLDB)
public void testDBSchemasForSchemaPatternWithWildcards() throws SQLException {
UUID id = UUID.randomUUID();
String testCatalog = "catalog" + id;
String[] schemas = {"some_schema", "some%schema", "some[schema"};
String[] schemaPatterns = {"some\\_schema", "some\\%schema", "some\\[schema"};

try (Connection conn = getConnection(); Statement stmt = conn.createStatement()) {
TestUtils.dropDatabaseIfExists(testCatalog, connectionString);
stmt.execute(String.format("CREATE DATABASE [%s]", testCatalog));
stmt.execute(String.format("USE [%s]", testCatalog));

for (int i = 0; i < schemas.length; ++i) {
stmt.execute(String.format("CREATE SCHEMA [%s]", schemas[i]));

try (ResultSet rs = conn.getMetaData().getSchemas(testCatalog, schemaPatterns[i])) {

MessageFormat schemaEmptyFormat = new MessageFormat(TestResource.getResource("R_nameEmpty"));
Object[] schemaMsgArgs = {schemas[i]};
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));
}
}
} catch (Exception e) {
fail(TestResource.getResource("R_unexpectedErrorMessage") + e.getMessage());
} finally {
TestUtils.dropDatabaseIfExists(testCatalog, connectionString);
}
}

/**
* Get All Tables.
*
Expand Down

0 comments on commit 3394474

Please sign in to comment.