Skip to content

Commit 1f3d43d

Browse files
committed
#481 Provide support for LocalTime and LocalDateTime property type
1 parent 8d789bb commit 1f3d43d

File tree

5 files changed

+185
-1
lines changed

5 files changed

+185
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import java.time.LocalDate;
77

88
/**
9-
* LocalDate property.
9+
* {@link LocalDate} property.
1010
*/
1111
public class LocalDateProperty extends TypeBasedProperty<LocalDate> {
1212

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package ch.jalu.configme.properties;
2+
3+
import ch.jalu.configme.properties.types.TemporalType;
4+
import org.jetbrains.annotations.NotNull;
5+
6+
import java.time.LocalDateTime;
7+
8+
/**
9+
* {@link LocalDateTime} property.
10+
*/
11+
public class LocalDateTimeProperty extends TypeBasedProperty<LocalDateTime> {
12+
13+
/**
14+
* Constructor.
15+
*
16+
* @param path the path of the property
17+
* @param defaultValue the default value of the property
18+
*/
19+
public LocalDateTimeProperty(@NotNull String path, @NotNull LocalDateTime defaultValue) {
20+
super(path, TemporalType.LOCAL_DATE_TIME, defaultValue);
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package ch.jalu.configme.properties;
2+
3+
import ch.jalu.configme.properties.types.TemporalType;
4+
import org.jetbrains.annotations.NotNull;
5+
6+
import java.time.LocalTime;
7+
8+
/**
9+
* {@link LocalTime} property.
10+
*/
11+
public class LocalTimeProperty extends TypeBasedProperty<LocalTime> {
12+
13+
/**
14+
* Constructor.
15+
*
16+
* @param path the path of the property
17+
* @param defaultValue the default value of the property
18+
*/
19+
public LocalTimeProperty(@NotNull String path, @NotNull LocalTime defaultValue) {
20+
super(path, TemporalType.LOCAL_TIME, defaultValue);
21+
}
22+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package ch.jalu.configme.properties;
2+
3+
import ch.jalu.configme.properties.convertresult.PropertyValue;
4+
import ch.jalu.configme.resource.PropertyReader;
5+
import org.junit.jupiter.api.BeforeAll;
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.api.extension.ExtendWith;
8+
import org.mockito.junit.jupiter.MockitoExtension;
9+
10+
import java.time.LocalDateTime;
11+
12+
import static ch.jalu.configme.TestUtils.isErrorValueOf;
13+
import static ch.jalu.configme.TestUtils.isValidValueOf;
14+
import static org.hamcrest.MatcherAssert.assertThat;
15+
import static org.hamcrest.Matchers.equalTo;
16+
import static org.mockito.Mockito.mock;
17+
import static org.mockito.Mockito.when;
18+
19+
/**
20+
* Test for {@link LocalDateTimeProperty}.
21+
*/
22+
@ExtendWith(MockitoExtension.class)
23+
class LocalDateTimePropertyTest {
24+
25+
private static PropertyReader reader;
26+
27+
@BeforeAll
28+
static void setUpConfiguration() {
29+
reader = mock(PropertyReader.class);
30+
when(reader.getObject("local-date-time.path.test")).thenReturn("2001-10-20 19:55:30");
31+
when(reader.getObject("local-date-time.path.wrong")).thenReturn("20-2001-10 55:19:30");
32+
}
33+
34+
@Test
35+
void shouldGetLocalDateTimeValue() {
36+
// given
37+
Property<LocalDateTime> property = new LocalDateTimeProperty("local-date-time.path.test", LocalDateTime.of(1970, 1, 31, 12, 0));
38+
39+
// when
40+
PropertyValue<LocalDateTime> result = property.determineValue(reader);
41+
42+
// then
43+
assertThat(result, isValidValueOf(LocalDateTime.of(2001, 10, 20, 19, 55, 30)));
44+
}
45+
46+
@Test
47+
void shouldGetLocalDateTimeDefault() {
48+
// given
49+
LocalDateTime defaultDateTime = LocalDateTime.of(1970, 1, 31, 12, 0);
50+
Property<LocalDateTime> property = new LocalDateTimeProperty("local-date-time.path.wrong", defaultDateTime);
51+
52+
// when
53+
PropertyValue<LocalDateTime> result = property.determineValue(reader);
54+
55+
// then
56+
assertThat(result, isErrorValueOf(defaultDateTime));
57+
}
58+
59+
@Test
60+
void shouldReturnValueForExport() {
61+
// given
62+
Property<LocalDateTime> property = new LocalDateTimeProperty("export.path.local-date-time", LocalDateTime.of(1970, 1, 31, 12, 0));
63+
64+
// when
65+
Object exportedValue = property.toExportValue(LocalDateTime.of(2001, 10, 22, 11, 39, 42));
66+
67+
// then
68+
assertThat(exportedValue, equalTo("2001-10-22 11:39:42"));
69+
}
70+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package ch.jalu.configme.properties;
2+
3+
import ch.jalu.configme.properties.convertresult.PropertyValue;
4+
import ch.jalu.configme.resource.PropertyReader;
5+
import org.junit.jupiter.api.BeforeAll;
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.api.extension.ExtendWith;
8+
import org.mockito.junit.jupiter.MockitoExtension;
9+
10+
import java.time.LocalTime;
11+
12+
import static ch.jalu.configme.TestUtils.isErrorValueOf;
13+
import static ch.jalu.configme.TestUtils.isValidValueOf;
14+
import static org.hamcrest.MatcherAssert.assertThat;
15+
import static org.hamcrest.Matchers.equalTo;
16+
import static org.mockito.Mockito.mock;
17+
import static org.mockito.Mockito.when;
18+
19+
/**
20+
* Test for {@link LocalTimeProperty}.
21+
*/
22+
@ExtendWith(MockitoExtension.class)
23+
class LocalTimePropertyTest {
24+
25+
private static PropertyReader reader;
26+
27+
@BeforeAll
28+
static void setUpConfiguration() {
29+
reader = mock(PropertyReader.class);
30+
when(reader.getObject("local-time.path.test")).thenReturn("19:55:30");
31+
when(reader.getObject("local-time.path.wrong")).thenReturn("55:19:30");
32+
}
33+
34+
@Test
35+
void shouldGetLocalTimeValue() {
36+
// given
37+
Property<LocalTime> property = new LocalTimeProperty("local-time.path.test", LocalTime.of(12, 0));
38+
39+
// when
40+
PropertyValue<LocalTime> result = property.determineValue(reader);
41+
42+
// then
43+
assertThat(result, isValidValueOf(LocalTime.of(19, 55, 30)));
44+
}
45+
46+
@Test
47+
void shouldGetLocalTimeDefault() {
48+
// given
49+
LocalTime defaultTime = LocalTime.of(12, 0);
50+
Property<LocalTime> property = new LocalTimeProperty("local-time.path.wrong", defaultTime);
51+
52+
// when
53+
PropertyValue<LocalTime> result = property.determineValue(reader);
54+
55+
// then
56+
assertThat(result, isErrorValueOf(defaultTime));
57+
}
58+
59+
@Test
60+
void shouldReturnValueForExport() {
61+
// given
62+
Property<LocalTime> property = new LocalTimeProperty("export.path.local-time", LocalTime.of(23, 59));
63+
64+
// when
65+
Object exportedValue = property.toExportValue(LocalTime.of(19, 55));
66+
67+
// then
68+
assertThat(exportedValue, equalTo("19:55:00"));
69+
}
70+
}

0 commit comments

Comments
 (0)