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