1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.artifact.repository.metadata;
20
21 import java.text.DateFormat;
22 import java.text.SimpleDateFormat;
23 import java.util.Date;
24 import java.util.GregorianCalendar;
25 import java.util.TimeZone;
26
27 import org.eclipse.aether.artifact.Artifact;
28 import org.eclipse.aether.artifact.DefaultArtifact;
29 import org.junit.jupiter.api.BeforeEach;
30 import org.junit.jupiter.api.Test;
31
32 import static org.junit.jupiter.api.Assertions.assertEquals;
33 import static org.junit.jupiter.api.Assertions.assertFalse;
34 import static org.junit.jupiter.api.Assertions.assertTrue;
35
36 public class MetadataTest {
37
38 Artifact artifact;
39
40 Metadata target;
41
42 @BeforeEach
43 void before() {
44 artifact = new DefaultArtifact("myGroup:myArtifact:1.0-SNAPSHOT");
45 target = createMetadataFromArtifact(artifact);
46 }
47
48
49 @Test
50 void mergeEmptyMetadata() throws Exception {
51 Metadata metadata = new Metadata();
52 assertFalse(metadata.merge(new Metadata()));
53 }
54
55 @Test
56 void mergeDifferentGAV() throws Exception {
57
58 Metadata source = new Metadata();
59 source.setArtifactId("source-artifact");
60 source.setGroupId("source-group");
61 source.setVersion("2.0");
62 assertFalse(target.merge(source));
63 assertEquals("myArtifact", target.getArtifactId());
64 assertEquals("myGroup", target.getGroupId());
65 assertEquals("1.0-SNAPSHOT", target.getVersion());
66 }
67
68
69
70 @Test
71 void mergeSnapshotWithEmptyList() throws Exception {
72 Snapshot snapshot = new Snapshot();
73 snapshot.setBuildNumber(3);
74 snapshot.setTimestamp("20200710.072412");
75 target.getVersioning().setSnapshot(snapshot);
76 target.getVersioning().setLastUpdated("20200921071745");
77 SnapshotVersion sv = new SnapshotVersion();
78 sv.setClassifier("sources");
79 sv.setExtension("jar");
80 sv.setUpdated("20200710072412");
81 target.getVersioning().addSnapshotVersion(sv);
82
83 Metadata source = createMetadataFromArtifact(artifact);
84
85 assertTrue(target.merge(source));
86
87
88 assertEquals("20200921071745", source.getVersioning().getLastUpdated());
89
90 assertEquals("myArtifact", target.getArtifactId());
91 assertEquals("myGroup", target.getGroupId());
92
93 assertEquals(3, target.getVersioning().getSnapshot().getBuildNumber());
94 assertEquals("20200710.072412", target.getVersioning().getSnapshot().getTimestamp());
95
96 assertEquals(1, target.getVersioning().getSnapshotVersions().size());
97 assertEquals(
98 "sources", target.getVersioning().getSnapshotVersions().get(0).getClassifier());
99 assertEquals("jar", target.getVersioning().getSnapshotVersions().get(0).getExtension());
100 assertEquals(
101 "20200710072412",
102 target.getVersioning().getSnapshotVersions().get(0).getUpdated());
103 }
104
105 @Test
106 void mergeWithSameSnapshotWithDifferentVersionsAndNewerLastUpdated() {
107 Metadata source = createMetadataFromArtifact(artifact);
108 Date before = new Date(System.currentTimeMillis() - 5000);
109 Date after = new Date(System.currentTimeMillis());
110 addSnapshotVersion(target.getVersioning(), "jar", before, "1", 1);
111 SnapshotVersion sv2 =
112 addSnapshotVersion(source.getVersioning(), "jar", after, "1.0-" + formatDate(after, true) + "-2", 2);
113 SnapshotVersion sv3 =
114 addSnapshotVersion(source.getVersioning(), "pom", after, "1.0-" + formatDate(after, true) + "-2", 2);
115 assertTrue(target.merge(source));
116 Versioning actualVersioning = target.getVersioning();
117 assertEquals(2, actualVersioning.getSnapshotVersions().size());
118 assertEquals(sv2, actualVersioning.getSnapshotVersions().get(0));
119 assertEquals(sv3, actualVersioning.getSnapshotVersions().get(1));
120 assertEquals(formatDate(after, false), actualVersioning.getLastUpdated());
121 assertEquals(formatDate(after, true), actualVersioning.getSnapshot().getTimestamp());
122 assertEquals(2, actualVersioning.getSnapshot().getBuildNumber());
123 }
124
125 @Test
126 void mergeWithSameSnapshotWithDifferentVersionsAndOlderLastUpdated() {
127 Metadata source = createMetadataFromArtifact(artifact);
128 Date before = new Date(System.currentTimeMillis() - 5000);
129 Date after = new Date(System.currentTimeMillis());
130 SnapshotVersion sv1 = addSnapshotVersion(target.getVersioning(), after, artifact);
131 addSnapshotVersion(source.getVersioning(), before, artifact);
132
133 assertFalse(target.merge(source));
134 assertEquals(1, target.getVersioning().getSnapshotVersions().size());
135 assertEquals(sv1, target.getVersioning().getSnapshotVersions().get(0));
136 assertEquals(formatDate(after, false), target.getVersioning().getLastUpdated());
137 assertEquals(
138 formatDate(after, true), target.getVersioning().getSnapshot().getTimestamp());
139 }
140
141 @Test
142 void mergeWithSameSnapshotWithSameVersionAndTimestamp() {
143 Metadata source = createMetadataFromArtifact(artifact);
144 Date date = new Date();
145 addSnapshotVersion(target.getVersioning(), date, artifact);
146 SnapshotVersion sv1 = addSnapshotVersion(source.getVersioning(), date, artifact);
147
148
149 assertTrue(target.merge(source));
150 assertEquals(1, target.getVersioning().getSnapshotVersions().size());
151 assertEquals(sv1, target.getVersioning().getSnapshotVersions().get(0));
152 assertEquals(formatDate(date, false), target.getVersioning().getLastUpdated());
153 assertEquals(
154 formatDate(date, true), target.getVersioning().getSnapshot().getTimestamp());
155 }
156
157 @Test
158 void mergeLegacyWithSnapshotLegacy() {
159 Metadata source = createMetadataFromArtifact(artifact);
160 Date before = new Date(System.currentTimeMillis() - 5000);
161 Date after = new Date(System.currentTimeMillis());
162
163 addSnapshotVersionLegacy(target.getVersioning(), before, 1);
164 addSnapshotVersionLegacy(source.getVersioning(), after, 2);
165
166
167 assertTrue(target.merge(source));
168 assertEquals(0, target.getVersioning().getSnapshotVersions().size());
169 assertEquals(formatDate(after, false), target.getVersioning().getLastUpdated());
170 assertEquals(
171 formatDate(after, true), target.getVersioning().getSnapshot().getTimestamp());
172 }
173
174 @Test
175 void mergeLegacyWithSnapshot() {
176 Metadata source = createMetadataFromArtifact(artifact);
177 Date before = new Date(System.currentTimeMillis() - 5000);
178 Date after = new Date(System.currentTimeMillis());
179
180 addSnapshotVersionLegacy(target.getVersioning(), before, 1);
181 addSnapshotVersion(source.getVersioning(), after, artifact);
182
183
184 assertTrue(target.merge(source));
185
186 assertEquals(0, target.getVersioning().getSnapshotVersions().size());
187 assertEquals(formatDate(after, false), target.getVersioning().getLastUpdated());
188 assertEquals(
189 formatDate(after, true), target.getVersioning().getSnapshot().getTimestamp());
190 }
191
192 @Test
193 void mergeWithSnapshotLegacy() {
194 Metadata source = createMetadataFromArtifact(artifact);
195 Date before = new Date(System.currentTimeMillis() - 5000);
196 Date after = new Date(System.currentTimeMillis());
197 addSnapshotVersion(target.getVersioning(), before, artifact);
198
199 addSnapshotVersionLegacy(source.getVersioning(), after, 2);
200
201
202 assertTrue(target.merge(source));
203
204 assertEquals(0, target.getVersioning().getSnapshotVersions().size());
205 assertEquals(formatDate(after, false), target.getVersioning().getLastUpdated());
206 assertEquals(
207 formatDate(after, true), target.getVersioning().getSnapshot().getTimestamp());
208 assertEquals(2, target.getVersioning().getSnapshot().getBuildNumber());
209 }
210
211
212
213 private static final String SNAPSHOT = "SNAPSHOT";
214
215 private static final String DEFAULT_SNAPSHOT_TIMESTAMP_FORMAT = "yyyyMMdd.HHmmss";
216
217 private static final String DEFAULT_DATE_FORMAT = "yyyyMMddHHmmss";
218
219 private static String formatDate(Date date, boolean forSnapshotTimestamp) {
220
221 TimeZone timezone = TimeZone.getTimeZone("UTC");
222 DateFormat fmt =
223 new SimpleDateFormat(forSnapshotTimestamp ? DEFAULT_SNAPSHOT_TIMESTAMP_FORMAT : DEFAULT_DATE_FORMAT);
224 fmt.setCalendar(new GregorianCalendar());
225 fmt.setTimeZone(timezone);
226 return fmt.format(date);
227 }
228
229 private static Metadata createMetadataFromArtifact(Artifact artifact) {
230 Metadata metadata = new Metadata();
231 metadata.setArtifactId(artifact.getArtifactId());
232 metadata.setGroupId(artifact.getGroupId());
233 metadata.setVersion(artifact.getVersion());
234 metadata.setVersioning(new Versioning());
235 return metadata;
236 }
237
238 private static SnapshotVersion addSnapshotVersion(Versioning versioning, Date timestamp, Artifact artifact) {
239 int buildNumber = 1;
240
241
242 String version = artifact.getVersion();
243 String qualifier = formatDate(timestamp, true) + '-' + buildNumber;
244 version = version.substring(0, version.length() - SNAPSHOT.length()) + qualifier;
245 return addSnapshotVersion(versioning, artifact.getExtension(), timestamp, version, buildNumber);
246 }
247
248 private static SnapshotVersion addSnapshotVersion(
249 Versioning versioning, String extension, Date timestamp, String version, int buildNumber) {
250 Snapshot snapshot = new Snapshot();
251 snapshot.setBuildNumber(buildNumber);
252 snapshot.setTimestamp(formatDate(timestamp, true));
253
254 SnapshotVersion sv = new SnapshotVersion();
255 sv.setExtension(extension);
256 sv.setVersion(version);
257 sv.setUpdated(formatDate(timestamp, false));
258 versioning.addSnapshotVersion(sv);
259
260
261 versioning.setSnapshot(snapshot);
262 versioning.setLastUpdatedTimestamp(timestamp);
263 return sv;
264 }
265
266
267
268 private static void addSnapshotVersionLegacy(Versioning versioning, Date timestamp, int buildNumber) {
269 Snapshot snapshot = new Snapshot();
270 snapshot.setBuildNumber(buildNumber);
271 snapshot.setTimestamp(formatDate(timestamp, true));
272
273 versioning.setSnapshot(snapshot);
274 versioning.setLastUpdatedTimestamp(timestamp);
275 }
276
277 }