IndexMetaDataLoader { public static Collection<IndexMetaData> load(final Connection connection, final String table, final String databaseType) throws SQLException { Collection<IndexMetaData> result = new HashSet<>(); try (ResultSet resultSet = connection.getMetaData().getIndexInfo(connection.getCatalog(), JdbcUtil.getSchema(connection, databaseType), table, false, false)) { while (resultSet.next()) { String indexName = resultSet.getString(INDEX_NAME); if (null != indexName) { result.add(new IndexMetaData(indexName)); } } } return result; }  static Collection<IndexMetaData> load(final Connection connection, final String table, final String databaseType);  }
@Test public void assertLoad() throws SQLException { when(databaseMetaData.getIndexInfo(TEST_CATALOG, JdbcUtil.getSchema(connection, "Mysql"), TEST_TABLE, false, false)).thenReturn(indexResultSet); when(indexResultSet.next()).thenReturn(true, true, false); when(indexResultSet.getString("INDEX_NAME")).thenReturn("my_index"); Collection<IndexMetaData> actual = IndexMetaDataLoader.load(connection, TEST_TABLE, "Mysql"); assertThat(actual.size(), is(1)); IndexMetaData indexMetaData = actual.iterator().next(); assertThat(indexMetaData.getName(), is("my_index")); }
@Test public void assertOracleLoad() throws SQLException { when(databaseMetaData.getIndexInfo(TEST_CATALOG, JdbcUtil.getSchema(connection, "Oracle"), TEST_TABLE, false, false)).thenReturn(indexResultSet); when(indexResultSet.next()).thenReturn(true, true, false); when(indexResultSet.getString("INDEX_NAME")).thenReturn("my_index"); Collection<IndexMetaData> actual = IndexMetaDataLoader.load(connection, TEST_TABLE, "Oracle"); assertThat(actual.size(), is(1)); IndexMetaData indexMetaData = actual.iterator().next(); assertThat(indexMetaData.getName(), is("my_index")); }