1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.internal.impl;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.nio.file.Files;
24 import java.nio.file.Path;
25 import java.util.Collections;
26 import java.util.List;
27
28 import org.eclipse.aether.ConfigurationProperties;
29 import org.eclipse.aether.DefaultRepositorySystemSession;
30 import org.eclipse.aether.RepositoryEvent;
31 import org.eclipse.aether.RepositoryEvent.EventType;
32 import org.eclipse.aether.artifact.Artifact;
33 import org.eclipse.aether.artifact.DefaultArtifact;
34 import org.eclipse.aether.installation.InstallRequest;
35 import org.eclipse.aether.installation.InstallResult;
36 import org.eclipse.aether.installation.InstallationException;
37 import org.eclipse.aether.internal.test.util.TestFileUtils;
38 import org.eclipse.aether.internal.test.util.TestLocalRepositoryManager;
39 import org.eclipse.aether.internal.test.util.TestUtils;
40 import org.eclipse.aether.metadata.DefaultMetadata;
41 import org.eclipse.aether.metadata.Metadata;
42 import org.eclipse.aether.metadata.Metadata.Nature;
43 import org.eclipse.aether.spi.io.PathProcessorSupport;
44 import org.junit.jupiter.api.AfterEach;
45 import org.junit.jupiter.api.BeforeEach;
46 import org.junit.jupiter.api.Disabled;
47 import org.junit.jupiter.api.Test;
48
49 import static org.junit.jupiter.api.Assertions.*;
50
51 public class DefaultInstallerTest {
52
53 private Artifact artifact;
54
55 private Metadata metadata;
56
57 private DefaultRepositorySystemSession session;
58
59 private String localArtifactPath;
60
61 private String localMetadataPath;
62
63 private DefaultInstaller installer;
64
65 private InstallRequest request;
66
67 private RecordingRepositoryListener listener;
68
69 private File localArtifactFile;
70
71 private TestLocalRepositoryManager lrm;
72
73 @BeforeEach
74 void setup() throws IOException {
75 artifact = new DefaultArtifact("gid", "aid", "jar", "ver");
76 artifact = artifact.setFile(TestFileUtils.createTempFile("artifact".getBytes(), 1));
77 metadata = new DefaultMetadata(
78 "gid",
79 "aid",
80 "ver",
81 "type",
82 Nature.RELEASE_OR_SNAPSHOT,
83 TestFileUtils.createTempFile("metadata".getBytes(), 1));
84
85 session = TestUtils.newSession();
86 localArtifactPath = session.getLocalRepositoryManager().getPathForLocalArtifact(artifact);
87 localMetadataPath = session.getLocalRepositoryManager().getPathForLocalMetadata(metadata);
88
89 localArtifactFile = new File(session.getLocalRepository().getBasedir(), localArtifactPath);
90
91 installer = new DefaultInstaller(
92 new PathProcessorSupport(),
93 new StubRepositoryEventDispatcher(),
94 Collections.emptyMap(),
95 Collections.emptyMap(),
96 Collections.emptyMap(),
97 new StubSyncContextFactory());
98 request = new InstallRequest();
99 listener = new RecordingRepositoryListener();
100 session.setRepositoryListener(listener);
101
102 lrm = (TestLocalRepositoryManager) session.getLocalRepositoryManager();
103
104 TestFileUtils.deleteFile(session.getLocalRepository().getBasedir());
105 }
106
107 @AfterEach
108 void teardown() throws Exception {
109 TestFileUtils.deleteFile(session.getLocalRepository().getBasedir());
110 }
111
112 @Test
113 void testSuccessfulInstall() throws InstallationException, IOException {
114 File artifactFile =
115 new File(session.getLocalRepositoryManager().getRepository().getBasedir(), localArtifactPath);
116 File metadataFile =
117 new File(session.getLocalRepositoryManager().getRepository().getBasedir(), localMetadataPath);
118
119 artifactFile.delete();
120 metadataFile.delete();
121
122 request.addArtifact(artifact);
123 request.addMetadata(metadata);
124
125 InstallResult result = installer.install(session, request);
126
127 assertTrue(artifactFile.exists());
128 assertEquals("artifact", TestFileUtils.readString(artifactFile));
129
130 assertTrue(metadataFile.exists());
131 assertEquals("metadata", TestFileUtils.readString(metadataFile));
132
133 assertEquals(result.getRequest(), request);
134
135 assertEquals(1, result.getArtifacts().size());
136 assertTrue(result.getArtifacts().contains(artifact));
137
138 assertEquals(1, result.getMetadata().size());
139 assertTrue(result.getMetadata().contains(metadata));
140
141 assertEquals(1, lrm.getMetadataRegistration().size());
142 assertTrue(lrm.getMetadataRegistration().contains(metadata));
143 assertEquals(1, lrm.getArtifactRegistration().size());
144 assertTrue(lrm.getArtifactRegistration().contains(artifact));
145 }
146
147 @Test
148 void testNullArtifactFile() {
149 InstallRequest request = new InstallRequest();
150 request.addArtifact(artifact.setFile(null));
151
152 assertThrows(InstallationException.class, () -> installer.install(session, request));
153 }
154
155 @Test
156 void testNullMetadataFile() {
157 InstallRequest request = new InstallRequest();
158 request.addMetadata(metadata.setFile(null));
159
160 assertThrows(InstallationException.class, () -> installer.install(session, request));
161 }
162
163 @Test
164 void testNonExistentArtifactFile() {
165 InstallRequest request = new InstallRequest();
166 request.addArtifact(artifact.setFile(new File("missing.txt")));
167
168 assertThrows(InstallationException.class, () -> installer.install(session, request));
169 }
170
171 @Test
172 void testNonExistentMetadataFile() {
173 InstallRequest request = new InstallRequest();
174 request.addMetadata(metadata.setFile(new File("missing.xml")));
175
176 assertThrows(InstallationException.class, () -> installer.install(session, request));
177 }
178
179 @Test
180 void testArtifactExistsAsDir() {
181 String path = session.getLocalRepositoryManager().getPathForLocalArtifact(artifact);
182 File file = new File(session.getLocalRepository().getBasedir(), path);
183 assertFalse(file.isFile(), file.getAbsolutePath() + " is a file, not directory");
184 assertFalse(file.exists(), file.getAbsolutePath() + " already exists");
185 assertTrue(
186 file.mkdirs() || file.isDirectory(),
187 "failed to setup test: could not create " + file.getAbsolutePath());
188
189 request.addArtifact(artifact);
190 assertThrows(InstallationException.class, () -> installer.install(session, request));
191 }
192
193 @Test
194 void testMetadataExistsAsDir() {
195 String path = session.getLocalRepositoryManager().getPathForLocalMetadata(metadata);
196 assertTrue(
197 new File(session.getLocalRepository().getBasedir(), path).mkdirs(),
198 "failed to setup test: could not create " + path);
199
200 request.addMetadata(metadata);
201 assertThrows(InstallationException.class, () -> installer.install(session, request));
202 }
203
204 @Test
205 void testArtifactDestinationEqualsSource() throws IOException {
206 String path = session.getLocalRepositoryManager().getPathForLocalArtifact(artifact);
207 File file = new File(session.getLocalRepository().getBasedir(), path);
208 artifact = artifact.setFile(file);
209 TestFileUtils.writeString(file, "test");
210
211 request.addArtifact(artifact);
212 assertThrows(InstallationException.class, () -> installer.install(session, request));
213 }
214
215 @Test
216 void testMetadataDestinationEqualsSource() throws IOException {
217 String path = session.getLocalRepositoryManager().getPathForLocalMetadata(metadata);
218 File file = new File(session.getLocalRepository().getBasedir(), path);
219 metadata = metadata.setFile(file);
220 TestFileUtils.writeString(file, "test");
221
222 request.addMetadata(metadata);
223 assertThrows(InstallationException.class, () -> installer.install(session, request));
224 }
225
226 @Test
227 void testSuccessfulArtifactEvents() throws InstallationException {
228 InstallRequest request = new InstallRequest();
229 request.addArtifact(artifact);
230
231 installer.install(session, request);
232 checkEvents("Repository Event problem", artifact, false);
233 }
234
235 @Test
236 void testSuccessfulMetadataEvents() throws InstallationException {
237 InstallRequest request = new InstallRequest();
238 request.addMetadata(metadata);
239
240 installer.install(session, request);
241 checkEvents("Repository Event problem", metadata, false);
242 }
243
244 @Test
245 void testFailingEventsNullArtifactFile() {
246 checkFailedEvents("null artifact file", this.artifact.setFile(null));
247 }
248
249 @Test
250 void testFailingEventsNullMetadataFile() {
251 checkFailedEvents("null metadata file", this.metadata.setFile(null));
252 }
253
254 @Test
255 void testFailingEventsArtifactExistsAsDir() {
256 String path = session.getLocalRepositoryManager().getPathForLocalArtifact(artifact);
257 assertTrue(
258 new File(session.getLocalRepository().getBasedir(), path).mkdirs(),
259 "failed to setup test: could not create " + path);
260 checkFailedEvents("target exists as dir", artifact);
261 }
262
263 @Test
264 void testFailingEventsMetadataExistsAsDir() {
265 String path = session.getLocalRepositoryManager().getPathForLocalMetadata(metadata);
266 assertTrue(
267 new File(session.getLocalRepository().getBasedir(), path).mkdirs(),
268 "failed to setup test: could not create " + path);
269 checkFailedEvents("target exists as dir", metadata);
270 }
271
272 private void checkFailedEvents(String msg, Metadata metadata) {
273 InstallRequest request = new InstallRequest().addMetadata(metadata);
274 msg = "Repository events problem (case: " + msg + ")";
275
276 try {
277 installer.install(session, request);
278 fail("expected exception");
279 } catch (InstallationException e) {
280 checkEvents(msg, metadata, true);
281 }
282 }
283
284 private void checkEvents(String msg, Metadata metadata, boolean failed) {
285 List<RepositoryEvent> events = listener.getEvents();
286 assertEquals(2, events.size(), msg);
287 RepositoryEvent event = events.get(0);
288 assertEquals(EventType.METADATA_INSTALLING, event.getType(), msg);
289 assertEquals(metadata, event.getMetadata(), msg);
290 assertNull(event.getException(), msg);
291
292 event = events.get(1);
293 assertEquals(EventType.METADATA_INSTALLED, event.getType(), msg);
294 assertEquals(metadata, event.getMetadata(), msg);
295 if (failed) {
296 assertNotNull(event.getException(), msg);
297 } else {
298 assertNull(event.getException(), msg);
299 }
300 }
301
302 private void checkFailedEvents(String msg, Artifact artifact) {
303 InstallRequest request = new InstallRequest().addArtifact(artifact);
304 msg = "Repository events problem (case: " + msg + ")";
305
306 try {
307 installer.install(session, request);
308 fail("expected exception");
309 } catch (InstallationException e) {
310 checkEvents(msg, artifact, true);
311 }
312 }
313
314 private void checkEvents(String msg, Artifact artifact, boolean failed) {
315 List<RepositoryEvent> events = listener.getEvents();
316 assertEquals(2, events.size(), msg);
317 RepositoryEvent event = events.get(0);
318 assertEquals(EventType.ARTIFACT_INSTALLING, event.getType(), msg);
319 assertEquals(artifact, event.getArtifact(), msg);
320 assertNull(event.getException(), msg);
321
322 event = events.get(1);
323 assertEquals(EventType.ARTIFACT_INSTALLED, event.getType(), msg);
324 assertEquals(artifact, event.getArtifact(), msg);
325 if (failed) {
326 assertNotNull(event.getException(), msg + " > expected exception");
327 } else {
328 assertNull(event.getException(), msg + " > " + event.getException());
329 }
330 }
331
332 @Test
333 @Disabled("Naive change detection is removed (MRESOLVER-392)")
334 void testDoNotUpdateUnchangedArtifact() throws InstallationException {
335 request.addArtifact(artifact);
336 installer.install(session, request);
337
338 installer = new DefaultInstaller(
339 new DefaultPathProcessor() {
340 @Override
341 public long copy(Path src, Path target, ProgressListener listener) throws IOException {
342 throw new IOException("copy called");
343 }
344 },
345 new StubRepositoryEventDispatcher(),
346 Collections.emptyMap(),
347 Collections.emptyMap(),
348 Collections.emptyMap(),
349 new StubSyncContextFactory());
350
351 request = new InstallRequest();
352 request.addArtifact(artifact);
353 installer.install(session, request);
354 }
355
356 @Test
357 void testSetArtifactTimestamps() throws InstallationException {
358 artifact.getFile().setLastModified(artifact.getFile().lastModified() - 60000);
359
360 request.addArtifact(artifact);
361
362 installer.install(session, request);
363
364 assertEquals(
365 artifact.getFile().lastModified(),
366 localArtifactFile.lastModified(),
367 "artifact timestamp was not set to src file");
368
369 request = new InstallRequest();
370
371 request.addArtifact(artifact);
372
373 artifact.getFile().setLastModified(artifact.getFile().lastModified() - 60000);
374
375 installer.install(session, request);
376
377 assertEquals(
378 artifact.getFile().lastModified(),
379 localArtifactFile.lastModified(),
380 "artifact timestamp was not set to src file");
381 }
382
383 @Test
384 void testHardLinkInstallCreatesHardLink() throws InstallationException, IOException {
385
386
387
388
389
390 CountingPathProcessor countingProcessor = new CountingPathProcessor();
391 installer = new DefaultInstaller(
392 countingProcessor,
393 new StubRepositoryEventDispatcher(),
394 Collections.emptyMap(),
395 Collections.emptyMap(),
396 Collections.emptyMap(),
397 new StubSyncContextFactory());
398
399 request.addArtifact(artifact);
400 installer.install(session, request);
401
402 assertTrue(localArtifactFile.exists());
403 assertEquals("artifact", TestFileUtils.readString(localArtifactFile));
404
405
406
407
408 Path srcPath = artifact.getFile().toPath();
409 Path dstPath = localArtifactFile.toPath();
410 try {
411 boolean sameDevice =
412 Files.getAttribute(srcPath, "unix:dev").equals(Files.getAttribute(dstPath, "unix:dev"));
413 if (sameDevice) {
414
415 assertEquals(
416 Files.getAttribute(srcPath, "unix:ino"),
417 Files.getAttribute(dstPath, "unix:ino"),
418 "installed artifact should be a hard link to the source on same-device FS");
419 assertEquals(
420 0,
421 countingProcessor.copyWithTimestampCount,
422 "copyWithTimestamp must NOT be called on same-device FS");
423 } else {
424
425 assertEquals(
426 1,
427 countingProcessor.copyWithTimestampCount,
428 "copyWithTimestamp must be called as fallback on cross-device FS");
429 }
430 } catch (UnsupportedOperationException | IllegalArgumentException ignored) {
431
432
433
434 assertTrue(
435 countingProcessor.copyWithTimestampCount <= 1,
436 "copyWithTimestamp must be called at most once on non-POSIX FS (got "
437 + countingProcessor.copyWithTimestampCount + ")");
438 }
439 }
440
441 @Test
442 void testHardLinkDisabledFallsThroughToCopy() throws InstallationException, IOException {
443
444 CountingPathProcessor countingProcessor = new CountingPathProcessor();
445 installer = new DefaultInstaller(
446 countingProcessor,
447 new StubRepositoryEventDispatcher(),
448 Collections.emptyMap(),
449 Collections.emptyMap(),
450 Collections.emptyMap(),
451 new StubSyncContextFactory());
452
453 session.setConfigProperty(ConfigurationProperties.INSTALLER_HARD_LINK, false);
454 request.addArtifact(artifact);
455 installer.install(session, request);
456
457 assertTrue(localArtifactFile.exists());
458 assertEquals("artifact", TestFileUtils.readString(localArtifactFile));
459
460 assertEquals(
461 1, countingProcessor.copyWithTimestampCount, "copyWithTimestamp must be called when hardLink=false");
462 }
463
464
465 private static class CountingPathProcessor extends PathProcessorSupport {
466 int copyWithTimestampCount = 0;
467
468 @Override
469 public void copyWithTimestamp(Path src, Path dst) throws IOException {
470 copyWithTimestampCount++;
471 super.copyWithTimestamp(src, dst);
472 }
473 }
474 }