TableMetaDataManager { public TableMetaDataView getTableMetaDataView(Connection connection, RegionMapping regionMapping) { return JavaWorkarounds.computeIfAbsent(tableToMetaDataMap, computeTableName(regionMapping), k -> computeTableMetaDataView(connection, k, regionMapping)); }  TableMetaDataView getTableMetaDataView(Connection connection,
      RegionMapping regionMapping);  }
@Test public void returnsSinglePrimaryKeyColumnName() throws Exception { setupPrimaryKeysMetaData(); when(primaryKeysResultSet.next()).thenReturn(true).thenReturn(false); when(regionMapping.getIds()).thenReturn(""); TableMetaDataView data = tableMetaDataManager.getTableMetaDataView(connection, regionMapping); assertThat(data.getKeyColumnNames()).isEqualTo(Arrays.asList(KEY_COLUMN)); verify(connection).getMetaData(); }
@Test public void returnsCompositePrimaryKeyColumnNames() throws Exception { setupCompositePrimaryKeysMetaData(); when(regionMapping.getIds()).thenReturn(""); TableMetaDataView data = tableMetaDataManager.getTableMetaDataView(connection, regionMapping); assertThat(data.getKeyColumnNames()).isEqualTo(Arrays.asList(KEY_COLUMN, KEY_COLUMN2)); verify(connection).getMetaData(); verify(databaseMetaData).getTables("", "", "%", null); }
@Test public void verifyPostgreUsesPublicSchemaByDefault() throws Exception { setupCompositePrimaryKeysMetaData(); when(regionMapping.getIds()).thenReturn(""); ResultSet schemas = mock(ResultSet.class); when(schemas.next()).thenReturn(true).thenReturn(false); when(schemas.getString("TABLE_SCHEM")).thenReturn("PUBLIC"); when(databaseMetaData.getSchemas(any(), any())).thenReturn(schemas); when(databaseMetaData.getDatabaseProductName()).thenReturn("PostgreSQL"); TableMetaDataView data = tableMetaDataManager.getTableMetaDataView(connection, regionMapping); assertThat(data.getKeyColumnNames()).isEqualTo(Arrays.asList(KEY_COLUMN, KEY_COLUMN2)); verify(connection).getMetaData(); verify(databaseMetaData).getTables("", "PUBLIC", "%", null); }
@Test public void givenNoColumnsAndNonNullIdsThenExpectException() throws Exception { setupTableMetaData(); when(columnResultSet.next()).thenReturn(false); when(regionMapping.getIds()).thenReturn("nonExistentId"); assertThatThrownBy( () -> tableMetaDataManager.getTableMetaDataView(connection, regionMapping)) .isInstanceOf(JdbcConnectorException.class) .hasMessageContaining("The table testTable does not have a column named nonExistentId"); }
@Test public void givenOneColumnAndNonNullIdsThatDoesNotMatchThenExpectException() throws Exception { setupTableMetaData(); when(columnResultSet.next()).thenReturn(true).thenReturn(false); when(columnResultSet.getString("COLUMN_NAME")).thenReturn("existingColumn"); when(regionMapping.getIds()).thenReturn("nonExistentId"); assertThatThrownBy( () -> tableMetaDataManager.getTableMetaDataView(connection, regionMapping)) .isInstanceOf(JdbcConnectorException.class) .hasMessageContaining("The table testTable does not have a column named nonExistentId"); }
@Test public void givenTwoColumnsAndNonNullIdsThatDoesNotExactlyMatchThenExpectException() throws Exception { setupTableMetaData(); when(columnResultSet.next()).thenReturn(true).thenReturn(true).thenReturn(false); when(columnResultSet.getString("COLUMN_NAME")).thenReturn("nonexistentid") .thenReturn("NONEXISTENTID"); when(regionMapping.getIds()).thenReturn("nonExistentId"); assertThatThrownBy( () -> tableMetaDataManager.getTableMetaDataView(connection, regionMapping)) .isInstanceOf(JdbcConnectorException.class).hasMessageContaining( "The table testTable has more than one column that matches nonExistentId"); }
@Test public void givenThreeColumnsAndNonNullIdsThatDoesExactlyMatchThenKeyColumnNameIsReturned() throws Exception { setupTableMetaData(); when(columnResultSet.next()).thenReturn(true).thenReturn(true).thenReturn(true) .thenReturn(false); when(columnResultSet.getString("COLUMN_NAME")).thenReturn("existentid").thenReturn("EXISTENTID") .thenReturn("ExistentId"); when(regionMapping.getIds()).thenReturn("ExistentId"); TableMetaDataView data = tableMetaDataManager.getTableMetaDataView(connection, regionMapping); assertThat(data.getKeyColumnNames()).isEqualTo(Arrays.asList("ExistentId")); }
@Test public void givenFourColumnsAndCompositeIdsThenOnlyKeyColumnNamesAreReturned() throws Exception { setupTableMetaData(); when(columnResultSet.next()).thenReturn(true).thenReturn(true).thenReturn(true) .thenReturn(true).thenReturn(false); when(columnResultSet.getString("COLUMN_NAME")).thenReturn("LeadingNonKeyColumn") .thenReturn(KEY_COLUMN).thenReturn(KEY_COLUMN2) .thenReturn("NonKeyColumn"); when(regionMapping.getIds()).thenReturn(KEY_COLUMN + "," + KEY_COLUMN2); TableMetaDataView data = tableMetaDataManager.getTableMetaDataView(connection, regionMapping); assertThat(data.getKeyColumnNames()).isEqualTo(Arrays.asList(KEY_COLUMN, KEY_COLUMN2)); }
@Test public void givenColumnAndNonNullIdsThatDoesInexactlyMatchThenKeyColumnNameIsReturned() throws Exception { setupTableMetaData(); when(columnResultSet.next()).thenReturn(true).thenReturn(false); when(columnResultSet.getString("COLUMN_NAME")).thenReturn("existentid"); when(regionMapping.getIds()).thenReturn("ExistentId"); TableMetaDataView data = tableMetaDataManager.getTableMetaDataView(connection, regionMapping); assertThat(data.getKeyColumnNames()).isEqualTo(Arrays.asList("ExistentId")); }
@Test public void returnsDefaultQuoteString() throws Exception { setupPrimaryKeysMetaData(); when(primaryKeysResultSet.next()).thenReturn(true).thenReturn(false); TableMetaDataView data = tableMetaDataManager.getTableMetaDataView(connection, regionMapping); assertThat(data.getIdentifierQuoteString()).isEqualTo(""); verify(connection).getMetaData(); }
@Test public void returnsQuoteStringFromMetaData() throws Exception { setupPrimaryKeysMetaData(); when(primaryKeysResultSet.next()).thenReturn(true).thenReturn(false); String expectedQuoteString = "123"; when(databaseMetaData.getIdentifierQuoteString()).thenReturn(expectedQuoteString); TableMetaDataView data = tableMetaDataManager.getTableMetaDataView(connection, regionMapping); assertThat(data.getIdentifierQuoteString()).isEqualTo(expectedQuoteString); verify(connection).getMetaData(); }
@Test public void secondCallDoesNotUseMetaData() throws Exception { setupPrimaryKeysMetaData(); when(primaryKeysResultSet.next()).thenReturn(true).thenReturn(false); tableMetaDataManager.getTableMetaDataView(connection, regionMapping); tableMetaDataManager.getTableMetaDataView(connection, regionMapping); verify(connection).getMetaData(); }
@Test public void throwsExceptionWhenFailsToGetTableMetadata() throws Exception { SQLException cause = new SQLException("sql message"); when(connection.getMetaData()).thenThrow(cause); assertThatThrownBy( () -> tableMetaDataManager.getTableMetaDataView(connection, regionMapping)) .isInstanceOf(JdbcConnectorException.class).hasMessageContaining("sql message"); }
@Test public void throwsExceptionWhenDesiredTableNotFound() throws Exception { when(tablesResultSet.next()).thenReturn(true).thenReturn(false); when(tablesResultSet.getString("TABLE_NAME")).thenReturn("otherTable"); assertThatThrownBy( () -> tableMetaDataManager.getTableMetaDataView(connection, regionMapping)) .isInstanceOf(JdbcConnectorException.class) .hasMessage("No table was found that matches \"" + TABLE_NAME + '"'); }
@Test public void returnsExactMatchTableNameWhenTwoTablesHasCaseInsensitiveSameName() throws Exception { setupPrimaryKeysMetaData(); when(primaryKeysResultSet.next()).thenReturn(true).thenReturn(false); when(tablesResultSet.next()).thenReturn(true).thenReturn(true).thenReturn(false); when(tablesResultSet.getString("TABLE_NAME")).thenReturn(TABLE_NAME.toUpperCase()) .thenReturn(TABLE_NAME); TableMetaDataView data = tableMetaDataManager.getTableMetaDataView(connection, regionMapping); assertThat(data.getQuotedTablePath()).isEqualTo(TABLE_NAME); }
@Test public void returnsQuotedTableNameWhenMetaDataHasQuoteId() throws Exception { setupPrimaryKeysMetaData(); when(primaryKeysResultSet.next()).thenReturn(true).thenReturn(false); when(tablesResultSet.next()).thenReturn(true).thenReturn(true).thenReturn(false); when(tablesResultSet.getString("TABLE_NAME")).thenReturn(TABLE_NAME.toUpperCase()) .thenReturn(TABLE_NAME); String QUOTE = "@@"; when(this.databaseMetaData.getIdentifierQuoteString()).thenReturn(QUOTE); TableMetaDataView data = tableMetaDataManager.getTableMetaDataView(connection, regionMapping); assertThat(data.getQuotedTablePath()).isEqualTo(QUOTE + TABLE_NAME + QUOTE); }
@Test public void returnsMatchTableNameWhenMetaDataHasOneInexactMatch() throws Exception { setupPrimaryKeysMetaData(); when(databaseMetaData.getColumns(any(), any(), eq(TABLE_NAME.toUpperCase()), any())) .thenReturn(columnResultSet); when(primaryKeysResultSet.next()).thenReturn(true).thenReturn(false); when(tablesResultSet.next()).thenReturn(true).thenReturn(false); when(tablesResultSet.getString("TABLE_NAME")).thenReturn(TABLE_NAME.toUpperCase()); TableMetaDataView data = tableMetaDataManager.getTableMetaDataView(connection, regionMapping); assertThat(data.getQuotedTablePath()).isEqualTo(TABLE_NAME.toUpperCase()); }
@Test public void throwsExceptionWhenTwoTablesHasCaseInsensitiveSameName() throws Exception { setupPrimaryKeysMetaData(); when(primaryKeysResultSet.next()).thenReturn(true).thenReturn(false); when(tablesResultSet.next()).thenReturn(true).thenReturn(true).thenReturn(false); when(tablesResultSet.getString("TABLE_NAME")).thenReturn(TABLE_NAME.toLowerCase()) .thenReturn(TABLE_NAME.toUpperCase()); assertThatThrownBy( () -> tableMetaDataManager.getTableMetaDataView(connection, regionMapping)) .isInstanceOf(JdbcConnectorException.class) .hasMessage("Multiple tables were found that match \"" + TABLE_NAME + '"'); }
@Test public void throwsExceptionWhenTwoTablesHaveExactSameName() throws Exception { setupPrimaryKeysMetaData(); when(primaryKeysResultSet.next()).thenReturn(true).thenReturn(false); when(tablesResultSet.next()).thenReturn(true).thenReturn(true).thenReturn(false); when(tablesResultSet.getString("TABLE_NAME")).thenReturn(TABLE_NAME).thenReturn(TABLE_NAME); assertThatThrownBy( () -> tableMetaDataManager.getTableMetaDataView(connection, regionMapping)) .isInstanceOf(JdbcConnectorException.class) .hasMessage("Multiple tables were found that match \"" + TABLE_NAME + '"'); }
@Test public void throwsExceptionWhenNoPrimaryKeyInTable() throws Exception { setupPrimaryKeysMetaData(); when(primaryKeysResultSet.next()).thenReturn(false); assertThatThrownBy( () -> tableMetaDataManager.getTableMetaDataView(connection, regionMapping)) .isInstanceOf(JdbcConnectorException.class) .hasMessage("The table " + TABLE_NAME + " does not have a primary key column."); }
@Test public void unknownColumnsDataTypeIsZero() throws SQLException { setupPrimaryKeysMetaData(); when(primaryKeysResultSet.next()).thenReturn(true).thenReturn(false); TableMetaDataView data = tableMetaDataManager.getTableMetaDataView(connection, regionMapping); JDBCType dataType = data.getColumnDataType("unknownColumn"); assertThat(dataType).isEqualTo(JDBCType.NULL); }
@Test public void validateExpectedDataTypes() throws SQLException { setupPrimaryKeysMetaData(); when(primaryKeysResultSet.next()).thenReturn(true).thenReturn(false); when(columnResultSet.next()).thenReturn(true).thenReturn(true).thenReturn(false); String columnName1 = "columnName1"; int columnDataType1 = 1; String columnName2 = "columnName2"; int columnDataType2 = 2; when(columnResultSet.getString("COLUMN_NAME")).thenReturn(columnName1).thenReturn(columnName2); when(columnResultSet.getInt("DATA_TYPE")).thenReturn(columnDataType1) .thenReturn(columnDataType2); TableMetaDataView data = tableMetaDataManager.getTableMetaDataView(connection, regionMapping); JDBCType dataType1 = data.getColumnDataType(columnName1); JDBCType dataType2 = data.getColumnDataType(columnName2); assertThat(dataType1.getVendorTypeNumber()).isEqualTo(columnDataType1); assertThat(dataType2.getVendorTypeNumber()).isEqualTo(columnDataType2); verify(primaryKeysResultSet).close(); verify(columnResultSet).close(); }
@Test public void validateExpectedColumnNames() throws SQLException { setupPrimaryKeysMetaData(); when(primaryKeysResultSet.next()).thenReturn(true).thenReturn(false); when(columnResultSet.next()).thenReturn(true).thenReturn(true).thenReturn(false); String columnName1 = "columnName1"; int columnDataType1 = 1; String columnName2 = "columnName2"; int columnDataType2 = 2; when(columnResultSet.getString("COLUMN_NAME")).thenReturn(columnName1).thenReturn(columnName2); when(columnResultSet.getInt("DATA_TYPE")).thenReturn(columnDataType1) .thenReturn(columnDataType2); Set<String> expectedColumnNames = new HashSet<>(Arrays.asList(columnName1, columnName2)); TableMetaDataView data = tableMetaDataManager.getTableMetaDataView(connection, regionMapping); Set<String> columnNames = data.getColumnNames(); assertThat(columnNames).isEqualTo(expectedColumnNames); }
@Test public void validateThatCloseOnPrimaryKeysResultSetIsCalledByGetTableMetaDataView() throws SQLException { setupPrimaryKeysMetaData(); when(primaryKeysResultSet.next()).thenReturn(true).thenReturn(false); TableMetaDataView data = tableMetaDataManager.getTableMetaDataView(connection, regionMapping); verify(primaryKeysResultSet).close(); }
@Test public void validateThatCloseOnColumnResultSetIsCalledByGetTableMetaDataView() throws SQLException { setupPrimaryKeysMetaData(); when(primaryKeysResultSet.next()).thenReturn(true).thenReturn(false); TableMetaDataView data = tableMetaDataManager.getTableMetaDataView(connection, regionMapping); verify(columnResultSet).close(); }
@Test public void validateTableNameIsSetByGetTableMetaDataView() throws SQLException { setupPrimaryKeysMetaData(); when(primaryKeysResultSet.next()).thenReturn(true).thenReturn(false); TableMetaDataView data = tableMetaDataManager.getTableMetaDataView(connection, regionMapping); assertThat(data.getQuotedTablePath()).isEqualTo(TABLE_NAME); }