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.util.Collections;
24 import java.util.List;
25
26 import org.eclipse.aether.DefaultRepositorySystemSession;
27 import org.eclipse.aether.RepositoryEvent;
28 import org.eclipse.aether.RepositoryEvent.EventType;
29 import org.eclipse.aether.artifact.Artifact;
30 import org.eclipse.aether.artifact.DefaultArtifact;
31 import org.eclipse.aether.installation.InstallRequest;
32 import org.eclipse.aether.installation.InstallResult;
33 import org.eclipse.aether.installation.InstallationException;
34 import org.eclipse.aether.internal.test.util.TestFileProcessor;
35 import org.eclipse.aether.internal.test.util.TestFileUtils;
36 import org.eclipse.aether.internal.test.util.TestLocalRepositoryManager;
37 import org.eclipse.aether.internal.test.util.TestUtils;
38 import org.eclipse.aether.metadata.DefaultMetadata;
39 import org.eclipse.aether.metadata.Metadata;
40 import org.eclipse.aether.metadata.Metadata.Nature;
41 import org.junit.jupiter.api.AfterEach;
42 import org.junit.jupiter.api.BeforeEach;
43 import org.junit.jupiter.api.Disabled;
44 import org.junit.jupiter.api.Test;
45
46 import static org.junit.jupiter.api.Assertions.*;
47
48 public class DefaultInstallerTest {
49
50 private Artifact artifact;
51
52 private Metadata metadata;
53
54 private DefaultRepositorySystemSession session;
55
56 private String localArtifactPath;
57
58 private String localMetadataPath;
59
60 private DefaultInstaller installer;
61
62 private InstallRequest request;
63
64 private RecordingRepositoryListener listener;
65
66 private File localArtifactFile;
67
68 private TestLocalRepositoryManager lrm;
69
70 @BeforeEach
71 void setup() throws IOException {
72 artifact = new DefaultArtifact("gid", "aid", "jar", "ver");
73 artifact = artifact.setFile(TestFileUtils.createTempFile("artifact".getBytes(), 1));
74 metadata = new DefaultMetadata(
75 "gid",
76 "aid",
77 "ver",
78 "type",
79 Nature.RELEASE_OR_SNAPSHOT,
80 TestFileUtils.createTempFile("metadata".getBytes(), 1));
81
82 session = TestUtils.newSession();
83 localArtifactPath = session.getLocalRepositoryManager().getPathForLocalArtifact(artifact);
84 localMetadataPath = session.getLocalRepositoryManager().getPathForLocalMetadata(metadata);
85
86 localArtifactFile = new File(session.getLocalRepository().getBasedir(), localArtifactPath);
87
88 installer = new DefaultInstaller(
89 new TestFileProcessor(),
90 new StubRepositoryEventDispatcher(),
91 Collections.emptyMap(),
92 new StubSyncContextFactory());
93 request = new InstallRequest();
94 listener = new RecordingRepositoryListener();
95 session.setRepositoryListener(listener);
96
97 lrm = (TestLocalRepositoryManager) session.getLocalRepositoryManager();
98
99 TestFileUtils.deleteFile(session.getLocalRepository().getBasedir());
100 }
101
102 @AfterEach
103 void teardown() throws Exception {
104 TestFileUtils.deleteFile(session.getLocalRepository().getBasedir());
105 }
106
107 @Test
108 void testSuccessfulInstall() throws InstallationException, IOException {
109 File artifactFile =
110 new File(session.getLocalRepositoryManager().getRepository().getBasedir(), localArtifactPath);
111 File metadataFile =
112 new File(session.getLocalRepositoryManager().getRepository().getBasedir(), localMetadataPath);
113
114 artifactFile.delete();
115 metadataFile.delete();
116
117 request.addArtifact(artifact);
118 request.addMetadata(metadata);
119
120 InstallResult result = installer.install(session, request);
121
122 assertTrue(artifactFile.exists());
123 assertEquals("artifact", TestFileUtils.readString(artifactFile));
124
125 assertTrue(metadataFile.exists());
126 assertEquals("metadata", TestFileUtils.readString(metadataFile));
127
128 assertEquals(result.getRequest(), request);
129
130 assertEquals(result.getArtifacts().size(), 1);
131 assertTrue(result.getArtifacts().contains(artifact));
132
133 assertEquals(result.getMetadata().size(), 1);
134 assertTrue(result.getMetadata().contains(metadata));
135
136 assertEquals(1, lrm.getMetadataRegistration().size());
137 assertTrue(lrm.getMetadataRegistration().contains(metadata));
138 assertEquals(1, lrm.getArtifactRegistration().size());
139 assertTrue(lrm.getArtifactRegistration().contains(artifact));
140 }
141
142 @Test
143 void testNullArtifactFile() {
144 InstallRequest request = new InstallRequest();
145 request.addArtifact(artifact.setFile(null));
146
147 assertThrows(InstallationException.class, () -> installer.install(session, request));
148 }
149
150 @Test
151 void testNullMetadataFile() {
152 InstallRequest request = new InstallRequest();
153 request.addMetadata(metadata.setFile(null));
154
155 assertThrows(InstallationException.class, () -> installer.install(session, request));
156 }
157
158 @Test
159 void testNonExistentArtifactFile() {
160 InstallRequest request = new InstallRequest();
161 request.addArtifact(artifact.setFile(new File("missing.txt")));
162
163 assertThrows(InstallationException.class, () -> installer.install(session, request));
164 }
165
166 @Test
167 void testNonExistentMetadataFile() {
168 InstallRequest request = new InstallRequest();
169 request.addMetadata(metadata.setFile(new File("missing.xml")));
170
171 assertThrows(InstallationException.class, () -> installer.install(session, request));
172 }
173
174 @Test
175 void testArtifactExistsAsDir() {
176 String path = session.getLocalRepositoryManager().getPathForLocalArtifact(artifact);
177 File file = new File(session.getLocalRepository().getBasedir(), path);
178 assertFalse(file.isFile(), file.getAbsolutePath() + " is a file, not directory");
179 assertFalse(file.exists(), file.getAbsolutePath() + " already exists");
180 assertTrue(
181 file.mkdirs() || file.isDirectory(),
182 "failed to setup test: could not create " + file.getAbsolutePath());
183
184 request.addArtifact(artifact);
185 assertThrows(InstallationException.class, () -> installer.install(session, request));
186 }
187
188 @Test
189 void testMetadataExistsAsDir() {
190 String path = session.getLocalRepositoryManager().getPathForLocalMetadata(metadata);
191 assertTrue(
192 new File(session.getLocalRepository().getBasedir(), path).mkdirs(),
193 "failed to setup test: could not create " + path);
194
195 request.addMetadata(metadata);
196 assertThrows(InstallationException.class, () -> installer.install(session, request));
197 }
198
199 @Test
200 void testArtifactDestinationEqualsSource() throws IOException {
201 String path = session.getLocalRepositoryManager().getPathForLocalArtifact(artifact);
202 File file = new File(session.getLocalRepository().getBasedir(), path);
203 artifact = artifact.setFile(file);
204 TestFileUtils.writeString(file, "test");
205
206 request.addArtifact(artifact);
207 assertThrows(InstallationException.class, () -> installer.install(session, request));
208 }
209
210 @Test
211 void testMetadataDestinationEqualsSource() throws IOException {
212 String path = session.getLocalRepositoryManager().getPathForLocalMetadata(metadata);
213 File file = new File(session.getLocalRepository().getBasedir(), path);
214 metadata = metadata.setFile(file);
215 TestFileUtils.writeString(file, "test");
216
217 request.addMetadata(metadata);
218 assertThrows(InstallationException.class, () -> installer.install(session, request));
219 }
220
221 @Test
222 void testSuccessfulArtifactEvents() throws InstallationException {
223 InstallRequest request = new InstallRequest();
224 request.addArtifact(artifact);
225
226 installer.install(session, request);
227 checkEvents("Repository Event problem", artifact, false);
228 }
229
230 @Test
231 void testSuccessfulMetadataEvents() throws InstallationException {
232 InstallRequest request = new InstallRequest();
233 request.addMetadata(metadata);
234
235 installer.install(session, request);
236 checkEvents("Repository Event problem", metadata, false);
237 }
238
239 @Test
240 void testFailingEventsNullArtifactFile() {
241 checkFailedEvents("null artifact file", this.artifact.setFile(null));
242 }
243
244 @Test
245 void testFailingEventsNullMetadataFile() {
246 checkFailedEvents("null metadata file", this.metadata.setFile(null));
247 }
248
249 @Test
250 void testFailingEventsArtifactExistsAsDir() {
251 String path = session.getLocalRepositoryManager().getPathForLocalArtifact(artifact);
252 assertTrue(
253 new File(session.getLocalRepository().getBasedir(), path).mkdirs(),
254 "failed to setup test: could not create " + path);
255 checkFailedEvents("target exists as dir", artifact);
256 }
257
258 @Test
259 void testFailingEventsMetadataExistsAsDir() {
260 String path = session.getLocalRepositoryManager().getPathForLocalMetadata(metadata);
261 assertTrue(
262 new File(session.getLocalRepository().getBasedir(), path).mkdirs(),
263 "failed to setup test: could not create " + path);
264 checkFailedEvents("target exists as dir", metadata);
265 }
266
267 private void checkFailedEvents(String msg, Metadata metadata) {
268 InstallRequest request = new InstallRequest().addMetadata(metadata);
269 msg = "Repository events problem (case: " + msg + ")";
270
271 try {
272 installer.install(session, request);
273 fail("expected exception");
274 } catch (InstallationException e) {
275 checkEvents(msg, metadata, true);
276 }
277 }
278
279 private void checkEvents(String msg, Metadata metadata, boolean failed) {
280 List<RepositoryEvent> events = listener.getEvents();
281 assertEquals(2, events.size(), msg);
282 RepositoryEvent event = events.get(0);
283 assertEquals(EventType.METADATA_INSTALLING, event.getType(), msg);
284 assertEquals(metadata, event.getMetadata(), msg);
285 assertNull(event.getException(), msg);
286
287 event = events.get(1);
288 assertEquals(EventType.METADATA_INSTALLED, event.getType(), msg);
289 assertEquals(metadata, event.getMetadata(), msg);
290 if (failed) {
291 assertNotNull(event.getException(), msg);
292 } else {
293 assertNull(event.getException(), msg);
294 }
295 }
296
297 private void checkFailedEvents(String msg, Artifact artifact) {
298 InstallRequest request = new InstallRequest().addArtifact(artifact);
299 msg = "Repository events problem (case: " + msg + ")";
300
301 try {
302 installer.install(session, request);
303 fail("expected exception");
304 } catch (InstallationException e) {
305 checkEvents(msg, artifact, true);
306 }
307 }
308
309 private void checkEvents(String msg, Artifact artifact, boolean failed) {
310 List<RepositoryEvent> events = listener.getEvents();
311 assertEquals(2, events.size(), msg);
312 RepositoryEvent event = events.get(0);
313 assertEquals(EventType.ARTIFACT_INSTALLING, event.getType(), msg);
314 assertEquals(artifact, event.getArtifact(), msg);
315 assertNull(event.getException(), msg);
316
317 event = events.get(1);
318 assertEquals(EventType.ARTIFACT_INSTALLED, event.getType(), msg);
319 assertEquals(artifact, event.getArtifact(), msg);
320 if (failed) {
321 assertNotNull(event.getException(), msg + " > expected exception");
322 } else {
323 assertNull(event.getException(), msg + " > " + event.getException());
324 }
325 }
326
327 @Test
328 @Disabled("Naive change detection is removed (MRESOLVER-392)")
329 void testDoNotUpdateUnchangedArtifact() throws InstallationException {
330 request.addArtifact(artifact);
331 installer.install(session, request);
332
333 installer = new DefaultInstaller(
334 new DefaultFileProcessor() {
335 @Override
336 public long copy(File src, File target, ProgressListener listener) throws IOException {
337 throw new IOException("copy called");
338 }
339 },
340 new StubRepositoryEventDispatcher(),
341 Collections.emptyMap(),
342 new StubSyncContextFactory());
343
344 request = new InstallRequest();
345 request.addArtifact(artifact);
346 installer.install(session, request);
347 }
348
349 @Test
350 void testSetArtifactTimestamps() throws InstallationException {
351 artifact.getFile().setLastModified(artifact.getFile().lastModified() - 60000);
352
353 request.addArtifact(artifact);
354
355 installer.install(session, request);
356
357 assertEquals(
358 artifact.getFile().lastModified(),
359 localArtifactFile.lastModified(),
360 "artifact timestamp was not set to src file");
361
362 request = new InstallRequest();
363
364 request.addArtifact(artifact);
365
366 artifact.getFile().setLastModified(artifact.getFile().lastModified() - 60000);
367
368 installer.install(session, request);
369
370 assertEquals(
371 artifact.getFile().lastModified(),
372 localArtifactFile.lastModified(),
373 "artifact timestamp was not set to src file");
374 }
375 }