TableMetaDataManager { String findMatchInResultSet(String stringToFind, ResultSet resultSet, String column, String description) throws SQLException { int exactMatches = 0; String exactMatch = null; int inexactMatches = 0; String inexactMatch = null; if (resultSet != null) { while (resultSet.next()) { String name = resultSet.getString(column); if (name.equals(stringToFind)) { exactMatches++; exactMatch = name; } else if (name.equalsIgnoreCase(stringToFind)) { inexactMatches++; inexactMatch = name; } } } if (exactMatches == 1) { return exactMatch; } if (inexactMatches > 1 || exactMatches > 1) { throw new JdbcConnectorException( "Multiple " + description + "s were found that match \"" + stringToFind + '"'); } if (inexactMatches == 1) { return inexactMatch; } throw new JdbcConnectorException( "No " + description + " was found that matches \"" + stringToFind + '"'); }  TableMetaDataView getTableMetaDataView(Connection connection,
      RegionMapping regionMapping);  }
@Test public void findMatchInResultSetGivenEmptyResultSetThrows() throws SQLException { String stringToFind = "stringToFind"; ResultSet resultSet = mock(ResultSet.class); String column = "column"; String description = "description"; assertThatThrownBy( () -> tableMetaDataManager.findMatchInResultSet(stringToFind, resultSet, column, description)) .isInstanceOf(JdbcConnectorException.class) .hasMessageContaining( "No " + description + " was found that matches \"" + stringToFind + '"'); }
@Test public void findMatchInResultSetGivenNullResultSetThrows() throws SQLException { String stringToFind = "stringToFind"; ResultSet resultSet = null; String column = "column"; String description = "description"; assertThatThrownBy( () -> tableMetaDataManager.findMatchInResultSet(stringToFind, resultSet, column, description)) .isInstanceOf(JdbcConnectorException.class) .hasMessageContaining( "No " + description + " was found that matches \"" + stringToFind + '"'); }
@Test public void findMatchInResultSetGivenResultSetWithNoMatchThrows() throws SQLException { String stringToFind = "stringToFind"; String column = "column"; String description = "description"; ResultSet resultSet = mock(ResultSet.class); when(resultSet.next()).thenReturn(true).thenReturn(false); when(resultSet.getString(column)).thenReturn("doesNotMatch"); assertThatThrownBy( () -> tableMetaDataManager.findMatchInResultSet(stringToFind, resultSet, column, description)) .isInstanceOf(JdbcConnectorException.class) .hasMessageContaining( "No " + description + " was found that matches \"" + stringToFind + '"'); }
@Test public void findMatchInResultSetGivenResultSetWithMultipleExactMatchesThrows() throws SQLException { String stringToFind = "stringToFind"; String column = "column"; String description = "description"; ResultSet resultSet = mock(ResultSet.class); when(resultSet.next()).thenReturn(true).thenReturn(true).thenReturn(false); when(resultSet.getString(column)).thenReturn("stringToFind"); assertThatThrownBy( () -> tableMetaDataManager.findMatchInResultSet(stringToFind, resultSet, column, description)) .isInstanceOf(JdbcConnectorException.class) .hasMessageContaining( "Multiple " + description + "s were found that match \"" + stringToFind + '"'); }
@Test public void findMatchInResultSetGivenResultSetWithMultipleInexactMatchesThrows() throws SQLException { String stringToFind = "stringToFind"; String column = "column"; String description = "description"; ResultSet resultSet = mock(ResultSet.class); when(resultSet.next()).thenReturn(true).thenReturn(true).thenReturn(false); when(resultSet.getString(column)).thenReturn("STRINGToFind"); assertThatThrownBy( () -> tableMetaDataManager.findMatchInResultSet(stringToFind, resultSet, column, description)) .isInstanceOf(JdbcConnectorException.class) .hasMessageContaining( "Multiple " + description + "s were found that match \"" + stringToFind + '"'); }
@Test public void findMatchInResultSetGivenResultSetWithOneInexactMatchReturnsIt() throws SQLException { String stringToFind = "stringToFind"; String column = "column"; String description = "description"; ResultSet resultSet = mock(ResultSet.class); when(resultSet.next()).thenReturn(true).thenReturn(false); String inexactMatch = "STRINGToFind"; when(resultSet.getString(column)).thenReturn(inexactMatch); String result = tableMetaDataManager.findMatchInResultSet(stringToFind, resultSet, column, description); assertThat(result).isEqualTo(inexactMatch); }
@Test public void findMatchInResultSetGivenResultSetWithOneExactMatchAndMultipleInexactReturnsTheExactMatch() throws SQLException { String stringToFind = "stringToFind"; String column = "column"; String description = "description"; ResultSet resultSet = mock(ResultSet.class); when(resultSet.next()).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(false); String inexactMatch = "STRINGToFind"; when(resultSet.getString(column)).thenReturn(inexactMatch).thenReturn(stringToFind) .thenReturn(inexactMatch); String result = tableMetaDataManager.findMatchInResultSet(stringToFind, resultSet, column, description); assertThat(result).isEqualTo(stringToFind); }