View Javadoc
1   /*
2    * The MIT License (MIT)
3    *
4    * Copyright (c) 2013-2026 The Coveralls Maven Plugin Project Contributors:
5    *     https://github.com/hazendaz/coveralls-maven-plugin/graphs/contributors
6    *
7    * Permission is hereby granted, free of charge, to any person obtaining a copy
8    * of this software and associated documentation files (the "Software"), to deal
9    * in the Software without restriction, including without limitation the rights
10   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11   * copies of the Software, and to permit persons to whom the Software is
12   * furnished to do so, subject to the following conditions:
13   *
14   * The above copyright notice and this permission notice shall be included in
15   * all copies or substantial portions of the Software.
16   *
17   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23   * THE SOFTWARE.
24   */
25  package org.eluder.coveralls.maven.plugin.util;
26  
27  import java.time.ZoneOffset;
28  import java.time.format.DateTimeFormatter;
29  
30  import org.eluder.coveralls.maven.plugin.ProcessingException;
31  import org.junit.jupiter.api.Assertions;
32  import org.junit.jupiter.api.Test;
33  
34  /**
35   * The Class TimestampParserTest.
36   */
37  class TimestampParserTest {
38  
39      /**
40       * Invalid format.
41       */
42      @Test
43      void invalidFormat() {
44          Assertions.assertThrows(IllegalArgumentException.class, () -> new TimestampParser("scsscdfsd"));
45      }
46  
47      /**
48       * Parses the epoch millis.
49       *
50       * @throws ProcessingException
51       *             the processing exception
52       */
53      @Test
54      void parseEpochMillis() throws ProcessingException {
55          final var format = TimestampParser.EPOCH_MILLIS;
56          final var time = System.currentTimeMillis();
57          final var parsed = new TimestampParser(format).parse(String.valueOf(time));
58  
59          Assertions.assertEquals(time, parsed.toEpochMilli());
60      }
61  
62      /**
63       * Parses the simple format.
64       *
65       * @throws ProcessingException
66       *             the processing exception
67       */
68      @Test
69      void parseSimpleFormat() throws ProcessingException {
70          final var format = "yyyy-MM-dd";
71          final var parsed = new TimestampParser(format).parse("2015-08-20");
72          final var formatted = DateTimeFormatter.ofPattern(format).withZone(ZoneOffset.UTC).format(parsed);
73  
74          Assertions.assertEquals("2015-08-20", formatted);
75      }
76  
77      /**
78       * Parses the default format.
79       *
80       * @throws ProcessingException
81       *             the processing exception
82       */
83      @Test
84      void parseDefaultFormat() throws ProcessingException {
85          final var format = TimestampParser.DEFAULT_FORMAT;
86          final var parsed = new TimestampParser(null).parse("2015-08-20T20:10:00Z");
87          final var formatted = DateTimeFormatter.ofPattern(format).withZone(ZoneOffset.UTC).format(parsed);
88  
89          Assertions.assertEquals("2015-08-20T20:10:00Z", formatted);
90      }
91  
92      /**
93       * Parses the null.
94       *
95       * @throws ProcessingException
96       *             the processing exception
97       */
98      @Test
99      void parseNull() throws ProcessingException {
100         final var parsed = new TimestampParser(null).parse(null);
101 
102         Assertions.assertNull(parsed);
103     }
104 
105     /**
106      * Parses the invalid timestamp.
107      */
108     @Test
109     void parseInvalidTimestamp() {
110         Assertions.assertThrows(ProcessingException.class, () -> new TimestampParser(null).parse("2015-08-20"));
111     }
112 
113     /**
114      * Parses a blank string (returns null).
115      *
116      * @throws ProcessingException
117      *             the processing exception
118      */
119     @Test
120     void parseBlankString() throws ProcessingException {
121         final var parsed = new TimestampParser(null).parse("   ");
122 
123         Assertions.assertNull(parsed);
124     }
125 
126     /**
127      * Parses with a custom format without timezone (LocalDateTime path).
128      *
129      * @throws ProcessingException
130      *             the processing exception
131      */
132     @Test
133     void parseLocalDateTimeFormat() throws ProcessingException {
134         final var format = "yyyy-MM-dd HH:mm:ss";
135         final var parsed = new TimestampParser(format).parse("2015-08-20 20:10:00");
136 
137         Assertions.assertNotNull(parsed);
138         Assertions.assertEquals(2015, parsed.atZone(java.time.ZoneOffset.UTC).getYear());
139         Assertions.assertEquals(8, parsed.atZone(java.time.ZoneOffset.UTC).getMonthValue());
140         Assertions.assertEquals(20, parsed.atZone(java.time.ZoneOffset.UTC).getDayOfMonth());
141     }
142 
143     /**
144      * Parses with a format that uses lowercase 'z' (timezone name) triggering the z-path in hasZone.
145      *
146      * @throws ProcessingException
147      *             the processing exception
148      */
149     @Test
150     void parseTimezoneNameFormat() throws ProcessingException {
151         final var format = "yyyy-MM-dd'T'HH:mm:ss z";
152         final var parsed = new TimestampParser(format).parse("2015-08-20T20:10:00 UTC");
153 
154         Assertions.assertNotNull(parsed);
155         Assertions.assertEquals(2015, parsed.atZone(ZoneOffset.UTC).getYear());
156         Assertions.assertEquals(20, parsed.atZone(ZoneOffset.UTC).getDayOfMonth());
157     }
158 
159     /**
160      * Parses with a format that uses capital 'Z' (basic offset) triggering the Z-path in hasZone.
161      *
162      * @throws ProcessingException
163      *             the processing exception
164      */
165     @Test
166     void parseBasicOffsetFormat() throws ProcessingException {
167         final var format = "yyyy-MM-dd'T'HH:mm:ssZ";
168         final var parsed = new TimestampParser(format).parse("2015-08-20T20:10:00+0000");
169 
170         Assertions.assertNotNull(parsed);
171         Assertions.assertEquals(2015, parsed.atZone(ZoneOffset.UTC).getYear());
172         Assertions.assertEquals(20, parsed.atZone(ZoneOffset.UTC).getHour());
173     }
174 
175 }