View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.internal.impl;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.nio.file.Path;
24  import java.util.Collections;
25  import java.util.List;
26  
27  import org.eclipse.aether.DefaultRepositorySystemSession;
28  import org.eclipse.aether.RepositoryEvent;
29  import org.eclipse.aether.RepositoryEvent.EventType;
30  import org.eclipse.aether.artifact.Artifact;
31  import org.eclipse.aether.artifact.DefaultArtifact;
32  import org.eclipse.aether.installation.InstallRequest;
33  import org.eclipse.aether.installation.InstallResult;
34  import org.eclipse.aether.installation.InstallationException;
35  import org.eclipse.aether.internal.test.util.*;
36  import org.eclipse.aether.metadata.DefaultMetadata;
37  import org.eclipse.aether.metadata.Metadata;
38  import org.eclipse.aether.metadata.Metadata.Nature;
39  import org.junit.jupiter.api.AfterEach;
40  import org.junit.jupiter.api.BeforeEach;
41  import org.junit.jupiter.api.Disabled;
42  import org.junit.jupiter.api.Test;
43  
44  import static org.junit.jupiter.api.Assertions.*;
45  
46  public class DefaultInstallerTest {
47  
48      private Artifact artifact;
49  
50      private Metadata metadata;
51  
52      private DefaultRepositorySystemSession session;
53  
54      private String localArtifactPath;
55  
56      private String localMetadataPath;
57  
58      private DefaultInstaller installer;
59  
60      private InstallRequest request;
61  
62      private RecordingRepositoryListener listener;
63  
64      private File localArtifactFile;
65  
66      private TestLocalRepositoryManager lrm;
67  
68      @BeforeEach
69      void setup() throws IOException {
70          artifact = new DefaultArtifact("gid", "aid", "jar", "ver");
71          artifact = artifact.setFile(TestFileUtils.createTempFile("artifact".getBytes(), 1));
72          metadata = new DefaultMetadata(
73                  "gid",
74                  "aid",
75                  "ver",
76                  "type",
77                  Nature.RELEASE_OR_SNAPSHOT,
78                  TestFileUtils.createTempFile("metadata".getBytes(), 1));
79  
80          session = TestUtils.newSession();
81          localArtifactPath = session.getLocalRepositoryManager().getPathForLocalArtifact(artifact);
82          localMetadataPath = session.getLocalRepositoryManager().getPathForLocalMetadata(metadata);
83  
84          localArtifactFile = new File(session.getLocalRepository().getBasedir(), localArtifactPath);
85  
86          installer = new DefaultInstaller(
87                  new TestPathProcessor(),
88                  new StubRepositoryEventDispatcher(),
89                  Collections.emptyMap(),
90                  Collections.emptyMap(),
91                  new StubSyncContextFactory());
92          request = new InstallRequest();
93          listener = new RecordingRepositoryListener();
94          session.setRepositoryListener(listener);
95  
96          lrm = (TestLocalRepositoryManager) session.getLocalRepositoryManager();
97  
98          TestFileUtils.deleteFile(session.getLocalRepository().getBasedir());
99      }
100 
101     @AfterEach
102     void teardown() throws Exception {
103         TestFileUtils.deleteFile(session.getLocalRepository().getBasedir());
104     }
105 
106     @Test
107     void testSuccessfulInstall() throws InstallationException, IOException {
108         File artifactFile =
109                 new File(session.getLocalRepositoryManager().getRepository().getBasedir(), localArtifactPath);
110         File metadataFile =
111                 new File(session.getLocalRepositoryManager().getRepository().getBasedir(), localMetadataPath);
112 
113         artifactFile.delete();
114         metadataFile.delete();
115 
116         request.addArtifact(artifact);
117         request.addMetadata(metadata);
118 
119         InstallResult result = installer.install(session, request);
120 
121         assertTrue(artifactFile.exists());
122         assertEquals("artifact", TestFileUtils.readString(artifactFile));
123 
124         assertTrue(metadataFile.exists());
125         assertEquals("metadata", TestFileUtils.readString(metadataFile));
126 
127         assertEquals(result.getRequest(), request);
128 
129         assertEquals(result.getArtifacts().size(), 1);
130         assertTrue(result.getArtifacts().contains(artifact));
131 
132         assertEquals(result.getMetadata().size(), 1);
133         assertTrue(result.getMetadata().contains(metadata));
134 
135         assertEquals(1, lrm.getMetadataRegistration().size());
136         assertTrue(lrm.getMetadataRegistration().contains(metadata));
137         assertEquals(1, lrm.getArtifactRegistration().size());
138         assertTrue(lrm.getArtifactRegistration().contains(artifact));
139     }
140 
141     @Test
142     void testNullArtifactFile() {
143         InstallRequest request = new InstallRequest();
144         request.addArtifact(artifact.setFile(null));
145 
146         assertThrows(InstallationException.class, () -> installer.install(session, request));
147     }
148 
149     @Test
150     void testNullMetadataFile() {
151         InstallRequest request = new InstallRequest();
152         request.addMetadata(metadata.setFile(null));
153 
154         assertThrows(InstallationException.class, () -> installer.install(session, request));
155     }
156 
157     @Test
158     void testNonExistentArtifactFile() {
159         InstallRequest request = new InstallRequest();
160         request.addArtifact(artifact.setFile(new File("missing.txt")));
161 
162         assertThrows(InstallationException.class, () -> installer.install(session, request));
163     }
164 
165     @Test
166     void testNonExistentMetadataFile() {
167         InstallRequest request = new InstallRequest();
168         request.addMetadata(metadata.setFile(new File("missing.xml")));
169 
170         assertThrows(InstallationException.class, () -> installer.install(session, request));
171     }
172 
173     @Test
174     void testArtifactExistsAsDir() {
175         String path = session.getLocalRepositoryManager().getPathForLocalArtifact(artifact);
176         File file = new File(session.getLocalRepository().getBasedir(), path);
177         assertFalse(file.isFile(), file.getAbsolutePath() + " is a file, not directory");
178         assertFalse(file.exists(), file.getAbsolutePath() + " already exists");
179         assertTrue(
180                 file.mkdirs() || file.isDirectory(),
181                 "failed to setup test: could not create " + file.getAbsolutePath());
182 
183         request.addArtifact(artifact);
184         assertThrows(InstallationException.class, () -> installer.install(session, request));
185     }
186 
187     @Test
188     void testMetadataExistsAsDir() {
189         String path = session.getLocalRepositoryManager().getPathForLocalMetadata(metadata);
190         assertTrue(
191                 new File(session.getLocalRepository().getBasedir(), path).mkdirs(),
192                 "failed to setup test: could not create " + path);
193 
194         request.addMetadata(metadata);
195         assertThrows(InstallationException.class, () -> installer.install(session, request));
196     }
197 
198     @Test
199     void testArtifactDestinationEqualsSource() throws IOException {
200         String path = session.getLocalRepositoryManager().getPathForLocalArtifact(artifact);
201         File file = new File(session.getLocalRepository().getBasedir(), path);
202         artifact = artifact.setFile(file);
203         TestFileUtils.writeString(file, "test");
204 
205         request.addArtifact(artifact);
206         assertThrows(InstallationException.class, () -> installer.install(session, request));
207     }
208 
209     @Test
210     void testMetadataDestinationEqualsSource() throws IOException {
211         String path = session.getLocalRepositoryManager().getPathForLocalMetadata(metadata);
212         File file = new File(session.getLocalRepository().getBasedir(), path);
213         metadata = metadata.setFile(file);
214         TestFileUtils.writeString(file, "test");
215 
216         request.addMetadata(metadata);
217         assertThrows(InstallationException.class, () -> installer.install(session, request));
218     }
219 
220     @Test
221     void testSuccessfulArtifactEvents() throws InstallationException {
222         InstallRequest request = new InstallRequest();
223         request.addArtifact(artifact);
224 
225         installer.install(session, request);
226         checkEvents("Repository Event problem", artifact, false);
227     }
228 
229     @Test
230     void testSuccessfulMetadataEvents() throws InstallationException {
231         InstallRequest request = new InstallRequest();
232         request.addMetadata(metadata);
233 
234         installer.install(session, request);
235         checkEvents("Repository Event problem", metadata, false);
236     }
237 
238     @Test
239     void testFailingEventsNullArtifactFile() {
240         checkFailedEvents("null artifact file", this.artifact.setFile(null));
241     }
242 
243     @Test
244     void testFailingEventsNullMetadataFile() {
245         checkFailedEvents("null metadata file", this.metadata.setFile(null));
246     }
247 
248     @Test
249     void testFailingEventsArtifactExistsAsDir() {
250         String path = session.getLocalRepositoryManager().getPathForLocalArtifact(artifact);
251         assertTrue(
252                 new File(session.getLocalRepository().getBasedir(), path).mkdirs(),
253                 "failed to setup test: could not create " + path);
254         checkFailedEvents("target exists as dir", artifact);
255     }
256 
257     @Test
258     void testFailingEventsMetadataExistsAsDir() {
259         String path = session.getLocalRepositoryManager().getPathForLocalMetadata(metadata);
260         assertTrue(
261                 new File(session.getLocalRepository().getBasedir(), path).mkdirs(),
262                 "failed to setup test: could not create " + path);
263         checkFailedEvents("target exists as dir", metadata);
264     }
265 
266     private void checkFailedEvents(String msg, Metadata metadata) {
267         InstallRequest request = new InstallRequest().addMetadata(metadata);
268         msg = "Repository events problem (case: " + msg + ")";
269 
270         try {
271             installer.install(session, request);
272             fail("expected exception");
273         } catch (InstallationException e) {
274             checkEvents(msg, metadata, true);
275         }
276     }
277 
278     private void checkEvents(String msg, Metadata metadata, boolean failed) {
279         List<RepositoryEvent> events = listener.getEvents();
280         assertEquals(2, events.size(), msg);
281         RepositoryEvent event = events.get(0);
282         assertEquals(EventType.METADATA_INSTALLING, event.getType(), msg);
283         assertEquals(metadata, event.getMetadata(), msg);
284         assertNull(event.getException(), msg);
285 
286         event = events.get(1);
287         assertEquals(EventType.METADATA_INSTALLED, event.getType(), msg);
288         assertEquals(metadata, event.getMetadata(), msg);
289         if (failed) {
290             assertNotNull(event.getException(), msg);
291         } else {
292             assertNull(event.getException(), msg);
293         }
294     }
295 
296     private void checkFailedEvents(String msg, Artifact artifact) {
297         InstallRequest request = new InstallRequest().addArtifact(artifact);
298         msg = "Repository events problem (case: " + msg + ")";
299 
300         try {
301             installer.install(session, request);
302             fail("expected exception");
303         } catch (InstallationException e) {
304             checkEvents(msg, artifact, true);
305         }
306     }
307 
308     private void checkEvents(String msg, Artifact artifact, boolean failed) {
309         List<RepositoryEvent> events = listener.getEvents();
310         assertEquals(2, events.size(), msg);
311         RepositoryEvent event = events.get(0);
312         assertEquals(EventType.ARTIFACT_INSTALLING, event.getType(), msg);
313         assertEquals(artifact, event.getArtifact(), msg);
314         assertNull(event.getException(), msg);
315 
316         event = events.get(1);
317         assertEquals(EventType.ARTIFACT_INSTALLED, event.getType(), msg);
318         assertEquals(artifact, event.getArtifact(), msg);
319         if (failed) {
320             assertNotNull(event.getException(), msg + " > expected exception");
321         } else {
322             assertNull(event.getException(), msg + " > " + event.getException());
323         }
324     }
325 
326     @Test
327     @Disabled("Naive change detection is removed (MRESOLVER-392)")
328     void testDoNotUpdateUnchangedArtifact() throws InstallationException {
329         request.addArtifact(artifact);
330         installer.install(session, request);
331 
332         installer = new DefaultInstaller(
333                 new DefaultPathProcessor() {
334                     @Override
335                     public long copy(Path src, Path target, ProgressListener listener) throws IOException {
336                         throw new IOException("copy called");
337                     }
338                 },
339                 new StubRepositoryEventDispatcher(),
340                 Collections.emptyMap(),
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 }