1 package org.apache.archiva.metadata.repository.storage.maven2;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.Collections;
25 import java.util.Comparator;
26 import java.util.List;
28 import org.apache.archiva.metadata.model.ArtifactMetadata;
29 import org.apache.archiva.metadata.model.Dependency;
30 import org.apache.archiva.metadata.model.License;
31 import org.apache.archiva.metadata.model.MailingList;
32 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
33 import org.apache.archiva.metadata.repository.MetadataRepository;
34 import org.apache.archiva.metadata.repository.MetadataResolverException;
35 import org.apache.archiva.metadata.repository.filter.ExcludesFilter;
36 import org.apache.archiva.metadata.repository.storage.StorageMetadataResolver;
37 import org.apache.archiva.reports.RepositoryProblemFacet;
38 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
39 import org.apache.maven.archiva.configuration.Configuration;
40 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
41 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
43 public class Maven2RepositoryMetadataResolverTest
44 extends PlexusInSpringTestCase
46 private Maven2RepositoryMetadataResolver resolver;
48 private static final String TEST_REPO_ID = "test";
50 private static final String ASF_SCM_CONN_BASE = "scm:svn:http://svn.apache.org/repos/asf/";
52 private static final String ASF_SCM_DEV_CONN_BASE = "scm:svn:https://svn.apache.org/repos/asf/";
54 private static final String ASF_SCM_VIEWVC_BASE = "http://svn.apache.org/viewvc/";
56 private static final String EMPTY_MD5 = "d41d8cd98f00b204e9800998ecf8427e";
58 private static final String EMPTY_SHA1 = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
60 private MetadataRepository metadataRepository;
67 ArchivaConfiguration configuration = (ArchivaConfiguration) lookup( ArchivaConfiguration.class );
68 Configuration c = new Configuration();
69 ManagedRepositoryConfiguration testRepo = new ManagedRepositoryConfiguration();
70 testRepo.setId( TEST_REPO_ID );
71 testRepo.setLocation( getTestPath( "src/test/repositories/test" ) );
72 c.addManagedRepository( testRepo );
73 configuration.save( c );
75 resolver = (Maven2RepositoryMetadataResolver) lookup( StorageMetadataResolver.class, "maven2" );
76 metadataRepository = (MetadataRepository) lookup( MetadataRepository.class );
77 metadataRepository.removeMetadataFacets( TEST_REPO_ID, RepositoryProblemFacet.FACET_ID );
80 public void testGetProjectVersionMetadata()
81 throws MetadataResolverException
83 ProjectVersionMetadata metadata =
84 resolver.getProjectVersion( TEST_REPO_ID, "org.apache.archiva", "archiva-common", "1.2.1" );
85 MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet( MavenProjectFacet.FACET_ID );
86 assertEquals( "jar", facet.getPackaging() );
87 assertEquals( "http://archiva.apache.org/ref/1.2.1/archiva-base/archiva-common", metadata.getUrl() );
88 assertEquals( "org.apache.archiva", facet.getParent().getGroupId() );
89 assertEquals( "archiva-base", facet.getParent().getArtifactId() );
90 assertEquals( "1.2.1", facet.getParent().getVersion() );
91 assertEquals( "archiva-common", facet.getArtifactId() );
92 assertEquals( "org.apache.archiva", facet.getGroupId() );
93 assertEquals( "continuum", metadata.getCiManagement().getSystem() );
94 assertEquals( "http://vmbuild.apache.org/continuum", metadata.getCiManagement().getUrl() );
95 assertNotNull( metadata.getDescription() );
96 // TODO: this would be better
98 // "Archiva is an application for managing one or more remote repositories, including administration, artifact handling, browsing and searching.",
99 // metadata.getDescription() );
100 assertEquals( "1.2.1", metadata.getId() );
101 assertEquals( "jira", metadata.getIssueManagement().getSystem() );
102 assertEquals( "http://jira.codehaus.org/browse/MRM", metadata.getIssueManagement().getUrl() );
103 checkApacheLicense( metadata );
104 assertEquals( "Archiva Base :: Common", metadata.getName() );
105 String path = "archiva/tags/archiva-1.2.1/archiva-modules/archiva-base/archiva-common";
106 assertEquals( ASF_SCM_CONN_BASE + path, metadata.getScm().getConnection() );
107 assertEquals( ASF_SCM_DEV_CONN_BASE + path, metadata.getScm().getDeveloperConnection() );
108 assertEquals( ASF_SCM_VIEWVC_BASE + path, metadata.getScm().getUrl() );
109 checkOrganizationApache( metadata );
111 assertEquals( 4, metadata.getMailingLists().size() );
112 assertMailingList( "users", metadata.getMailingLists().get( 0 ), "Archiva User List", true,
113 "http://www.nabble.com/archiva-users-f16426.html" );
114 assertMailingList( "dev", metadata.getMailingLists().get( 1 ), "Archiva Developer List", true,
115 "http://www.nabble.com/archiva-dev-f16427.html" );
116 assertMailingList( "commits", metadata.getMailingLists().get( 2 ), "Archiva Commits List", false, null );
117 assertMailingList( "issues", metadata.getMailingLists().get( 3 ), "Archiva Issues List", false,
118 "http://www.nabble.com/Archiva---Issues-f29617.html" );
120 List<Dependency> dependencies = metadata.getDependencies();
121 assertEquals( 10, dependencies.size() );
122 assertDependency( dependencies.get( 0 ), "commons-lang", "commons-lang", "2.2" );
123 assertDependency( dependencies.get( 1 ), "commons-io", "commons-io", "1.4" );
124 assertDependency( dependencies.get( 2 ), "org.slf4j", "slf4j-api", "1.5.0" );
125 assertDependency( dependencies.get( 3 ), "org.codehaus.plexus", "plexus-component-api", "1.0-alpha-22" );
126 assertDependency( dependencies.get( 4 ), "org.codehaus.plexus", "plexus-spring", "1.2", "test" );
127 assertDependency( dependencies.get( 5 ), "xalan", "xalan", "2.7.0" );
128 assertDependency( dependencies.get( 6 ), "dom4j", "dom4j", "1.6.1", "test" );
129 assertDependency( dependencies.get( 7 ), "junit", "junit", "3.8.1", "test" );
130 assertDependency( dependencies.get( 8 ), "easymock", "easymock", "1.2_Java1.3", "test" );
131 assertDependency( dependencies.get( 9 ), "easymock", "easymockclassextension", "1.2", "test" );
134 private void assertDependency( Dependency dependency, String groupId, String artifactId, String version )
136 assertDependency( dependency, groupId, artifactId, version, "compile" );
139 private void assertDependency( Dependency dependency, String groupId, String artifactId, String version,
142 assertEquals( artifactId, dependency.getArtifactId() );
143 assertEquals( "jar", dependency.getType() );
144 assertEquals( version, dependency.getVersion() );
145 assertEquals( groupId, dependency.getGroupId() );
146 assertEquals( scope, dependency.getScope() );
147 assertNull( dependency.getClassifier() );
148 assertNull( dependency.getSystemPath() );
151 public void testGetProjectVersionMetadataForTimestampedSnapshot()
152 throws MetadataResolverException
154 ProjectVersionMetadata metadata =
155 resolver.getProjectVersion( TEST_REPO_ID, "org.apache", "apache", "5-SNAPSHOT" );
156 MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet( MavenProjectFacet.FACET_ID );
157 assertEquals( "pom", facet.getPackaging() );
158 assertEquals( "http://www.apache.org/", metadata.getUrl() );
159 assertNull( facet.getParent() );
160 assertEquals( "org.apache", facet.getGroupId() );
161 assertEquals( "apache", facet.getArtifactId() );
162 assertNull( metadata.getCiManagement() );
163 assertNotNull( metadata.getDescription() );
164 // TODO: this would be better
166 // "The Apache Software Foundation provides support for the Apache community of open-source software projects. " +
167 // "The Apache projects are characterized by a collaborative, consensus based development process, an open " +
168 // "and pragmatic software license, and a desire to create high quality software that leads the way in its " +
169 // "field. We consider ourselves not simply a group of projects sharing a server, but rather a community of " +
170 // "developers and users.", metadata.getDescription() );
171 assertEquals( "5-SNAPSHOT", metadata.getId() );
172 assertNull( metadata.getIssueManagement() );
173 checkApacheLicense( metadata );
174 assertEquals( "The Apache Software Foundation", metadata.getName() );
175 String path = "maven/pom/trunk/asf";
176 assertEquals( ASF_SCM_CONN_BASE + path, metadata.getScm().getConnection() );
177 assertEquals( ASF_SCM_DEV_CONN_BASE + path, metadata.getScm().getDeveloperConnection() );
178 assertEquals( ASF_SCM_VIEWVC_BASE + path, metadata.getScm().getUrl() );
179 checkOrganizationApache( metadata );
180 assertEquals( 1, metadata.getMailingLists().size() );
181 assertMailingList( metadata.getMailingLists().get( 0 ), "Apache Announce List",
182 "http://mail-archives.apache.org/mod_mbox/www-announce/", "announce@apache.org",
183 "announce-subscribe@apache.org", "announce-unsubscribe@apache.org",
184 Collections.<String>emptyList(), true );
185 assertEquals( Collections.<Dependency>emptyList(), metadata.getDependencies() );
188 public void testGetProjectVersionMetadataForTimestampedSnapshotMissingMetadata()
189 throws MetadataResolverException
191 ProjectVersionMetadata metadata =
192 resolver.getProjectVersion( TEST_REPO_ID, "com.example.test", "missing-metadata", "1.0-SNAPSHOT" );
193 assertNull( metadata );
196 public void testGetProjectVersionMetadataForTimestampedSnapshotMalformedMetadata()
197 throws MetadataResolverException
199 ProjectVersionMetadata metadata =
200 resolver.getProjectVersion( TEST_REPO_ID, "com.example.test", "malformed-metadata", "1.0-SNAPSHOT" );
201 assertNull( metadata );
204 public void testGetProjectVersionMetadataForTimestampedSnapshotIncompleteMetadata()
205 throws MetadataResolverException
207 ProjectVersionMetadata metadata =
208 resolver.getProjectVersion( TEST_REPO_ID, "com.example.test", "incomplete-metadata", "1.0-SNAPSHOT" );
209 assertNull( metadata );
212 public void testGetProjectVersionMetadataForInvalidPom()
213 throws MetadataResolverException
215 assertTrue( metadataRepository.getMetadataFacets( TEST_REPO_ID, RepositoryProblemFacet.FACET_ID ).isEmpty() );
217 ProjectVersionMetadata metadata =
218 resolver.getProjectVersion( TEST_REPO_ID, "com.example.test", "invalid-pom", "1.0" );
219 assertNull( metadata );
221 assertFalse( metadataRepository.getMetadataFacets( TEST_REPO_ID, RepositoryProblemFacet.FACET_ID ).isEmpty() );
222 RepositoryProblemFacet facet =
223 (RepositoryProblemFacet) metadataRepository.getMetadataFacet( TEST_REPO_ID, RepositoryProblemFacet.FACET_ID,
224 "com.example.test/invalid-pom/1.0" );
225 assertEquals( "invalid-pom", facet.getProblem() );
228 public void testGetProjectVersionMetadataForMislocatedPom()
229 throws MetadataResolverException
231 assertTrue( metadataRepository.getMetadataFacets( TEST_REPO_ID, RepositoryProblemFacet.FACET_ID ).isEmpty() );
233 ProjectVersionMetadata metadata =
234 resolver.getProjectVersion( TEST_REPO_ID, "com.example.test", "mislocated-pom", "1.0" );
235 assertNull( metadata );
237 assertFalse( metadataRepository.getMetadataFacets( TEST_REPO_ID, RepositoryProblemFacet.FACET_ID ).isEmpty() );
238 RepositoryProblemFacet facet =
239 (RepositoryProblemFacet) metadataRepository.getMetadataFacet( TEST_REPO_ID, RepositoryProblemFacet.FACET_ID,
240 "com.example.test/mislocated-pom/1.0" );
241 assertEquals( "mislocated-pom", facet.getProblem() );
244 public void testGetProjectVersionMetadataForMissingPom()
245 throws MetadataResolverException
247 assertTrue( metadataRepository.getMetadataFacets( TEST_REPO_ID, RepositoryProblemFacet.FACET_ID ).isEmpty() );
249 ProjectVersionMetadata metadata =
250 resolver.getProjectVersion( TEST_REPO_ID, "com.example.test", "missing-pom", "1.0" );
251 assertNull( metadata );
253 assertFalse( metadataRepository.getMetadataFacets( TEST_REPO_ID, RepositoryProblemFacet.FACET_ID ).isEmpty() );
254 RepositoryProblemFacet facet =
255 (RepositoryProblemFacet) metadataRepository.getMetadataFacet( TEST_REPO_ID, RepositoryProblemFacet.FACET_ID,
256 "com.example.test/missing-pom/1.0" );
257 assertEquals( "missing-pom", facet.getProblem() );
261 public void testGetRootNamespaces()
263 assertEquals( Arrays.asList( "com", "org" ), resolver.getRootNamespaces( TEST_REPO_ID ) );
266 public void testGetNamespaces()
268 assertEquals( Arrays.asList( "example" ), resolver.getNamespaces( TEST_REPO_ID, "com" ) );
269 assertEquals( Arrays.asList( "test" ), resolver.getNamespaces( TEST_REPO_ID, "com.example" ) );
270 assertEquals( Collections.<String>emptyList(), resolver.getNamespaces( TEST_REPO_ID, "com.example.test" ) );
272 assertEquals( Arrays.asList( "apache", "codehaus" ), resolver.getNamespaces( TEST_REPO_ID, "org" ) );
273 assertEquals( Arrays.asList( "archiva", "maven" ), resolver.getNamespaces( TEST_REPO_ID, "org.apache" ) );
274 assertEquals( Collections.<String>emptyList(), resolver.getNamespaces( TEST_REPO_ID, "org.apache.archiva" ) );
275 assertEquals( Arrays.asList( "plugins", "shared" ),
276 resolver.getNamespaces( TEST_REPO_ID, "org.apache.maven" ) );
277 assertEquals( Collections.<String>emptyList(),
278 resolver.getNamespaces( TEST_REPO_ID, "org.apache.maven.plugins" ) );
279 assertEquals( Collections.<String>emptyList(),
280 resolver.getNamespaces( TEST_REPO_ID, "org.apache.maven.shared" ) );
282 assertEquals( Arrays.asList( "plexus" ), resolver.getNamespaces( TEST_REPO_ID, "org.codehaus" ) );
283 assertEquals( Collections.<String>emptyList(), resolver.getNamespaces( TEST_REPO_ID, "org.codehaus.plexus" ) );
286 public void testGetProjects()
288 assertEquals( Collections.<String>emptyList(), resolver.getProjects( TEST_REPO_ID, "com" ) );
289 assertEquals( Collections.<String>emptyList(), resolver.getProjects( TEST_REPO_ID, "com.example" ) );
290 assertEquals( Arrays.asList( "incomplete-metadata", "invalid-pom", "malformed-metadata", "mislocated-pom",
291 "missing-metadata" ), resolver.getProjects( TEST_REPO_ID, "com.example.test" ) );
293 assertEquals( Collections.<String>emptyList(), resolver.getProjects( TEST_REPO_ID, "org" ) );
294 assertEquals( Arrays.asList( "apache" ), resolver.getProjects( TEST_REPO_ID, "org.apache" ) );
295 assertEquals( Arrays.asList( "archiva", "archiva-base", "archiva-common", "archiva-modules", "archiva-parent" ),
296 resolver.getProjects( TEST_REPO_ID, "org.apache.archiva" ) );
297 assertEquals( Collections.<String>emptyList(), resolver.getProjects( TEST_REPO_ID, "org.apache.maven" ) );
298 assertEquals( Collections.<String>emptyList(),
299 resolver.getProjects( TEST_REPO_ID, "org.apache.maven.plugins" ) );
300 assertEquals( Arrays.asList( "maven-downloader" ),
301 resolver.getProjects( TEST_REPO_ID, "org.apache.maven.shared" ) );
304 public void testGetProjectVersions()
306 assertEquals( Arrays.asList( "1.0-SNAPSHOT" ),
307 resolver.getProjectVersions( TEST_REPO_ID, "com.example.test", "incomplete-metadata" ) );
308 assertEquals( Arrays.asList( "1.0-SNAPSHOT" ),
309 resolver.getProjectVersions( TEST_REPO_ID, "com.example.test", "malformed-metadata" ) );
310 assertEquals( Arrays.asList( "1.0-SNAPSHOT" ),
311 resolver.getProjectVersions( TEST_REPO_ID, "com.example.test", "missing-metadata" ) );
312 assertEquals( Arrays.asList( "1.0" ),
313 resolver.getProjectVersions( TEST_REPO_ID, "com.example.test", "invalid-pom" ) );
315 assertEquals( Arrays.asList( "4", "5-SNAPSHOT" ),
316 resolver.getProjectVersions( TEST_REPO_ID, "org.apache", "apache" ) );
318 assertEquals( Arrays.asList( "1.2.1" ),
319 resolver.getProjectVersions( TEST_REPO_ID, "org.apache.archiva", "archiva" ) );
320 assertEquals( Arrays.asList( "1.2.1" ),
321 resolver.getProjectVersions( TEST_REPO_ID, "org.apache.archiva", "archiva-base" ) );
322 assertEquals( Arrays.asList( "1.2.1" ),
323 resolver.getProjectVersions( TEST_REPO_ID, "org.apache.archiva", "archiva-common" ) );
324 assertEquals( Arrays.asList( "1.2.1" ),
325 resolver.getProjectVersions( TEST_REPO_ID, "org.apache.archiva", "archiva-modules" ) );
326 assertEquals( Arrays.asList( "3" ),
327 resolver.getProjectVersions( TEST_REPO_ID, "org.apache.archiva", "archiva-parent" ) );
329 assertEquals( Collections.<String>emptyList(),
330 resolver.getProjectVersions( TEST_REPO_ID, "org.apache.maven.shared", "maven-downloader" ) );
333 public void testGetArtifacts()
335 List<ArtifactMetadata> artifacts = new ArrayList<ArtifactMetadata>(
336 resolver.getArtifacts( TEST_REPO_ID, "org.codehaus.plexus", "plexus-spring", "1.2" ) );
337 assertEquals( 3, artifacts.size() );
338 Collections.sort( artifacts, new Comparator<ArtifactMetadata>()
340 public int compare( ArtifactMetadata o1, ArtifactMetadata o2 )
342 return o1.getId().compareTo( o2.getId() );
346 assertArtifact( artifacts.get( 0 ), "plexus-spring-1.2-sources.jar", 0, EMPTY_SHA1, EMPTY_MD5 );
347 assertArtifact( artifacts.get( 1 ), "plexus-spring-1.2.jar", 0, EMPTY_SHA1, EMPTY_MD5 );
348 assertArtifact( artifacts.get( 2 ), "plexus-spring-1.2.pom", 7407, "96b14cf880e384b2d15e8193c57b65c5420ca4c5",
349 "f83aa25f016212a551a4b2249985effc" );
352 public void testGetArtifactsFiltered()
354 ExcludesFilter<String> filter =
355 new ExcludesFilter<String>( Collections.singletonList( "plexus-spring-1.2.pom" ) );
356 List<ArtifactMetadata> artifacts = new ArrayList<ArtifactMetadata>(
357 resolver.getArtifacts( TEST_REPO_ID, "org.codehaus.plexus", "plexus-spring", "1.2", filter ) );
358 assertEquals( 2, artifacts.size() );
359 Collections.sort( artifacts, new Comparator<ArtifactMetadata>()
361 public int compare( ArtifactMetadata o1, ArtifactMetadata o2 )
363 return o1.getId().compareTo( o2.getId() );
367 assertArtifact( artifacts.get( 0 ), "plexus-spring-1.2-sources.jar", 0, EMPTY_SHA1, EMPTY_MD5 );
368 assertArtifact( artifacts.get( 1 ), "plexus-spring-1.2.jar", 0, EMPTY_SHA1, EMPTY_MD5 );
371 private void assertArtifact( ArtifactMetadata artifact, String id, int size, String sha1, String md5 )
373 assertEquals( id, artifact.getId() );
374 assertEquals( md5, artifact.getMd5() );
375 assertEquals( sha1, artifact.getSha1() );
376 assertEquals( size, artifact.getSize() );
377 assertEquals( "org.codehaus.plexus", artifact.getNamespace() );
378 assertEquals( "plexus-spring", artifact.getProject() );
379 assertEquals( "1.2", artifact.getVersion() );
380 assertEquals( TEST_REPO_ID, artifact.getRepositoryId() );
383 private void assertMailingList( MailingList mailingList, String name, String archive, String post, String subscribe,
384 String unsubscribe, List<String> otherArchives, boolean allowPost )
386 assertEquals( archive, mailingList.getMainArchiveUrl() );
389 assertEquals( post, mailingList.getPostAddress() );
393 assertNull( mailingList.getPostAddress() );
395 assertEquals( subscribe, mailingList.getSubscribeAddress() );
396 assertEquals( unsubscribe, mailingList.getUnsubscribeAddress() );
397 assertEquals( name, mailingList.getName() );
398 assertEquals( otherArchives, mailingList.getOtherArchives() );
401 private void assertMailingList( String prefix, MailingList mailingList, String name, boolean allowPost,
404 List<String> otherArchives = new ArrayList<String>();
405 otherArchives.add( "http://www.mail-archive.com/" + prefix + "@archiva.apache.org" );
406 if ( nabbleUrl != null )
408 otherArchives.add( nabbleUrl );
410 otherArchives.add( "http://markmail.org/list/org.apache.archiva." + prefix );
411 assertMailingList( mailingList, name, "http://mail-archives.apache.org/mod_mbox/archiva-" + prefix + "/",
412 prefix + "@archiva.apache.org", prefix + "-subscribe@archiva.apache.org",
413 prefix + "-unsubscribe@archiva.apache.org", otherArchives, allowPost );
416 private void checkApacheLicense( ProjectVersionMetadata metadata )
418 assertEquals( Arrays.asList( new License( "The Apache Software License, Version 2.0",
419 "http://www.apache.org/licenses/LICENSE-2.0.txt" ) ),
420 metadata.getLicenses() );
423 private void checkOrganizationApache( ProjectVersionMetadata metadata )
425 assertEquals( "The Apache Software Foundation", metadata.getOrganization().getName() );
426 assertEquals( "http://www.apache.org/", metadata.getOrganization().getUrl() );