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 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.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.filter.AllFilter;
34 import org.apache.archiva.metadata.repository.filter.Filter;
35 import org.apache.archiva.metadata.repository.storage.ReadMetadataRequest;
36 import org.apache.archiva.metadata.repository.storage.RepositoryStorageRuntimeException;
37 import org.apache.archiva.proxy.maven.WagonFactory;
38 import org.apache.archiva.proxy.maven.WagonFactoryRequest;
39 import org.apache.archiva.repository.RepositoryRegistry;
40 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
41 import org.apache.commons.io.FileUtils;
42 import org.apache.maven.wagon.Wagon;
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.springframework.test.context.ContextConfiguration;
48 import javax.inject.Inject;
49 import javax.inject.Named;
50 import java.io.IOException;
51 import java.nio.file.Files;
52 import java.nio.file.Path;
53 import java.nio.file.Paths;
54 import java.util.ArrayList;
55 import java.util.Arrays;
56 import java.util.HashMap;
57 import java.util.List;
59 import static org.mockito.Mockito.mock;
60 import static org.mockito.Mockito.when;
62 @RunWith ( ArchivaSpringJUnit4ClassRunner.class )
63 @ContextConfiguration ( { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
64 public class Maven2RepositoryMetadataResolverMRM1411Test
67 private static final Filter<String> ALL = new AllFilter<String>();
70 @Named ( "repositoryStorage#maven2")
71 private Maven2RepositoryStorage storage;
73 private static final String TEST_REPO_ID = "test";
75 private static final String TEST_REMOTE_REPO_ID = "central";
77 private static final String ASF_SCM_CONN_BASE = "scm:svn:http://svn.apache.org/repos/asf/";
79 private static final String ASF_SCM_DEV_CONN_BASE = "scm:svn:https://svn.apache.org/repos/asf/";
81 private static final String ASF_SCM_VIEWVC_BASE = "http://svn.apache.org/viewvc/";
83 private static final String TEST_SCM_CONN_BASE = "scm:svn:http://svn.example.com/repos/";
85 private static final String TEST_SCM_DEV_CONN_BASE = "scm:svn:https://svn.example.com/repos/";
87 private static final String TEST_SCM_URL_BASE = "http://svn.example.com/repos/";
89 private static final String EMPTY_MD5 = "d41d8cd98f00b204e9800998ecf8427e";
91 private static final String EMPTY_SHA1 = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
94 @Named ( "archivaConfiguration#default" )
95 private ArchivaConfiguration configuration;
98 RepositoryRegistry repositoryRegistry;
100 private WagonFactory wagonFactory;
102 ManagedRepositoryConfiguration testRepo;
113 c = new Configuration();
114 testRepo = new ManagedRepositoryConfiguration();
115 testRepo.setId( TEST_REPO_ID );
116 testRepo.setLocation( Paths.get( "target/test-repository" ).toAbsolutePath().toString() );
117 testRepo.setReleases( true );
118 testRepo.setSnapshots( true );
119 c.addManagedRepository( testRepo );
121 RemoteRepositoryConfiguration testRemoteRepo = new RemoteRepositoryConfiguration();
122 testRemoteRepo.setId( TEST_REMOTE_REPO_ID );
123 testRemoteRepo.setLayout( "default" );
124 testRemoteRepo.setName( "Central Repository" );
125 testRemoteRepo.setUrl( "http://central.repo.com/maven2" );
126 testRemoteRepo.setTimeout( 10 );
127 c.addRemoteRepository( testRemoteRepo );
129 ProxyConnectorConfiguration proxyConnector = new ProxyConnectorConfiguration();
130 proxyConnector.setSourceRepoId( TEST_REPO_ID );
131 proxyConnector.setTargetRepoId( TEST_REMOTE_REPO_ID );
132 proxyConnector.setDisabled( false );
133 c.addProxyConnector( proxyConnector );
135 configuration.save( c );
137 repositoryRegistry.reload();
139 assertTrue( c.getManagedRepositories().get( 0 ).isSnapshots() );
140 assertTrue( c.getManagedRepositories().get( 0 ).isReleases() );
142 wagonFactory = mock( WagonFactory.class );
144 storage.setWagonFactory( wagonFactory );
146 Wagon wagon = new MockWagon();
147 when( wagonFactory.getWagon(
148 new WagonFactoryRequest( "wagon#http", new HashMap<String, String>() ) ) ).thenReturn( wagon );
151 // Tests for MRM-1411 - START
153 public void testGetProjectVersionMetadataWithParentSuccessful()
156 copyTestArtifactWithParent( "target/test-classes/com/example/test/test-artifact-module-a",
157 "target/test-repository/com/example/test/test-artifact-module-a" );
158 copyTestArtifactWithParent( "src/test/resources/com/example/test/test-artifact-parent",
159 "target/test-repository/com/example/test/test-artifact-parent" );
161 copyTestArtifactWithParent( "target/test-classes/com/example/test/test-artifact-root",
162 "target/test-repository/com/example/test/test-artifact-root" );
164 ProjectVersionMetadata metadata = storage.readProjectVersionMetadata(
165 new ReadMetadataRequest( TEST_REPO_ID, "com.example.test", "test-artifact-module-a", "1.0" ) );
167 MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet( MavenProjectFacet.FACET_ID );
168 assertEquals( "jar", facet.getPackaging() );
169 assertEquals( "http://maven.apache.org", metadata.getUrl() );
170 assertEquals( "com.example.test", facet.getParent().getGroupId() );
171 assertEquals( "test-artifact-root", facet.getParent().getArtifactId() );
172 assertEquals( "1.0", facet.getParent().getVersion() );
173 assertEquals( "test-artifact-module-a", facet.getArtifactId() );
174 assertEquals( "com.example.test", facet.getGroupId() );
175 assertNull( metadata.getCiManagement() );
176 assertNotNull( metadata.getDescription() );
178 checkApacheLicense( metadata );
180 assertEquals( "1.0", metadata.getId() );
181 assertEquals( "Test Artifact :: Module A", metadata.getName() );
182 String path = "test-artifact/trunk/test-artifact-module-a";
183 assertEquals( TEST_SCM_CONN_BASE + path, metadata.getScm().getConnection() );
184 assertEquals( TEST_SCM_DEV_CONN_BASE + path, metadata.getScm().getDeveloperConnection() );
185 assertEquals( TEST_SCM_URL_BASE + path, metadata.getScm().getUrl() );
187 List<Dependency> dependencies = metadata.getDependencies();
188 assertEquals( 2, dependencies.size() );
189 assertDependency( dependencies.get( 0 ), "commons-io", "commons-io", "1.4" );
190 assertDependency( dependencies.get( 1 ), "junit", "junit", "3.8.1", "test" );
192 List<String> paths = new ArrayList<>();
193 paths.add( "target/test-repository/com/example/test/test-artifact-module-a" );
194 paths.add( "target/test-repository/com/example/test/test-artifact-parent" );
195 paths.add( "target/test-repository/com/example/test/test-artifact-root" );
197 deleteTestArtifactWithParent( paths );
201 public void testGetProjectVersionMetadataWithParentNoRemoteReposConfigured()
204 // remove configuration
205 Configuration config = configuration.getConfiguration();
206 RemoteRepositoryConfiguration remoteRepo = config.findRemoteRepositoryById( TEST_REMOTE_REPO_ID );
207 config.removeRemoteRepository( remoteRepo );
209 configuration.save( config );
211 copyTestArtifactWithParent( "target/test-classes/com/example/test/test-artifact-module-a",
212 "target/test-repository/com/example/test/test-artifact-module-a" );
214 ProjectVersionMetadata metadata = storage.readProjectVersionMetadata(
215 new ReadMetadataRequest( TEST_REPO_ID, "com.example.test", "test-artifact-module-a", "1.0" ) );
216 assertEquals( "1.0", metadata.getId() );
218 MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet( MavenProjectFacet.FACET_ID );
219 assertNotNull( facet );
220 assertEquals( "com.example.test", facet.getGroupId() );
221 assertEquals( "test-artifact-module-a", facet.getArtifactId() );
222 assertEquals( "jar", facet.getPackaging() );
224 List<String> paths = new ArrayList<>();
225 paths.add( "target/test-repository/com/example/test/test-artifact-module-a" );
226 paths.add( "target/test-repository/com/example/test/test-artifact-parent" );
227 paths.add( "target/test-repository/com/example/test/test-artifact-root" );
229 deleteTestArtifactWithParent( paths );
233 public void testGetProjectVersionMetadataWithParentNotInAnyRemoteRepo()
236 copyTestArtifactWithParent( "target/test-classes/com/example/test/test-artifact-module-a",
237 "target/test-repository/com/example/test/test-artifact-module-a" );
239 ProjectVersionMetadata metadata = storage.readProjectVersionMetadata(
240 new ReadMetadataRequest( TEST_REPO_ID, "com.example.test", "missing-parent", "1.1" ) );
242 assertEquals( "1.1", metadata.getId() );
244 MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet( MavenProjectFacet.FACET_ID );
245 assertNotNull( facet );
246 assertEquals( "com.example.test", facet.getGroupId() );
247 assertEquals( "missing-parent", facet.getArtifactId() );
248 assertEquals( "jar", facet.getPackaging() );
250 List<String> paths = new ArrayList<>();
251 paths.add( "target/test-repository/com/example/test/test-artifact-module-a" );
252 paths.add( "target/test-repository/com/example/test/test-artifact-parent" );
253 paths.add( "target/test-repository/com/example/test/test-artifact-root" );
255 deleteTestArtifactWithParent( paths );
259 public void testGetProjectVersionMetadataWithParentSnapshotVersion()
263 copyTestArtifactWithParent( "target/test-classes/com/example/test/test-snapshot-artifact-module-a",
264 "target/test-repository/com/example/test/test-snapshot-artifact-module-a" );
266 copyTestArtifactWithParent( "src/test/resources/com/example/test/test-artifact-parent",
267 "target/test-repository/com/example/test/test-artifact-parent" );
269 copyTestArtifactWithParent( "target/test-classes/com/example/test/test-snapshot-artifact-root",
270 "target/test-repository/com/example/test/test-snapshot-artifact-root" );
272 ProjectVersionMetadata metadata = storage.readProjectVersionMetadata(
273 new ReadMetadataRequest( TEST_REPO_ID, "com.example.test", "test-snapshot-artifact-module-a",
276 MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet( MavenProjectFacet.FACET_ID );
277 assertEquals( "jar", facet.getPackaging() );
278 assertEquals( "com.example.test", facet.getParent().getGroupId() );
279 assertEquals( "test-snapshot-artifact-root", facet.getParent().getArtifactId() );
280 assertEquals( "1.1-SNAPSHOT", facet.getParent().getVersion() );
281 assertEquals( "test-snapshot-artifact-module-a", facet.getArtifactId() );
282 assertEquals( "com.example.test", facet.getGroupId() );
283 assertNull( metadata.getCiManagement() );
284 assertNotNull( metadata.getDescription() );
286 checkApacheLicense( metadata );
288 assertEquals( "1.1-SNAPSHOT", metadata.getId() );
289 assertEquals( "Test Snapshot Artifact :: Module A", metadata.getName() );
290 String path = "test-snapshot-artifact/trunk/test-snapshot-artifact-module-a";
291 assertEquals( TEST_SCM_CONN_BASE + path, metadata.getScm().getConnection() );
292 assertEquals( TEST_SCM_DEV_CONN_BASE + path, metadata.getScm().getDeveloperConnection() );
293 assertEquals( TEST_SCM_URL_BASE + path, metadata.getScm().getUrl() );
295 List<Dependency> dependencies = metadata.getDependencies();
296 assertEquals( 2, dependencies.size() );
297 assertDependency( dependencies.get( 0 ), "commons-io", "commons-io", "1.4" );
298 assertDependency( dependencies.get( 1 ), "junit", "junit", "3.8.1", "test" );
300 List<String> paths = new ArrayList<>();
301 paths.add( "target/test-repository/com/example/test/test-snapshot-artifact-module-a" );
302 paths.add( "target/test-repository/com/example/test/test-snapshot-artifact-root" );
304 deleteTestArtifactWithParent( paths );
308 public void testGetProjectVersionMetadataWithParentSnapshotVersionAndSnapNotAllowed()
311 testRepo.setSnapshots( false );
312 configuration.save( c );
313 repositoryRegistry.reload();
314 assertFalse( c.getManagedRepositories().get( 0 ).isSnapshots() );
315 copyTestArtifactWithParent( "target/test-classes/com/example/test/test-snapshot-artifact-module-a",
316 "target/test-repository/com/example/test/test-snapshot-artifact-module-a" );
320 ProjectVersionMetadata metadata = storage.readProjectVersionMetadata(
321 new ReadMetadataRequest( TEST_REPO_ID, "com.example.test", "test-snapshot-artifact-module-a",
323 fail( "Should not be found" );
325 catch ( RepositoryStorageRuntimeException e )
329 List<String> paths = new ArrayList<>();
330 paths.add( "target/test-repository/com/example/test/test-snapshot-artifact-module-a" );
331 paths.add( "target/test-repository/com/example/test/test-snapshot-artifact-root" );
333 deleteTestArtifactWithParent( paths );
335 // Tests for MRM-1411 - END
337 private void assertDependency( Dependency dependency, String groupId, String artifactId, String version )
339 assertDependency( dependency, groupId, artifactId, version, "compile" );
342 private void assertDependency( Dependency dependency, String groupId, String artifactId, String version,
345 assertEquals( artifactId, dependency.getArtifactId() );
346 assertEquals( "jar", dependency.getType() );
347 assertEquals( version, dependency.getVersion() );
348 assertEquals( groupId, dependency.getGroupId() );
349 assertEquals( scope, dependency.getScope() );
350 assertNull( dependency.getClassifier() );
351 assertNull( dependency.getSystemPath() );
354 private void assertArtifact( ArtifactMetadata artifact, String id, int size, String sha1, String md5 )
356 assertEquals( id, artifact.getId() );
357 assertEquals( md5, artifact.getMd5() );
358 assertEquals( sha1, artifact.getSha1() );
359 assertEquals( size, artifact.getSize() );
360 assertEquals( "org.codehaus.plexus", artifact.getNamespace() );
361 assertEquals( "plexus-spring", artifact.getProject() );
362 assertEquals( "1.2", artifact.getVersion() );
363 assertEquals( TEST_REPO_ID, artifact.getRepositoryId() );
366 private void assertMailingList( MailingList mailingList, String name, String archive, String post, String subscribe,
367 String unsubscribe, List<String> otherArchives, boolean allowPost )
369 assertEquals( archive, mailingList.getMainArchiveUrl() );
372 assertEquals( post, mailingList.getPostAddress() );
376 assertNull( mailingList.getPostAddress() );
378 assertEquals( subscribe, mailingList.getSubscribeAddress() );
379 assertEquals( unsubscribe, mailingList.getUnsubscribeAddress() );
380 assertEquals( name, mailingList.getName() );
381 assertEquals( otherArchives, mailingList.getOtherArchives() );
384 private void assertMailingList( String prefix, MailingList mailingList, String name, boolean allowPost,
387 List<String> otherArchives = new ArrayList<>();
388 otherArchives.add( "http://www.mail-archive.com/" + prefix + "@archiva.apache.org" );
389 if ( nabbleUrl != null )
391 otherArchives.add( nabbleUrl );
393 otherArchives.add( "http://markmail.org/list/org.apache.archiva." + prefix );
394 assertMailingList( mailingList, name, "http://mail-archives.apache.org/mod_mbox/archiva-" + prefix + "/",
395 prefix + "@archiva.apache.org", prefix + "-subscribe@archiva.apache.org",
396 prefix + "-unsubscribe@archiva.apache.org", otherArchives, allowPost );
399 private void checkApacheLicense( ProjectVersionMetadata metadata )
401 assertEquals( Arrays.asList( new License( "The Apache Software License, Version 2.0",
402 "http://www.apache.org/licenses/LICENSE-2.0.txt" ) ),
403 metadata.getLicenses() );
406 private void checkOrganizationApache( ProjectVersionMetadata metadata )
408 assertEquals( "The Apache Software Foundation", metadata.getOrganization().getName() );
409 assertEquals( "http://www.apache.org/", metadata.getOrganization().getUrl() );
412 private void deleteTestArtifactWithParent( List<String> pathsToBeDeleted )
415 for ( String path : pathsToBeDeleted )
417 Path dir = Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), path );
418 org.apache.archiva.common.utils.FileUtils.deleteDirectory( dir );
420 assertFalse(Files.exists( dir) );
422 Path dest = Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/test-repository/com/example/test/test-artifact-module-a" );
424 Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/test-repository/com/example/test/test-artifact-parent" );
425 Path rootPom = Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/test-repository/com/example/test/test-artifact-root" );
427 org.apache.archiva.common.utils.FileUtils.deleteDirectory( dest );
428 org.apache.archiva.common.utils.FileUtils.deleteDirectory( parentPom );
429 org.apache.archiva.common.utils.FileUtils.deleteDirectory( rootPom );
431 assertFalse( Files.exists(dest) );
432 assertFalse( Files.exists(parentPom) );
433 assertFalse( Files.exists(rootPom) );
436 private Path copyTestArtifactWithParent( String srcPath, String destPath )
439 Path src = Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), srcPath );
440 Path dest = Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), destPath );
442 FileUtils.copyDirectory( src.toFile(), dest.toFile() );
443 assertTrue( Files.exists(dest) );