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