View Javadoc
1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  package org.basepom.mojo.propertyhelper.fields;
15  
16  import static org.assertj.core.api.Assertions.assertThat;
17  import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.dateDefinition;
18  import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.setInitialValue;
19  
20  import org.basepom.mojo.propertyhelper.FieldContext;
21  import org.basepom.mojo.propertyhelper.ValueCache;
22  import org.basepom.mojo.propertyhelper.ValueProvider.SingleValueProvider;
23  import org.basepom.mojo.propertyhelper.definitions.DateDefinition;
24  
25  import java.time.Instant;
26  import java.time.ZoneId;
27  import java.time.ZonedDateTime;
28  import java.time.format.DateTimeFormatter;
29  
30  import org.junit.jupiter.api.Test;
31  
32  class TestDateFieldOverrides {
33  
34      @Test
35      void testZeroValue() throws Exception {
36          DateDefinition dateDefinition = dateDefinition("hello");
37          setInitialValue(dateDefinition, "0");
38  
39          ZonedDateTime localEpochTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(0), ZoneId.systemDefault());
40  
41          var context = FieldContext.forTesting();
42          ValueCache valueCache = new ValueCache();
43          var dateField = dateDefinition.createField(context, valueCache);
44          var value = dateField.getValue();
45  
46          assertThat(value).isEqualTo(DateTimeFormatter.ISO_DATE_TIME.format(localEpochTime));
47      }
48  
49      @Test
50      void testLoadedValueOverridesInitialValue() throws Exception {
51          DateDefinition dateDefinition = dateDefinition("hello");
52          setInitialValue(dateDefinition, "0");
53  
54          ZonedDateTime localEpochPlusOneTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(1), ZoneId.systemDefault());
55  
56          var valueProvider = new SingleValueProvider();
57          valueProvider.setValue("1");
58          var dateField = DateField.forTesting(dateDefinition, valueProvider);
59  
60          var value = dateField.getValue();
61  
62          assertThat(value).isEqualTo(DateTimeFormatter.ISO_DATE_TIME.format(localEpochPlusOneTime));
63      }
64  }