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.net.URI;
27  import java.util.Arrays;
28  import java.util.Collection;
29  import java.util.Collections;
30  
31  import org.eclipse.aether.RepositorySystemSession;
32  import org.eclipse.aether.artifact.Artifact;
33  import org.eclipse.aether.artifact.DefaultArtifact;
34  import org.eclipse.aether.internal.impl.EnhancedLocalRepositoryManager;
35  import org.eclipse.aether.internal.test.util.TestFileUtils;
36  import org.eclipse.aether.internal.test.util.TestUtils;
37  import org.eclipse.aether.metadata.DefaultMetadata;
38  import org.eclipse.aether.metadata.Metadata;
39  import org.eclipse.aether.metadata.Metadata.Nature;
40  import org.eclipse.aether.repository.LocalArtifactRegistration;
41  import org.eclipse.aether.repository.LocalArtifactRequest;
42  import org.eclipse.aether.repository.LocalArtifactResult;
43  import org.eclipse.aether.repository.LocalMetadataRequest;
44  import org.eclipse.aether.repository.LocalMetadataResult;
45  import org.eclipse.aether.repository.RemoteRepository;
46  import org.junit.After;
47  import org.junit.Before;
48  import org.junit.Test;
49  
50  public class EnhancedLocalRepositoryManagerTest
51  {
52  
53      private Artifact artifact;
54  
55      private Artifact snapshot;
56  
57      private File basedir;
58  
59      private EnhancedLocalRepositoryManager manager;
60  
61      private File artifactFile;
62  
63      private RemoteRepository repository;
64  
65      private String testContext = "project/compile";
66  
67      private RepositorySystemSession session;
68  
69      private Metadata metadata;
70  
71      private Metadata noVerMetadata;
72  
73      @Before
74      public void setup()
75          throws Exception
76      {
77          String url = TestFileUtils.createTempDir( "enhanced-remote-repo" ).toURI().toURL().toString();
78          repository =
79              new RemoteRepository.Builder( "enhanced-remote-repo", "default", url ).setRepositoryManager( true ).build();
80  
81          artifact =
82              new DefaultArtifact( "gid", "aid", "", "jar", "1-test", Collections.<String, String> emptyMap(),
83                                   TestFileUtils.createTempFile( "artifact" ) );
84  
85          snapshot =
86              new DefaultArtifact( "gid", "aid", "", "jar", "1.0-20120710.231549-9",
87                                   Collections.<String, String> emptyMap(), TestFileUtils.createTempFile( "artifact" ) );
88  
89          metadata =
90              new DefaultMetadata( "gid", "aid", "1-test", "maven-metadata.xml", Nature.RELEASE,
91                                   TestFileUtils.createTempFile( "metadata" ) );
92  
93          noVerMetadata =
94              new DefaultMetadata( "gid", "aid", null, "maven-metadata.xml", Nature.RELEASE,
95                                   TestFileUtils.createTempFile( "metadata" ) );
96  
97          basedir = TestFileUtils.createTempDir( "enhanced-repo" );
98          session = TestUtils.newSession();
99          manager = new EnhancedLocalRepositoryManager( basedir, session );
100 
101         artifactFile = new File( basedir, manager.getPathForLocalArtifact( artifact ) );
102     }
103 
104     @After
105     public void tearDown()
106         throws Exception
107     {
108         TestFileUtils.deleteFile( basedir );
109         TestFileUtils.deleteFile( new File( new URI( repository.getUrl() ) ) );
110 
111         session = null;
112         manager = null;
113         repository = null;
114         artifact = null;
115     }
116 
117     private long addLocalArtifact( Artifact artifact )
118         throws IOException
119     {
120         manager.add( session, new LocalArtifactRegistration( artifact ) );
121         String path = manager.getPathForLocalArtifact( artifact );
122 
123         return copy( artifact, path );
124     }
125 
126     private long addRemoteArtifact( Artifact artifact )
127         throws IOException
128     {
129         Collection<String> contexts = Arrays.asList( testContext );
130         manager.add( session, new LocalArtifactRegistration( artifact, repository, contexts ) );
131         String path = manager.getPathForRemoteArtifact( artifact, repository, testContext );
132         return copy( artifact, path );
133     }
134 
135     private long copy( Metadata metadata, String path )
136         throws IOException
137     {
138         if ( metadata.getFile() == null )
139         {
140             return -1L;
141         }
142         return TestFileUtils.copyFile( metadata.getFile(), new File( basedir, path ) );
143     }
144 
145     private long copy( Artifact artifact, String path )
146         throws IOException
147     {
148         if ( artifact.getFile() == null )
149         {
150             return -1L;
151         }
152         File artifactFile = new File( basedir, path );
153         return TestFileUtils.copyFile( artifact.getFile(), artifactFile );
154     }
155 
156     @Test
157     public void testGetPathForLocalArtifact()
158     {
159         Artifact artifact = new DefaultArtifact( "g.i.d:a.i.d:1.0-SNAPSHOT" );
160         assertEquals( "1.0-SNAPSHOT", artifact.getBaseVersion() );
161         assertEquals( "g/i/d/a.i.d/1.0-SNAPSHOT/a.i.d-1.0-SNAPSHOT.jar", manager.getPathForLocalArtifact( artifact ) );
162 
163         artifact = new DefaultArtifact( "g.i.d:a.i.d:1.0-20110329.221805-4" );
164         assertEquals( "1.0-SNAPSHOT", artifact.getBaseVersion() );
165         assertEquals( "g/i/d/a.i.d/1.0-SNAPSHOT/a.i.d-1.0-SNAPSHOT.jar", manager.getPathForLocalArtifact( artifact ) );
166     }
167 
168     @Test
169     public void testGetPathForRemoteArtifact()
170     {
171         RemoteRepository remoteRepo = new RemoteRepository.Builder( "repo", "default", "ram:/void" ).build();
172 
173         Artifact artifact = new DefaultArtifact( "g.i.d:a.i.d:1.0-SNAPSHOT" );
174         assertEquals( "1.0-SNAPSHOT", artifact.getBaseVersion() );
175         assertEquals( "g/i/d/a.i.d/1.0-SNAPSHOT/a.i.d-1.0-SNAPSHOT.jar",
176                       manager.getPathForRemoteArtifact( artifact, remoteRepo, "" ) );
177 
178         artifact = new DefaultArtifact( "g.i.d:a.i.d:1.0-20110329.221805-4" );
179         assertEquals( "1.0-SNAPSHOT", artifact.getBaseVersion() );
180         assertEquals( "g/i/d/a.i.d/1.0-SNAPSHOT/a.i.d-1.0-20110329.221805-4.jar",
181                       manager.getPathForRemoteArtifact( artifact, remoteRepo, "" ) );
182     }
183 
184     @Test
185     public void testFindLocalArtifact()
186         throws Exception
187     {
188         addLocalArtifact( artifact );
189 
190         LocalArtifactRequest request = new LocalArtifactRequest( artifact, null, null );
191         LocalArtifactResult result = manager.find( session, request );
192         assertTrue( result.isAvailable() );
193         assertEquals( null, result.getRepository() );
194 
195         snapshot = snapshot.setVersion( snapshot.getBaseVersion() );
196         addLocalArtifact( snapshot );
197 
198         request = new LocalArtifactRequest( snapshot, null, null );
199         result = manager.find( session, request );
200         assertTrue( result.isAvailable() );
201         assertEquals( null, result.getRepository() );
202     }
203 
204     @Test
205     public void testFindRemoteArtifact()
206         throws Exception
207     {
208         addRemoteArtifact( artifact );
209 
210         LocalArtifactRequest request = new LocalArtifactRequest( artifact, Arrays.asList( repository ), testContext );
211         LocalArtifactResult result = manager.find( session, request );
212         assertTrue( result.isAvailable() );
213         assertEquals( repository, result.getRepository() );
214 
215         addRemoteArtifact( snapshot );
216 
217         request = new LocalArtifactRequest( snapshot, Arrays.asList( repository ), testContext );
218         result = manager.find( session, request );
219         assertTrue( result.isAvailable() );
220         assertEquals( repository, result.getRepository() );
221     }
222 
223     @Test
224     public void testDoNotFindDifferentContext()
225         throws Exception
226     {
227         addRemoteArtifact( artifact );
228 
229         LocalArtifactRequest request = new LocalArtifactRequest( artifact, Arrays.asList( repository ), "different" );
230         LocalArtifactResult result = manager.find( session, request );
231         assertFalse( result.isAvailable() );
232     }
233 
234     @Test
235     public void testDoNotFindNullFile()
236         throws Exception
237     {
238         artifact = artifact.setFile( null );
239         addLocalArtifact( artifact );
240 
241         LocalArtifactRequest request = new LocalArtifactRequest( artifact, Arrays.asList( repository ), testContext );
242         LocalArtifactResult result = manager.find( session, request );
243         assertFalse( result.isAvailable() );
244     }
245 
246     @Test
247     public void testDoNotFindDeletedFile()
248         throws Exception
249     {
250         addLocalArtifact( artifact );
251         assertTrue( "could not delete artifact file", artifactFile.delete() );
252 
253         LocalArtifactRequest request = new LocalArtifactRequest( artifact, Arrays.asList( repository ), testContext );
254         LocalArtifactResult result = manager.find( session, request );
255         assertFalse( result.isAvailable() );
256     }
257 
258     @Test
259     public void testFindUntrackedFile()
260         throws Exception
261     {
262         copy( artifact, manager.getPathForLocalArtifact( artifact ) );
263 
264         LocalArtifactRequest request = new LocalArtifactRequest( artifact, Arrays.asList( repository ), testContext );
265         LocalArtifactResult result = manager.find( session, request );
266         assertTrue( result.isAvailable() );
267     }
268 
269     private long addMetadata( Metadata metadata, RemoteRepository repo )
270         throws IOException
271     {
272         String path;
273         if ( repo == null )
274         {
275             path = manager.getPathForLocalMetadata( metadata );
276         }
277         else
278         {
279             path = manager.getPathForRemoteMetadata( metadata, repo, testContext );
280         }
281         System.err.println( path );
282 
283         return copy( metadata, path );
284     }
285 
286     @Test
287     public void testFindLocalMetadata()
288         throws Exception
289     {
290         addMetadata( metadata, null );
291 
292         LocalMetadataRequest request = new LocalMetadataRequest( metadata, null, testContext );
293         LocalMetadataResult result = manager.find( session, request );
294 
295         assertNotNull( result.getFile() );
296     }
297 
298     @Test
299     public void testFindLocalMetadataNoVersion()
300         throws Exception
301     {
302         addMetadata( noVerMetadata, null );
303 
304         LocalMetadataRequest request = new LocalMetadataRequest( noVerMetadata, null, testContext );
305         LocalMetadataResult result = manager.find( session, request );
306 
307         assertNotNull( result.getFile() );
308     }
309 
310     @Test
311     public void testDoNotFindRemoteMetadataDifferentContext()
312         throws Exception
313     {
314         addMetadata( noVerMetadata, repository );
315         addMetadata( metadata, repository );
316 
317         LocalMetadataRequest request = new LocalMetadataRequest( noVerMetadata, repository, "different" );
318         LocalMetadataResult result = manager.find( session, request );
319         assertNull( result.getFile() );
320 
321         request = new LocalMetadataRequest( metadata, repository, "different" );
322         result = manager.find( session, request );
323         assertNull( result.getFile() );
324     }
325 
326     @Test
327     public void testFindArtifactUsesTimestampedVersion()
328         throws Exception
329     {
330         Artifact artifact = new DefaultArtifact( "g.i.d:a.i.d:1.0-SNAPSHOT" );
331         File file = new File( basedir, manager.getPathForLocalArtifact( artifact ) );
332         TestFileUtils.writeString( file, "test" );
333         addLocalArtifact( artifact );
334 
335         artifact = artifact.setVersion( "1.0-20110329.221805-4" );
336         LocalArtifactRequest request = new LocalArtifactRequest();
337         request.setArtifact( artifact );
338         LocalArtifactResult result = manager.find( session, request );
339         assertNull( result.toString(), result.getFile() );
340         assertFalse( result.toString(), result.isAvailable() );
341     }
342 
343 }