TableMetaDataManager { String getSchemaNameFromMetaData(DatabaseMetaData metaData, RegionMapping regionMapping, String catalogFilter) throws SQLException { String schemaFilter = regionMapping.getSchema(); if (schemaFilter == null || schemaFilter.isEmpty()) { if ("PostgreSQL".equals(metaData.getDatabaseProductName())) { schemaFilter = "public"; } else { return DEFAULT_SCHEMA; } } try (ResultSet schemas = metaData.getSchemas(catalogFilter, "%")) { return findMatchInResultSet(schemaFilter, schemas, "TABLE_SCHEM", "schema"); } }  TableMetaDataView getTableMetaDataView(Connection connection,
      RegionMapping regionMapping);  }
@Test public void getSchemaNameFromMetaDataGivenNullSchemaReturnsEmptyString() throws SQLException { when(regionMapping.getSchema()).thenReturn(null); String result = tableMetaDataManager.getSchemaNameFromMetaData(databaseMetaData, regionMapping, null); assertThat(result).isEqualTo(""); }
@Test public void getSchemaNameFromMetaDataGivenEmptySchemaReturnsEmptyString() throws SQLException { when(regionMapping.getSchema()).thenReturn(""); String result = tableMetaDataManager.getSchemaNameFromMetaData(databaseMetaData, regionMapping, null); assertThat(result).isEqualTo(""); }
@Test public void getSchemaNameFromMetaDataGivenSchemaReturnsIt() throws SQLException { String mySchema = "mySchema"; when(regionMapping.getSchema()).thenReturn(mySchema); ResultSet schemasResultSet = mock(ResultSet.class); when(schemasResultSet.next()).thenReturn(true).thenReturn(false); when(schemasResultSet.getString("TABLE_SCHEM")).thenReturn(mySchema); String catalogFilter = "myCatalogFilter"; when(databaseMetaData.getSchemas(catalogFilter, "%")).thenReturn(schemasResultSet); String result = tableMetaDataManager.getSchemaNameFromMetaData(databaseMetaData, regionMapping, catalogFilter); assertThat(result).isEqualTo(mySchema); }
@Test public void getSchemaNameFromMetaDataGivenNullSchemaOnPostgresReturnsPublic() throws SQLException { String defaultPostgresSchema = "public"; when(regionMapping.getSchema()).thenReturn(null); ResultSet schemasResultSet = mock(ResultSet.class); when(schemasResultSet.next()).thenReturn(true).thenReturn(false); when(schemasResultSet.getString("TABLE_SCHEM")).thenReturn(defaultPostgresSchema); String catalogFilter = "myCatalogFilter"; when(databaseMetaData.getSchemas(catalogFilter, "%")).thenReturn(schemasResultSet); when(databaseMetaData.getDatabaseProductName()).thenReturn("PostgreSQL"); String result = tableMetaDataManager.getSchemaNameFromMetaData(databaseMetaData, regionMapping, catalogFilter); assertThat(result).isEqualTo(defaultPostgresSchema); }