TableMetaDataLoader { public static Optional<TableMetaData> load(final DataSource dataSource, final String tableNamePattern, final String databaseType) throws SQLException { try (MetaDataConnection connection = new MetaDataConnection(dataSource.getConnection())) { if (!isTableExist(connection, tableNamePattern, databaseType)) { return Optional.empty(); } return Optional.of(new TableMetaData(ColumnMetaDataLoader.load(connection, tableNamePattern, databaseType), IndexMetaDataLoader.load(connection, tableNamePattern, databaseType))); } }  static Optional<TableMetaData> load(final DataSource dataSource, final String tableNamePattern, final String databaseType);  }
@Test public void assertLoad() throws SQLException { Optional<TableMetaData> actual = TableMetaDataLoader.load(dataSource, TEST_TABLE, ""); assertTrue(actual.isPresent()); Map<String, ColumnMetaData> columnMetaDataMap = actual.get().getColumns(); assertThat(columnMetaDataMap.size(), is(2)); assertColumnMetaData(columnMetaDataMap.get("pk_col"), "pk_col", Types.INTEGER, "INT", true, true); assertColumnMetaData(columnMetaDataMap.get("col"), "col", Types.VARCHAR, "VARCHAR", false, false); Map<String, IndexMetaData> indexMetaDataMap = actual.get().getIndexes(); assertThat(indexMetaDataMap.size(), is(1)); assertTrue(indexMetaDataMap.containsKey("my_index")); }
@Test public void assertTableNotExist() throws SQLException { when(databaseMetaData.getTables(TEST_CATALOG, null, TEST_TABLE, null)).thenReturn(tableNotExistResultSet); when(tableNotExistResultSet.next()).thenReturn(false); Optional<TableMetaData> actual = TableMetaDataLoader.load(dataSource, TEST_TABLE, ""); assertFalse(actual.isPresent()); }