View Javadoc
1   package org.eclipse.aether.internal.impl;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.junit.Assert.*;
23  
24  import java.io.File;
25  import java.io.IOException;
26  import java.util.List;
27  
28  import org.eclipse.aether.DefaultRepositorySystemSession;
29  import org.eclipse.aether.RepositoryEvent;
30  import org.eclipse.aether.RepositoryEvent.EventType;
31  import org.eclipse.aether.artifact.Artifact;
32  import org.eclipse.aether.artifact.DefaultArtifact;
33  import org.eclipse.aether.installation.InstallRequest;
34  import org.eclipse.aether.installation.InstallResult;
35  import org.eclipse.aether.installation.InstallationException;
36  import org.eclipse.aether.internal.impl.DefaultFileProcessor;
37  import org.eclipse.aether.internal.impl.DefaultInstaller;
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.junit.After;
46  import org.junit.Before;
47  import org.junit.Test;
48  
49  public class DefaultInstallerTest
50  {
51  
52      private Artifact artifact;
53  
54      private Metadata metadata;
55  
56      private DefaultRepositorySystemSession session;
57  
58      private String localArtifactPath;
59  
60      private String localMetadataPath;
61  
62      private DefaultInstaller installer;
63  
64      private InstallRequest request;
65  
66      private RecordingRepositoryListener listener;
67  
68      private File localArtifactFile;
69  
70      private TestLocalRepositoryManager lrm;
71  
72      @Before
73      public void setup()
74          throws IOException
75      {
76          artifact = new DefaultArtifact( "gid", "aid", "jar", "ver" );
77          artifact = artifact.setFile( TestFileUtils.createTempFile( "artifact".getBytes(), 1 ) );
78          metadata =
79              new DefaultMetadata( "gid", "aid", "ver", "type", 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          installer.setFileProcessor( new TestFileProcessor() );
90          installer.setRepositoryEventDispatcher( new StubRepositoryEventDispatcher() );
91          installer.setSyncContextFactory( 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     @After
102     public void teardown()
103         throws Exception
104     {
105         TestFileUtils.deleteFile( session.getLocalRepository().getBasedir() );
106     }
107 
108     @Test
109     public void testSuccessfulInstall()
110         throws InstallationException, IOException
111     {
112         File artifactFile =
113             new File( session.getLocalRepositoryManager().getRepository().getBasedir(), localArtifactPath );
114         File metadataFile =
115             new File( session.getLocalRepositoryManager().getRepository().getBasedir(), localMetadataPath );
116 
117         artifactFile.delete();
118         metadataFile.delete();
119 
120         request.addArtifact( artifact );
121         request.addMetadata( metadata );
122 
123         InstallResult result = installer.install( session, request );
124 
125         assertTrue( artifactFile.exists() );
126         assertEquals( "artifact", TestFileUtils.readString( artifactFile ) );
127 
128         assertTrue( metadataFile.exists() );
129         assertEquals( "metadata", TestFileUtils.readString( metadataFile ) );
130 
131         assertEquals( result.getRequest(), request );
132 
133         assertEquals( result.getArtifacts().size(), 1 );
134         assertTrue( result.getArtifacts().contains( artifact ) );
135 
136         assertEquals( result.getMetadata().size(), 1 );
137         assertTrue( result.getMetadata().contains( metadata ) );
138 
139         assertEquals( 1, lrm.getMetadataRegistration().size() );
140         assertTrue( lrm.getMetadataRegistration().contains( metadata ) );
141         assertEquals( 1, lrm.getArtifactRegistration().size() );
142         assertTrue( lrm.getArtifactRegistration().contains( artifact ) );
143     }
144 
145     @Test( expected = InstallationException.class )
146     public void testNullArtifactFile()
147         throws InstallationException
148     {
149         InstallRequest request = new InstallRequest();
150         request.addArtifact( artifact.setFile( null ) );
151 
152         installer.install( session, request );
153     }
154 
155     @Test( expected = InstallationException.class )
156     public void testNullMetadataFile()
157         throws InstallationException
158     {
159         InstallRequest request = new InstallRequest();
160         request.addMetadata( metadata.setFile( null ) );
161 
162         installer.install( session, request );
163     }
164 
165     @Test( expected = InstallationException.class )
166     public void testNonExistentArtifactFile()
167         throws InstallationException
168     {
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()
177         throws InstallationException
178     {
179         InstallRequest request = new InstallRequest();
180         request.addMetadata( metadata.setFile( new File( "missing.xml" ) ) );
181 
182         installer.install( session, request );
183     }
184 
185     @Test( expected = InstallationException.class )
186     public void testArtifactExistsAsDir()
187         throws InstallationException
188     {
189         String path = session.getLocalRepositoryManager().getPathForLocalArtifact( artifact );
190         File file = new File( session.getLocalRepository().getBasedir(), path );
191         assertFalse( file.getAbsolutePath() + " is a file, not directory", file.isFile() );
192         assertFalse( file.getAbsolutePath() + " already exists", file.exists() );
193         assertTrue( "failed to setup test: could not create " + file.getAbsolutePath(),
194                     file.mkdirs() || file.isDirectory() );
195 
196         request.addArtifact( artifact );
197         installer.install( session, request );
198     }
199 
200     @Test( expected = InstallationException.class )
201     public void testMetadataExistsAsDir()
202         throws InstallationException
203     {
204         String path = session.getLocalRepositoryManager().getPathForLocalMetadata( metadata );
205         assertTrue( "failed to setup test: could not create " + path,
206                     new File( session.getLocalRepository().getBasedir(), path ).mkdirs() );
207 
208         request.addMetadata( metadata );
209         installer.install( session, request );
210     }
211 
212     @Test( expected = InstallationException.class )
213     public void testArtifactDestinationEqualsSource()
214         throws Exception
215     {
216         String path = session.getLocalRepositoryManager().getPathForLocalArtifact( artifact );
217         File file = new File( session.getLocalRepository().getBasedir(), path );
218         artifact = artifact.setFile( file );
219         TestFileUtils.writeString( file, "test" );
220 
221         request.addArtifact( artifact );
222         installer.install( session, request );
223     }
224 
225     @Test( expected = InstallationException.class )
226     public void testMetadataDestinationEqualsSource()
227         throws Exception
228     {
229         String path = session.getLocalRepositoryManager().getPathForLocalMetadata( metadata );
230         File file = new File( session.getLocalRepository().getBasedir(), path );
231         metadata = metadata.setFile( file );
232         TestFileUtils.writeString( file, "test" );
233 
234         request.addMetadata( metadata );
235         installer.install( session, request );
236     }
237 
238     @Test
239     public void testSuccessfulArtifactEvents()
240         throws InstallationException
241     {
242         InstallRequest request = new InstallRequest();
243         request.addArtifact( artifact );
244 
245         installer.install( session, request );
246         checkEvents( "Repository Event problem", artifact, false );
247     }
248 
249     @Test
250     public void testSuccessfulMetadataEvents()
251         throws InstallationException
252     {
253         InstallRequest request = new InstallRequest();
254         request.addMetadata( metadata );
255 
256         installer.install( session, request );
257         checkEvents( "Repository Event problem", metadata, false );
258     }
259 
260     @Test
261     public void testFailingEventsNullArtifactFile()
262     {
263         checkFailedEvents( "null artifact file", this.artifact.setFile( null ) );
264     }
265 
266     @Test
267     public void testFailingEventsNullMetadataFile()
268     {
269         checkFailedEvents( "null metadata file", this.metadata.setFile( null ) );
270     }
271 
272     @Test
273     public void testFailingEventsArtifactExistsAsDir()
274     {
275         String path = session.getLocalRepositoryManager().getPathForLocalArtifact( artifact );
276         assertTrue( "failed to setup test: could not create " + path,
277                     new File( session.getLocalRepository().getBasedir(), path ).mkdirs() );
278         checkFailedEvents( "target exists as dir", artifact );
279     }
280 
281     @Test
282     public void testFailingEventsMetadataExistsAsDir()
283     {
284         String path = session.getLocalRepositoryManager().getPathForLocalMetadata( metadata );
285         assertTrue( "failed to setup test: could not create " + path,
286                     new File( session.getLocalRepository().getBasedir(), path ).mkdirs() );
287         checkFailedEvents( "target exists as dir", metadata );
288     }
289 
290     private void checkFailedEvents( String msg, Metadata metadata )
291     {
292         InstallRequest request = new InstallRequest().addMetadata( metadata );
293         msg = "Repository events problem (case: " + msg + ")";
294 
295         try
296         {
297             installer.install( session, request );
298             fail( "expected exception" );
299         }
300         catch ( InstallationException e )
301         {
302             checkEvents( msg, metadata, true );
303         }
304 
305     }
306 
307     private void checkEvents( String msg, Metadata metadata, boolean failed )
308     {
309         List<RepositoryEvent> events = listener.getEvents();
310         assertEquals( msg, 2, events.size() );
311         RepositoryEvent event = events.get( 0 );
312         assertEquals( msg, EventType.METADATA_INSTALLING, event.getType() );
313         assertEquals( msg, metadata, event.getMetadata() );
314         assertNull( msg, event.getException() );
315 
316         event = events.get( 1 );
317         assertEquals( msg, EventType.METADATA_INSTALLED, event.getType() );
318         assertEquals( msg, metadata, event.getMetadata() );
319         if ( failed )
320         {
321             assertNotNull( msg, event.getException() );
322         }
323         else
324         {
325             assertNull( msg, event.getException() );
326         }
327     }
328 
329     private void checkFailedEvents( String msg, Artifact artifact )
330     {
331         InstallRequest request = new InstallRequest().addArtifact( artifact );
332         msg = "Repository events problem (case: " + msg + ")";
333 
334         try
335         {
336             installer.install( session, request );
337             fail( "expected exception" );
338         }
339         catch ( InstallationException e )
340         {
341             checkEvents( msg, artifact, true );
342         }
343     }
344 
345     private void checkEvents( String msg, Artifact artifact, boolean failed )
346     {
347         List<RepositoryEvent> events = listener.getEvents();
348         assertEquals( msg, 2, events.size() );
349         RepositoryEvent event = events.get( 0 );
350         assertEquals( msg, EventType.ARTIFACT_INSTALLING, event.getType() );
351         assertEquals( msg, artifact, event.getArtifact() );
352         assertNull( msg, event.getException() );
353         
354         event = events.get( 1 );
355         assertEquals( msg, EventType.ARTIFACT_INSTALLED, event.getType() );
356         assertEquals( msg, artifact, event.getArtifact() );
357         if ( failed )
358         {
359             assertNotNull( msg + " > expected exception", event.getException() );
360         }
361         else
362         {
363             assertNull( msg + " > " + event.getException(), event.getException() );
364         }
365     }
366 
367     @Test
368     public void testDoNotUpdateUnchangedArtifact()
369         throws InstallationException
370     {
371         request.addArtifact( artifact );
372         installer.install( session, request );
373 
374         installer.setFileProcessor( new DefaultFileProcessor()
375         {
376             @Override
377             public long copy( File src, File target, ProgressListener listener )
378                 throws IOException
379             {
380                 throw new IOException( "copy called" );
381             }
382         } );
383 
384         request = new InstallRequest();
385         request.addArtifact( artifact );
386         installer.install( session, request );
387     }
388 
389     @Test
390     public void testSetArtifactTimestamps()
391         throws InstallationException
392     {
393         artifact.getFile().setLastModified( artifact.getFile().lastModified() - 60000 );
394 
395         request.addArtifact( artifact );
396 
397         installer.install( session, request );
398 
399         assertEquals( "artifact timestamp was not set to src file", artifact.getFile().lastModified(),
400                       localArtifactFile.lastModified() );
401 
402         request = new InstallRequest();
403 
404         request.addArtifact( artifact );
405 
406         artifact.getFile().setLastModified( artifact.getFile().lastModified() - 60000 );
407 
408         installer.install( session, request );
409 
410         assertEquals( "artifact timestamp was not set to src file", artifact.getFile().lastModified(),
411                       localArtifactFile.lastModified() );
412     }
413 }