1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.basepom.mojo.propertyhelper.fields;
16
17 import static java.lang.String.format;
18 import static org.assertj.core.api.Assertions.assertThat;
19 import static org.assertj.core.api.Assertions.within;
20 import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.dateDefinition;
21 import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.setFormat;
22 import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.setTimezone;
23 import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.setValue;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
26
27 import org.basepom.mojo.propertyhelper.ValueProvider;
28 import org.basepom.mojo.propertyhelper.ValueProvider.PropertyBackedValueAdapter;
29 import org.basepom.mojo.propertyhelper.definitions.DateDefinition;
30
31 import java.time.Duration;
32 import java.time.Instant;
33 import java.time.LocalDateTime;
34 import java.time.ZoneId;
35 import java.time.ZonedDateTime;
36 import java.time.format.DateTimeFormatter;
37 import java.time.temporal.ChronoUnit;
38 import java.util.Properties;
39
40 import org.junit.jupiter.api.Test;
41
42
43 public class TestDateField {
44
45 @Test
46 public void testSimple() {
47 final DateDefinition d1 = dateDefinition("hello");
48 setValue(d1, 0L);
49 setTimezone(d1, "UTC");
50 setFormat(d1, "yyyyMMdd_HHmmss");
51
52 d1.check();
53
54 final DateField sd1 = DateField.forTesting(d1, ValueProvider.NULL_PROVIDER);
55 assertEquals("19700101_000000", sd1.getValue());
56 }
57
58 @Test
59 public void testProperty() {
60 final String format = "yyyyMMdd_HHmmss";
61 final DateDefinition d1 = dateDefinition("hello");
62 setFormat(d1, format);
63
64 d1.check();
65
66 final var now = LocalDateTime.now();
67 final Properties props = new Properties();
68 final String value = DateTimeFormatter.ofPattern(format).format(now);
69 props.setProperty("hello", value);
70 final DateField sd1 = DateField.forTesting(d1, new PropertyBackedValueAdapter(props, d1.getId()));
71
72 assertEquals(value, sd1.getValue());
73 }
74
75 @Test
76 public void testUnformattedLongProperty() {
77 final DateDefinition d1 = dateDefinition("hello");
78
79 d1.check();
80
81 final var now = Instant.now();
82 final Properties props = new Properties();
83 props.setProperty("hello", Long.toString(now.toEpochMilli()));
84 final DateField sd1 = DateField.forTesting(d1, new PropertyBackedValueAdapter(props, d1.getId()));
85
86 ZonedDateTime result = ZonedDateTime.parse(sd1.getValue(), DateTimeFormatter.ISO_DATE_TIME);
87
88 assertThat(result).isCloseTo(ZonedDateTime.ofInstant(now, ZoneId.systemDefault()), within(1, ChronoUnit.MILLIS));
89 }
90
91 @Test
92 public void testUnformattedStringProperty() {
93 final DateDefinition d1 = dateDefinition("hello");
94
95 d1.check();
96
97 final String value = DateTimeFormatter.ISO_DATE_TIME.format(ZonedDateTime.now());
98
99 final Properties props = new Properties();
100 props.setProperty("hello", value);
101 final DateField sd1 = DateField.forTesting(d1, new PropertyBackedValueAdapter(props, d1.getId()));
102
103 assertEquals(value, sd1.getValue());
104 }
105
106 @Test
107 public void testNow() {
108 final String format = "yyyyMMdd_HHmmss";
109 final DateDefinition d1 = dateDefinition("hello");
110 setFormat(d1, format);
111
112 d1.check();
113
114 final DateField sd1 = DateField.forTesting(d1, ValueProvider.NULL_PROVIDER);
115
116 final var value = sd1.getValue();
117
118 var now = ZonedDateTime.now().withNano(0);
119
120 var propTime = LocalDateTime.parse(value, DateTimeFormatter.ofPattern(format));
121 final Duration d = Duration.between(propTime, now);
122 assertTrue(d.getSeconds() <= 1, format("propTime: %s, now: %s, diff is %s", propTime, now, d));
123 }
124 }