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.common.utils.FileUtil;
24 import org.apache.archiva.configuration.ArchivaConfiguration;
25 import org.apache.archiva.configuration.Configuration;
26 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
27 import org.apache.archiva.configuration.ProxyConnectorConfiguration;
28 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
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.filter.AllFilter;
35 import org.apache.archiva.metadata.repository.filter.Filter;
36 import org.apache.archiva.metadata.repository.storage.ReadMetadataRequest;
37 import org.apache.archiva.metadata.repository.storage.RepositoryStorageRuntimeException;
38 import org.apache.archiva.proxy.common.WagonFactory;
39 import org.apache.archiva.proxy.common.WagonFactoryRequest;
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;
51 import java.io.IOException;
52 import java.util.ArrayList;
53 import java.util.Arrays;
54 import java.util.HashMap;
55 import java.util.List;
57 import static org.mockito.Mockito.mock;
58 import static org.mockito.Mockito.when;
60 @RunWith ( ArchivaSpringJUnit4ClassRunner.class )
61 @ContextConfiguration ( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
62 public class Maven2RepositoryMetadataResolverMRM1411Test
65 private static final Filter<String> ALL = new AllFilter<String>();
68 @Named ( value = "repositoryStorage#maven2" )
69 private Maven2RepositoryStorage storage;
71 private static final String TEST_REPO_ID = "test";
73 private static final String TEST_REMOTE_REPO_ID = "central";
75 private static final String ASF_SCM_CONN_BASE = "scm:svn:http://svn.apache.org/repos/asf/";
77 private static final String ASF_SCM_DEV_CONN_BASE = "scm:svn:https://svn.apache.org/repos/asf/";
79 private static final String ASF_SCM_VIEWVC_BASE = "http://svn.apache.org/viewvc/";
81 private static final String TEST_SCM_CONN_BASE = "scm:svn:http://svn.example.com/repos/";
83 private static final String TEST_SCM_DEV_CONN_BASE = "scm:svn:https://svn.example.com/repos/";
85 private static final String TEST_SCM_URL_BASE = "http://svn.example.com/repos/";
87 private static final String EMPTY_MD5 = "d41d8cd98f00b204e9800998ecf8427e";
89 private static final String EMPTY_SHA1 = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
92 private ArchivaConfiguration configuration;
94 private WagonFactory wagonFactory;
96 ManagedRepositoryConfiguration testRepo;
107 c = new Configuration();
108 testRepo = new ManagedRepositoryConfiguration();
109 testRepo.setId( TEST_REPO_ID );
110 testRepo.setLocation( new File( "target/test-repository" ).getAbsolutePath() );
111 testRepo.setReleases( true );
112 testRepo.setSnapshots( true );
113 c.addManagedRepository( testRepo );
115 RemoteRepositoryConfiguration testRemoteRepo = new RemoteRepositoryConfiguration();
116 testRemoteRepo.setId( TEST_REMOTE_REPO_ID );
117 testRemoteRepo.setLayout( "default" );
118 testRemoteRepo.setName( "Central Repository" );
119 testRemoteRepo.setUrl( "http://central.repo.com/maven2" );
120 testRemoteRepo.setTimeout( 10 );
121 c.addRemoteRepository( testRemoteRepo );
123 ProxyConnectorConfiguration proxyConnector = new ProxyConnectorConfiguration();
124 proxyConnector.setSourceRepoId( TEST_REPO_ID );
125 proxyConnector.setTargetRepoId( TEST_REMOTE_REPO_ID );
126 proxyConnector.setDisabled( false );
127 c.addProxyConnector( proxyConnector );
129 configuration.save( c );
131 assertTrue( c.getManagedRepositories().get( 0 ).isSnapshots() );
132 assertTrue( c.getManagedRepositories().get( 0 ).isReleases() );
134 wagonFactory = mock( WagonFactory.class );
136 storage.setWagonFactory( wagonFactory );
138 Wagon wagon = new MockWagon();
139 when( wagonFactory.getWagon(
140 new WagonFactoryRequest( "wagon#http", new HashMap<String, String>() ) ) ).thenReturn( wagon );
143 // Tests for MRM-1411 - START
145 public void testGetProjectVersionMetadataWithParentSuccessful()
148 copyTestArtifactWithParent( "target/test-classes/com/example/test/test-artifact-module-a",
149 "target/test-repository/com/example/test/test-artifact-module-a" );
151 ProjectVersionMetadata metadata = storage.readProjectVersionMetadata(
152 new ReadMetadataRequest( TEST_REPO_ID, "com.example.test", "test-artifact-module-a", "1.0" ) );
154 MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet( MavenProjectFacet.FACET_ID );
155 assertEquals( "jar", facet.getPackaging() );
156 assertEquals( "http://maven.apache.org", metadata.getUrl() );
157 assertEquals( "com.example.test", facet.getParent().getGroupId() );
158 assertEquals( "test-artifact-root", facet.getParent().getArtifactId() );
159 assertEquals( "1.0", facet.getParent().getVersion() );
160 assertEquals( "test-artifact-module-a", facet.getArtifactId() );
161 assertEquals( "com.example.test", facet.getGroupId() );
162 assertNull( metadata.getCiManagement() );
163 assertNotNull( metadata.getDescription() );
165 checkApacheLicense( metadata );
167 assertEquals( "1.0", metadata.getId() );
168 assertEquals( "Test Artifact :: Module A", metadata.getName() );
169 String path = "test-artifact/trunk/test-artifact-module-a";
170 assertEquals( TEST_SCM_CONN_BASE + path, metadata.getScm().getConnection() );
171 assertEquals( TEST_SCM_DEV_CONN_BASE + path, metadata.getScm().getDeveloperConnection() );
172 assertEquals( TEST_SCM_URL_BASE + path, metadata.getScm().getUrl() );
174 List<Dependency> dependencies = metadata.getDependencies();
175 assertEquals( 2, dependencies.size() );
176 assertDependency( dependencies.get( 0 ), "commons-io", "commons-io", "1.4" );
177 assertDependency( dependencies.get( 1 ), "junit", "junit", "3.8.1", "test" );
179 List<String> paths = new ArrayList<String>();
180 paths.add( "target/test-repository/com/example/test/test-artifact-module-a" );
181 paths.add( "target/test-repository/com/example/test/test-artifact-parent" );
182 paths.add( "target/test-repository/com/example/test/test-artifact-root" );
184 deleteTestArtifactWithParent( paths );
188 public void testGetProjectVersionMetadataWithParentNoRemoteReposConfigured()
191 // remove configuration
192 Configuration config = configuration.getConfiguration();
193 RemoteRepositoryConfiguration remoteRepo = config.findRemoteRepositoryById( TEST_REMOTE_REPO_ID );
194 config.removeRemoteRepository( remoteRepo );
196 configuration.save( config );
198 copyTestArtifactWithParent( "target/test-classes/com/example/test/test-artifact-module-a",
199 "target/test-repository/com/example/test/test-artifact-module-a" );
201 ProjectVersionMetadata metadata = storage.readProjectVersionMetadata(
202 new ReadMetadataRequest( TEST_REPO_ID, "com.example.test", "test-artifact-module-a", "1.0" ) );
203 assertEquals( "1.0", metadata.getId() );
205 MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet( MavenProjectFacet.FACET_ID );
206 assertNotNull( facet );
207 assertEquals( "com.example.test", facet.getGroupId() );
208 assertEquals( "test-artifact-module-a", facet.getArtifactId() );
209 assertEquals( "jar", facet.getPackaging() );
211 List<String> paths = new ArrayList<String>();
212 paths.add( "target/test-repository/com/example/test/test-artifact-module-a" );
213 paths.add( "target/test-repository/com/example/test/test-artifact-parent" );
214 paths.add( "target/test-repository/com/example/test/test-artifact-root" );
216 deleteTestArtifactWithParent( paths );
220 public void testGetProjectVersionMetadataWithParentNotInAnyRemoteRepo()
223 copyTestArtifactWithParent( "target/test-classes/com/example/test/test-artifact-module-a",
224 "target/test-repository/com/example/test/test-artifact-module-a" );
226 ProjectVersionMetadata metadata = storage.readProjectVersionMetadata(
227 new ReadMetadataRequest( TEST_REPO_ID, "com.example.test", "missing-parent", "1.1" ) );
229 assertEquals( "1.1", metadata.getId() );
231 MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet( MavenProjectFacet.FACET_ID );
232 assertNotNull( facet );
233 assertEquals( "com.example.test", facet.getGroupId() );
234 assertEquals( "missing-parent", facet.getArtifactId() );
235 assertEquals( "jar", facet.getPackaging() );
237 List<String> paths = new ArrayList<String>();
238 paths.add( "target/test-repository/com/example/test/test-artifact-module-a" );
239 paths.add( "target/test-repository/com/example/test/test-artifact-parent" );
240 paths.add( "target/test-repository/com/example/test/test-artifact-root" );
242 deleteTestArtifactWithParent( paths );
246 public void testGetProjectVersionMetadataWithParentSnapshotVersion()
250 copyTestArtifactWithParent( "target/test-classes/com/example/test/test-snapshot-artifact-module-a",
251 "target/test-repository/com/example/test/test-snapshot-artifact-module-a" );
253 //copyTestArtifactWithParent( "target/test-classes/com/example/test/test-snapshot-artifact-root",
254 // "target/test-repository/com/example/test/test-snapshot-artifact-root" );
256 ProjectVersionMetadata metadata = storage.readProjectVersionMetadata(
257 new ReadMetadataRequest( TEST_REPO_ID, "com.example.test", "test-snapshot-artifact-module-a",
260 MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet( MavenProjectFacet.FACET_ID );
261 assertEquals( "jar", facet.getPackaging() );
262 assertEquals( "com.example.test", facet.getParent().getGroupId() );
263 assertEquals( "test-snapshot-artifact-root", facet.getParent().getArtifactId() );
264 assertEquals( "1.1-SNAPSHOT", facet.getParent().getVersion() );
265 assertEquals( "test-snapshot-artifact-module-a", facet.getArtifactId() );
266 assertEquals( "com.example.test", facet.getGroupId() );
267 assertNull( metadata.getCiManagement() );
268 assertNotNull( metadata.getDescription() );
270 checkApacheLicense( metadata );
272 assertEquals( "1.1-SNAPSHOT", metadata.getId() );
273 assertEquals( "Test Snapshot Artifact :: Module A", metadata.getName() );
274 String path = "test-snapshot-artifact/trunk/test-snapshot-artifact-module-a";
275 assertEquals( TEST_SCM_CONN_BASE + path, metadata.getScm().getConnection() );
276 assertEquals( TEST_SCM_DEV_CONN_BASE + path, metadata.getScm().getDeveloperConnection() );
277 assertEquals( TEST_SCM_URL_BASE + path, metadata.getScm().getUrl() );
279 List<Dependency> dependencies = metadata.getDependencies();
280 assertEquals( 2, dependencies.size() );
281 assertDependency( dependencies.get( 0 ), "commons-io", "commons-io", "1.4" );
282 assertDependency( dependencies.get( 1 ), "junit", "junit", "3.8.1", "test" );
284 List<String> paths = new ArrayList<String>();
285 paths.add( "target/test-repository/com/example/test/test-snapshot-artifact-module-a" );
286 paths.add( "target/test-repository/com/example/test/test-snapshot-artifact-root" );
288 deleteTestArtifactWithParent( paths );
292 public void testGetProjectVersionMetadataWithParentSnapshotVersionAndSnapNotAllowed()
295 testRepo.setSnapshots( false );
296 configuration.save( c );
297 assertFalse( c.getManagedRepositories().get( 0 ).isSnapshots() );
298 copyTestArtifactWithParent( "target/test-classes/com/example/test/test-snapshot-artifact-module-a",
299 "target/test-repository/com/example/test/test-snapshot-artifact-module-a" );
303 ProjectVersionMetadata metadata = storage.readProjectVersionMetadata(
304 new ReadMetadataRequest( TEST_REPO_ID, "com.example.test", "test-snapshot-artifact-module-a",
306 fail( "Should not be found" );
308 catch ( RepositoryStorageRuntimeException e )
312 List<String> paths = new ArrayList<String>();
313 paths.add( "target/test-repository/com/example/test/test-snapshot-artifact-module-a" );
314 paths.add( "target/test-repository/com/example/test/test-snapshot-artifact-root" );
316 deleteTestArtifactWithParent( paths );
318 // Tests for MRM-1411 - END
320 private void assertDependency( Dependency dependency, String groupId, String artifactId, String version )
322 assertDependency( dependency, groupId, artifactId, version, "compile" );
325 private void assertDependency( Dependency dependency, String groupId, String artifactId, String version,
328 assertEquals( artifactId, dependency.getArtifactId() );
329 assertEquals( "jar", dependency.getType() );
330 assertEquals( version, dependency.getVersion() );
331 assertEquals( groupId, dependency.getGroupId() );
332 assertEquals( scope, dependency.getScope() );
333 assertNull( dependency.getClassifier() );
334 assertNull( dependency.getSystemPath() );
337 private void assertArtifact( ArtifactMetadata artifact, String id, int size, String sha1, String md5 )
339 assertEquals( id, artifact.getId() );
340 assertEquals( md5, artifact.getMd5() );
341 assertEquals( sha1, artifact.getSha1() );
342 assertEquals( size, artifact.getSize() );
343 assertEquals( "org.codehaus.plexus", artifact.getNamespace() );
344 assertEquals( "plexus-spring", artifact.getProject() );
345 assertEquals( "1.2", artifact.getVersion() );
346 assertEquals( TEST_REPO_ID, artifact.getRepositoryId() );
349 private void assertMailingList( MailingList mailingList, String name, String archive, String post, String subscribe,
350 String unsubscribe, List<String> otherArchives, boolean allowPost )
352 assertEquals( archive, mailingList.getMainArchiveUrl() );
355 assertEquals( post, mailingList.getPostAddress() );
359 assertNull( mailingList.getPostAddress() );
361 assertEquals( subscribe, mailingList.getSubscribeAddress() );
362 assertEquals( unsubscribe, mailingList.getUnsubscribeAddress() );
363 assertEquals( name, mailingList.getName() );
364 assertEquals( otherArchives, mailingList.getOtherArchives() );
367 private void assertMailingList( String prefix, MailingList mailingList, String name, boolean allowPost,
370 List<String> otherArchives = new ArrayList<String>();
371 otherArchives.add( "http://www.mail-archive.com/" + prefix + "@archiva.apache.org" );
372 if ( nabbleUrl != null )
374 otherArchives.add( nabbleUrl );
376 otherArchives.add( "http://markmail.org/list/org.apache.archiva." + prefix );
377 assertMailingList( mailingList, name, "http://mail-archives.apache.org/mod_mbox/archiva-" + prefix + "/",
378 prefix + "@archiva.apache.org", prefix + "-subscribe@archiva.apache.org",
379 prefix + "-unsubscribe@archiva.apache.org", otherArchives, allowPost );
382 private void checkApacheLicense( ProjectVersionMetadata metadata )
384 assertEquals( Arrays.asList( new License( "The Apache Software License, Version 2.0",
385 "http://www.apache.org/licenses/LICENSE-2.0.txt" ) ),
386 metadata.getLicenses() );
389 private void checkOrganizationApache( ProjectVersionMetadata metadata )
391 assertEquals( "The Apache Software Foundation", metadata.getOrganization().getName() );
392 assertEquals( "http://www.apache.org/", metadata.getOrganization().getUrl() );
395 private void deleteTestArtifactWithParent( List<String> pathsToBeDeleted )
398 for ( String path : pathsToBeDeleted )
400 File dir = new File( FileUtil.getBasedir(), path );
401 FileUtils.deleteDirectory( dir );
403 assertFalse( dir.exists() );
405 File dest = new File( FileUtil.getBasedir(), "target/test-repository/com/example/test/test-artifact-module-a" );
407 new File( FileUtil.getBasedir(), "target/test-repository/com/example/test/test-artifact-parent" );
408 File rootPom = new File( FileUtil.getBasedir(), "target/test-repository/com/example/test/test-artifact-root" );
410 FileUtils.deleteDirectory( dest );
411 FileUtils.deleteDirectory( parentPom );
412 FileUtils.deleteDirectory( rootPom );
414 assertFalse( dest.exists() );
415 assertFalse( parentPom.exists() );
416 assertFalse( rootPom.exists() );
419 private File copyTestArtifactWithParent( String srcPath, String destPath )
422 File src = new File( FileUtil.getBasedir(), srcPath );
423 File dest = new File( FileUtil.getBasedir(), destPath );
425 FileUtils.copyDirectory( src, dest );
426 assertTrue( dest.exists() );