Skip to content

Commit

Permalink
Merge pull request #190 from sta-szek/#189-Support-for-java.time.Instant
Browse files Browse the repository at this point in the history
#189. New field value changer and documentations improvements
  • Loading branch information
Piotr Joński authored Mar 13, 2017
2 parents 8602ae8 + f36e354 commit e2d6963
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 17 deletions.
47 changes: 31 additions & 16 deletions src/book/writing-tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Instant> {

@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);
}
}
Original file line number Diff line number Diff line change
@@ -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<DynamicTest> 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;
}
}

0 comments on commit e2d6963

Please sign in to comment.