Skip to content

Commit aac8c1c

Browse files
committed
#481 Support default TemporalTypes for bean mapper
1 parent 1f3d43d commit aac8c1c

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/LeafValueHandlerImpl.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import ch.jalu.configme.properties.types.NumberType;
88
import ch.jalu.configme.properties.types.RegexType;
99
import ch.jalu.configme.properties.types.StringType;
10+
import ch.jalu.configme.properties.types.TemporalType;
1011
import ch.jalu.typeresolver.TypeInfo;
1112
import org.jetbrains.annotations.NotNull;
1213
import org.jetbrains.annotations.Nullable;
@@ -74,7 +75,10 @@ public LeafValueHandlerImpl(@NotNull MapperLeafType @NotNull ... leafTypes) {
7475
NumberType.SHORT,
7576
NumberType.BIG_INTEGER,
7677
NumberType.BIG_DECIMAL,
77-
RegexType.REGEX)
78+
RegexType.REGEX,
79+
TemporalType.LOCAL_DATE,
80+
TemporalType.LOCAL_TIME,
81+
TemporalType.LOCAL_DATE_TIME)
7882
.collect(Collectors.toCollection(ArrayList::new));
7983
}
8084

src/test/java/ch/jalu/configme/beanmapper/leafvaluehandler/LeafValueHandlerImplTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
import ch.jalu.configme.properties.types.NumberType;
1010
import ch.jalu.configme.properties.types.RegexType;
1111
import ch.jalu.configme.properties.types.StringType;
12+
import ch.jalu.configme.properties.types.TemporalType;
1213
import ch.jalu.typeresolver.typeimpl.WildcardTypeImpl;
1314
import org.junit.jupiter.api.Test;
1415
import org.junit.jupiter.api.extension.ExtendWith;
1516
import org.mockito.junit.jupiter.MockitoExtension;
1617

17-
import java.time.LocalDate;
1818
import java.util.ArrayList;
1919
import java.util.Arrays;
2020
import java.util.List;
@@ -59,7 +59,7 @@ void shouldReturnDefaultLeafTypes() {
5959
List<MapperLeafType> leafTypes = LeafValueHandlerImpl.createDefaultLeafTypes();
6060

6161
// then
62-
assertThat(leafTypes, hasSize(12));
62+
assertThat(leafTypes, hasSize(15));
6363
assertThat(leafTypes.get(0), sameInstance(BooleanType.BOOLEAN));
6464
assertThat(leafTypes.get(1), sameInstance(StringType.STRING));
6565
assertThat(leafTypes.get(2), sameInstance(NumberType.INTEGER));
@@ -72,6 +72,9 @@ void shouldReturnDefaultLeafTypes() {
7272
assertThat(leafTypes.get(9), sameInstance(NumberType.BIG_INTEGER));
7373
assertThat(leafTypes.get(10), sameInstance(NumberType.BIG_DECIMAL));
7474
assertThat(leafTypes.get(11), sameInstance(RegexType.REGEX));
75+
assertThat(leafTypes.get(12), sameInstance(TemporalType.LOCAL_DATE));
76+
assertThat(leafTypes.get(13), sameInstance(TemporalType.LOCAL_TIME));
77+
assertThat(leafTypes.get(14), sameInstance(TemporalType.LOCAL_DATE_TIME));
7578
}
7679

7780
@Test
@@ -96,7 +99,7 @@ void shouldCreateValueHandlerWithBuilder() {
9699
.addType(leafType1)
97100
.addDefaults()
98101
.addType(leafType2)
99-
.removeMatchingTypes(type -> type instanceof NumberType)
102+
.removeMatchingTypes(type -> type instanceof NumberType || type instanceof TemporalType)
100103
.build();
101104

102105
// then
@@ -166,13 +169,11 @@ void shouldNotConvertForUnsupportedTargetTypes() {
166169
Object object = "2020-02-13";
167170

168171
ConvertErrorRecorder errorRecorder = mock(ConvertErrorRecorder.class);
169-
MappingContext dateContext = MappingContextImpl.createRoot(of(LocalDate.class), errorRecorder);
170172
MappingContext wildcardContext = MappingContextImpl.createRoot(of(WildcardTypeImpl.newUnboundedWildcard()), errorRecorder);
171173

172174
LeafValueHandlerImpl leafValueHandler = new LeafValueHandlerImpl(LeafValueHandlerImpl.createDefaultLeafTypes());
173175

174176
// when / then
175-
assertThat(leafValueHandler.convert(object, dateContext), nullValue());
176177
assertThat(leafValueHandler.convert(object, wildcardContext), nullValue());
177178
}
178179

@@ -194,7 +195,6 @@ void shouldNotConvertUnsupportedValuesToExportValues() {
194195
ExportContext exportContext = ExportContextImpl.createRoot();
195196

196197
// when / then
197-
assertThat(leafValueHandler.toExportValue(LocalDate.now(), exportContext), nullValue());
198198
assertThat(leafValueHandler.toExportValue(new Object(), exportContext), nullValue());
199199
}
200200

src/test/java/ch/jalu/configme/properties/types/TemporalTypeTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010

1111
import static ch.jalu.typeresolver.TypeInfo.of;
1212
import static org.hamcrest.MatcherAssert.assertThat;
13-
import static org.hamcrest.Matchers.*;
13+
import static org.hamcrest.Matchers.equalTo;
14+
import static org.hamcrest.Matchers.matchesPattern;
15+
import static org.hamcrest.Matchers.notNullValue;
16+
import static org.hamcrest.Matchers.nullValue;
1417
import static org.junit.jupiter.api.Assertions.assertThrows;
1518

1619
/**

0 commit comments

Comments
 (0)