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