/** This method tests the {@code isOfType} assertion method. */ @Test @NeedReload public void test_is_of_type() { Changes changes = new Changes(source).setStartPointNow(); update("update test set var14 = 1 where var1 = 1"); update( "insert into test(var1, var2, var11, var10, var9, var3, var12, var8) values(5, true, FILE_READ('classpath:h2-logo-2.png'), '2014-05-24 09:46:30', '2014-05-24', 3, 'test', '09:46:30')"); changes.setEndPointNow(); Table table = new Table(source, "test"); Table table2 = new Table(source, "test2"); ChangeColumnAssert changeColumnAssert1 = assertThat(changes).change().column("var2"); ChangeColumnAssert changeColumnAssertReturn1 = changeColumnAssert1.isOfType(ValueType.BOOLEAN, true); Assertions.assertThat(changeColumnAssert1).isSameAs(changeColumnAssertReturn1); ChangeColumnAssert changeColumnAssert2 = assertThat(changes).change(1).column("var2"); ChangeColumnAssert changeColumnAssertReturn2 = changeColumnAssert2.isOfType(ValueType.BOOLEAN, false); Assertions.assertThat(changeColumnAssert2).isSameAs(changeColumnAssertReturn2); TableColumnAssert tableColumnAssert1 = assertThat(table).column("var2"); TableColumnAssert tableColumnAssertReturn1 = tableColumnAssert1.isOfType(ValueType.BOOLEAN, false); Assertions.assertThat(tableColumnAssert1).isSameAs(tableColumnAssertReturn1); TableColumnAssert tableColumnAssert2 = assertThat(table2).column("var2"); TableColumnAssert tableColumnAssertReturn2 = tableColumnAssert2.isOfType(ValueType.BOOLEAN, true); Assertions.assertThat(tableColumnAssert2).isSameAs(tableColumnAssertReturn2); }
/** This method should fail because the value is not null. */ @Test @NeedReload public void should_fail_because_value_is_not_null() { Table table = new Table(source, "test"); Changes changes = new Changes(table).setStartPointNow(); update("update test set var14 = 1 where var1 = 1"); changes.setEndPointNow(); try { assertThat(changes).change().column("var3").valueAtEndPoint().isNull(); fail("An exception must be raised"); } catch (AssertionError e) { Assertions.assertThat(e.getMessage()) .isEqualTo( String.format( "[Value at end point of Column at index 2 (column name : VAR3) of Change at index 0 (with primary key : [1]) of Changes on TEST table of 'sa/jdbc:h2:mem:test' source] expected:<null> but was:<2>")); } try { assertThat(table).column("var3").value().isNull(); fail("An exception must be raised"); } catch (AssertionError e) { Assertions.assertThat(e.getMessage()) .isEqualTo( String.format( "[Value at index 0 of Column at index 2 (column name : VAR3) of TEST table] expected:<null> but was:<2>")); } }
/** This method tests the {@code hasModifiedColumns} assertion method. */ @Test @NeedReload public void test_has_modified_columns() throws Exception { Changes changes = new Changes(source).setStartPointNow(); updateChangesForTests(); changes.setEndPointNow(); ChangeAssert changeAssert = assertThat(changes).change(3); ChangeAssert changeAssert2 = changeAssert.hasModifiedColumns("FIRSTNAME"); Assertions.assertThat(changeAssert).isSameAs(changeAssert2); }
/** This method should fail because the values are different. */ @Test @NeedReload public void should_fail_because_values_are_different() { Table table = new Table(source, "actor"); Changes changes = new Changes(table).setStartPointNow(); updateChangesForTests(); changes.setEndPointNow(); try { assertThat(changes) .change() .rowAtEndPoint() .hasValues( 4, "Murray", "Billy", "1950-09-21", UUID.fromString("30B443AE-C0C9-4790-9BEC-CE1380808435")); } catch (AssertionError e) { Assertions.assertThat(e.getMessage()) .isEqualTo( String.format( "[Row at end point of Change at index 0 (with primary key : [4]) of Changes on actor table of 'sa/jdbc:h2:mem:test' source] %n" + "Expecting that the value at index 2:%n" + " <\"Bill\">%n" + "to be equal to: %n" + " <\"Billy\">")); } try { assertThat(table) .row() .hasValues( 1, "Weaver", "Sigourney", "1949-10-08", UUID.fromString("648DFAC8-14AC-47F7-95CF-3475525A3BE3")); } catch (AssertionError e) { Assertions.assertThat(e.getMessage()) .isEqualTo( String.format( "[Row at index 0 of actor table] %n" + "Expecting that the value at index 2:%n" + " <\"Susan Alexandra\">%n" + "to be equal to: %n" + " <\"Sigourney\">")); } }
/** This method should fail because the value is no close to. */ @Test @NeedReload public void should_fail_because_value_is_not_close_to() { Table table = new Table(source, "test"); Changes changes = new Changes(table).setStartPointNow(); update("update test set var14 = 1 where var1 = 1"); changes.setEndPointNow(); try { assertThat(changes) .change() .column("var10") .valueAtEndPoint() .isCloseTo( DateTimeValue.of(DateValue.of(2014, 5, 24), TimeValue.of(9, 46, 31)), TimeValue.of(0, 0, 0, 1)); fail("An exception must be raised"); } catch (AssertionError e) { Assertions.assertThat(e.getMessage()) .isEqualTo( String.format( "[Value at end point of Column at index 9 (column name : VAR10) of Change at index 0 (with primary key : [1]) of Changes on TEST table of 'sa/jdbc:h2:mem:test' source] %n" + "Expecting:%n" + " <2014-05-24T09:46:30.000000000>%n" + "to be close to: %n" + " <2014-05-24T09:46:31.000000000> %n" + " with tolerance <00:00:00.000000001>")); } try { assertThat(table) .column("var10") .value() .isCloseTo( DateTimeValue.of(DateValue.of(2014, 5, 24), TimeValue.of(9, 46, 31)), TimeValue.of(0, 0, 0, 100)); fail("An exception must be raised"); } catch (AssertionError e) { Assertions.assertThat(e.getMessage()) .isEqualTo( String.format( "[Value at index 0 of Column at index 9 (column name : VAR10) of TEST table] %n" + "Expecting:%n" + " <2014-05-24T09:46:30.000000000>%n" + "to be close to: %n" + " <2014-05-24T09:46:31.000000000> %n" + " with tolerance <00:00:00.000000100>")); } }
/** This method tests the {@code isNull} assertion method. */ @Test @NeedReload public void test_is_null() { Table table = new Table(source, "test2"); Changes changes = new Changes(table).setStartPointNow(); update("update test2 set var14 = 1 where var1 is null"); changes.setEndPointNow(); ChangeColumnValueAssert changeColumnValueAssert = assertThat(changes).change().column("var10").valueAtEndPoint(); ChangeColumnValueAssert changeColumnValueAssert2 = changeColumnValueAssert.isNull(); Assertions.assertThat(changeColumnValueAssert).isSameAs(changeColumnValueAssert2); TableColumnValueAssert tableColumnValueAssert = assertThat(table).column("var10").value(1); TableColumnValueAssert tableColumnValueAssert2 = tableColumnValueAssert.isNull(); Assertions.assertThat(tableColumnValueAssert).isSameAs(tableColumnValueAssert2); }
/** This example shows simple cases of navigation for changes. */ @Test public void basic_navigation_examples_for_changes() throws SQLException { Changes changes = new Changes(dataSource); changes.setStartPointNow(); // Start point (the moment when the changes start to be taken into // account) makeChangesInTheData(); changes .setEndPointNow(); // End point (the moment when the changes stop to be taken into account) assertThat(changes) .change() // First change .isCreation() // Assertion on the first change .rowAtStartPoint() // Row at the start point .doesNotExist() // Assertion on the row at start point of the first change .returnToChange() // Go back to the change .rowAtEndPoint() // Row at the end point .hasNumberOfColumns(6) // Assertion on the row at end point of the first change .exists() // Another assertion on the same row .value(1) // Value at index 1 of the row at end point of the first change .isEqualTo("McGuiness") // Assertion on the value .returnToRow() // Go back to the row .returnToChange() // Go back to the change .change() // Next change .rowAtEndPoint() // Row at end point of this change .hasValues(1, "Hewson", "Paul David", "Bono Vox", "1960-05-10", 1.75) .column("surname") // Column with name is "surname" of the second change (note that // returnToChange() is not mandatory) .isModified() // Assertion on column .hasValues("Bono", "Bono Vox") .column() // Next column .isNotModified() // Assertion on the column .valueAtEndPoint() // Value at end point in the column after "surname" ("birth") of the // second change .isEqualTo(DateValue.of(1960, 5, 10)) .ofDeletion() // All the changes of deletion (note that the returnToXxxx() methods are not // mandatory) .change() // First change of these changes of deletion .isOnTable("albums") .hasPksValues(15) .changeOnTableWithPks("members", 5) // Change with primary key 5 on "members" table .isCreation() .rowAtEndPoint() // Row at end point of change with primary key 5 on "members" table .hasValues(5, "McGuiness", "Paul", null, "1951-06-17", null); }
/** This method tests the {@code isBefore} assertion method. */ @Test @NeedReload public void test_is_before() throws ParseException { Table table = new Table(source, "test"); Changes changes = new Changes(table).setStartPointNow(); update("update test set var14 = 1 where var1 = 1"); changes.setEndPointNow(); ChangeColumnValueAssert changeColumnValueAssert = assertThat(changes).change().column("var10").valueAtEndPoint(); ChangeColumnValueAssert changeColumnValueAssert2 = changeColumnValueAssert.isBefore(DateTimeValue.parse("2014-05-24T09:46:31")); Assertions.assertThat(changeColumnValueAssert).isSameAs(changeColumnValueAssert2); TableColumnValueAssert tableColumnValueAssert = assertThat(table).column("var10").value(); TableColumnValueAssert tableColumnValueAssert2 = tableColumnValueAssert.isBefore(DateTimeValue.parse("2014-05-24T09:46:31")); Assertions.assertThat(tableColumnValueAssert).isSameAs(tableColumnValueAssert2); }
/** This method should fail because the value is after or equal to. */ @Test @NeedReload public void should_fail_because_value_is_after_or_equal_to() throws ParseException { Table table = new Table(source, "test"); Changes changes = new Changes(table).setStartPointNow(); update("update test set var14 = 1 where var1 = 1"); changes.setEndPointNow(); try { assertThat(changes) .change() .column("var10") .valueAtEndPoint() .isBefore(DateTimeValue.parse("2014-05-24T09:46:30")); fail("An exception must be raised"); } catch (AssertionError e) { Assertions.assertThat(e.getMessage()) .isEqualTo( String.format( "[Value at end point of Column at index 9 (column name : VAR10) of Change at index 0 (with primary key : [1]) of Changes on TEST table of 'sa/jdbc:h2:mem:test' source] %n" + "Expecting:%n" + " <2014-05-24T09:46:30.000000000>%n" + "to be before %n" + " <2014-05-24T09:46:30.000000000>")); } try { assertThat(table) .column("var10") .value() .isBefore(DateTimeValue.parse("2014-05-24T09:46:30")); fail("An exception must be raised"); } catch (AssertionError e) { Assertions.assertThat(e.getMessage()) .isEqualTo( String.format( "[Value at index 0 of Column at index 9 (column name : VAR10) of TEST table] %n" + "Expecting:%n" + " <2014-05-24T09:46:30.000000000>%n" + "to be before %n" + " <2014-05-24T09:46:30.000000000>")); } }
/** This method should fail because the modified columns are different. */ @Test @NeedReload public void should_fail_because_modified_columns_are_different() throws Exception { Changes changes = new Changes(source).setStartPointNow(); updateChangesForTests(); changes.setEndPointNow(); try { assertThat(changes).change(3).hasModifiedColumns("ID"); fail("An exception must be raised"); } catch (AssertionError e) { Assertions.assertThat(e.getMessage()) .isEqualTo( String.format( "[Change at index 3 (on table : ACTOR and with primary key : [1]) of Changes on tables of 'sa/jdbc:h2:mem:test' source] %n" + "Expecting :%n" + " [\"ID\"]%n" + "as modified columns but was:%n" + " [\"FIRSTNAME\"]")); } }
/** This method should fail because the value is not a boolean. */ @Test @NeedReload public void should_fail_because_value_is_not_a_boolean() { Table table = new Table(source, "test"); Changes changes = new Changes(table).setStartPointNow(); update("update test set var14 = 1 where var1 = 1"); changes.setEndPointNow(); try { assertThat(changes).change().column("var1").valueAtEndPoint().isBoolean(); fail("An exception must be raised"); } catch (AssertionError e) { Assertions.assertThat(e.getMessage()) .isEqualTo( String.format( "[Value at end point of Column at index 0 (column name : VAR1) of Change at index 0 (with primary key : [1]) of Changes on TEST table of 'sa/jdbc:h2:mem:test' source] %n" + "Expecting:%n" + " <1>%n" + "to be of type%n" + " <BOOLEAN>%n" + "but was of type%n" + " <NUMBER>")); } try { assertThat(table).column("var1").value().isBoolean(); fail("An exception must be raised"); } catch (AssertionError e) { Assertions.assertThat(e.getMessage()) .isEqualTo( String.format( "[Value at index 0 of Column at index 0 (column name : VAR1) of TEST table] %n" + "Expecting:%n" + " <1>%n" + "to be of type%n" + " <BOOLEAN>%n" + "but was of type%n" + " <NUMBER>")); } }
/** This method tests the {@code isCloseTo} assertion method. */ @Test @NeedReload public void test_is_close_to() { Table table = new Table(source, "test"); Changes changes = new Changes(table).setStartPointNow(); update("update test set var14 = 1 where var1 = 1"); changes.setEndPointNow(); ChangeColumnValueAssert changeColumnValueAssert = assertThat(changes).change().column("var10").valueAtEndPoint(); ChangeColumnValueAssert changeColumnValueAssert2 = changeColumnValueAssert.isCloseTo( DateTimeValue.of(DateValue.of(2014, 5, 24), TimeValue.of(9, 46, 30)), TimeValue.of(0, 0, 0)); Assertions.assertThat(changeColumnValueAssert).isSameAs(changeColumnValueAssert2); TableColumnValueAssert tableColumnValueAssert = assertThat(table).column("var10").value(); TableColumnValueAssert tableColumnValueAssert2 = tableColumnValueAssert.isCloseTo( DateTimeValue.of(DateValue.of(2014, 5, 24), TimeValue.of(9, 46, 30)), TimeValue.of(0, 0, 0)); Assertions.assertThat(tableColumnValueAssert).isSameAs(tableColumnValueAssert2); }
/** This method tests the {@code hasValues} assertion method. */ @Test @NeedReload public void test_has_values() { Table table = new Table(source, "actor"); Changes changes = new Changes(table).setStartPointNow(); updateChangesForTests(); changes.setEndPointNow(); ChangeRowAssert changeRowAssert = assertThat(changes).change().rowAtEndPoint(); ChangeRowAssert changeRowAssert2 = changeRowAssert .hasValues(4, "Murray", "Bill", "1950-09-21", "30B443AE-C0C9-4790-9BEC-CE1380808435") .hasValues( 4, "Murray", "Bill", "1950-09-21", UUID.fromString("30B443AE-C0C9-4790-9BEC-CE1380808435")); Assertions.assertThat(changeRowAssert).isSameAs(changeRowAssert2); TableRowAssert tableRowAssert = assertThat(table).row(); TableRowAssert tableRowAssert2 = tableRowAssert .hasValues( 1, "Weaver", "Susan Alexandra", "1949-10-08", "30B443AE-C0C9-4790-9BEC-CE1380808435") .hasValues( 1, "Weaver", "Susan Alexandra", "1949-10-08", UUID.fromString("30B443AE-C0C9-4790-9BEC-CE1380808435")); Assertions.assertThat(tableRowAssert).isSameAs(tableRowAssert2); }
/** This method tests the {@code changeOfCreation} with index navigation method. */ @Test @NeedReload public void test_change_of_creation_with_index_with_assertions() throws Exception { Changes changes = new Changes(source).setStartPointNow(); updateChangesForTests(); changes.setEndPointNow(); Field fieldPosition = ChangesAssert.class.getDeclaredField("changesPosition"); fieldPosition.setAccessible(true); Field fieldList = Changes.class.getDeclaredField("changesList"); fieldList.setAccessible(true); Field fieldIndex = PositionWithChanges.class.getDeclaredField("indexNextChangeMap"); fieldIndex.setAccessible(true); Field fieldChange = ChangeAssert.class.getDeclaredField("change"); fieldChange.setAccessible(true); ChangesAssert changesAssert = assertThat(changes); PositionWithChanges position = (PositionWithChanges) fieldPosition.get(changesAssert); Map<ChangeType, Map<String, Integer>> map = (Map<ChangeType, Map<String, Integer>>) fieldIndex.get(position); assertThat(map).hasSize(0); assertThat(map.get(null)).isNull(); ChangeAssert changeAssert0 = changesAssert.changeOfCreation(0); assertThat(map).hasSize(1); assertThat(map.get(null)).isNull(); assertThat(map.get(ChangeType.CREATION)).hasSize(1); assertThat(map.get(ChangeType.CREATION).get(null)).isEqualTo(1); ChangeAssert changeAssert1 = changesAssert.changeOfCreation(1); assertThat(map).hasSize(1); assertThat(map.get(null)).isNull(); assertThat(map.get(ChangeType.CREATION)).hasSize(1); assertThat(map.get(ChangeType.CREATION).get(null)).isEqualTo(2); ChangeAssert changeAssert2 = changesAssert.changeOfCreation(2); assertThat(map).hasSize(1); assertThat(map.get(null)).isNull(); assertThat(map.get(ChangeType.CREATION)).hasSize(1); assertThat(map.get(ChangeType.CREATION).get(null)).isEqualTo(3); try { changesAssert.changeOfCreation(3); fail("An exception must be raised"); } catch (AssertJDBException e) { Assertions.assertThat(e.getMessage()).isEqualTo("Index 3 out of the limits [0, 3["); } try { changesAssert.changeOfCreation(-1); fail("An exception must be raised"); } catch (AssertJDBException e) { Assertions.assertThat(e.getMessage()).isEqualTo("Index -1 out of the limits [0, 3["); } ChangeAssert changeAssertAgain0 = changesAssert.changeOfCreation(0); assertThat(changeAssert0).isSameAs(changeAssertAgain0); ChangesAssert changesAssertBis = assertThat(changes); PositionWithChanges positionBis = (PositionWithChanges) fieldPosition.get(changesAssertBis); map = (Map<ChangeType, Map<String, Integer>>) fieldIndex.get(positionBis); assertThat(map).hasSize(0); assertThat(map.get(null)).isNull(); ChangeAssert changeAssertBis0 = changesAssertBis.changeOfCreation(0); assertThat(map).hasSize(1); assertThat(map.get(null)).isNull(); assertThat(map.get(ChangeType.CREATION)).hasSize(1); assertThat(map.get(ChangeType.CREATION).get(null)).isEqualTo(1); ChangeAssert changeAssertBis1 = changeAssertBis0.changeOfCreation(1); assertThat(map).hasSize(1); assertThat(map.get(null)).isNull(); assertThat(map.get(ChangeType.CREATION)).hasSize(1); assertThat(map.get(ChangeType.CREATION).get(null)).isEqualTo(2); ChangeAssert changeAssertBis2 = changeAssertBis1.changeOfCreation(2); assertThat(map).hasSize(1); assertThat(map.get(null)).isNull(); assertThat(map.get(ChangeType.CREATION)).hasSize(1); assertThat(map.get(ChangeType.CREATION).get(null)).isEqualTo(3); try { changeAssertBis2.changeOfCreation(3); fail("An exception must be raised"); } catch (AssertJDBException e) { Assertions.assertThat(e.getMessage()).isEqualTo("Index 3 out of the limits [0, 3["); } try { changeAssertBis2.changeOfCreation(-1); fail("An exception must be raised"); } catch (AssertJDBException e) { Assertions.assertThat(e.getMessage()).isEqualTo("Index -1 out of the limits [0, 3["); } ChangeAssert changeAssertBisAgain0 = changeAssertBis2.changeOfCreation(0); assertThat(changeAssertBis0).isSameAs(changeAssertBisAgain0); List<Changes> changesList = (List<Changes>) fieldList.get(changes); assertThat(fieldChange.get(changeAssert0)) .isSameAs(fieldChange.get(changeAssertBis0)) .isSameAs(changesList.get(0)); assertThat(fieldChange.get(changeAssert1)) .isSameAs(fieldChange.get(changeAssertBis1)) .isSameAs(changesList.get(1)); assertThat(fieldChange.get(changeAssert2)) .isSameAs(fieldChange.get(changeAssertBis2)) .isSameAs(changesList.get(2)); }
/** This method should fail because a value have different type. */ @Test @NeedReload public void should_fail_because_value_have_different_type() { Changes changes = new Changes(source).setStartPointNow(); update("update test set var14 = 1 where var1 = 1"); update( "insert into test(var1, var2, var11, var10, var9, var3, var12, var8) values(5, true, FILE_READ('classpath:h2-logo-2.png'), '2014-05-24 09:46:30', '2014-05-24', 3, 'test', '09:46:30')"); changes.setEndPointNow(); Table table = new Table(source, "test"); Table table2 = new Table(source, "test2"); try { assertThat(changes).change().column("var2").isOfType(ValueType.BOOLEAN, false); fail("An exception must be raised"); } catch (AssertionError e) { Assertions.assertThat(e.getMessage()) .isEqualTo( String.format( "[Column at index 1 (column name : VAR2) of Change at index 0 (on table : TEST and with primary key : [5]) of Changes on tables of 'sa/jdbc:h2:mem:test' source] %n" + "Expecting that the value at start point:%n" + " <null>%n" + "to be of type%n" + " <BOOLEAN>%n" + "but was of type%n" + " <NOT_IDENTIFIED>")); } try { assertThat(changes).change(1).column("var1").isOfType(ValueType.BOOLEAN, true); fail("An exception must be raised"); } catch (AssertionError e) { Assertions.assertThat(e.getMessage()) .isEqualTo( String.format( "[Column at index 0 (column name : VAR1) of Change at index 1 (on table : TEST and with primary key : [1]) of Changes on tables of 'sa/jdbc:h2:mem:test' source] %n" + "Expecting that the value at start point:%n" + " <1>%n" + "to be of type%n" + " <[BOOLEAN, NOT_IDENTIFIED]>%n" + "but was of type%n" + " <NUMBER>")); } try { assertThat(table).column("var1").isOfType(ValueType.BOOLEAN, true); fail("An exception must be raised"); } catch (AssertionError e) { Assertions.assertThat(e.getMessage()) .isEqualTo( String.format( "[Column at index 0 (column name : VAR1) of test table] %n" + "Expecting that the value at index 0:%n" + " <1>%n" + "to be of type%n" + " <[BOOLEAN, NOT_IDENTIFIED]>%n" + "but was of type%n" + " <NUMBER>")); } try { assertThat(table2).column("var2").isOfType(ValueType.BOOLEAN, false); fail("An exception must be raised"); } catch (AssertionError e) { Assertions.assertThat(e.getMessage()) .isEqualTo( String.format( "[Column at index 1 (column name : VAR2) of test2 table] %n" + "Expecting that the value at index 1:%n" + " <null>%n" + "to be of type%n" + " <BOOLEAN>%n" + "but was of type%n" + " <NOT_IDENTIFIED>")); } }