]> source.dussan.org Git - archiva.git/blob
e094a20ad5b9c48c933544371a09c35aa3eac726
[archiva.git] /
1 package org.apache.archiva.metadata.repository.storage.maven2;
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 junit.framework.TestCase;
23 import org.apache.archiva.configuration.ArchivaConfiguration;
24 import org.apache.archiva.configuration.Configuration;
25 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
26 import org.apache.archiva.configuration.ProxyConnectorConfiguration;
27 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
28 import org.apache.archiva.metadata.model.ArtifactMetadata;
29 import org.apache.archiva.metadata.model.Dependency;
30 import org.apache.archiva.metadata.model.License;
31 import org.apache.archiva.metadata.model.MailingList;
32 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
33 import org.apache.archiva.metadata.repository.filter.AllFilter;
34 import org.apache.archiva.metadata.repository.filter.Filter;
35 import org.apache.archiva.metadata.repository.storage.ReadMetadataRequest;
36 import org.apache.archiva.metadata.repository.storage.RepositoryStorageRuntimeException;
37 import org.apache.archiva.proxy.maven.WagonFactory;
38 import org.apache.archiva.proxy.maven.WagonFactoryRequest;
39 import org.apache.archiva.repository.RepositoryRegistry;
40 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
41 import org.apache.commons.io.FileUtils;
42 import org.apache.maven.wagon.Wagon;
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.springframework.test.context.ContextConfiguration;
47
48 import javax.inject.Inject;
49 import javax.inject.Named;
50 import java.io.IOException;
51 import java.nio.file.Files;
52 import java.nio.file.Path;
53 import java.nio.file.Paths;
54 import java.util.ArrayList;
55 import java.util.Arrays;
56 import java.util.HashMap;
57 import java.util.List;
58
59 import static org.mockito.Mockito.mock;
60 import static org.mockito.Mockito.when;
61
62 @RunWith ( ArchivaSpringJUnit4ClassRunner.class )
63 @ContextConfiguration ( { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
64 public class Maven2RepositoryMetadataResolverMRM1411Test
65     extends TestCase
66 {
67     private static final Filter<String> ALL = new AllFilter<String>();
68
69     @Inject
70     @Named ( "repositoryStorage#maven2")
71     private Maven2RepositoryStorage storage;
72
73     private static final String TEST_REPO_ID = "test";
74
75     private static final String TEST_REMOTE_REPO_ID = "central";
76
77     private static final String ASF_SCM_CONN_BASE = "scm:svn:http://svn.apache.org/repos/asf/";
78
79     private static final String ASF_SCM_DEV_CONN_BASE = "scm:svn:https://svn.apache.org/repos/asf/";
80
81     private static final String ASF_SCM_VIEWVC_BASE = "http://svn.apache.org/viewvc/";
82
83     private static final String TEST_SCM_CONN_BASE = "scm:svn:http://svn.example.com/repos/";
84
85     private static final String TEST_SCM_DEV_CONN_BASE = "scm:svn:https://svn.example.com/repos/";
86
87     private static final String TEST_SCM_URL_BASE = "http://svn.example.com/repos/";
88
89     private static final String EMPTY_MD5 = "d41d8cd98f00b204e9800998ecf8427e";
90
91     private static final String EMPTY_SHA1 = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
92
93     @Inject
94     @Named ( "archivaConfiguration#default" )
95     private ArchivaConfiguration configuration;
96
97     @Inject
98     RepositoryRegistry repositoryRegistry;
99
100     private WagonFactory wagonFactory;
101
102     ManagedRepositoryConfiguration testRepo;
103
104     Configuration c;
105
106     @Before
107     @Override
108     public void setUp()
109         throws Exception
110     {
111         super.setUp();
112
113         c = new Configuration();
114         testRepo = new ManagedRepositoryConfiguration();
115         testRepo.setId( TEST_REPO_ID );
116         testRepo.setLocation( Paths.get( "target/test-repository" ).toAbsolutePath().toString() );
117         testRepo.setReleases( true );
118         testRepo.setSnapshots( true );
119         c.addManagedRepository( testRepo );
120
121         RemoteRepositoryConfiguration testRemoteRepo = new RemoteRepositoryConfiguration();
122         testRemoteRepo.setId( TEST_REMOTE_REPO_ID );
123         testRemoteRepo.setLayout( "default" );
124         testRemoteRepo.setName( "Central Repository" );
125         testRemoteRepo.setUrl( "http://central.repo.com/maven2" );
126         testRemoteRepo.setTimeout( 10 );
127         c.addRemoteRepository( testRemoteRepo );
128
129         ProxyConnectorConfiguration proxyConnector = new ProxyConnectorConfiguration();
130         proxyConnector.setSourceRepoId( TEST_REPO_ID );
131         proxyConnector.setTargetRepoId( TEST_REMOTE_REPO_ID );
132         proxyConnector.setDisabled( false );
133         c.addProxyConnector( proxyConnector );
134
135         configuration.save( c );
136
137         repositoryRegistry.reload();
138
139         assertTrue( c.getManagedRepositories().get( 0 ).isSnapshots() );
140         assertTrue( c.getManagedRepositories().get( 0 ).isReleases() );
141
142         wagonFactory = mock( WagonFactory.class );
143
144         storage.setWagonFactory( wagonFactory );
145
146         Wagon wagon = new MockWagon();
147         when( wagonFactory.getWagon(
148             new WagonFactoryRequest( "wagon#http", new HashMap<String, String>() ) ) ).thenReturn( wagon );
149     }
150
151     // Tests for MRM-1411 - START
152     @Test
153     public void testGetProjectVersionMetadataWithParentSuccessful()
154         throws Exception
155     {
156         copyTestArtifactWithParent( "target/test-classes/com/example/test/test-artifact-module-a",
157                                     "target/test-repository/com/example/test/test-artifact-module-a" );
158         copyTestArtifactWithParent( "src/test/resources/com/example/test/test-artifact-parent",
159                 "target/test-repository/com/example/test/test-artifact-parent" );
160
161         copyTestArtifactWithParent( "target/test-classes/com/example/test/test-artifact-root",
162                 "target/test-repository/com/example/test/test-artifact-root" );
163
164         ProjectVersionMetadata metadata = storage.readProjectVersionMetadata(
165             new ReadMetadataRequest( TEST_REPO_ID, "com.example.test", "test-artifact-module-a", "1.0" ) );
166
167         MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet( MavenProjectFacet.FACET_ID );
168         assertEquals( "jar", facet.getPackaging() );
169         assertEquals( "http://maven.apache.org", metadata.getUrl() );
170         assertEquals( "com.example.test", facet.getParent().getGroupId() );
171         assertEquals( "test-artifact-root", facet.getParent().getArtifactId() );
172         assertEquals( "1.0", facet.getParent().getVersion() );
173         assertEquals( "test-artifact-module-a", facet.getArtifactId() );
174         assertEquals( "com.example.test", facet.getGroupId() );
175         assertNull( metadata.getCiManagement() );
176         assertNotNull( metadata.getDescription() );
177
178         checkApacheLicense( metadata );
179
180         assertEquals( "1.0", metadata.getId() );
181         assertEquals( "Test Artifact :: Module A", metadata.getName() );
182         String path = "test-artifact/trunk/test-artifact-module-a";
183         assertEquals( TEST_SCM_CONN_BASE + path, metadata.getScm().getConnection() );
184         assertEquals( TEST_SCM_DEV_CONN_BASE + path, metadata.getScm().getDeveloperConnection() );
185         assertEquals( TEST_SCM_URL_BASE + path, metadata.getScm().getUrl() );
186
187         List<Dependency> dependencies = metadata.getDependencies();
188         assertEquals( 2, dependencies.size() );
189         assertDependency( dependencies.get( 0 ), "commons-io", "commons-io", "1.4" );
190         assertDependency( dependencies.get( 1 ), "junit", "junit", "3.8.1", "test" );
191
192         List<String> paths = new ArrayList<>();
193         paths.add( "target/test-repository/com/example/test/test-artifact-module-a" );
194         paths.add( "target/test-repository/com/example/test/test-artifact-parent" );
195         paths.add( "target/test-repository/com/example/test/test-artifact-root" );
196
197         deleteTestArtifactWithParent( paths );
198     }
199
200     @Test
201     public void testGetProjectVersionMetadataWithParentNoRemoteReposConfigured()
202         throws Exception
203     {
204         // remove configuration
205         Configuration config = configuration.getConfiguration();
206         RemoteRepositoryConfiguration remoteRepo = config.findRemoteRepositoryById( TEST_REMOTE_REPO_ID );
207         config.removeRemoteRepository( remoteRepo );
208
209         configuration.save( config );
210
211         copyTestArtifactWithParent( "target/test-classes/com/example/test/test-artifact-module-a",
212                                     "target/test-repository/com/example/test/test-artifact-module-a" );
213
214         ProjectVersionMetadata metadata = storage.readProjectVersionMetadata(
215             new ReadMetadataRequest( TEST_REPO_ID, "com.example.test", "test-artifact-module-a", "1.0" ) );
216         assertEquals( "1.0", metadata.getId() );
217
218         MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet( MavenProjectFacet.FACET_ID );
219         assertNotNull( facet );
220         assertEquals( "com.example.test", facet.getGroupId() );
221         assertEquals( "test-artifact-module-a", facet.getArtifactId() );
222         assertEquals( "jar", facet.getPackaging() );
223
224         List<String> paths = new ArrayList<>();
225         paths.add( "target/test-repository/com/example/test/test-artifact-module-a" );
226         paths.add( "target/test-repository/com/example/test/test-artifact-parent" );
227         paths.add( "target/test-repository/com/example/test/test-artifact-root" );
228
229         deleteTestArtifactWithParent( paths );
230     }
231
232     @Test
233     public void testGetProjectVersionMetadataWithParentNotInAnyRemoteRepo()
234         throws Exception
235     {
236         copyTestArtifactWithParent( "target/test-classes/com/example/test/test-artifact-module-a",
237                                     "target/test-repository/com/example/test/test-artifact-module-a" );
238
239         ProjectVersionMetadata metadata = storage.readProjectVersionMetadata(
240             new ReadMetadataRequest( TEST_REPO_ID, "com.example.test", "missing-parent", "1.1" ) );
241
242         assertEquals( "1.1", metadata.getId() );
243
244         MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet( MavenProjectFacet.FACET_ID );
245         assertNotNull( facet );
246         assertEquals( "com.example.test", facet.getGroupId() );
247         assertEquals( "missing-parent", facet.getArtifactId() );
248         assertEquals( "jar", facet.getPackaging() );
249
250         List<String> paths = new ArrayList<>();
251         paths.add( "target/test-repository/com/example/test/test-artifact-module-a" );
252         paths.add( "target/test-repository/com/example/test/test-artifact-parent" );
253         paths.add( "target/test-repository/com/example/test/test-artifact-root" );
254
255         deleteTestArtifactWithParent( paths );
256     }
257
258     @Test
259     public void testGetProjectVersionMetadataWithParentSnapshotVersion()
260         throws Exception
261     {
262
263         copyTestArtifactWithParent( "target/test-classes/com/example/test/test-snapshot-artifact-module-a",
264                                     "target/test-repository/com/example/test/test-snapshot-artifact-module-a" );
265
266         copyTestArtifactWithParent( "src/test/resources/com/example/test/test-artifact-parent",
267                 "target/test-repository/com/example/test/test-artifact-parent" );
268
269         copyTestArtifactWithParent( "target/test-classes/com/example/test/test-snapshot-artifact-root",
270                                     "target/test-repository/com/example/test/test-snapshot-artifact-root" );
271
272         ProjectVersionMetadata metadata = storage.readProjectVersionMetadata(
273             new ReadMetadataRequest( TEST_REPO_ID, "com.example.test", "test-snapshot-artifact-module-a",
274                                      "1.1-SNAPSHOT" ) );
275
276         MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet( MavenProjectFacet.FACET_ID );
277         assertEquals( "jar", facet.getPackaging() );
278         assertEquals( "com.example.test", facet.getParent().getGroupId() );
279         assertEquals( "test-snapshot-artifact-root", facet.getParent().getArtifactId() );
280         assertEquals( "1.1-SNAPSHOT", facet.getParent().getVersion() );
281         assertEquals( "test-snapshot-artifact-module-a", facet.getArtifactId() );
282         assertEquals( "com.example.test", facet.getGroupId() );
283         assertNull( metadata.getCiManagement() );
284         assertNotNull( metadata.getDescription() );
285
286         checkApacheLicense( metadata );
287
288         assertEquals( "1.1-SNAPSHOT", metadata.getId() );
289         assertEquals( "Test Snapshot Artifact :: Module A", metadata.getName() );
290         String path = "test-snapshot-artifact/trunk/test-snapshot-artifact-module-a";
291         assertEquals( TEST_SCM_CONN_BASE + path, metadata.getScm().getConnection() );
292         assertEquals( TEST_SCM_DEV_CONN_BASE + path, metadata.getScm().getDeveloperConnection() );
293         assertEquals( TEST_SCM_URL_BASE + path, metadata.getScm().getUrl() );
294
295         List<Dependency> dependencies = metadata.getDependencies();
296         assertEquals( 2, dependencies.size() );
297         assertDependency( dependencies.get( 0 ), "commons-io", "commons-io", "1.4" );
298         assertDependency( dependencies.get( 1 ), "junit", "junit", "3.8.1", "test" );
299
300         List<String> paths = new ArrayList<>();
301         paths.add( "target/test-repository/com/example/test/test-snapshot-artifact-module-a" );
302         paths.add( "target/test-repository/com/example/test/test-snapshot-artifact-root" );
303
304         deleteTestArtifactWithParent( paths );
305     }
306
307     @Test
308     public void testGetProjectVersionMetadataWithParentSnapshotVersionAndSnapNotAllowed()
309         throws Exception
310     {
311         testRepo.setSnapshots( false );
312         configuration.save( c );
313         repositoryRegistry.reload();
314         assertFalse( c.getManagedRepositories().get( 0 ).isSnapshots() );
315         copyTestArtifactWithParent( "target/test-classes/com/example/test/test-snapshot-artifact-module-a",
316                                     "target/test-repository/com/example/test/test-snapshot-artifact-module-a" );
317
318         try
319         {
320             ProjectVersionMetadata metadata = storage.readProjectVersionMetadata(
321                 new ReadMetadataRequest( TEST_REPO_ID, "com.example.test", "test-snapshot-artifact-module-a",
322                                          "1.1-SNAPSHOT" ) );
323             fail( "Should not be found" );
324         }
325         catch ( RepositoryStorageRuntimeException e )
326         {
327         }
328
329         List<String> paths = new ArrayList<>();
330         paths.add( "target/test-repository/com/example/test/test-snapshot-artifact-module-a" );
331         paths.add( "target/test-repository/com/example/test/test-snapshot-artifact-root" );
332
333         deleteTestArtifactWithParent( paths );
334     }
335     // Tests for MRM-1411 - END
336
337     private void assertDependency( Dependency dependency, String groupId, String artifactId, String version )
338     {
339         assertDependency( dependency, groupId, artifactId, version, "compile" );
340     }
341
342     private void assertDependency( Dependency dependency, String groupId, String artifactId, String version,
343                                    String scope )
344     {
345         assertEquals( artifactId, dependency.getArtifactId() );
346         assertEquals( "jar", dependency.getType() );
347         assertEquals( version, dependency.getVersion() );
348         assertEquals( groupId, dependency.getGroupId() );
349         assertEquals( scope, dependency.getScope() );
350         assertNull( dependency.getClassifier() );
351         assertNull( dependency.getSystemPath() );
352     }
353
354     private void assertArtifact( ArtifactMetadata artifact, String id, int size, String sha1, String md5 )
355     {
356         assertEquals( id, artifact.getId() );
357         assertEquals( md5, artifact.getMd5() );
358         assertEquals( sha1, artifact.getSha1() );
359         assertEquals( size, artifact.getSize() );
360         assertEquals( "org.codehaus.plexus", artifact.getNamespace() );
361         assertEquals( "plexus-spring", artifact.getProject() );
362         assertEquals( "1.2", artifact.getVersion() );
363         assertEquals( TEST_REPO_ID, artifact.getRepositoryId() );
364     }
365
366     private void assertMailingList( MailingList mailingList, String name, String archive, String post, String subscribe,
367                                     String unsubscribe, List<String> otherArchives, boolean allowPost )
368     {
369         assertEquals( archive, mailingList.getMainArchiveUrl() );
370         if ( allowPost )
371         {
372             assertEquals( post, mailingList.getPostAddress() );
373         }
374         else
375         {
376             assertNull( mailingList.getPostAddress() );
377         }
378         assertEquals( subscribe, mailingList.getSubscribeAddress() );
379         assertEquals( unsubscribe, mailingList.getUnsubscribeAddress() );
380         assertEquals( name, mailingList.getName() );
381         assertEquals( otherArchives, mailingList.getOtherArchives() );
382     }
383
384     private void assertMailingList( String prefix, MailingList mailingList, String name, boolean allowPost,
385                                     String nabbleUrl )
386     {
387         List<String> otherArchives = new ArrayList<>();
388         otherArchives.add( "http://www.mail-archive.com/" + prefix + "@archiva.apache.org" );
389         if ( nabbleUrl != null )
390         {
391             otherArchives.add( nabbleUrl );
392         }
393         otherArchives.add( "http://markmail.org/list/org.apache.archiva." + prefix );
394         assertMailingList( mailingList, name, "http://mail-archives.apache.org/mod_mbox/archiva-" + prefix + "/",
395                            prefix + "@archiva.apache.org", prefix + "-subscribe@archiva.apache.org",
396                            prefix + "-unsubscribe@archiva.apache.org", otherArchives, allowPost );
397     }
398
399     private void checkApacheLicense( ProjectVersionMetadata metadata )
400     {
401         assertEquals( Arrays.asList( new License( "The Apache Software License, Version 2.0",
402                                                   "http://www.apache.org/licenses/LICENSE-2.0.txt" ) ),
403                       metadata.getLicenses() );
404     }
405
406     private void checkOrganizationApache( ProjectVersionMetadata metadata )
407     {
408         assertEquals( "The Apache Software Foundation", metadata.getOrganization().getName() );
409         assertEquals( "http://www.apache.org/", metadata.getOrganization().getUrl() );
410     }
411
412     private void deleteTestArtifactWithParent( List<String> pathsToBeDeleted )
413         throws IOException
414     {
415         for ( String path : pathsToBeDeleted )
416         {
417             Path dir = Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), path );
418             org.apache.archiva.common.utils.FileUtils.deleteDirectory( dir );
419
420             assertFalse(Files.exists( dir) );
421         }
422         Path dest = Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/test-repository/com/example/test/test-artifact-module-a" );
423         Path parentPom =
424             Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/test-repository/com/example/test/test-artifact-parent" );
425         Path rootPom = Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/test-repository/com/example/test/test-artifact-root" );
426
427         org.apache.archiva.common.utils.FileUtils.deleteDirectory( dest );
428         org.apache.archiva.common.utils.FileUtils.deleteDirectory( parentPom );
429         org.apache.archiva.common.utils.FileUtils.deleteDirectory( rootPom );
430
431         assertFalse( Files.exists(dest) );
432         assertFalse( Files.exists(parentPom) );
433         assertFalse( Files.exists(rootPom) );
434     }
435
436     private Path copyTestArtifactWithParent( String srcPath, String destPath )
437         throws IOException
438     {
439         Path src = Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), srcPath );
440         Path dest = Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), destPath );
441
442         FileUtils.copyDirectory( src.toFile(), dest.toFile() );
443         assertTrue( Files.exists(dest) );
444         return dest;
445     }
446
447 }