|
22 | 22 |
|
23 | 23 | import java.io.File; |
24 | 24 | import java.io.IOException; |
| 25 | +import java.time.Instant; |
| 26 | +import java.util.Date; |
| 27 | + |
| 28 | +import junitparams.JUnitParamsRunner; |
| 29 | +import junitparams.Parameters; |
25 | 30 | import org.junit.Test; |
| 31 | +import org.junit.runner.RunWith; |
26 | 32 | import pl.project13.core.PropertiesFileGenerator; |
27 | 33 |
|
28 | 34 | /** |
29 | 35 | * Testcases to verify that the git-commit-id works properly. |
30 | 36 | */ |
| 37 | +@RunWith(JUnitParamsRunner.class) |
31 | 38 | public class GitCommitIdMojoTest { |
32 | 39 | @Test |
33 | 40 | public void testCraftPropertiesOutputFileWithRelativePath() throws IOException { |
@@ -66,4 +73,137 @@ public void testCraftPropertiesOutputFileWithFullPath() throws IOException { |
66 | 73 | .toFile() |
67 | 74 | .getCanonicalPath()); |
68 | 75 | } |
| 76 | + |
| 77 | + private Object[] parametersParseOutputTimestamp() { |
| 78 | + return new Object[] { |
| 79 | + // long since epoch |
| 80 | + new Object[] { |
| 81 | + "1644689403", |
| 82 | + Instant.ofEpochMilli(1644689403000L) |
| 83 | + }, |
| 84 | + // Date only: |
| 85 | + new Object[] { |
| 86 | + "2022-02", |
| 87 | + Instant.ofEpochMilli(1643670000000L) |
| 88 | + }, |
| 89 | + new Object[] { |
| 90 | + "2022-02-12", |
| 91 | + Instant.ofEpochMilli(1644620400000L) |
| 92 | + }, |
| 93 | + // Date and time: |
| 94 | + new Object[] { |
| 95 | + "2022-02-12T15:30", |
| 96 | + Instant.ofEpochMilli(1644676200000L) |
| 97 | + }, |
| 98 | + new Object[] { |
| 99 | + "2022-02-12T15:30:45", |
| 100 | + Instant.ofEpochMilli(1644676245000L) |
| 101 | + }, |
| 102 | + // Date and time with timezone: |
| 103 | + new Object[] { |
| 104 | + "2022-02-12T15:30+00:00", |
| 105 | + Instant.ofEpochMilli(1644679800000L) |
| 106 | + }, |
| 107 | + new Object[] { |
| 108 | + "2022-02-12T15:30:45-05:00", |
| 109 | + Instant.ofEpochMilli(1644697845000L) |
| 110 | + }, |
| 111 | + new Object[] { |
| 112 | + "2022-02-12T15:30:00+00:00", |
| 113 | + Instant.ofEpochMilli(1644679800000L) |
| 114 | + }, |
| 115 | + new Object[] { |
| 116 | + "2023-11-30T09:17:06+05:30", |
| 117 | + Instant.ofEpochMilli(1701316026000L) |
| 118 | + }, |
| 119 | + new Object[] { |
| 120 | + "2024-08-15T20:45:30-03:00", |
| 121 | + Instant.ofEpochMilli(1723765530000L) |
| 122 | + }, |
| 123 | + new Object[] { |
| 124 | + "2022-02-12T15:30:00Z", |
| 125 | + Instant.ofEpochMilli(1644679800000L) |
| 126 | + }, |
| 127 | + // Not valid according to the ISO 8601 standard. The issue is with the time zone |
| 128 | + // representation. ISO 8601 uses a specific format for time zones, either as "Z" for UTC or |
| 129 | + // in the format "+HH:MM" or "-HH:MM" for the offset from UTC. |
| 130 | + // The time zone "EST" or "PST" does not follow this format. |
| 131 | + // new Object[] { "2023-11-30T09:17:06PST", null }, |
| 132 | + // new Object[] { "2024-08-15T20:45:30EST", null }, |
| 133 | + new Object[] { |
| 134 | + "2023-11-30T09:17:06+0100", |
| 135 | + Instant.ofEpochMilli(1701332226000L) |
| 136 | + }, |
| 137 | + // Week date: |
| 138 | + new Object[] { |
| 139 | + "2022-W06", |
| 140 | + Instant.ofEpochMilli(1644188400000L) |
| 141 | + }, |
| 142 | + new Object[] { |
| 143 | + "2022-W06-5", |
| 144 | + Instant.ofEpochMilli(1644534000000L) |
| 145 | + }, |
| 146 | + // Week date with time: |
| 147 | + new Object[] { |
| 148 | + "2022-W06-5T15:30", |
| 149 | + Instant.ofEpochMilli(1644589800000L) |
| 150 | + }, |
| 151 | + new Object[] { |
| 152 | + "2022-W06-5T15:30:45", |
| 153 | + Instant.ofEpochMilli(1644589845000L) |
| 154 | + }, |
| 155 | + // https://tc39.es/proposal-uniform-interchange-date-parsing/cases.html |
| 156 | + // positive leap second |
| 157 | + // not working: new Object[] { "1972-06-30T23:59:60Z", null }, |
| 158 | + // Too few fractional second digits |
| 159 | + new Object[] { |
| 160 | + "2019-03-26T14:00:00.9Z", |
| 161 | + Instant.ofEpochMilli(1553608800900L) |
| 162 | + }, |
| 163 | + // Too many fractional second digits |
| 164 | + new Object[] { |
| 165 | + "2019-03-26T14:00:00.4999Z", |
| 166 | + Instant.ofEpochMilli(1553608800499L) |
| 167 | + }, |
| 168 | + // Too many fractional second digits (pre-epoch) |
| 169 | + new Object[] { |
| 170 | + "1969-03-26T14:00:00.4999Z", |
| 171 | + Instant.ofEpochMilli(-24227999501L) |
| 172 | + }, |
| 173 | + // Too many fractional second digits (BCE) |
| 174 | + new Object[] { |
| 175 | + "-000043-03-15T14:00:00.4999Z", |
| 176 | + Instant.ofEpochMilli(-63517773599501L) |
| 177 | + }, |
| 178 | + // Lowercase time designator |
| 179 | + new Object[] { |
| 180 | + "2019-03-26t14:00Z", |
| 181 | + Instant.ofEpochMilli(1553608800000L) |
| 182 | + }, |
| 183 | + // Lowercase UTC designator |
| 184 | + new Object[] { |
| 185 | + "2019-03-26T14:00z", |
| 186 | + Instant.ofEpochMilli(1553608800000L) |
| 187 | + }, |
| 188 | + // Hours-only offset |
| 189 | + new Object[] { |
| 190 | + "2019-03-26T10:00-04", |
| 191 | + Instant.ofEpochMilli(1553608800000L) |
| 192 | + }, |
| 193 | + // Fractional minutes |
| 194 | + new Object[] { |
| 195 | + "2019-03-26T14:00.9Z", |
| 196 | + Instant.ofEpochMilli(1553608854000L) |
| 197 | + }, |
| 198 | + // ISO basic format date and time |
| 199 | + // not working: new Object[] { "20190326T1400Z", null }, |
| 200 | + }; |
| 201 | + } |
| 202 | + |
| 203 | + @Test |
| 204 | + @Parameters(method = "parametersParseOutputTimestamp") |
| 205 | + public void testParseOutputTimestamp(String input, Instant expected) { |
| 206 | + Date actual = GitCommitIdMojo.parseOutputTimestamp(input); |
| 207 | + assertThat(actual.toInstant()).isEqualTo(expected); |
| 208 | + } |
69 | 209 | } |
0 commit comments