From f36e3549dffe9e0ed06b2e326c6542af38b62904 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Jo=C5=84ski?= Date: Mon, 13 Mar 2017 19:18:36 +0100 Subject: [PATCH] #189. New field value changer and documentations improvements --- src/book/writing-tests/README.md | 47 ++++++---- .../DefaultDateAndTimeFieldValueChanger.java | 3 +- .../field/date/InstantFieldValueChanger.java | 18 ++++ .../date/InstantFieldValueChangerTest.java | 85 +++++++++++++++++++ 4 files changed, 136 insertions(+), 17 deletions(-) create mode 100644 src/main/java/pl/pojo/tester/internal/field/date/InstantFieldValueChanger.java create mode 100644 src/test/java/pl/pojo/tester/internal/field/date/InstantFieldValueChangerTest.java diff --git a/src/book/writing-tests/README.md b/src/book/writing-tests/README.md index 080f1474..590acba2 100644 --- a/src/book/writing-tests/README.md +++ b/src/book/writing-tests/README.md @@ -276,19 +276,7 @@ final AbstractFieldValueChanger valueChanger = DefaultFieldValueChanger.INSTANCE ### Default fields values changer Default field value changer (`DefaultFieldValueChanger.INSTANCE`) is a composition of listed changers: -* `UUIDValueChanger` -* `EnumValueChanger` -* `BooleanValueChanger` -* `ByteValueChanger` -* `CharacterValueChanger` -* `DoubleValueChanger` -* `IntegerValueChanger` -* `LongValueChanger` -* `ShortValueChanger` -* `StringValueChanger` -* `FloatValueChanger` -* `ArrayValueChanger` -* `StreamValueChanger` +######Collections * `ArrayListValueChanger` * `DequeValueChanger` * `HashSetValueChanger` @@ -301,17 +289,44 @@ Default field value changer (`DefaultFieldValueChanger.INSTANCE`) is a compositi * `StackValueChanger` * `TreeSetValueChanger` * `VectorValueChanger` +* `IterableValueChanger` +* `IteratorValueChanger` * `HashMapValueChanger` * `HashtableValueChanger` * `LinkedHashMapValueChanger` * `MapValueChanger` * `SortedMapValueChanger` * `TreeMapValueChanger` -* `IteratorValueChanger` -* `IterableValueChanger` -* `BigIntegerValueChanger` +* `ArrayValueChanger` +* `StreamValueChanger` + +######Date and time +* `DateFieldValueChanger` +* `InstantFieldValueChanger` +* `LocalDateFieldValueChanger` +* `LocalDateTimeFieldValueChanger` +* `LocalTimeFieldValueChanger` +* `SqlDateFieldValueChanger` +* `ZonedDateTimeFieldValueChanger` + +######Math * `BigDecimalValueChanger` +* `BigIntegerValueChanger` +######Primitives and wrappers +* `BooleanValueChanger` +* `ByteValueChanger` +* `CharacterValueChanger` +* `DoubleValueChanger` +* `FloatValueChanger` +* `IntegerValueChanger` +* `LongValueChanger` +* `ShortValueChanger` + +######Others +* `EnumValueChanger` +* `StringValueChanger` +* `UUIDValueChanger` ## Create class using selected constructor {#choose-constructor} Sometimes you want to choose which constructor is used to instantiate your class or what parameters are passed. Common example is when constructor validates parameters and throws exceptions. diff --git a/src/main/java/pl/pojo/tester/internal/field/date/DefaultDateAndTimeFieldValueChanger.java b/src/main/java/pl/pojo/tester/internal/field/date/DefaultDateAndTimeFieldValueChanger.java index a8b77531..1af34a8c 100644 --- a/src/main/java/pl/pojo/tester/internal/field/date/DefaultDateAndTimeFieldValueChanger.java +++ b/src/main/java/pl/pojo/tester/internal/field/date/DefaultDateAndTimeFieldValueChanger.java @@ -8,7 +8,8 @@ public final class DefaultDateAndTimeFieldValueChanger { .attachNext(new LocalDateFieldValueChanger()) .attachNext(new LocalDateTimeFieldValueChanger()) .attachNext(new LocalTimeFieldValueChanger()) - .attachNext(new SqlDateFieldValueChanger()); + .attachNext(new SqlDateFieldValueChanger()) + .attachNext(new InstantFieldValueChanger()); private DefaultDateAndTimeFieldValueChanger() { } diff --git a/src/main/java/pl/pojo/tester/internal/field/date/InstantFieldValueChanger.java b/src/main/java/pl/pojo/tester/internal/field/date/InstantFieldValueChanger.java new file mode 100644 index 00000000..b9516e3d --- /dev/null +++ b/src/main/java/pl/pojo/tester/internal/field/date/InstantFieldValueChanger.java @@ -0,0 +1,18 @@ +package pl.pojo.tester.internal.field.date; + +import pl.pojo.tester.internal.field.AbstractFieldValueChanger; + +import java.time.Instant; + +class InstantFieldValueChanger extends AbstractFieldValueChanger { + + @Override + protected Instant increaseValue(final Instant value, final Class type) { + return value.plusSeconds(3600); + } + + @Override + protected boolean canChange(final Class type) { + return type.equals(Instant.class); + } +} diff --git a/src/test/java/pl/pojo/tester/internal/field/date/InstantFieldValueChangerTest.java b/src/test/java/pl/pojo/tester/internal/field/date/InstantFieldValueChangerTest.java new file mode 100644 index 00000000..48019610 --- /dev/null +++ b/src/test/java/pl/pojo/tester/internal/field/date/InstantFieldValueChangerTest.java @@ -0,0 +1,85 @@ +package pl.pojo.tester.internal.field.date; + +import lombok.AllArgsConstructor; +import org.junit.jupiter.api.DynamicTest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestFactory; +import org.junit.jupiter.api.function.Executable; + +import java.time.Instant; +import java.util.Date; +import java.util.stream.Stream; + +import static helpers.TestHelper.getDefaultDisplayName; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.DynamicTest.dynamicTest; + +class InstantFieldValueChangerTest { + + private final InstantFieldValueChanger valueChanger = new InstantFieldValueChanger(); + + @TestFactory + public Stream Should_Return_True_Or_False_Whether_Values_Are_Different_Or_Not() { + final Instant instant1 = new Date(123456).toInstant(); + final Instant instant2 = new Date(654321).toInstant(); + return Stream.of(new AreDifferentCase(null, null, false), + new AreDifferentCase(instant1, instant1, false), + new AreDifferentCase(null, instant2, true), + new AreDifferentCase(instant1, null, true), + new AreDifferentCase(instant1, instant2, true)) + .map(value -> dynamicTest(getDefaultDisplayName(value.value1 + " " + value.value2), + Should_Return_True_Or_False_Whether_Values_Are_Different_Or_Not(value))); + } + + public Executable Should_Return_True_Or_False_Whether_Values_Are_Different_Or_Not(final AreDifferentCase testCase) { + return () -> { + // when + final boolean result = valueChanger.areDifferentValues(testCase.value1, testCase.value2); + + // then + assertThat(result).isEqualTo(testCase.result); + }; + } + + @Test + public void Should_Increase_Value() { + // given + final Instant beforeChange = new Date().toInstant(); + + // when + final Instant result = valueChanger.increaseValue(beforeChange, beforeChange.getClass()); + + // then + assertThat(result).isNotEqualTo(beforeChange); + } + + @Test + public void Should_Return_True_If_Can_Change() { + // given + + // when + final boolean result = valueChanger.canChange(Instant.class); + + // then + assertThat(result).isTrue(); + } + + @Test + public void Should_Return_False_If_Can_Not_Change() { + // given + + // when + final boolean result = valueChanger.canChange(String.class); + + // then + assertThat(result).isFalse(); + } + + @AllArgsConstructor + private class AreDifferentCase { + + private Instant value1; + private Instant value2; + private boolean result; + } +}