1 package org.apache.archiva.repository.maven.metadata.storage;
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
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
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.filter.AllFilter;
28 import org.apache.archiva.filter.Filter;
29 import org.apache.archiva.metadata.model.ArtifactMetadata;
30 import org.apache.archiva.metadata.model.Dependency;
31 import org.apache.archiva.metadata.model.License;
32 import org.apache.archiva.metadata.model.MailingList;
33 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
34 import org.apache.archiva.metadata.repository.storage.ReadMetadataRequest;
35 import org.apache.archiva.metadata.repository.storage.RepositoryStorageRuntimeException;
36 import org.apache.archiva.maven.proxy.WagonFactory;
37 import org.apache.archiva.maven.proxy.WagonFactoryRequest;
38 import org.apache.archiva.repository.ReleaseScheme;
39 import org.apache.archiva.repository.RepositoryRegistry;
40 import org.apache.archiva.repository.base.RepositoryHandlerDependencies;
41 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
42 import org.apache.commons.io.FileUtils;
43 import org.apache.maven.wagon.Wagon;
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 import org.springframework.test.context.ContextConfiguration;
49 import javax.inject.Inject;
50 import javax.inject.Named;
51 import java.io.IOException;
52 import java.nio.file.Files;
53 import java.nio.file.Path;
54 import java.nio.file.Paths;
55 import java.util.ArrayList;
56 import java.util.Arrays;
57 import java.util.HashMap;
58 import java.util.List;
60 import static org.mockito.Mockito.mock;
61 import static org.mockito.Mockito.when;
63 @RunWith ( ArchivaSpringJUnit4ClassRunner.class )
64 @ContextConfiguration ( { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
65 public class Maven2RepositoryMetadataResolverMRM1411Test
68 private static final Filter<String> ALL = new AllFilter<String>();
71 @Named ( "repositoryStorage#maven2")
72 private Maven2RepositoryStorage storage;
74 private static final String TEST_REPO_ID = "test";
76 private static final String TEST_REMOTE_REPO_ID = "central";
78 private static final String ASF_SCM_CONN_BASE = "scm:svn:http://svn.apache.org/repos/asf/";
80 private static final String ASF_SCM_DEV_CONN_BASE = "scm:svn:https://svn.apache.org/repos/asf/";
82 private static final String ASF_SCM_VIEWVC_BASE = "http://svn.apache.org/viewvc/";
84 private static final String TEST_SCM_CONN_BASE = "scm:svn:http://svn.example.com/repos/";
86 private static final String TEST_SCM_DEV_CONN_BASE = "scm:svn:https://svn.example.com/repos/";
88 private static final String TEST_SCM_URL_BASE = "http://svn.example.com/repos/";
90 private static final String EMPTY_MD5 = "d41d8cd98f00b204e9800998ecf8427e";
92 private static final String EMPTY_SHA1 = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
95 @Named ( "archivaConfiguration#default" )
96 private ArchivaConfiguration configuration;
99 RepositoryRegistry repositoryRegistry;
101 @SuppressWarnings( "unused" )
103 RepositoryHandlerDependencies repositoryHandlerDependencies;
106 private WagonFactory wagonFactory;
108 ManagedRepositoryConfiguration testRepo;
119 c = new Configuration();
120 testRepo = new ManagedRepositoryConfiguration();
121 testRepo.setId( TEST_REPO_ID );
122 testRepo.setLocation( Paths.get( "target/test-repository" ).toAbsolutePath().toString() );
123 testRepo.setReleases( true );
124 testRepo.setSnapshots( true );
125 c.addManagedRepository( testRepo );
127 RemoteRepositoryConfiguration testRemoteRepo = new RemoteRepositoryConfiguration();
128 testRemoteRepo.setId( TEST_REMOTE_REPO_ID );
129 testRemoteRepo.setLayout( "default" );
130 testRemoteRepo.setName( "Central Repository" );
131 testRemoteRepo.setUrl( "http://central.repo.com/maven2" );
132 testRemoteRepo.setTimeout( 10 );
133 c.addRemoteRepository( testRemoteRepo );
135 ProxyConnectorConfiguration proxyConnector = new ProxyConnectorConfiguration();
136 proxyConnector.setSourceRepoId( TEST_REPO_ID );
137 proxyConnector.setTargetRepoId( TEST_REMOTE_REPO_ID );
138 proxyConnector.setDisabled( false );
139 c.addProxyConnector( proxyConnector );
141 configuration.save( c );
143 repositoryRegistry.reload();
145 assertTrue( c.getManagedRepositories().get( 0 ).isSnapshots() );
146 assertTrue( c.getManagedRepositories().get( 0 ).isReleases() );
148 wagonFactory = mock( WagonFactory.class );
150 assertNotNull( storage );
151 storage.setWagonFactory( wagonFactory );
153 Wagon wagon = new MockWagon();
154 when( wagonFactory.getWagon(
155 new WagonFactoryRequest( "wagon#http", new HashMap<String, String>() ) ) ).thenReturn( wagon );
158 // Tests for MRM-1411 - START
160 public void testGetProjectVersionMetadataWithParentSuccessful()
163 assertNotNull( storage );
164 copyTestArtifactWithParent( "target/test-classes/com/example/test/test-artifact-module-a",
165 "target/test-repository/com/example/test/test-artifact-module-a" );
166 copyTestArtifactWithParent( "src/test/resources/com/example/test/test-artifact-parent",
167 "target/test-repository/com/example/test/test-artifact-parent" );
169 copyTestArtifactWithParent( "target/test-classes/com/example/test/test-artifact-root",
170 "target/test-repository/com/example/test/test-artifact-root" );
172 ProjectVersionMetadata metadata = storage.readProjectVersionMetadata(
173 new ReadMetadataRequest( TEST_REPO_ID, "com.example.test", "test-artifact-module-a", "1.0" ) );
175 MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet( MavenProjectFacet.FACET_ID );
176 assertEquals( "jar", facet.getPackaging() );
177 assertEquals( "http://maven.apache.org", metadata.getUrl() );
178 assertEquals( "com.example.test", facet.getParent().getGroupId() );
179 assertEquals( "test-artifact-root", facet.getParent().getArtifactId() );
180 assertEquals( "1.0", facet.getParent().getVersion() );
181 assertEquals( "test-artifact-module-a", facet.getArtifactId() );
182 assertEquals( "com.example.test", facet.getGroupId() );
183 assertNull( metadata.getCiManagement() );
184 assertNotNull( metadata.getDescription() );
186 checkApacheLicense( metadata );
188 assertEquals( "1.0", metadata.getId() );
189 assertEquals( "Test Artifact :: Module A", metadata.getName() );
190 String path = "test-artifact/trunk/test-artifact-module-a";
191 assertEquals( TEST_SCM_CONN_BASE + path, metadata.getScm().getConnection() );
192 assertEquals( TEST_SCM_DEV_CONN_BASE + path, metadata.getScm().getDeveloperConnection() );
193 assertEquals( TEST_SCM_URL_BASE + path, metadata.getScm().getUrl() );
195 List<Dependency> dependencies = metadata.getDependencies();
196 assertEquals( 2, dependencies.size() );
197 assertDependency( dependencies.get( 0 ), "commons-io", "commons-io", "1.4" );
198 assertDependency( dependencies.get( 1 ), "junit", "junit", "3.8.1", "test" );
200 List<String> paths = new ArrayList<>();
201 paths.add( "target/test-repository/com/example/test/test-artifact-module-a" );
202 paths.add( "target/test-repository/com/example/test/test-artifact-parent" );
203 paths.add( "target/test-repository/com/example/test/test-artifact-root" );
205 deleteTestArtifactWithParent( paths );
209 public void testGetProjectVersionMetadataWithParentNoRemoteReposConfigured()
212 assertNotNull( storage );
213 // remove configuration
214 Configuration config = configuration.getConfiguration();
215 RemoteRepositoryConfiguration remoteRepo = config.findRemoteRepositoryById( TEST_REMOTE_REPO_ID );
216 config.removeRemoteRepository( remoteRepo );
218 configuration.save( config );
219 assertNotNull( storage );
221 copyTestArtifactWithParent( "target/test-classes/com/example/test/test-artifact-module-a",
222 "target/test-repository/com/example/test/test-artifact-module-a" );
224 ProjectVersionMetadata metadata = storage.readProjectVersionMetadata(
225 new ReadMetadataRequest( TEST_REPO_ID, "com.example.test", "test-artifact-module-a", "1.0" ) );
226 assertEquals( "1.0", metadata.getId() );
228 MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet( MavenProjectFacet.FACET_ID );
229 assertNotNull( facet );
230 assertEquals( "com.example.test", facet.getGroupId() );
231 assertEquals( "test-artifact-module-a", facet.getArtifactId() );
232 assertEquals( "jar", facet.getPackaging() );
234 List<String> paths = new ArrayList<>();
235 paths.add( "target/test-repository/com/example/test/test-artifact-module-a" );
236 paths.add( "target/test-repository/com/example/test/test-artifact-parent" );
237 paths.add( "target/test-repository/com/example/test/test-artifact-root" );
239 deleteTestArtifactWithParent( paths );
243 public void testGetProjectVersionMetadataWithParentNotInAnyRemoteRepo()
246 assertNotNull( storage );
247 copyTestArtifactWithParent( "target/test-classes/com/example/test/test-artifact-module-a",
248 "target/test-repository/com/example/test/test-artifact-module-a" );
250 ProjectVersionMetadata metadata = storage.readProjectVersionMetadata(
251 new ReadMetadataRequest( TEST_REPO_ID, "com.example.test", "missing-parent", "1.1" ) );
253 assertEquals( "1.1", metadata.getId() );
255 MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet( MavenProjectFacet.FACET_ID );
256 assertNotNull( facet );
257 assertEquals( "com.example.test", facet.getGroupId() );
258 assertEquals( "missing-parent", facet.getArtifactId() );
259 assertEquals( "jar", facet.getPackaging() );
261 List<String> paths = new ArrayList<>();
262 paths.add( "target/test-repository/com/example/test/test-artifact-module-a" );
263 paths.add( "target/test-repository/com/example/test/test-artifact-parent" );
264 paths.add( "target/test-repository/com/example/test/test-artifact-root" );
266 deleteTestArtifactWithParent( paths );
270 public void testGetProjectVersionMetadataWithParentSnapshotVersion()
274 copyTestArtifactWithParent( "target/test-classes/com/example/test/test-snapshot-artifact-module-a",
275 "target/test-repository/com/example/test/test-snapshot-artifact-module-a" );
277 copyTestArtifactWithParent( "src/test/resources/com/example/test/test-artifact-parent",
278 "target/test-repository/com/example/test/test-artifact-parent" );
280 copyTestArtifactWithParent( "target/test-classes/com/example/test/test-snapshot-artifact-root",
281 "target/test-repository/com/example/test/test-snapshot-artifact-root" );
283 ProjectVersionMetadata metadata = storage.readProjectVersionMetadata(
284 new ReadMetadataRequest( TEST_REPO_ID, "com.example.test", "test-snapshot-artifact-module-a",
287 MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet( MavenProjectFacet.FACET_ID );
288 assertEquals( "jar", facet.getPackaging() );
289 assertEquals( "com.example.test", facet.getParent().getGroupId() );
290 assertEquals( "test-snapshot-artifact-root", facet.getParent().getArtifactId() );
291 assertEquals( "1.1-SNAPSHOT", facet.getParent().getVersion() );
292 assertEquals( "test-snapshot-artifact-module-a", facet.getArtifactId() );
293 assertEquals( "com.example.test", facet.getGroupId() );
294 assertNull( metadata.getCiManagement() );
295 assertNotNull( metadata.getDescription() );
297 checkApacheLicense( metadata );
299 assertEquals( "1.1-SNAPSHOT", metadata.getId() );
300 assertEquals( "Test Snapshot Artifact :: Module A", metadata.getName() );
301 String path = "test-snapshot-artifact/trunk/test-snapshot-artifact-module-a";
302 assertEquals( TEST_SCM_CONN_BASE + path, metadata.getScm().getConnection() );
303 assertEquals( TEST_SCM_DEV_CONN_BASE + path, metadata.getScm().getDeveloperConnection() );
304 assertEquals( TEST_SCM_URL_BASE + path, metadata.getScm().getUrl() );
306 List<Dependency> dependencies = metadata.getDependencies();
307 assertEquals( 2, dependencies.size() );
308 assertDependency( dependencies.get( 0 ), "commons-io", "commons-io", "1.4" );
309 assertDependency( dependencies.get( 1 ), "junit", "junit", "3.8.1", "test" );
311 List<String> paths = new ArrayList<>();
312 paths.add( "target/test-repository/com/example/test/test-snapshot-artifact-module-a" );
313 paths.add( "target/test-repository/com/example/test/test-snapshot-artifact-root" );
315 deleteTestArtifactWithParent( paths );
319 public void testGetProjectVersionMetadataWithParentSnapshotVersionAndSnapNotAllowed()
322 testRepo.setSnapshots( false );
323 configuration.save( c );
324 repositoryRegistry.reload();
325 assertFalse(repositoryRegistry.getManagedRepository(testRepo.getId()).getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT));
326 assertFalse( c.getManagedRepositories().get( 0 ).isSnapshots() );
327 copyTestArtifactWithParent( "target/test-classes/com/example/test/test-snapshot-artifact-module-a",
328 "target/test-repository/com/example/test/test-snapshot-artifact-module-a" );
332 ProjectVersionMetadata metadata = storage.readProjectVersionMetadata(
333 new ReadMetadataRequest( TEST_REPO_ID, "com.example.test", "test-snapshot-artifact-module-a",
335 fail( "Should not be found" );
337 catch ( RepositoryStorageRuntimeException e )
341 List<String> paths = new ArrayList<>();
342 paths.add( "target/test-repository/com/example/test/test-snapshot-artifact-module-a" );
343 paths.add( "target/test-repository/com/example/test/test-snapshot-artifact-root" );
345 deleteTestArtifactWithParent( paths );
347 // Tests for MRM-1411 - END
349 private void assertDependency( Dependency dependency, String groupId, String artifactId, String version )
351 assertDependency( dependency, groupId, artifactId, version, "compile" );
354 private void assertDependency( Dependency dependency, String groupId, String artifactId, String version,
357 assertEquals( artifactId, dependency.getArtifactId() );
358 assertEquals( "jar", dependency.getType() );
359 assertEquals( version, dependency.getVersion() );
360 assertEquals( groupId, dependency.getNamespace() );
361 assertEquals( scope, dependency.getScope() );
362 assertNull( dependency.getClassifier() );
363 assertNull( dependency.getSystemPath() );
366 private void assertArtifact( ArtifactMetadata artifact, String id, int size, String sha1, String md5 )
368 assertEquals( id, artifact.getId() );
369 assertEquals( md5, artifact.getMd5() );
370 assertEquals( sha1, artifact.getSha1() );
371 assertEquals( size, artifact.getSize() );
372 assertEquals( "org.codehaus.plexus", artifact.getNamespace() );
373 assertEquals( "plexus-spring", artifact.getProject() );
374 assertEquals( "1.2", artifact.getVersion() );
375 assertEquals( TEST_REPO_ID, artifact.getRepositoryId() );
378 private void assertMailingList( MailingList mailingList, String name, String archive, String post, String subscribe,
379 String unsubscribe, List<String> otherArchives, boolean allowPost )
381 assertEquals( archive, mailingList.getMainArchiveUrl() );
384 assertEquals( post, mailingList.getPostAddress() );
388 assertNull( mailingList.getPostAddress() );
390 assertEquals( subscribe, mailingList.getSubscribeAddress() );
391 assertEquals( unsubscribe, mailingList.getUnsubscribeAddress() );
392 assertEquals( name, mailingList.getName() );
393 assertEquals( otherArchives, mailingList.getOtherArchives() );
396 private void assertMailingList( String prefix, MailingList mailingList, String name, boolean allowPost,
399 List<String> otherArchives = new ArrayList<>();
400 otherArchives.add( "http://www.mail-archive.com/" + prefix + "@archiva.apache.org" );
401 if ( nabbleUrl != null )
403 otherArchives.add( nabbleUrl );
405 otherArchives.add( "http://markmail.org/list/org.apache.archiva." + prefix );
406 assertMailingList( mailingList, name, "http://mail-archives.apache.org/mod_mbox/archiva-" + prefix + "/",
407 prefix + "@archiva.apache.org", prefix + "-subscribe@archiva.apache.org",
408 prefix + "-unsubscribe@archiva.apache.org", otherArchives, allowPost );
411 private void checkApacheLicense( ProjectVersionMetadata metadata )
413 assertEquals( Arrays.asList( new License( "The Apache Software License, Version 2.0",
414 "http://www.apache.org/licenses/LICENSE-2.0.txt" ) ),
415 metadata.getLicenses() );
418 private void checkOrganizationApache( ProjectVersionMetadata metadata )
420 assertEquals( "The Apache Software Foundation", metadata.getOrganization().getName() );
421 assertEquals( "http://www.apache.org/", metadata.getOrganization().getUrl() );
424 private void deleteTestArtifactWithParent( List<String> pathsToBeDeleted )
427 for ( String path : pathsToBeDeleted )
429 Path dir = Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), path );
430 org.apache.archiva.common.utils.FileUtils.deleteDirectory( dir );
432 assertFalse(Files.exists( dir) );
434 Path dest = Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/test-repository/com/example/test/test-artifact-module-a" );
436 Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/test-repository/com/example/test/test-artifact-parent" );
437 Path rootPom = Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/test-repository/com/example/test/test-artifact-root" );
439 org.apache.archiva.common.utils.FileUtils.deleteDirectory( dest );
440 org.apache.archiva.common.utils.FileUtils.deleteDirectory( parentPom );
441 org.apache.archiva.common.utils.FileUtils.deleteDirectory( rootPom );
443 assertFalse( Files.exists(dest) );
444 assertFalse( Files.exists(parentPom) );
445 assertFalse( Files.exists(rootPom) );
448 private Path copyTestArtifactWithParent( String srcPath, String destPath )
451 Path src = Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), srcPath );
452 Path dest = Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), destPath );
454 FileUtils.copyDirectory( src.toFile(), dest.toFile() );
455 assertTrue( Files.exists(dest) );