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 testGetProjectVersionMetadataForMissingPom()
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", "missing-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/missing-pom/1.0" );
241 assertEquals( "missing-pom", facet.getProblem() );
245 public void testGetRootNamespaces()
247 assertEquals( Arrays.asList( "com", "org" ), resolver.getRootNamespaces( TEST_REPO_ID ) );
250 public void testGetNamespaces()
252 assertEquals( Arrays.asList( "example" ), resolver.getNamespaces( TEST_REPO_ID, "com" ) );
253 assertEquals( Arrays.asList( "test" ), resolver.getNamespaces( TEST_REPO_ID, "com.example" ) );
254 assertEquals( Collections.<String>emptyList(), resolver.getNamespaces( TEST_REPO_ID, "com.example.test" ) );
256 assertEquals( Arrays.asList( "apache", "codehaus" ), resolver.getNamespaces( TEST_REPO_ID, "org" ) );
257 assertEquals( Arrays.asList( "archiva", "maven" ), resolver.getNamespaces( TEST_REPO_ID, "org.apache" ) );
258 assertEquals( Collections.<String>emptyList(), resolver.getNamespaces( TEST_REPO_ID, "org.apache.archiva" ) );
259 assertEquals( Arrays.asList( "plugins", "shared" ),
260 resolver.getNamespaces( TEST_REPO_ID, "org.apache.maven" ) );
261 assertEquals( Collections.<String>emptyList(),
262 resolver.getNamespaces( TEST_REPO_ID, "org.apache.maven.plugins" ) );
263 assertEquals( Collections.<String>emptyList(),
264 resolver.getNamespaces( TEST_REPO_ID, "org.apache.maven.shared" ) );
266 assertEquals( Arrays.asList( "plexus" ), resolver.getNamespaces( TEST_REPO_ID, "org.codehaus" ) );
267 assertEquals( Collections.<String>emptyList(), resolver.getNamespaces( TEST_REPO_ID, "org.codehaus.plexus" ) );
270 public void testGetProjects()
272 assertEquals( Collections.<String>emptyList(), resolver.getProjects( TEST_REPO_ID, "com" ) );
273 assertEquals( Collections.<String>emptyList(), resolver.getProjects( TEST_REPO_ID, "com.example" ) );
274 assertEquals( Arrays.asList( "incomplete-metadata", "invalid-pom", "malformed-metadata", "missing-metadata" ),
275 resolver.getProjects( TEST_REPO_ID, "com.example.test" ) );
277 assertEquals( Collections.<String>emptyList(), resolver.getProjects( TEST_REPO_ID, "org" ) );
278 assertEquals( Arrays.asList( "apache" ), resolver.getProjects( TEST_REPO_ID, "org.apache" ) );
279 assertEquals( Arrays.asList( "archiva", "archiva-base", "archiva-common", "archiva-modules", "archiva-parent" ),
280 resolver.getProjects( TEST_REPO_ID, "org.apache.archiva" ) );
281 assertEquals( Collections.<String>emptyList(), resolver.getProjects( TEST_REPO_ID, "org.apache.maven" ) );
282 assertEquals( Collections.<String>emptyList(),
283 resolver.getProjects( TEST_REPO_ID, "org.apache.maven.plugins" ) );
284 assertEquals( Arrays.asList( "maven-downloader" ),
285 resolver.getProjects( TEST_REPO_ID, "org.apache.maven.shared" ) );
288 public void testGetProjectVersions()
290 assertEquals( Arrays.asList( "1.0-SNAPSHOT" ),
291 resolver.getProjectVersions( TEST_REPO_ID, "com.example.test", "incomplete-metadata" ) );
292 assertEquals( Arrays.asList( "1.0-SNAPSHOT" ),
293 resolver.getProjectVersions( TEST_REPO_ID, "com.example.test", "malformed-metadata" ) );
294 assertEquals( Arrays.asList( "1.0-SNAPSHOT" ),
295 resolver.getProjectVersions( TEST_REPO_ID, "com.example.test", "missing-metadata" ) );
296 assertEquals( Arrays.asList( "1.0" ),
297 resolver.getProjectVersions( TEST_REPO_ID, "com.example.test", "invalid-pom" ) );
299 assertEquals( Arrays.asList( "4", "5-SNAPSHOT" ),
300 resolver.getProjectVersions( TEST_REPO_ID, "org.apache", "apache" ) );
302 assertEquals( Arrays.asList( "1.2.1" ),
303 resolver.getProjectVersions( TEST_REPO_ID, "org.apache.archiva", "archiva" ) );
304 assertEquals( Arrays.asList( "1.2.1" ),
305 resolver.getProjectVersions( TEST_REPO_ID, "org.apache.archiva", "archiva-base" ) );
306 assertEquals( Arrays.asList( "1.2.1" ),
307 resolver.getProjectVersions( TEST_REPO_ID, "org.apache.archiva", "archiva-common" ) );
308 assertEquals( Arrays.asList( "1.2.1" ),
309 resolver.getProjectVersions( TEST_REPO_ID, "org.apache.archiva", "archiva-modules" ) );
310 assertEquals( Arrays.asList( "3" ),
311 resolver.getProjectVersions( TEST_REPO_ID, "org.apache.archiva", "archiva-parent" ) );
313 assertEquals( Collections.<String>emptyList(),
314 resolver.getProjectVersions( TEST_REPO_ID, "org.apache.maven.shared", "maven-downloader" ) );
317 public void testGetArtifacts()
319 List<ArtifactMetadata> artifacts = new ArrayList<ArtifactMetadata>(
320 resolver.getArtifacts( TEST_REPO_ID, "org.codehaus.plexus", "plexus-spring", "1.2" ) );
321 assertEquals( 3, artifacts.size() );
322 Collections.sort( artifacts, new Comparator<ArtifactMetadata>()
324 public int compare( ArtifactMetadata o1, ArtifactMetadata o2 )
326 return o1.getId().compareTo( o2.getId() );
330 assertArtifact( artifacts.get( 0 ), "plexus-spring-1.2-sources.jar", 0, EMPTY_SHA1, EMPTY_MD5 );
331 assertArtifact( artifacts.get( 1 ), "plexus-spring-1.2.jar", 0, EMPTY_SHA1, EMPTY_MD5 );
332 assertArtifact( artifacts.get( 2 ), "plexus-spring-1.2.pom", 7407, "96b14cf880e384b2d15e8193c57b65c5420ca4c5",
333 "f83aa25f016212a551a4b2249985effc" );
336 public void testGetArtifactsFiltered()
338 ExcludesFilter<String> filter =
339 new ExcludesFilter<String>( Collections.singletonList( "plexus-spring-1.2.pom" ) );
340 List<ArtifactMetadata> artifacts = new ArrayList<ArtifactMetadata>(
341 resolver.getArtifacts( TEST_REPO_ID, "org.codehaus.plexus", "plexus-spring", "1.2", filter ) );
342 assertEquals( 2, artifacts.size() );
343 Collections.sort( artifacts, new Comparator<ArtifactMetadata>()
345 public int compare( ArtifactMetadata o1, ArtifactMetadata o2 )
347 return o1.getId().compareTo( o2.getId() );
351 assertArtifact( artifacts.get( 0 ), "plexus-spring-1.2-sources.jar", 0, EMPTY_SHA1, EMPTY_MD5 );
352 assertArtifact( artifacts.get( 1 ), "plexus-spring-1.2.jar", 0, EMPTY_SHA1, EMPTY_MD5 );
355 private void assertArtifact( ArtifactMetadata artifact, String id, int size, String sha1, String md5 )
357 assertEquals( id, artifact.getId() );
358 assertEquals( md5, artifact.getMd5() );
359 assertEquals( sha1, artifact.getSha1() );
360 assertEquals( size, artifact.getSize() );
361 assertEquals( "org.codehaus.plexus", artifact.getNamespace() );
362 assertEquals( "plexus-spring", artifact.getProject() );
363 assertEquals( "1.2", artifact.getVersion() );
364 assertEquals( TEST_REPO_ID, artifact.getRepositoryId() );
367 private void assertMailingList( MailingList mailingList, String name, String archive, String post, String subscribe,
368 String unsubscribe, List<String> otherArchives, boolean allowPost )
370 assertEquals( archive, mailingList.getMainArchiveUrl() );
373 assertEquals( post, mailingList.getPostAddress() );
377 assertNull( mailingList.getPostAddress() );
379 assertEquals( subscribe, mailingList.getSubscribeAddress() );
380 assertEquals( unsubscribe, mailingList.getUnsubscribeAddress() );
381 assertEquals( name, mailingList.getName() );
382 assertEquals( otherArchives, mailingList.getOtherArchives() );
385 private void assertMailingList( String prefix, MailingList mailingList, String name, boolean allowPost,
388 List<String> otherArchives = new ArrayList<String>();
389 otherArchives.add( "http://www.mail-archive.com/" + prefix + "@archiva.apache.org" );
390 if ( nabbleUrl != null )
392 otherArchives.add( nabbleUrl );
394 otherArchives.add( "http://markmail.org/list/org.apache.archiva." + prefix );
395 assertMailingList( mailingList, name, "http://mail-archives.apache.org/mod_mbox/archiva-" + prefix + "/",
396 prefix + "@archiva.apache.org", prefix + "-subscribe@archiva.apache.org",
397 prefix + "-unsubscribe@archiva.apache.org", otherArchives, allowPost );
400 private void checkApacheLicense( ProjectVersionMetadata metadata )
402 assertEquals( Arrays.asList( new License( "The Apache Software License, Version 2.0",
403 "http://www.apache.org/licenses/LICENSE-2.0.txt" ) ),
404 metadata.getLicenses() );
407 private void checkOrganizationApache( ProjectVersionMetadata metadata )
409 assertEquals( "The Apache Software Foundation", metadata.getOrganization().getName() );
410 assertEquals( "http://www.apache.org/", metadata.getOrganization().getUrl() );