|
|
|
@ -37,11 +37,37 @@ public class PropertyMapperTests {
|
|
|
|
|
@Rule
|
|
|
|
|
public ExpectedException thrown = ExpectedException.none();
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void fromNullValue() {
|
|
|
|
|
ExampleDest dest = new ExampleDest();
|
|
|
|
|
this.map.from((String) null).to(dest::setName);
|
|
|
|
|
assertThat(dest.getName()).isNull();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void fromValue() {
|
|
|
|
|
ExampleDest dest = new ExampleDest();
|
|
|
|
|
this.map.from("Hello World").to(dest::setName);
|
|
|
|
|
assertThat(dest.getName()).isEqualTo("Hello World");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void fromValueAsIntShouldAdaptSupplier() {
|
|
|
|
|
Integer result = this.map.from("123").asInt(Long::valueOf)
|
|
|
|
|
.toInstance(Integer::new);
|
|
|
|
|
assertThat(result).isEqualTo(123);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void fromValueAlwaysApplyingWhenNonNullShouldAlwaysApplyNonNullToSource() {
|
|
|
|
|
this.map.alwaysApplyingWhenNonNull().from((String) null).toCall(Assert::fail);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void fromWhenSupplierIsNullShouldThrowException() {
|
|
|
|
|
this.thrown.expect(IllegalArgumentException.class);
|
|
|
|
|
this.thrown.expectMessage("Supplier must not be null");
|
|
|
|
|
this.map.from(null);
|
|
|
|
|
this.map.from((Supplier<?>) null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@ -94,24 +120,24 @@ public class PropertyMapperTests {
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void whenTrueWhenValueIsTrueShouldMap() {
|
|
|
|
|
Boolean result = this.map.from(() -> true).whenTrue().toInstance(Boolean::new);
|
|
|
|
|
Boolean result = this.map.from(true).whenTrue().toInstance(Boolean::new);
|
|
|
|
|
assertThat(result).isTrue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void whenTrueWhenValueIsFalseShouldNotMap() {
|
|
|
|
|
this.map.from(() -> false).whenTrue().toCall(Assert::fail);
|
|
|
|
|
this.map.from(false).whenTrue().toCall(Assert::fail);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void whenFalseWhenValueIsFalseShouldMap() {
|
|
|
|
|
Boolean result = this.map.from(() -> false).whenFalse().toInstance(Boolean::new);
|
|
|
|
|
Boolean result = this.map.from(false).whenFalse().toInstance(Boolean::new);
|
|
|
|
|
assertThat(result).isFalse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void whenFalseWhenValueIsTrueShouldNotMap() {
|
|
|
|
|
this.map.from(() -> true).whenFalse().toCall(Assert::fail);
|
|
|
|
|
this.map.from(true).whenFalse().toCall(Assert::fail);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@ -121,30 +147,29 @@ public class PropertyMapperTests {
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void whenHasTextWhenValueIsEmptyShouldNotMap() {
|
|
|
|
|
this.map.from(() -> "").whenHasText().toCall(Assert::fail);
|
|
|
|
|
this.map.from("").whenHasText().toCall(Assert::fail);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void whenHasTextWhenValueHasTextShouldMap() {
|
|
|
|
|
Integer result = this.map.from(() -> 123).whenHasText().toInstance(Integer::new);
|
|
|
|
|
Integer result = this.map.from(123).whenHasText().toInstance(Integer::new);
|
|
|
|
|
assertThat(result).isEqualTo(123);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void whenEqualToWhenValueIsEqualShouldMatch() {
|
|
|
|
|
String result = this.map.from(() -> "123").whenEqualTo("123")
|
|
|
|
|
.toInstance(String::new);
|
|
|
|
|
String result = this.map.from("123").whenEqualTo("123").toInstance(String::new);
|
|
|
|
|
assertThat(result).isEqualTo("123");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void whenEqualToWhenValueIsNotEqualShouldNotMatch() {
|
|
|
|
|
this.map.from(() -> "123").whenEqualTo("321").toCall(Assert::fail);
|
|
|
|
|
this.map.from("123").whenEqualTo("321").toCall(Assert::fail);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void whenInstanceOfWhenValueIsTargetTypeShouldMatch() {
|
|
|
|
|
Long result = this.map.from(() -> 123L).whenInstanceOf(Long.class)
|
|
|
|
|
Long result = this.map.from(123L).whenInstanceOf(Long.class)
|
|
|
|
|
.toInstance((value) -> value + 1);
|
|
|
|
|
assertThat(result).isEqualTo(124L);
|
|
|
|
|
}
|
|
|
|
@ -157,14 +182,13 @@ public class PropertyMapperTests {
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void whenWhenValueMatchesShouldMap() {
|
|
|
|
|
String result = this.map.from(() -> "123").when("123"::equals)
|
|
|
|
|
.toInstance(String::new);
|
|
|
|
|
String result = this.map.from("123").when("123"::equals).toInstance(String::new);
|
|
|
|
|
assertThat(result).isEqualTo("123");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void whenWhenValueDoesNotMatchShouldNotMap() {
|
|
|
|
|
this.map.from(() -> "123").when("321"::equals).toCall(Assert::fail);
|
|
|
|
|
this.map.from("123").when("321"::equals).toCall(Assert::fail);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|