001/* 002 * Licensed under the Apache License, Version 2.0 (the "License"); 003 * you may not use this file except in compliance with the License. 004 * You may obtain a copy of the License at 005 * 006 * http://www.apache.org/licenses/LICENSE-2.0 007 * 008 * Unless required by applicable law or agreed to in writing, software 009 * distributed under the License is distributed on an "AS IS" BASIS, 010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 011 * See the License for the specific language governing permissions and 012 * limitations under the License. 013 */ 014 015package org.basepom.mojo.propertyhelper.fields; 016 017import static java.lang.String.format; 018import static org.assertj.core.api.Assertions.assertThat; 019import static org.assertj.core.api.Assertions.within; 020import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.dateDefinition; 021import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.setFormat; 022import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.setTimezone; 023import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.setValue; 024import static org.junit.jupiter.api.Assertions.assertEquals; 025import static org.junit.jupiter.api.Assertions.assertTrue; 026 027import org.basepom.mojo.propertyhelper.ValueProvider; 028import org.basepom.mojo.propertyhelper.ValueProvider.PropertyBackedValueAdapter; 029import org.basepom.mojo.propertyhelper.definitions.DateDefinition; 030 031import java.time.Duration; 032import java.time.Instant; 033import java.time.LocalDateTime; 034import java.time.ZoneId; 035import java.time.ZonedDateTime; 036import java.time.format.DateTimeFormatter; 037import java.time.temporal.ChronoUnit; 038import java.util.Properties; 039 040import org.junit.jupiter.api.Test; 041 042 043public class TestDateField { 044 045 @Test 046 public void testSimple() { 047 final DateDefinition d1 = dateDefinition("hello"); 048 setValue(d1, 0L); 049 setTimezone(d1, "UTC"); 050 setFormat(d1, "yyyyMMdd_HHmmss"); 051 052 d1.check(); 053 054 final DateField sd1 = DateField.forTesting(d1, ValueProvider.NULL_PROVIDER); 055 assertEquals("19700101_000000", sd1.getValue()); 056 } 057 058 @Test 059 public void testProperty() { 060 final String format = "yyyyMMdd_HHmmss"; 061 final DateDefinition d1 = dateDefinition("hello"); 062 setFormat(d1, format); 063 064 d1.check(); 065 066 final var now = LocalDateTime.now(); 067 final Properties props = new Properties(); 068 final String value = DateTimeFormatter.ofPattern(format).format(now); 069 props.setProperty("hello", value); 070 final DateField sd1 = DateField.forTesting(d1, new PropertyBackedValueAdapter(props, d1.getId())); 071 072 assertEquals(value, sd1.getValue()); 073 } 074 075 @Test 076 public void testUnformattedLongProperty() { 077 final DateDefinition d1 = dateDefinition("hello"); 078 079 d1.check(); 080 081 final var now = Instant.now(); 082 final Properties props = new Properties(); 083 props.setProperty("hello", Long.toString(now.toEpochMilli())); 084 final DateField sd1 = DateField.forTesting(d1, new PropertyBackedValueAdapter(props, d1.getId())); 085 086 ZonedDateTime result = ZonedDateTime.parse(sd1.getValue(), DateTimeFormatter.ISO_DATE_TIME); 087 088 assertThat(result).isCloseTo(ZonedDateTime.ofInstant(now, ZoneId.systemDefault()), within(1, ChronoUnit.MILLIS)); 089 } 090 091 @Test 092 public void testUnformattedStringProperty() { 093 final DateDefinition d1 = dateDefinition("hello"); 094 095 d1.check(); 096 097 final String value = DateTimeFormatter.ISO_DATE_TIME.format(ZonedDateTime.now()); 098 099 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}