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