Skip to content

Commit b90f3a1

Browse files
committed
#481 Add newProperty methods for temporal properties; minor test adjustments
1 parent 58d3e92 commit b90f3a1

File tree

5 files changed

+54
-6
lines changed

5 files changed

+54
-6
lines changed

src/main/java/ch/jalu/configme/properties/PropertyInitializer.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import ch.jalu.configme.properties.types.PropertyType;
99
import org.jetbrains.annotations.NotNull;
1010

11+
import java.time.LocalDate;
12+
import java.time.LocalDateTime;
13+
import java.time.LocalTime;
1114
import java.util.Collection;
1215
import java.util.List;
1316
import java.util.Map;
@@ -121,6 +124,42 @@ protected PropertyInitializer() {
121124
return new EnumProperty<>(path, clazz, defaultValue);
122125
}
123126

127+
/**
128+
* Creates a new LocalDate property.
129+
*
130+
* @param path the property's path
131+
* @param defaultValue the default value
132+
* @return the created property
133+
*/
134+
public static @NotNull LocalDateProperty newProperty(@NotNull String path,
135+
@NotNull LocalDate defaultValue) {
136+
return new LocalDateProperty(path, defaultValue);
137+
}
138+
139+
/**
140+
* Creates a new LocalTime property.
141+
*
142+
* @param path the property's path
143+
* @param defaultValue the default value
144+
* @return the created property
145+
*/
146+
public static @NotNull LocalTimeProperty newProperty(@NotNull String path,
147+
@NotNull LocalTime defaultValue) {
148+
return new LocalTimeProperty(path, defaultValue);
149+
}
150+
151+
/**
152+
* Creates a new LocalDateTime property.
153+
*
154+
* @param path the property's path
155+
* @param defaultValue the default value
156+
* @return the created property
157+
*/
158+
public static @NotNull LocalDateTimeProperty newProperty(@NotNull String path,
159+
@NotNull LocalDateTime defaultValue) {
160+
return new LocalDateTimeProperty(path, defaultValue);
161+
}
162+
124163
/**
125164
* Creates a new regex pattern property.
126165
*

src/main/java/ch/jalu/configme/properties/types/TemporalType.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ public TemporalType(@NotNull Class<T> clazz, @NotNull List<String> supportedForm
6767
return DateTimeFormatter.ofPattern(this.defaultExportFormat).format(value);
6868
}
6969

70-
private T convertToTemporalType(String temporalText) {
71-
for (String format: this.supportedFormats) {
70+
private @Nullable T convertToTemporalType(@NotNull String temporalText) {
71+
for (String format : this.supportedFormats) {
7272
try {
7373
T parsedValue = this.temporalParser.apply(temporalText, DateTimeFormatter.ofPattern(format));
7474
this.defaultExportFormat = format;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.junit.jupiter.api.extension.ExtendWith;
1616
import org.mockito.junit.jupiter.MockitoExtension;
1717

18+
import java.time.Duration;
1819
import java.util.ArrayList;
1920
import java.util.Arrays;
2021
import java.util.List;
@@ -169,11 +170,13 @@ void shouldNotConvertForUnsupportedTargetTypes() {
169170
Object object = "2020-02-13";
170171

171172
ConvertErrorRecorder errorRecorder = mock(ConvertErrorRecorder.class);
173+
MappingContext durationContext = MappingContextImpl.createRoot(of(Duration.class), errorRecorder);
172174
MappingContext wildcardContext = MappingContextImpl.createRoot(of(WildcardTypeImpl.newUnboundedWildcard()), errorRecorder);
173175

174176
LeafValueHandlerImpl leafValueHandler = new LeafValueHandlerImpl(LeafValueHandlerImpl.createDefaultLeafTypes());
175177

176178
// when / then
179+
assertThat(leafValueHandler.convert(object, durationContext), nullValue());
177180
assertThat(leafValueHandler.convert(object, wildcardContext), nullValue());
178181
}
179182

@@ -195,6 +198,7 @@ void shouldNotConvertUnsupportedValuesToExportValues() {
195198
ExportContext exportContext = ExportContextImpl.createRoot();
196199

197200
// when / then
201+
assertThat(leafValueHandler.toExportValue(Duration.ofSeconds(3), exportContext), nullValue());
198202
assertThat(leafValueHandler.toExportValue(new Object(), exportContext), nullValue());
199203
}
200204

src/test/java/ch/jalu/configme/properties/PropertyInitializerTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
import ch.jalu.configme.samples.TestEnum;
1212
import org.junit.jupiter.api.Test;
1313

14+
import java.time.LocalDate;
15+
import java.time.LocalDateTime;
16+
import java.time.LocalTime;
1417
import java.util.Arrays;
1518
import java.util.LinkedHashSet;
1619
import java.util.regex.Pattern;
@@ -55,6 +58,9 @@ void shouldInstantiateProperties() {
5558
assertThat(newProperty("my.path", -8.4d), instanceOf(DoubleProperty.class));
5659
assertThat(newProperty("my.path", "default"), instanceOf(StringProperty.class));
5760
assertThat(newProperty(TestEnum.class, "my.path", TestEnum.FIRST), instanceOf(EnumProperty.class));
61+
assertThat(newProperty("my.path", LocalDate.now()), instanceOf(LocalDateProperty.class));
62+
assertThat(newProperty("my.path", LocalTime.now()), instanceOf(LocalTimeProperty.class));
63+
assertThat(newProperty("my.path", LocalDateTime.now()), instanceOf(LocalDateTimeProperty.class));
5864
assertThat(newRegexProperty("reg.path", "abc?"), instanceOf(RegexProperty.class));
5965
assertThat(newRegexProperty("reg.path", Pattern.compile("w[0-9]*")), instanceOf(RegexProperty.class));
6066
assertThat(newListProperty("path", "default", "entries"), instanceOf(StringListProperty.class));

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import static ch.jalu.typeresolver.TypeInfo.of;
1515
import static org.hamcrest.MatcherAssert.assertThat;
1616
import static org.hamcrest.Matchers.equalTo;
17-
import static org.hamcrest.Matchers.matchesPattern;
1817
import static org.hamcrest.Matchers.notNullValue;
1918
import static org.hamcrest.Matchers.nullValue;
2019
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -23,7 +22,7 @@
2322
* Test for {@link TemporalType}.
2423
*/
2524
@ExtendWith(MockitoExtension.class)
26-
public class TemporalTypeTest {
25+
class TemporalTypeTest {
2726

2827
@AfterAll
2928
static void reset() {
@@ -35,12 +34,12 @@ static void reset() {
3534

3635
@Test
3736
void shouldNotAllowToInstantiateTemporalTypeWithInvalidArguments() {
38-
// when
37+
// given / when
3938
IllegalArgumentException noSupportedFormats = assertThrows(IllegalArgumentException.class,
4039
() -> new TemporalType<>(LocalDate.class, Collections.emptyList(), LocalDate::parse));
4140

4241
// then
43-
assertThat(noSupportedFormats.getMessage(), matchesPattern("At least one supported format must be provided."));
42+
assertThat(noSupportedFormats.getMessage(), equalTo("At least one supported format must be provided."));
4443
}
4544

4645
@Test

0 commit comments

Comments
 (0)