DataSetComparator { public void compare(final IDataSet currentDataSet, final IDataSet expectedDataSet, final AssertionErrorCollector errorCollector) throws DatabaseUnitException { if (expectedDataSet.getTableNames().length == 0) { shouldBeEmpty(currentDataSet, errorCollector); } else { compareContent(currentDataSet, expectedDataSet, errorCollector); } } DataSetComparator(final String[] orderBy, final String[] toExclude, final boolean isStrict,
            final Set<Class<? extends IColumnFilter>> columnFilters); void compare(final IDataSet currentDataSet, final IDataSet expectedDataSet, final AssertionErrorCollector errorCollector);  }
@Test public void testCurrentDataSetContainsDataAndExpectedDataSetIsEmpty() throws Exception { final String[] orderBy = new String[] {}; final String[] toExclude = new String[] {}; final Set<Class<? extends IColumnFilter>> columnFilters = Collections.emptySet(); final DataSetComparator comparator = new DataSetComparator(orderBy, toExclude, false, columnFilters); when(currentDataSet.getTableNames()).thenReturn(new String[] { TABLE_1_NAME }); when(currentDataSet.getTable(any(String.class))).thenAnswer((final InvocationOnMock invocation) -> { final String tableName = (String) invocation.getArguments()[0]; if (tableName == TABLE_1_NAME) { return table1; } else { throw new NoSuchTableException(tableName); } }); when(expectedDataSet.getTableNames()).thenReturn(new String[] {}); when(expectedDataSet.getTable(any(String.class))).thenThrow(new NoSuchTableException()); comparator.compare(currentDataSet, expectedDataSet, errorCollector); assertThat(errorCollector.amountOfErrors(), equalTo(1)); try { errorCollector.report(); fail("AssertionError expected"); } catch (final AssertionError e) { assertThat(e.getMessage(), containsString("failed in 1 case")); assertThat(e.getMessage(), containsString(TABLE_1_NAME + " was expected to be empty, but has <" + TABLE_1_ENTRIES + "> entries")); } }
@Test public void testCurrentDataSetIsEmptyAndExpectedDataContainsData() throws Exception { final String[] orderBy = new String[] {}; final String[] toExclude = new String[] {}; final Set<Class<? extends IColumnFilter>> columnFilters = Collections.emptySet(); final DataSetComparator comparator = new DataSetComparator(orderBy, toExclude, false, columnFilters); when(currentDataSet.getTableNames()).thenReturn(new String[] {}); when(currentDataSet.getTable(any(String.class))).thenThrow(new NoSuchTableException()); when(expectedDataSet.getTableNames()).thenReturn(new String[] { TABLE_1_NAME }); when(expectedDataSet.getTable(any(String.class))).thenAnswer((final InvocationOnMock invocation) -> { final String tableName = (String) invocation.getArguments()[0]; if (tableName == TABLE_1_NAME) { return table1; } else { throw new NoSuchTableException(tableName); } }); comparator.compare(currentDataSet, expectedDataSet, errorCollector); assertThat(errorCollector.amountOfErrors(), equalTo(1)); try { errorCollector.report(); fail("AssertionError expected"); } catch (final AssertionError e) { assertThat(e.getMessage(), containsString("failed in 1 case")); assertThat(e.getMessage(), containsString( TABLE_1_NAME + " was expected to be present and to contain <" + TABLE_1_ENTRIES + "> entries, but not found")); } }
@Test public void testCurrentDataSetAndExpectedDataSetContainDataButAreFullyDisjunctiveUsingNotStrictMode() throws Exception { final String[] orderBy = new String[] {}; final String[] toExclude = new String[] {}; final Set<Class<? extends IColumnFilter>> columnFilters = Collections.emptySet(); final DataSetComparator comparator = new DataSetComparator(orderBy, toExclude, false, columnFilters); when(expectedDataSet.getTableNames()).thenReturn(new String[] { TABLE_1_NAME }); when(expectedDataSet.getTable(any(String.class))).thenAnswer((final InvocationOnMock invocation) -> { final String tableName = (String) invocation.getArguments()[0]; if (tableName == TABLE_1_NAME) { return table1; } else { throw new NoSuchTableException(tableName); } }); when(currentDataSet.getTableNames()).thenReturn(new String[] { TABLE_2_NAME }); when(currentDataSet.getTable(any(String.class))).thenAnswer((final InvocationOnMock invocation) -> { final String tableName = (String) invocation.getArguments()[0]; if (tableName == TABLE_2_NAME) { return table2; } else { throw new NoSuchTableException(tableName); } }); comparator.compare(currentDataSet, expectedDataSet, errorCollector); assertThat(errorCollector.amountOfErrors(), equalTo(1)); try { errorCollector.report(); fail("AssertionError expected"); } catch (final AssertionError e) { assertThat(e.getMessage(), containsString("failed in 1 case")); } }
@Test public void testCurrentDataSetAndExpectedDataSetContainDataButAreFullyDisjunctiveUsingStrictMode() throws Exception { final String[] orderBy = new String[] {}; final String[] toExclude = new String[] {}; final Set<Class<? extends IColumnFilter>> columnFilters = Collections.emptySet(); final DataSetComparator comparator = new DataSetComparator(orderBy, toExclude, true, columnFilters); when(expectedDataSet.getTableNames()).thenReturn(new String[] { TABLE_1_NAME }); when(expectedDataSet.getTable(any(String.class))).thenAnswer((final InvocationOnMock invocation) -> { final String tableName = (String) invocation.getArguments()[0]; if (tableName == TABLE_1_NAME) { return table1; } else { throw new NoSuchTableException(tableName); } }); when(currentDataSet.getTableNames()).thenReturn(new String[] { TABLE_2_NAME }); when(currentDataSet.getTable(any(String.class))).thenAnswer((final InvocationOnMock invocation) -> { final String tableName = (String) invocation.getArguments()[0]; if (tableName == TABLE_2_NAME) { return table2; } else { throw new NoSuchTableException(tableName); } }); comparator.compare(currentDataSet, expectedDataSet, errorCollector); assertThat(errorCollector.amountOfErrors(), equalTo(2)); try { errorCollector.report(); fail("AssertionError expected"); } catch (final AssertionError e) { assertThat(e.getMessage(), containsString("failed in 2 case")); assertThat(e.getMessage(), containsString( TABLE_1_NAME + " was expected to be present and to contain <" + TABLE_1_ENTRIES + "> entries, but not found")); } }
@Test public void testCurrentDataSetAndExpectedDataSetHaveOnlyOneTableInCommonUsingNotStrictMode() throws Exception { final String[] orderBy = new String[] {}; final String[] toExclude = new String[] {}; final Set<Class<? extends IColumnFilter>> columnFilters = Collections.emptySet(); final DataSetComparator comparator = new DataSetComparator(orderBy, toExclude, false, columnFilters); when(expectedDataSet.getTableNames()).thenReturn(new String[] { TABLE_1_NAME, TABLE_2_NAME }); when(expectedDataSet.getTable(any(String.class))).thenAnswer((final InvocationOnMock invocation) -> { final String tableName = (String) invocation.getArguments()[0]; if (tableName == TABLE_1_NAME) { return table1; } else if (tableName == TABLE_2_NAME) { return table2; } else { throw new NoSuchTableException(tableName); } }); when(currentDataSet.getTableNames()).thenReturn(new String[] { TABLE_2_NAME, TABLE_3_NAME }); when(currentDataSet.getTable(any(String.class))).thenAnswer((final InvocationOnMock invocation) -> { final String tableName = (String) invocation.getArguments()[0]; if (tableName == TABLE_2_NAME) { return table2; } else if (tableName == TABLE_3_NAME) { return table3; } else { throw new NoSuchTableException(tableName); } }); comparator.compare(currentDataSet, expectedDataSet, errorCollector); assertThat(errorCollector.amountOfErrors(), equalTo(1)); try { errorCollector.report(); fail("AssertionError expected"); } catch (final AssertionError e) { assertThat(e.getMessage(), containsString("failed in 1 case")); assertThat(e.getMessage(), containsString( TABLE_1_NAME + " was expected to be present and to contain <" + TABLE_1_ENTRIES + "> entries, but not found")); } }
@Test public void testCurrentDataSetAndExpectedDataSetHaveOnlyOneTableInCommonUsingStrictMode() throws Exception { final String[] orderBy = new String[] {}; final String[] toExclude = new String[] {}; final Set<Class<? extends IColumnFilter>> columnFilters = Collections.emptySet(); final DataSetComparator comparator = new DataSetComparator(orderBy, toExclude, true, columnFilters); when(expectedDataSet.getTableNames()).thenReturn(new String[] { TABLE_1_NAME, TABLE_2_NAME }); when(expectedDataSet.getTable(any(String.class))).thenAnswer((final InvocationOnMock invocation) -> { final String tableName = (String) invocation.getArguments()[0]; if (tableName == TABLE_1_NAME) { return table1; } else if (tableName == TABLE_2_NAME) { return table2; } else { throw new NoSuchTableException(tableName); } }); when(currentDataSet.getTableNames()).thenReturn(new String[] { TABLE_2_NAME, TABLE_3_NAME }); when(currentDataSet.getTable(any(String.class))).thenAnswer((final InvocationOnMock invocation) -> { final String tableName = (String) invocation.getArguments()[0]; if (tableName == TABLE_2_NAME) { return table2; } else if (tableName == TABLE_3_NAME) { return table3; } else { throw new NoSuchTableException(tableName); } }); comparator.compare(currentDataSet, expectedDataSet, errorCollector); assertThat(errorCollector.amountOfErrors(), equalTo(2)); try { errorCollector.report(); fail("AssertionError expected"); } catch (final AssertionError e) { assertThat(e.getMessage(), containsString("failed in 2 case")); assertThat(e.getMessage(), containsString( TABLE_1_NAME + " was expected to be present and to contain <" + TABLE_1_ENTRIES + "> entries, but not found")); assertThat(e.getMessage(), containsString(TABLE_3_NAME + " was not expected, but is present and contains <" + TABLE_3_ENTRIES + "> entries")); } }
@Test public void testCurrentDataSetIsASubsetOfExpectedDataSet() throws Exception { final String[] orderBy = new String[] {}; final String[] toExclude = new String[] {}; final Set<Class<? extends IColumnFilter>> columnFilters = Collections.emptySet(); final DataSetComparator comparator = new DataSetComparator(orderBy, toExclude, false, columnFilters); when(expectedDataSet.getTableNames()).thenReturn(new String[] { TABLE_1_NAME, TABLE_2_NAME }); when(expectedDataSet.getTable(any(String.class))).thenAnswer((final InvocationOnMock invocation) -> { final String tableName = (String) invocation.getArguments()[0]; if (tableName == TABLE_1_NAME) { return table1; } else if (tableName == TABLE_2_NAME) { return table2; } else { throw new NoSuchTableException(tableName); } }); when(currentDataSet.getTableNames()).thenReturn(new String[] { TABLE_2_NAME }); when(currentDataSet.getTable(any(String.class))).thenAnswer((final InvocationOnMock invocation) -> { final String tableName = (String) invocation.getArguments()[0]; if (tableName == TABLE_2_NAME) { return table2; } else { throw new NoSuchTableException(tableName); } }); comparator.compare(currentDataSet, expectedDataSet, errorCollector); assertThat(errorCollector.amountOfErrors(), equalTo(1)); try { errorCollector.report(); fail("AssertionError expected"); } catch (final AssertionError e) { assertThat(e.getMessage(), containsString("failed in 1 case")); assertThat(e.getMessage(), containsString(TABLE_1_NAME + " was expected to be present and to contain <" + TABLE_1_ENTRIES + "> entries")); } }
@Test public void testExpectedDataSetIsASubsetOfCurrentDataSetUsingNotStringMode() throws Exception { final String[] orderBy = new String[] {}; final String[] toExclude = new String[] {}; final Set<Class<? extends IColumnFilter>> columnFilters = Collections.emptySet(); final DataSetComparator comparator = new DataSetComparator(orderBy, toExclude, false, columnFilters); when(expectedDataSet.getTableNames()).thenReturn(new String[] { TABLE_2_NAME }); when(expectedDataSet.getTable(any(String.class))).thenAnswer((final InvocationOnMock invocation) -> { final String tableName = (String) invocation.getArguments()[0]; if (tableName == TABLE_2_NAME) { return table2; } else { throw new NoSuchTableException(tableName); } }); when(currentDataSet.getTableNames()).thenReturn(new String[] { TABLE_1_NAME, TABLE_2_NAME }); when(currentDataSet.getTable(any(String.class))).thenAnswer((final InvocationOnMock invocation) -> { final String tableName = (String) invocation.getArguments()[0]; if (tableName == TABLE_1_NAME) { return table1; } else if (tableName == TABLE_2_NAME) { return table2; } else { throw new NoSuchTableException(tableName); } }); comparator.compare(currentDataSet, expectedDataSet, errorCollector); assertThat(errorCollector.amountOfErrors(), equalTo(0)); errorCollector.report(); }
@Test public void testExpectedDataSetIsASubsetOfCurrentDataSetUsingStringMode() throws Exception { final String[] orderBy = new String[] {}; final String[] toExclude = new String[] {}; final Set<Class<? extends IColumnFilter>> columnFilters = Collections.emptySet(); final DataSetComparator comparator = new DataSetComparator(orderBy, toExclude, true, columnFilters); when(expectedDataSet.getTableNames()).thenReturn(new String[] { TABLE_2_NAME }); when(expectedDataSet.getTable(any(String.class))).thenAnswer((final InvocationOnMock invocation) -> { final String tableName = (String) invocation.getArguments()[0]; if (tableName == TABLE_2_NAME) { return table2; } else { throw new NoSuchTableException(tableName); } }); when(currentDataSet.getTableNames()).thenReturn(new String[] { TABLE_1_NAME, TABLE_2_NAME }); when(currentDataSet.getTable(any(String.class))).thenAnswer((final InvocationOnMock invocation) -> { final String tableName = (String) invocation.getArguments()[0]; if (tableName == TABLE_1_NAME) { return table1; } else if (tableName == TABLE_2_NAME) { return table2; } else { throw new NoSuchTableException(tableName); } }); comparator.compare(currentDataSet, expectedDataSet, errorCollector); assertThat(errorCollector.amountOfErrors(), equalTo(1)); try { errorCollector.report(); fail("AssertionError expected"); } catch (final AssertionError e) { assertThat(e.getMessage(), containsString("failed in 1 case")); assertThat(e.getMessage(), containsString(TABLE_1_NAME + " was not expected, but is present and contains <" + TABLE_1_ENTRIES + "> entries")); } }
@Test public void testCurrentDataSetAndExpectedDataSetDifferInTableRecords() throws Exception { final String[] orderBy = new String[] {}; final String[] toExclude = new String[] {}; final Set<Class<? extends IColumnFilter>> columnFilters = Collections.emptySet(); final DataSetComparator comparator = new DataSetComparator(orderBy, toExclude, false, columnFilters); when(expectedDataSet.getTableNames()).thenReturn(new String[] { TABLE_1_NAME }); when(expectedDataSet.getTable(any(String.class))).thenAnswer((final InvocationOnMock invocation) -> { final String tableName = (String) invocation.getArguments()[0]; if (tableName == TABLE_1_NAME) { return table1; } else { throw new NoSuchTableException(tableName); } }); when(table1.getValue(eq(0), eq(TABLE_1_COLUMN_1.getColumnName()))).thenReturn("col1_value1"); when(table1.getValue(eq(1), eq(TABLE_1_COLUMN_1.getColumnName()))).thenReturn("col1_value2"); when(table1.getValue(eq(0), eq(TABLE_1_COLUMN_2.getColumnName()))).thenReturn("col2_value1"); when(table1.getValue(eq(1), eq(TABLE_1_COLUMN_2.getColumnName()))).thenReturn("col2_value2"); when(currentDataSet.getTableNames()).thenReturn(new String[] { TABLE_1_NAME }); when(currentDataSet.getTable(any(String.class))).thenAnswer((final InvocationOnMock invocation) -> { final String tableName = (String) invocation.getArguments()[0]; if (tableName == TABLE_1_NAME) { return table4; } else { throw new NoSuchTableException(tableName); } }); when(table4.getValue(eq(0), eq(TABLE_1_COLUMN_1.getColumnName()))).thenReturn("col1_value1"); when(table4.getValue(eq(1), eq(TABLE_1_COLUMN_1.getColumnName()))).thenReturn("col1_value2"); when(table4.getValue(eq(0), eq(TABLE_1_COLUMN_2.getColumnName()))).thenReturn("col2_other1"); when(table4.getValue(eq(1), eq(TABLE_1_COLUMN_2.getColumnName()))).thenReturn("col2_other2"); comparator.compare(currentDataSet, expectedDataSet, errorCollector); assertThat(errorCollector.amountOfErrors(), equalTo(2)); try { errorCollector.report(); fail("AssertionError expected"); } catch (final AssertionError e) { assertThat(e.getMessage(), containsString("failed in 2 cases")); assertThat(e.getMessage(), containsString("\"col2_value1\" but was \"col2_other1\"")); assertThat(e.getMessage(), containsString("\"col2_value2\" but was \"col2_other2\"")); } }
@Test public void testCurrentDataSetAndExpectedDataSetAreEqual() throws Exception { final String[] orderBy = new String[] {}; final String[] toExclude = new String[] {}; final Set<Class<? extends IColumnFilter>> columnFilters = Collections.emptySet(); final DataSetComparator comparator = new DataSetComparator(orderBy, toExclude, false, columnFilters); when(expectedDataSet.getTableNames()).thenReturn(new String[] { TABLE_1_NAME }); when(expectedDataSet.getTable(any(String.class))).thenAnswer((final InvocationOnMock invocation) -> { final String tableName = (String) invocation.getArguments()[0]; if (tableName == TABLE_1_NAME) { return table1; } else { throw new NoSuchTableException(tableName); } }); when(table1.getValue(eq(0), eq(TABLE_1_COLUMN_1.getColumnName()))).thenReturn("col1_value1"); when(table1.getValue(eq(1), eq(TABLE_1_COLUMN_1.getColumnName()))).thenReturn("col1_value2"); when(table1.getValue(eq(0), eq(TABLE_1_COLUMN_2.getColumnName()))).thenReturn("col2_value1"); when(table1.getValue(eq(1), eq(TABLE_1_COLUMN_2.getColumnName()))).thenReturn("col2_value2"); when(currentDataSet.getTableNames()).thenReturn(new String[] { TABLE_1_NAME }); when(currentDataSet.getTable(any(String.class))).thenAnswer((final InvocationOnMock invocation) -> { final String tableName = (String) invocation.getArguments()[0]; if (tableName == TABLE_1_NAME) { return table4; } else { throw new NoSuchTableException(tableName); } }); when(table4.getRowCount()).thenReturn(TABLE_1_ENTRIES); when(table4.getTableMetaData()).thenReturn(table1MetaData); when(table4.getValue(eq(0), eq(TABLE_1_COLUMN_1.getColumnName()))).thenReturn("col1_value1"); when(table4.getValue(eq(1), eq(TABLE_1_COLUMN_1.getColumnName()))).thenReturn("col1_value2"); when(table4.getValue(eq(0), eq(TABLE_1_COLUMN_2.getColumnName()))).thenReturn("col2_value1"); when(table4.getValue(eq(1), eq(TABLE_1_COLUMN_2.getColumnName()))).thenReturn("col2_value2"); comparator.compare(currentDataSet, expectedDataSet, errorCollector); assertThat(errorCollector.amountOfErrors(), equalTo(0)); errorCollector.report(); }
@Test public void testCustomFilter() throws Exception { final String[] orderBy = new String[] {}; final String[] toExclude = new String[] {}; final Set<Class<? extends IColumnFilter>> columnFilters = new HashSet<>(Arrays.asList(MyCustomFilter.class)); final DataSetComparator comparator = new DataSetComparator(orderBy, toExclude, false, columnFilters); when(expectedDataSet.getTableNames()).thenReturn(new String[] { TABLE_1_NAME }); when(expectedDataSet.getTable(any(String.class))).thenAnswer((final InvocationOnMock invocation) -> { final String tableName = (String) invocation.getArguments()[0]; if (tableName == TABLE_1_NAME) { return table1; } else { throw new NoSuchTableException(tableName); } }); when(table1.getValue(eq(0), eq(TABLE_1_COLUMN_2.getColumnName()))).thenReturn("col2_value1"); when(table1.getValue(eq(1), eq(TABLE_1_COLUMN_2.getColumnName()))).thenReturn("col2_value2"); when(currentDataSet.getTableNames()).thenReturn(new String[] { TABLE_1_NAME }); when(currentDataSet.getTable(any(String.class))).thenAnswer((final InvocationOnMock invocation) -> { final String tableName = (String) invocation.getArguments()[0]; if (tableName == TABLE_1_NAME) { return table4; } else { throw new NoSuchTableException(tableName); } }); when(table4.getRowCount()).thenReturn(TABLE_1_ENTRIES); when(table4.getTableMetaData()).thenReturn(table1MetaData); when(table4.getValue(eq(0), eq(TABLE_1_COLUMN_1.getColumnName()))).thenReturn("col1_value1"); when(table4.getValue(eq(1), eq(TABLE_1_COLUMN_1.getColumnName()))).thenReturn("col1_value2"); when(table4.getValue(eq(0), eq(TABLE_1_COLUMN_2.getColumnName()))).thenReturn("col2_value1"); when(table4.getValue(eq(1), eq(TABLE_1_COLUMN_2.getColumnName()))).thenReturn("col2_value2"); comparator.compare(currentDataSet, expectedDataSet, errorCollector); assertThat(errorCollector.amountOfErrors(), equalTo(0)); errorCollector.report(); }
@Test public void testCustomFilterImplementedNotAccordingToTheRequirements() throws Exception { final String[] orderBy = new String[] {}; final String[] toExclude = new String[] {}; final Set<Class<? extends IColumnFilter>> columnFilters = new HashSet<>(Arrays.asList(BadCustomFilter.class)); final DataSetComparator comparator = new DataSetComparator(orderBy, toExclude, false, columnFilters); when(expectedDataSet.getTableNames()).thenReturn(new String[] { TABLE_1_NAME }); when(expectedDataSet.getTable(any(String.class))).thenAnswer((final InvocationOnMock invocation) -> { final String tableName = (String) invocation.getArguments()[0]; if (tableName == TABLE_1_NAME) { return table1; } else { throw new NoSuchTableException(tableName); } }); when(table1.getValue(eq(0), eq(TABLE_1_COLUMN_2.getColumnName()))).thenReturn("col2_value1"); when(table1.getValue(eq(1), eq(TABLE_1_COLUMN_2.getColumnName()))).thenReturn("col2_value2"); when(currentDataSet.getTableNames()).thenReturn(new String[] { TABLE_1_NAME }); when(currentDataSet.getTable(any(String.class))).thenAnswer((final InvocationOnMock invocation) -> { final String tableName = (String) invocation.getArguments()[0]; if (tableName == TABLE_1_NAME) { return table4; } else { throw new NoSuchTableException(tableName); } }); when(table4.getRowCount()).thenReturn(TABLE_1_ENTRIES); when(table4.getTableMetaData()).thenReturn(table1MetaData); when(table4.getValue(eq(0), eq(TABLE_1_COLUMN_1.getColumnName()))).thenReturn("col1_value1"); when(table4.getValue(eq(1), eq(TABLE_1_COLUMN_1.getColumnName()))).thenReturn("col1_value2"); when(table4.getValue(eq(0), eq(TABLE_1_COLUMN_2.getColumnName()))).thenReturn("col2_value1"); when(table4.getValue(eq(1), eq(TABLE_1_COLUMN_2.getColumnName()))).thenReturn("col2_value2"); try { comparator.compare(currentDataSet, expectedDataSet, errorCollector); fail("JpaTestException expected"); } catch (final JpaUnitException e) { assertThat(e.getMessage(), containsString("Could not instanciate")); } }