]> source.dussan.org Git - archiva.git/blob
712aa45c2c0ec7ddf65da11a986d098963be25fc
[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.metadata.model.ArtifactMetadata;
24 import org.apache.archiva.metadata.model.Dependency;
25 import org.apache.archiva.metadata.model.License;
26 import org.apache.archiva.metadata.model.MailingList;
27 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
28 import org.apache.archiva.metadata.repository.filter.AllFilter;
29 import org.apache.archiva.metadata.repository.filter.ExcludesFilter;
30 import org.apache.archiva.metadata.repository.filter.Filter;
31 import org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataInvalidException;
32 import org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataNotFoundException;
33 import org.apache.archiva.proxy.common.WagonFactory;
34 import org.apache.commons.io.FileUtils;
35 import org.apache.archiva.common.utils.FileUtil;
36 import org.apache.archiva.configuration.ArchivaConfiguration;
37 import org.apache.archiva.configuration.Configuration;
38 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
39 import org.apache.archiva.configuration.ProxyConnectorConfiguration;
40 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
41 import org.apache.maven.wagon.Wagon;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.springframework.test.context.ContextConfiguration;
46 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
47
48 import static org.mockito.Mockito.mock;
49 import static org.mockito.Mockito.when;
50
51 import javax.inject.Inject;
52 import javax.inject.Named;
53 import java.io.File;
54 import java.io.IOException;
55 import java.util.ArrayList;
56 import java.util.Arrays;
57 import java.util.Collection;
58 import java.util.Collections;
59 import java.util.Comparator;
60 import java.util.List;
61
62 @RunWith( SpringJUnit4ClassRunner.class )
63 @ContextConfiguration( locations = {"classpath*:/META-INF/spring-context.xml","classpath:/spring-context.xml"} )
64 public class Maven2RepositoryMetadataResolverTest
65     extends TestCase
66 {
67     private static final Filter<String> ALL = new AllFilter<String>();
68
69     @Inject
70     @Named(value = "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     private ArchivaConfiguration configuration;
95
96     private WagonFactory wagonFactory;
97
98     @Before
99     public void setUp()
100         throws Exception
101     {
102         super.setUp();
103
104         Configuration c = new Configuration();
105         ManagedRepositoryConfiguration testRepo = new ManagedRepositoryConfiguration();
106         testRepo.setId( TEST_REPO_ID );
107         testRepo.setLocation( new File( "target/test-repository" ).getAbsolutePath() );
108         c.addManagedRepository( testRepo );
109
110         RemoteRepositoryConfiguration testRemoteRepo = new RemoteRepositoryConfiguration();
111         testRemoteRepo.setId( TEST_REMOTE_REPO_ID );
112         testRemoteRepo.setLayout( "default" );
113         testRemoteRepo.setName( "Central Repository" );
114         testRemoteRepo.setUrl( "http://central.repo.com/maven2" );
115         testRemoteRepo.setTimeout( 10 );
116         c.addRemoteRepository( testRemoteRepo );
117
118         ProxyConnectorConfiguration proxyConnector = new ProxyConnectorConfiguration();
119         proxyConnector.setSourceRepoId( TEST_REPO_ID );
120         proxyConnector.setTargetRepoId( TEST_REMOTE_REPO_ID );
121         proxyConnector.setDisabled( false );
122         c.addProxyConnector( proxyConnector );
123
124         configuration.save( c );
125
126         wagonFactory = mock( WagonFactory.class );
127
128         storage.setWagonFactory( wagonFactory );
129
130         Wagon wagon = new MockWagon();
131         when( wagonFactory.getWagon( "wagon#http" ) ).thenReturn( wagon );
132     }
133
134     @Test
135     public void testGetProjectVersionMetadata()
136         throws Exception
137     {
138         ProjectVersionMetadata metadata = storage.readProjectVersionMetadata( TEST_REPO_ID, "org.apache.archiva",
139                                                                               "archiva-common", "1.2.1" );
140         MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet( MavenProjectFacet.FACET_ID );
141         assertEquals( "jar", facet.getPackaging() );
142         assertEquals( "http://archiva.apache.org/ref/1.2.1/archiva-base/archiva-common", metadata.getUrl() );
143         assertEquals( "org.apache.archiva", facet.getParent().getGroupId() );
144         assertEquals( "archiva-base", facet.getParent().getArtifactId() );
145         assertEquals( "1.2.1", facet.getParent().getVersion() );
146         assertEquals( "archiva-common", facet.getArtifactId() );
147         assertEquals( "org.apache.archiva", facet.getGroupId() );
148         assertEquals( "continuum", metadata.getCiManagement().getSystem() );
149         assertEquals( "http://vmbuild.apache.org/continuum", metadata.getCiManagement().getUrl() );
150         assertNotNull( metadata.getDescription() );
151         // TODO: this would be better
152 //        assertEquals(
153 //            "Archiva is an application for managing one or more remote repositories, including administration, artifact handling, browsing and searching.",
154 //            metadata.getDescription() );
155         assertEquals( "1.2.1", metadata.getId() );
156         assertEquals( "jira", metadata.getIssueManagement().getSystem() );
157         assertEquals( "http://jira.codehaus.org/browse/MRM", metadata.getIssueManagement().getUrl() );
158         checkApacheLicense( metadata );
159         assertEquals( "Archiva Base :: Common", metadata.getName() );
160         String path = "archiva/tags/archiva-1.2.1/archiva-modules/archiva-base/archiva-common";
161         assertEquals( ASF_SCM_CONN_BASE + path, metadata.getScm().getConnection() );
162         assertEquals( ASF_SCM_DEV_CONN_BASE + path, metadata.getScm().getDeveloperConnection() );
163         assertEquals( ASF_SCM_VIEWVC_BASE + path, metadata.getScm().getUrl() );
164         checkOrganizationApache( metadata );
165
166         assertEquals( 4, metadata.getMailingLists().size() );
167         assertMailingList( "users", metadata.getMailingLists().get( 0 ), "Archiva User List", true,
168                            "http://www.nabble.com/archiva-users-f16426.html" );
169         assertMailingList( "dev", metadata.getMailingLists().get( 1 ), "Archiva Developer List", true,
170                            "http://www.nabble.com/archiva-dev-f16427.html" );
171         assertMailingList( "commits", metadata.getMailingLists().get( 2 ), "Archiva Commits List", false, null );
172         assertMailingList( "issues", metadata.getMailingLists().get( 3 ), "Archiva Issues List", false,
173                            "http://www.nabble.com/Archiva---Issues-f29617.html" );
174
175         List<Dependency> dependencies = metadata.getDependencies();
176         assertEquals( 10, dependencies.size() );
177         assertDependency( dependencies.get( 0 ), "commons-lang", "commons-lang", "2.2" );
178         assertDependency( dependencies.get( 1 ), "commons-io", "commons-io", "1.4" );
179         assertDependency( dependencies.get( 2 ), "org.slf4j", "slf4j-api", "1.5.0" );
180         assertDependency( dependencies.get( 3 ), "org.codehaus.plexus", "plexus-component-api", "1.0-alpha-22" );
181         assertDependency( dependencies.get( 4 ), "org.codehaus.plexus", "plexus-spring", "1.2", "test" );
182         assertDependency( dependencies.get( 5 ), "xalan", "xalan", "2.7.0" );
183         assertDependency( dependencies.get( 6 ), "dom4j", "dom4j", "1.6.1", "test" );
184         assertDependency( dependencies.get( 7 ), "junit", "junit", "3.8.1", "test" );
185         assertDependency( dependencies.get( 8 ), "easymock", "easymock", "1.2_Java1.3", "test" );
186         assertDependency( dependencies.get( 9 ), "easymock", "easymockclassextension", "1.2", "test" );
187     }
188
189     @Test
190     public void testGetArtifactMetadata()
191         throws Exception
192     {
193         Collection<ArtifactMetadata> springArtifacts = storage.readArtifactsMetadata( TEST_REPO_ID,
194                                                                                       "org.codehaus.plexus",
195                                                                                       "plexus-spring", "1.2", ALL );
196         List<ArtifactMetadata> artifacts = new ArrayList<ArtifactMetadata>( springArtifacts );
197         Collections.sort( artifacts, new Comparator<ArtifactMetadata>()
198         {
199             public int compare( ArtifactMetadata o1, ArtifactMetadata o2 )
200             {
201                 return o1.getId().compareTo( o2.getId() );
202             }
203         } );
204
205         assertEquals( 3, artifacts.size() );
206
207         ArtifactMetadata artifactMetadata = artifacts.get( 0 );
208         assertEquals( "plexus-spring-1.2-sources.jar", artifactMetadata.getId() );
209         MavenArtifactFacet facet = (MavenArtifactFacet) artifactMetadata.getFacet( MavenArtifactFacet.FACET_ID );
210         assertEquals( 0, facet.getBuildNumber() );
211         assertNull( facet.getTimestamp() );
212         assertEquals( "sources", facet.getClassifier() );
213         assertEquals( "java-source", facet.getType() );
214
215         artifactMetadata = artifacts.get( 1 );
216         assertEquals( "plexus-spring-1.2.jar", artifactMetadata.getId() );
217         facet = (MavenArtifactFacet) artifactMetadata.getFacet( MavenArtifactFacet.FACET_ID );
218         assertEquals( 0, facet.getBuildNumber() );
219         assertNull( facet.getTimestamp() );
220         assertNull( facet.getClassifier() );
221         assertEquals( "jar", facet.getType() );
222
223         artifactMetadata = artifacts.get( 2 );
224         assertEquals( "plexus-spring-1.2.pom", artifactMetadata.getId() );
225         facet = (MavenArtifactFacet) artifactMetadata.getFacet( MavenArtifactFacet.FACET_ID );
226         assertEquals( 0, facet.getBuildNumber() );
227         assertNull( facet.getTimestamp() );
228         assertNull( facet.getClassifier() );
229         assertEquals( "pom", facet.getType() );
230     }
231
232     @Test
233     public void testGetArtifactMetadataSnapshots()
234         throws Exception
235     {
236         Collection<ArtifactMetadata> testArtifacts = storage.readArtifactsMetadata( TEST_REPO_ID, "com.example.test",
237                                                                                     "test-artifact", "1.0-SNAPSHOT",
238                                                                                     ALL );
239         List<ArtifactMetadata> artifacts = new ArrayList<ArtifactMetadata>( testArtifacts );
240         Collections.sort( artifacts, new Comparator<ArtifactMetadata>()
241         {
242             public int compare( ArtifactMetadata o1, ArtifactMetadata o2 )
243             {
244                 return o1.getId().compareTo( o2.getId() );
245             }
246         } );
247
248         assertEquals( 6, artifacts.size() );
249
250         ArtifactMetadata artifactMetadata = artifacts.get( 0 );
251         assertEquals( "test-artifact-1.0-20100308.230825-1.jar", artifactMetadata.getId() );
252         MavenArtifactFacet facet = (MavenArtifactFacet) artifactMetadata.getFacet( MavenArtifactFacet.FACET_ID );
253         assertEquals( 1, facet.getBuildNumber() );
254         assertEquals( "20100308.230825", facet.getTimestamp() );
255         assertNull( facet.getClassifier() );
256         assertEquals( "jar", facet.getType() );
257
258         artifactMetadata = artifacts.get( 1 );
259         assertEquals( "test-artifact-1.0-20100308.230825-1.pom", artifactMetadata.getId() );
260         facet = (MavenArtifactFacet) artifactMetadata.getFacet( MavenArtifactFacet.FACET_ID );
261         assertEquals( 1, facet.getBuildNumber() );
262         assertEquals( "20100308.230825", facet.getTimestamp() );
263         assertNull( facet.getClassifier() );
264         assertEquals( "pom", facet.getType() );
265
266         artifactMetadata = artifacts.get( 2 );
267         assertEquals( "test-artifact-1.0-20100310.014828-2-javadoc.jar", artifactMetadata.getId() );
268         facet = (MavenArtifactFacet) artifactMetadata.getFacet( MavenArtifactFacet.FACET_ID );
269         assertEquals( 2, facet.getBuildNumber() );
270         assertEquals( "20100310.014828", facet.getTimestamp() );
271         assertEquals( "javadoc", facet.getClassifier() );
272         assertEquals( "javadoc", facet.getType() );
273
274         artifactMetadata = artifacts.get( 3 );
275         assertEquals( "test-artifact-1.0-20100310.014828-2-sources.jar", artifactMetadata.getId() );
276         facet = (MavenArtifactFacet) artifactMetadata.getFacet( MavenArtifactFacet.FACET_ID );
277         assertEquals( 2, facet.getBuildNumber() );
278         assertEquals( "20100310.014828", facet.getTimestamp() );
279         assertEquals( "sources", facet.getClassifier() );
280         assertEquals( "java-source", facet.getType() );
281
282         artifactMetadata = artifacts.get( 4 );
283         assertEquals( "test-artifact-1.0-20100310.014828-2.jar", artifactMetadata.getId() );
284         facet = (MavenArtifactFacet) artifactMetadata.getFacet( MavenArtifactFacet.FACET_ID );
285         assertEquals( 2, facet.getBuildNumber() );
286         assertEquals( "20100310.014828", facet.getTimestamp() );
287         assertNull( facet.getClassifier() );
288         assertEquals( "jar", facet.getType() );
289
290         artifactMetadata = artifacts.get( 5 );
291         assertEquals( "test-artifact-1.0-20100310.014828-2.pom", artifactMetadata.getId() );
292         facet = (MavenArtifactFacet) artifactMetadata.getFacet( MavenArtifactFacet.FACET_ID );
293         assertEquals( 2, facet.getBuildNumber() );
294         assertEquals( "20100310.014828", facet.getTimestamp() );
295         assertNull( facet.getClassifier() );
296         assertEquals( "pom", facet.getType() );
297     }
298
299     private void assertDependency( Dependency dependency, String groupId, String artifactId, String version )
300     {
301         assertDependency( dependency, groupId, artifactId, version, "compile" );
302     }
303
304     private void assertDependency( Dependency dependency, String groupId, String artifactId, String version,
305                                    String scope )
306     {
307         assertEquals( artifactId, dependency.getArtifactId() );
308         assertEquals( "jar", dependency.getType() );
309         assertEquals( version, dependency.getVersion() );
310         assertEquals( groupId, dependency.getGroupId() );
311         assertEquals( scope, dependency.getScope() );
312         assertNull( dependency.getClassifier() );
313         assertNull( dependency.getSystemPath() );
314     }
315
316     @Test
317     public void testGetProjectVersionMetadataForTimestampedSnapshot()
318         throws Exception
319     {
320         ProjectVersionMetadata metadata = storage.readProjectVersionMetadata( TEST_REPO_ID, "org.apache", "apache",
321                                                                               "5-SNAPSHOT" );
322         MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet( MavenProjectFacet.FACET_ID );
323         assertEquals( "pom", facet.getPackaging() );
324         assertEquals( "http://www.apache.org/", metadata.getUrl() );
325         assertNull( facet.getParent() );
326         assertEquals( "org.apache", facet.getGroupId() );
327         assertEquals( "apache", facet.getArtifactId() );
328         assertNull( metadata.getCiManagement() );
329         assertNotNull( metadata.getDescription() );
330         // TODO: this would be better
331 //        assertEquals(
332 //            "The Apache Software Foundation provides support for the Apache community of open-source software projects. " +
333 //                "The Apache projects are characterized by a collaborative, consensus based development process, an open " +
334 //                "and pragmatic software license, and a desire to create high quality software that leads the way in its " +
335 //                "field. We consider ourselves not simply a group of projects sharing a server, but rather a community of " +
336 //                "developers and users.", metadata.getDescription() );
337         assertEquals( "5-SNAPSHOT", metadata.getId() );
338         assertNull( metadata.getIssueManagement() );
339         checkApacheLicense( metadata );
340         assertEquals( "The Apache Software Foundation", metadata.getName() );
341         String path = "maven/pom/trunk/asf";
342         assertEquals( ASF_SCM_CONN_BASE + path, metadata.getScm().getConnection() );
343         assertEquals( ASF_SCM_DEV_CONN_BASE + path, metadata.getScm().getDeveloperConnection() );
344         assertEquals( ASF_SCM_VIEWVC_BASE + path, metadata.getScm().getUrl() );
345         checkOrganizationApache( metadata );
346         assertEquals( 1, metadata.getMailingLists().size() );
347         assertMailingList( metadata.getMailingLists().get( 0 ), "Apache Announce List",
348                            "http://mail-archives.apache.org/mod_mbox/www-announce/", "announce@apache.org",
349                            "announce-subscribe@apache.org", "announce-unsubscribe@apache.org",
350                            Collections.<String>emptyList(), true );
351         assertEquals( Collections.<Dependency>emptyList(), metadata.getDependencies() );
352     }
353
354     @Test
355     public void testGetProjectVersionMetadataForTimestampedSnapshotMissingMetadata()
356         throws Exception
357     {
358         try
359         {
360             storage.readProjectVersionMetadata( TEST_REPO_ID, "com.example.test", "missing-metadata", "1.0-SNAPSHOT" );
361             fail( "Should not be found" );
362         }
363         catch ( RepositoryStorageMetadataNotFoundException e )
364         {
365             assertEquals( "missing-pom", e.getId() );
366         }
367     }
368
369     @Test
370     public void testGetProjectVersionMetadataForTimestampedSnapshotMalformedMetadata()
371         throws Exception
372     {
373         try
374         {
375             storage.readProjectVersionMetadata( TEST_REPO_ID, "com.example.test", "malformed-metadata",
376                                                 "1.0-SNAPSHOT" );
377             fail( "Should not be found" );
378         }
379         catch ( RepositoryStorageMetadataNotFoundException e )
380         {
381             assertEquals( "missing-pom", e.getId() );
382         }
383     }
384
385     @Test
386     public void testGetProjectVersionMetadataForTimestampedSnapshotIncompleteMetadata()
387         throws Exception
388     {
389         try
390         {
391             storage.readProjectVersionMetadata( TEST_REPO_ID, "com.example.test", "incomplete-metadata",
392                                                 "1.0-SNAPSHOT" );
393             fail( "Should not be found" );
394         }
395         catch ( RepositoryStorageMetadataNotFoundException e )
396         {
397             assertEquals( "missing-pom", e.getId() );
398         }
399     }
400
401     @Test
402     public void testGetProjectVersionMetadataForInvalidPom()
403         throws Exception
404     {
405         try
406         {
407             storage.readProjectVersionMetadata( TEST_REPO_ID, "com.example.test", "invalid-pom", "1.0" );
408             fail( "Should have received an exception due to invalid POM" );
409         }
410         catch ( RepositoryStorageMetadataInvalidException e )
411         {
412             assertEquals( "invalid-pom", e.getId() );
413         }
414     }
415
416     @Test
417     public void testGetProjectVersionMetadataForMislocatedPom()
418         throws Exception
419     {
420         try
421         {
422             storage.readProjectVersionMetadata( TEST_REPO_ID, "com.example.test", "mislocated-pom", "1.0" );
423             fail( "Should have received an exception due to mislocated POM" );
424         }
425         catch ( RepositoryStorageMetadataInvalidException e )
426         {
427             assertEquals( "mislocated-pom", e.getId() );
428         }
429     }
430
431     @Test
432     public void testGetProjectVersionMetadataForMissingPom()
433         throws Exception
434     {
435         try
436         {
437             storage.readProjectVersionMetadata( TEST_REPO_ID, "com.example.test", "missing-pom", "1.0" );
438             fail( "Should not be found" );
439         }
440         catch ( RepositoryStorageMetadataNotFoundException e )
441         {
442             assertEquals( "missing-pom", e.getId() );
443         }
444     }
445
446     // Tests for MRM-1411 - START
447     @Test
448     public void testGetProjectVersionMetadataWithParentSuccessful()
449         throws Exception
450     {
451         copyTestArtifactWithParent( "target/test-classes/com/example/test/test-artifact-module-a",
452                                     "target/test-repository/com/example/test/test-artifact-module-a" );
453
454         ProjectVersionMetadata metadata = storage.readProjectVersionMetadata( TEST_REPO_ID, "com.example.test",
455                                                                                "test-artifact-module-a", "1.0" );
456
457         MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet( MavenProjectFacet.FACET_ID );
458         assertEquals( "jar", facet.getPackaging() );
459         assertEquals( "http://maven.apache.org", metadata.getUrl() );
460         assertEquals( "com.example.test", facet.getParent().getGroupId() );
461         assertEquals( "test-artifact-root", facet.getParent().getArtifactId() );
462         assertEquals( "1.0", facet.getParent().getVersion() );
463         assertEquals( "test-artifact-module-a", facet.getArtifactId() );
464         assertEquals( "com.example.test", facet.getGroupId() );
465         assertNull( metadata.getCiManagement() );
466         assertNotNull( metadata.getDescription() );
467
468         checkApacheLicense( metadata );
469
470         assertEquals( "1.0", metadata.getId() );
471         assertEquals( "Test Artifact :: Module A", metadata.getName() );
472         String path = "test-artifact/trunk/test-artifact-module-a";
473         assertEquals( TEST_SCM_CONN_BASE + path, metadata.getScm().getConnection() );
474         assertEquals( TEST_SCM_DEV_CONN_BASE + path, metadata.getScm().getDeveloperConnection() );
475         assertEquals( TEST_SCM_URL_BASE + path, metadata.getScm().getUrl() );
476
477         List<Dependency> dependencies = metadata.getDependencies();
478         assertEquals( 2, dependencies.size() );
479         assertDependency( dependencies.get( 0 ), "commons-io", "commons-io", "1.4" );
480         assertDependency( dependencies.get( 1 ), "junit", "junit", "3.8.1", "test" );
481
482         List<String> paths = new ArrayList<String>();
483         paths.add( "target/test-repository/com/example/test/test-artifact-module-a" );
484         paths.add( "target/test-repository/com/example/test/test-artifact-parent" );
485         paths.add( "target/test-repository/com/example/test/test-artifact-root" );
486
487         deleteTestArtifactWithParent( paths );
488     }
489
490     @Test
491     public void testGetProjectVersionMetadataWithParentNoRemoteReposConfigured()
492          throws Exception
493     {
494         // remove configuration
495         Configuration config = configuration.getConfiguration();
496         RemoteRepositoryConfiguration remoteRepo = config.findRemoteRepositoryById( TEST_REMOTE_REPO_ID );
497         config.removeRemoteRepository( remoteRepo );
498
499         configuration.save( config );
500
501         copyTestArtifactWithParent( "target/test-classes/com/example/test/test-artifact-module-a",
502                                     "target/test-repository/com/example/test/test-artifact-module-a" );
503
504         ProjectVersionMetadata metadata =  storage.readProjectVersionMetadata( TEST_REPO_ID, "com.example.test",
505                                                                 "test-artifact-module-a", "1.0" );
506         assertEquals( "1.0", metadata.getId() );
507
508         MavenProjectFacet facet = ( MavenProjectFacet ) metadata.getFacet( MavenProjectFacet.FACET_ID );
509         assertNotNull( facet );
510         assertEquals( "com.example.test", facet.getGroupId() );
511         assertEquals( "test-artifact-module-a", facet.getArtifactId() );
512         assertEquals( "jar", facet.getPackaging() );
513
514         List<String> paths = new ArrayList<String>();
515         paths.add( "target/test-repository/com/example/test/test-artifact-module-a" );
516         paths.add( "target/test-repository/com/example/test/test-artifact-parent" );
517         paths.add( "target/test-repository/com/example/test/test-artifact-root" );
518
519         deleteTestArtifactWithParent( paths );
520     }
521
522     @Test
523     public void testGetProjectVersionMetadataWithParentNotInAnyRemoteRepo()
524          throws Exception
525     {
526         copyTestArtifactWithParent( "target/test-classes/com/example/test/test-artifact-module-a",
527                                     "target/test-repository/com/example/test/test-artifact-module-a" );
528
529         ProjectVersionMetadata metadata = storage.readProjectVersionMetadata( TEST_REPO_ID, "com.example.test", "missing-parent", "1.1" );
530
531         assertEquals( "1.1", metadata.getId() );
532
533         MavenProjectFacet facet = ( MavenProjectFacet ) metadata.getFacet( MavenProjectFacet.FACET_ID );
534         assertNotNull( facet );
535         assertEquals( "com.example.test", facet.getGroupId() );
536         assertEquals( "missing-parent", facet.getArtifactId() );
537         assertEquals( "jar", facet.getPackaging() );
538
539         List<String> paths = new ArrayList<String>();
540         paths.add( "target/test-repository/com/example/test/test-artifact-module-a" );
541         paths.add( "target/test-repository/com/example/test/test-artifact-parent" );
542         paths.add( "target/test-repository/com/example/test/test-artifact-root" );
543
544         deleteTestArtifactWithParent( paths );
545     }
546
547     @Test
548     public void testGetProjectVersionMetadataWithParentSnapshotVersion()
549         throws Exception
550     {
551         copyTestArtifactWithParent( "target/test-classes/com/example/test/test-snapshot-artifact-module-a",
552                                     "target/test-repository/com/example/test/test-snapshot-artifact-module-a" );
553
554         ProjectVersionMetadata metadata = storage.readProjectVersionMetadata( TEST_REPO_ID, "com.example.test",
555                                                                                "test-snapshot-artifact-module-a", "1.1-SNAPSHOT" );
556
557         MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet( MavenProjectFacet.FACET_ID );
558         assertEquals( "jar", facet.getPackaging() );
559         assertEquals( "com.example.test", facet.getParent().getGroupId() );
560         assertEquals( "test-snapshot-artifact-root", facet.getParent().getArtifactId() );
561         assertEquals( "1.1-SNAPSHOT", facet.getParent().getVersion() );
562         assertEquals( "test-snapshot-artifact-module-a", facet.getArtifactId() );
563         assertEquals( "com.example.test", facet.getGroupId() );
564         assertNull( metadata.getCiManagement() );
565         assertNotNull( metadata.getDescription() );
566
567         checkApacheLicense( metadata );
568
569         assertEquals( "1.1-SNAPSHOT", metadata.getId() );
570         assertEquals( "Test Snapshot Artifact :: Module A", metadata.getName() );
571         String path = "test-snapshot-artifact/trunk/test-snapshot-artifact-module-a";
572         assertEquals( TEST_SCM_CONN_BASE + path, metadata.getScm().getConnection() );
573         assertEquals( TEST_SCM_DEV_CONN_BASE + path, metadata.getScm().getDeveloperConnection() );
574         assertEquals( TEST_SCM_URL_BASE + path, metadata.getScm().getUrl() );
575
576         List<Dependency> dependencies = metadata.getDependencies();
577         assertEquals( 2, dependencies.size() );
578         assertDependency( dependencies.get( 0 ), "commons-io", "commons-io", "1.4" );
579         assertDependency( dependencies.get( 1 ), "junit", "junit", "3.8.1", "test" );
580
581         List<String> paths = new ArrayList<String>();
582         paths.add( "target/test-repository/com/example/test/test-snapshot-artifact-module-a" );
583         paths.add( "target/test-repository/com/example/test/test-snapshot-artifact-root" );
584
585         deleteTestArtifactWithParent( paths );
586     }
587
588     // Tests for MRM-1411 - END
589
590     @Test
591     public void testGetRootNamespaces()
592     {
593         assertEquals( Arrays.asList( "com", "org" ), storage.listRootNamespaces( TEST_REPO_ID, ALL ) );
594     }
595
596     @Test
597     public void testGetNamespaces()
598     {
599         assertEquals( Arrays.asList( "example" ), storage.listNamespaces( TEST_REPO_ID, "com", ALL ) );
600         assertEquals( Arrays.asList( "test" ), storage.listNamespaces( TEST_REPO_ID, "com.example", ALL ) );
601         assertEquals( Collections.<String>emptyList(), storage.listNamespaces( TEST_REPO_ID, "com.example.test",
602                                                                                ALL ) );
603
604         assertEquals( Arrays.asList( "apache", "codehaus" ), storage.listNamespaces( TEST_REPO_ID, "org", ALL ) );
605         assertEquals( Arrays.asList( "archiva", "maven" ), storage.listNamespaces( TEST_REPO_ID, "org.apache", ALL ) );
606         assertEquals( Collections.<String>emptyList(), storage.listNamespaces( TEST_REPO_ID, "org.apache.archiva",
607                                                                                ALL ) );
608         assertEquals( Arrays.asList( "plugins", "shared" ), storage.listNamespaces( TEST_REPO_ID, "org.apache.maven",
609                                                                                     ALL ) );
610         assertEquals( Collections.<String>emptyList(), storage.listNamespaces( TEST_REPO_ID, "org.apache.maven.plugins",
611                                                                                ALL ) );
612         assertEquals( Collections.<String>emptyList(), storage.listNamespaces( TEST_REPO_ID, "org.apache.maven.shared",
613                                                                                ALL ) );
614
615         assertEquals( Arrays.asList( "plexus" ), storage.listNamespaces( TEST_REPO_ID, "org.codehaus", ALL ) );
616         assertEquals( Collections.<String>emptyList(), storage.listNamespaces( TEST_REPO_ID, "org.codehaus.plexus",
617                                                                                ALL ) );
618     }
619
620     @Test
621     public void testGetProjects()
622     {
623         assertEquals( Collections.<String>emptyList(), storage.listProjects( TEST_REPO_ID, "com", ALL ) );
624         assertEquals( Collections.<String>emptyList(), storage.listProjects( TEST_REPO_ID, "com.example", ALL ) );
625         assertEquals( Arrays.asList( "incomplete-metadata", "invalid-pom", "malformed-metadata", "mislocated-pom",
626                                      "missing-metadata", "missing-parent", "test-artifact" ), storage.listProjects( TEST_REPO_ID,
627                                                                                                   "com.example.test",
628                                                                                                   ALL ) );
629
630         assertEquals( Collections.<String>emptyList(), storage.listProjects( TEST_REPO_ID, "org", ALL ) );
631         assertEquals( Arrays.asList( "apache" ), storage.listProjects( TEST_REPO_ID, "org.apache", ALL ) );
632         assertEquals( Arrays.asList( "archiva", "archiva-base", "archiva-common", "archiva-modules", "archiva-parent" ),
633                       storage.listProjects( TEST_REPO_ID, "org.apache.archiva", ALL ) );
634         assertEquals( Collections.<String>emptyList(), storage.listProjects( TEST_REPO_ID, "org.apache.maven", ALL ) );
635         assertEquals( Collections.<String>emptyList(), storage.listProjects( TEST_REPO_ID, "org.apache.maven.plugins",
636                                                                              ALL ) );
637         assertEquals( Arrays.asList( "maven-downloader" ), storage.listProjects( TEST_REPO_ID,
638                                                                                  "org.apache.maven.shared", ALL ) );
639     }
640
641     @Test
642     public void testGetProjectVersions()
643     {
644         assertEquals( Arrays.asList( "1.0-SNAPSHOT" ), storage.listProjectVersions( TEST_REPO_ID, "com.example.test",
645                                                                                     "incomplete-metadata", ALL ) );
646         assertEquals( Arrays.asList( "1.0-SNAPSHOT" ), storage.listProjectVersions( TEST_REPO_ID, "com.example.test",
647                                                                                     "malformed-metadata", ALL ) );
648         assertEquals( Arrays.asList( "1.0-SNAPSHOT" ), storage.listProjectVersions( TEST_REPO_ID, "com.example.test",
649                                                                                     "missing-metadata", ALL ) );
650         assertEquals( Arrays.asList( "1.0" ), storage.listProjectVersions( TEST_REPO_ID, "com.example.test",
651                                                                            "invalid-pom", ALL ) );
652
653         assertEquals( Arrays.asList( "4", "5-SNAPSHOT" ), storage.listProjectVersions( TEST_REPO_ID, "org.apache",
654                                                                                        "apache", ALL ) );
655
656         assertEquals( Arrays.asList( "1.2.1", "1.2.2" ), storage.listProjectVersions( TEST_REPO_ID,
657                                                                                       "org.apache.archiva", "archiva",
658                                                                                       ALL ) );
659         assertEquals( Arrays.asList( "1.2.1" ), storage.listProjectVersions( TEST_REPO_ID, "org.apache.archiva",
660                                                                              "archiva-base", ALL ) );
661         assertEquals( Arrays.asList( "1.2.1" ), storage.listProjectVersions( TEST_REPO_ID, "org.apache.archiva",
662                                                                              "archiva-common", ALL ) );
663         assertEquals( Arrays.asList( "1.2.1" ), storage.listProjectVersions( TEST_REPO_ID, "org.apache.archiva",
664                                                                              "archiva-modules", ALL ) );
665         assertEquals( Arrays.asList( "3" ), storage.listProjectVersions( TEST_REPO_ID, "org.apache.archiva",
666                                                                          "archiva-parent", ALL ) );
667
668         assertEquals( Collections.<String>emptyList(), storage.listProjectVersions( TEST_REPO_ID,
669                                                                                     "org.apache.maven.shared",
670                                                                                     "maven-downloader", ALL ) );
671     }
672
673     @Test
674     public void testGetArtifacts()
675     {
676         List<ArtifactMetadata> artifacts = new ArrayList<ArtifactMetadata>( storage.readArtifactsMetadata( TEST_REPO_ID,
677                                                                                                            "org.codehaus.plexus",
678                                                                                                            "plexus-spring",
679                                                                                                            "1.2",
680                                                                                                            ALL ) );
681         assertEquals( 3, artifacts.size() );
682         Collections.sort( artifacts, new Comparator<ArtifactMetadata>()
683         {
684             public int compare( ArtifactMetadata o1, ArtifactMetadata o2 )
685             {
686                 return o1.getId().compareTo( o2.getId() );
687             }
688         } );
689
690         assertArtifact( artifacts.get( 0 ), "plexus-spring-1.2-sources.jar", 0, EMPTY_SHA1, EMPTY_MD5 );
691         assertArtifact( artifacts.get( 1 ), "plexus-spring-1.2.jar", 0, EMPTY_SHA1, EMPTY_MD5 );
692         assertArtifact( artifacts.get( 2 ), "plexus-spring-1.2.pom", 7407, "96b14cf880e384b2d15e8193c57b65c5420ca4c5",
693                         "f83aa25f016212a551a4b2249985effc" );
694     }
695
696     @Test
697     public void testGetArtifactsFiltered()
698     {
699         ExcludesFilter<String> filter = new ExcludesFilter<String>( Collections.singletonList(
700             "plexus-spring-1.2.pom" ) );
701         List<ArtifactMetadata> artifacts = new ArrayList<ArtifactMetadata>( storage.readArtifactsMetadata( TEST_REPO_ID,
702                                                                                                            "org.codehaus.plexus",
703                                                                                                            "plexus-spring",
704                                                                                                            "1.2",
705                                                                                                            filter ) );
706         assertEquals( 2, artifacts.size() );
707         Collections.sort( artifacts, new Comparator<ArtifactMetadata>()
708         {
709             public int compare( ArtifactMetadata o1, ArtifactMetadata o2 )
710             {
711                 return o1.getId().compareTo( o2.getId() );
712             }
713         } );
714
715         assertArtifact( artifacts.get( 0 ), "plexus-spring-1.2-sources.jar", 0, EMPTY_SHA1, EMPTY_MD5 );
716         assertArtifact( artifacts.get( 1 ), "plexus-spring-1.2.jar", 0, EMPTY_SHA1, EMPTY_MD5 );
717     }
718
719     @Test
720     public void testGetArtifactsTimestampedSnapshots()
721     {
722         List<ArtifactMetadata> artifacts = new ArrayList<ArtifactMetadata>( storage.readArtifactsMetadata( TEST_REPO_ID,
723                                                                                                            "com.example.test",
724                                                                                                            "missing-metadata",
725                                                                                                            "1.0-SNAPSHOT",
726                                                                                                            ALL ) );
727         assertEquals( 1, artifacts.size() );
728
729         ArtifactMetadata artifact = artifacts.get( 0 );
730         assertEquals( "missing-metadata-1.0-20091101.112233-1.pom", artifact.getId() );
731         assertEquals( "com.example.test", artifact.getNamespace() );
732         assertEquals( "missing-metadata", artifact.getProject() );
733         assertEquals( "1.0-20091101.112233-1", artifact.getVersion() );
734         assertEquals( TEST_REPO_ID, artifact.getRepositoryId() );
735     }
736
737     private void assertArtifact( ArtifactMetadata artifact, String id, int size, String sha1, String md5 )
738     {
739         assertEquals( id, artifact.getId() );
740         assertEquals( md5, artifact.getMd5() );
741         assertEquals( sha1, artifact.getSha1() );
742         assertEquals( size, artifact.getSize() );
743         assertEquals( "org.codehaus.plexus", artifact.getNamespace() );
744         assertEquals( "plexus-spring", artifact.getProject() );
745         assertEquals( "1.2", artifact.getVersion() );
746         assertEquals( TEST_REPO_ID, artifact.getRepositoryId() );
747     }
748
749     private void assertMailingList( MailingList mailingList, String name, String archive, String post, String subscribe,
750                                     String unsubscribe, List<String> otherArchives, boolean allowPost )
751     {
752         assertEquals( archive, mailingList.getMainArchiveUrl() );
753         if ( allowPost )
754         {
755             assertEquals( post, mailingList.getPostAddress() );
756         }
757         else
758         {
759             assertNull( mailingList.getPostAddress() );
760         }
761         assertEquals( subscribe, mailingList.getSubscribeAddress() );
762         assertEquals( unsubscribe, mailingList.getUnsubscribeAddress() );
763         assertEquals( name, mailingList.getName() );
764         assertEquals( otherArchives, mailingList.getOtherArchives() );
765     }
766
767     private void assertMailingList( String prefix, MailingList mailingList, String name, boolean allowPost,
768                                     String nabbleUrl )
769     {
770         List<String> otherArchives = new ArrayList<String>();
771         otherArchives.add( "http://www.mail-archive.com/" + prefix + "@archiva.apache.org" );
772         if ( nabbleUrl != null )
773         {
774             otherArchives.add( nabbleUrl );
775         }
776         otherArchives.add( "http://markmail.org/list/org.apache.archiva." + prefix );
777         assertMailingList( mailingList, name, "http://mail-archives.apache.org/mod_mbox/archiva-" + prefix + "/",
778                            prefix + "@archiva.apache.org", prefix + "-subscribe@archiva.apache.org",
779                            prefix + "-unsubscribe@archiva.apache.org", otherArchives, allowPost );
780     }
781
782     private void checkApacheLicense( ProjectVersionMetadata metadata )
783     {
784         assertEquals( Arrays.asList( new License( "The Apache Software License, Version 2.0",
785                                                   "http://www.apache.org/licenses/LICENSE-2.0.txt" ) ),
786                       metadata.getLicenses() );
787     }
788
789     private void checkOrganizationApache( ProjectVersionMetadata metadata )
790     {
791         assertEquals( "The Apache Software Foundation", metadata.getOrganization().getName() );
792         assertEquals( "http://www.apache.org/", metadata.getOrganization().getUrl() );
793     }
794
795     private void deleteTestArtifactWithParent( List<String> pathsToBeDeleted )
796          throws IOException
797     {
798         for( String path : pathsToBeDeleted )
799         {
800             File dir =  new File( FileUtil.getBasedir(), path );
801             FileUtils.deleteDirectory( dir );
802
803             assertFalse( dir.exists() );
804         }
805         File dest = new File( FileUtil.getBasedir(), "target/test-repository/com/example/test/test-artifact-module-a" );
806         File parentPom = new File( FileUtil.getBasedir(), "target/test-repository/com/example/test/test-artifact-parent" );
807         File rootPom = new File( FileUtil.getBasedir(), "target/test-repository/com/example/test/test-artifact-root" );
808
809         FileUtils.deleteDirectory( dest );
810         FileUtils.deleteDirectory( parentPom );
811         FileUtils.deleteDirectory( rootPom );
812
813         assertFalse( dest.exists() );
814         assertFalse( parentPom.exists() );
815         assertFalse( rootPom.exists() );
816     }
817
818     private File copyTestArtifactWithParent( String srcPath, String destPath )
819          throws IOException
820     {
821         File src = new File( FileUtil.getBasedir(), srcPath );
822         File dest = new File( FileUtil.getBasedir(), destPath );
823
824         FileUtils.copyDirectory( src, dest );
825         assertTrue( dest.exists() );
826         return dest;
827     }
828
829 }