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.configuration.RepositoryGroupConfiguration;
30 import org.apache.archiva.metadata.model.ArtifactMetadata;
31 import org.apache.archiva.metadata.model.Dependency;
32 import org.apache.archiva.metadata.model.License;
33 import org.apache.archiva.metadata.model.MailingList;
34 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
35 import org.apache.archiva.metadata.repository.filter.AllFilter;
36 import org.apache.archiva.metadata.repository.filter.Filter;
37 import org.apache.archiva.metadata.repository.storage.ReadMetadataRequest;
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.List;
56 import static org.mockito.Mockito.mock;
57 import static org.mockito.Mockito.when;
59 @RunWith (ArchivaSpringJUnit4ClassRunner.class)
60 @ContextConfiguration (locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" })
61 public class Maven2RepositoryMetadataResolverMRM1411RepoGroupTest
64 private static final Filter<String> ALL = new AllFilter<String>();
67 @Named (value = "repositoryStorage#maven2")
68 private Maven2RepositoryStorage storage;
70 private static final String TEST_REPO_ID = "test";
72 private static final String TEST_SNAP_REPO_ID = "tests";
74 private static final String TEST_REPO_GROUP_ID = "testrg";
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 private ArchivaConfiguration configuration;
97 private WagonFactory wagonFactory;
99 ManagedRepositoryConfiguration testRepo;
101 ManagedRepositoryConfiguration testRepoS;
112 c = new Configuration();
114 testRepo = new ManagedRepositoryConfiguration();
115 testRepo.setId( TEST_REPO_ID );
116 testRepo.setLocation( new File( "target/test-repository" ).getAbsolutePath() );
117 testRepo.setReleases( true );
118 testRepo.setSnapshots( false );
119 c.addManagedRepository( testRepo );
121 testRepoS = new ManagedRepositoryConfiguration();
122 testRepoS.setId( TEST_SNAP_REPO_ID );
123 testRepoS.setLocation( new File( "target/test-repositorys" ).getAbsolutePath() );
124 testRepoS.setReleases( false );
125 testRepoS.setSnapshots( true );
126 c.addManagedRepository( testRepoS );
128 RemoteRepositoryConfiguration testRemoteRepo = new RemoteRepositoryConfiguration();
129 testRemoteRepo.setId( TEST_REMOTE_REPO_ID );
130 testRemoteRepo.setLayout( "default" );
131 testRemoteRepo.setName( "Central Repository" );
132 testRemoteRepo.setUrl( "http://central.repo.com/maven2" );
133 testRemoteRepo.setTimeout( 10 );
134 c.addRemoteRepository( testRemoteRepo );
136 ProxyConnectorConfiguration proxyConnector = new ProxyConnectorConfiguration();
137 proxyConnector.setSourceRepoId( TEST_REPO_ID );
138 proxyConnector.setTargetRepoId( TEST_REMOTE_REPO_ID );
139 proxyConnector.setDisabled( false );
140 c.addProxyConnector( proxyConnector );
142 ProxyConnectorConfiguration proxyConnectors = new ProxyConnectorConfiguration();
143 proxyConnectors.setSourceRepoId( TEST_SNAP_REPO_ID );
144 proxyConnectors.setTargetRepoId( TEST_REMOTE_REPO_ID );
145 proxyConnectors.setDisabled( false );
146 c.addProxyConnector( proxyConnectors );
148 List<String> repos = new ArrayList<String>();
149 repos.add( TEST_REPO_ID );
150 repos.add( TEST_SNAP_REPO_ID );
152 RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
153 repoGroup.setId( TEST_REPO_GROUP_ID );
154 repoGroup.setRepositories( repos );
155 c.addRepositoryGroup( repoGroup );
157 configuration.save( c );
159 assertFalse( c.getManagedRepositories().get( 0 ).isSnapshots() );
160 assertTrue( c.getManagedRepositories().get( 0 ).isReleases() );
162 assertTrue( c.getManagedRepositories().get( 1 ).isSnapshots() );
163 assertFalse( c.getManagedRepositories().get( 1 ).isReleases() );
165 wagonFactory = mock( WagonFactory.class );
167 storage.setWagonFactory( wagonFactory );
169 Wagon wagon = new MockWagon();
170 when( wagonFactory.getWagon( new WagonFactoryRequest().protocol( "wagon#http" ) ) ).thenReturn( wagon );
173 // Tests for MRM-1411 - START
175 public void testGetProjectVersionMetadataWithParentSuccessful()
178 copyTestArtifactWithParent( "target/test-classes/com/example/test/test-artifact-module-a",
179 "target/test-repository/com/example/test/test-artifact-module-a" );
181 ReadMetadataRequest readMetadataRequest =
182 new ReadMetadataRequest().repositoryId( TEST_REPO_ID ).namespace( "com.example.test" ).projectId(
183 "test-artifact-module-a" ).projectVersion( "1.0" );
184 ProjectVersionMetadata metadata = storage.readProjectVersionMetadata( readMetadataRequest );
186 MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet( MavenProjectFacet.FACET_ID );
187 assertEquals( "jar", facet.getPackaging() );
188 assertEquals( "http://maven.apache.org", metadata.getUrl() );
189 assertEquals( "com.example.test", facet.getParent().getGroupId() );
190 assertEquals( "test-artifact-root", facet.getParent().getArtifactId() );
191 assertEquals( "1.0", facet.getParent().getVersion() );
192 assertEquals( "test-artifact-module-a", facet.getArtifactId() );
193 assertEquals( "com.example.test", facet.getGroupId() );
194 assertNull( metadata.getCiManagement() );
195 assertNotNull( metadata.getDescription() );
197 checkApacheLicense( metadata );
199 assertEquals( "1.0", metadata.getId() );
200 assertEquals( "Test Artifact :: Module A", metadata.getName() );
201 String path = "test-artifact/trunk/test-artifact-module-a";
202 assertEquals( TEST_SCM_CONN_BASE + path, metadata.getScm().getConnection() );
203 assertEquals( TEST_SCM_DEV_CONN_BASE + path, metadata.getScm().getDeveloperConnection() );
204 assertEquals( TEST_SCM_URL_BASE + path, metadata.getScm().getUrl() );
206 List<Dependency> dependencies = metadata.getDependencies();
207 assertEquals( 2, dependencies.size() );
208 assertDependency( dependencies.get( 0 ), "commons-io", "commons-io", "1.4" );
209 assertDependency( dependencies.get( 1 ), "junit", "junit", "3.8.1", "test" );
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 testGetProjectVersionMetadataWithParentNoRemoteReposConfigured()
223 // remove configuration
224 Configuration config = configuration.getConfiguration();
225 RemoteRepositoryConfiguration remoteRepo = config.findRemoteRepositoryById( TEST_REMOTE_REPO_ID );
226 config.removeRemoteRepository( remoteRepo );
228 configuration.save( config );
230 copyTestArtifactWithParent( "target/test-classes/com/example/test/test-artifact-module-a",
231 "target/test-repository/com/example/test/test-artifact-module-a" );
233 ReadMetadataRequest readMetadataRequest =
234 new ReadMetadataRequest().repositoryId( TEST_REPO_ID ).namespace( "com.example.test" ).projectId(
235 "test-artifact-module-a" ).projectVersion( "1.0" );
236 ProjectVersionMetadata metadata = storage.readProjectVersionMetadata( readMetadataRequest );
237 assertEquals( "1.0", metadata.getId() );
239 MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet( MavenProjectFacet.FACET_ID );
240 assertNotNull( facet );
241 assertEquals( "com.example.test", facet.getGroupId() );
242 assertEquals( "test-artifact-module-a", facet.getArtifactId() );
243 assertEquals( "jar", facet.getPackaging() );
245 List<String> paths = new ArrayList<String>();
246 paths.add( "target/test-repository/com/example/test/test-artifact-module-a" );
247 paths.add( "target/test-repository/com/example/test/test-artifact-parent" );
248 paths.add( "target/test-repository/com/example/test/test-artifact-root" );
250 deleteTestArtifactWithParent( paths );
254 public void testGetProjectVersionMetadataWithParentNotInAnyRemoteRepo()
257 copyTestArtifactWithParent( "target/test-classes/com/example/test/test-artifact-module-a",
258 "target/test-repository/com/example/test/test-artifact-module-a" );
260 ReadMetadataRequest readMetadataRequest =
261 new ReadMetadataRequest().repositoryId( TEST_REPO_ID ).namespace( "com.example.test" ).projectId(
262 "missing-parent" ).projectVersion( "1.1" );
264 ProjectVersionMetadata metadata = storage.readProjectVersionMetadata( readMetadataRequest );
266 assertEquals( "1.1", metadata.getId() );
268 MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet( MavenProjectFacet.FACET_ID );
269 assertNotNull( facet );
270 assertEquals( "com.example.test", facet.getGroupId() );
271 assertEquals( "missing-parent", facet.getArtifactId() );
272 assertEquals( "jar", facet.getPackaging() );
274 List<String> paths = new ArrayList<String>();
275 paths.add( "target/test-repository/com/example/test/test-artifact-module-a" );
276 paths.add( "target/test-repository/com/example/test/test-artifact-parent" );
277 paths.add( "target/test-repository/com/example/test/test-artifact-root" );
279 deleteTestArtifactWithParent( paths );
283 public void testGetProjectVersionMetadataWithParentSnapshotVersion()
286 copyTestArtifactWithParent( "target/test-classes/com/example/test/test-snapshot-artifact-module-a",
287 "target/test-repositorys/com/example/test/test-snapshot-artifact-module-a" );
288 copyTestArtifactWithParent( "target/test-classes/com/example/test/test-snapshot-artifact-root",
289 "target/test-repositorys/com/example/test/test-snapshot-artifact-root" );
291 ReadMetadataRequest readMetadataRequest =
292 new ReadMetadataRequest( TEST_SNAP_REPO_ID, "com.example.test", "test-snapshot-artifact-module-a",
295 ProjectVersionMetadata metadata = storage.readProjectVersionMetadata( readMetadataRequest );
297 MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet( MavenProjectFacet.FACET_ID );
298 assertEquals( "jar", facet.getPackaging() );
299 assertEquals( "com.example.test", facet.getParent().getGroupId() );
300 assertEquals( "test-snapshot-artifact-root", facet.getParent().getArtifactId() );
301 assertEquals( "1.1-SNAPSHOT", facet.getParent().getVersion() );
302 assertEquals( "test-snapshot-artifact-module-a", facet.getArtifactId() );
303 assertEquals( "com.example.test", facet.getGroupId() );
304 assertNull( metadata.getCiManagement() );
305 assertNotNull( metadata.getDescription() );
307 checkApacheLicense( metadata );
309 assertEquals( "1.1-SNAPSHOT", metadata.getId() );
310 assertEquals( "Test Snapshot Artifact :: Module A", metadata.getName() );
311 String path = "test-snapshot-artifact/trunk/test-snapshot-artifact-module-a";
312 assertEquals( TEST_SCM_CONN_BASE + path, metadata.getScm().getConnection() );
313 assertEquals( TEST_SCM_DEV_CONN_BASE + path, metadata.getScm().getDeveloperConnection() );
314 assertEquals( TEST_SCM_URL_BASE + path, metadata.getScm().getUrl() );
316 List<Dependency> dependencies = metadata.getDependencies();
317 assertEquals( 2, dependencies.size() );
318 assertDependency( dependencies.get( 0 ), "commons-io", "commons-io", "1.4" );
319 assertDependency( dependencies.get( 1 ), "junit", "junit", "3.8.1", "test" );
321 List<String> paths = new ArrayList<String>();
322 paths.add( "target/test-repositorys/com/example/test/test-snapshot-artifact-module-a" );
323 paths.add( "target/test-repositorys/com/example/test/test-snapshot-artifact-root" );
324 deleteTestArtifactWithParent( paths );
328 public void testGetProjectVersionMetadataWithParentSnapshotVersionAndSnapNotAllowed()
331 copyTestArtifactWithParent( "target/test-classes/com/example/test/test-snapshot-artifact-module-a",
332 "target/test-repositorys/com/example/test/test-snapshot-artifact-module-a" );
334 ReadMetadataRequest readMetadataRequest =
335 new ReadMetadataRequest().repositoryId( TEST_SNAP_REPO_ID ).namespace( "com.example.test" ).projectId(
336 "test-snapshot-artifact-module-a" ).projectVersion( "1.1-SNAPSHOT" );
337 ProjectVersionMetadata metadata = storage.readProjectVersionMetadata( readMetadataRequest );
339 MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet( MavenProjectFacet.FACET_ID );
340 assertEquals( "jar", facet.getPackaging() );
341 assertEquals( "com.example.test", facet.getParent().getGroupId() );
342 assertEquals( "test-snapshot-artifact-root", facet.getParent().getArtifactId() );
343 assertEquals( "1.1-SNAPSHOT", facet.getParent().getVersion() );
344 assertEquals( "test-snapshot-artifact-module-a", facet.getArtifactId() );
345 assertEquals( "com.example.test", facet.getGroupId() );
346 assertNull( metadata.getCiManagement() );
347 assertNotNull( metadata.getDescription() );
349 checkApacheLicense( metadata );
351 assertEquals( "1.1-SNAPSHOT", metadata.getId() );
352 assertEquals( "Test Snapshot Artifact :: Module A", metadata.getName() );
353 String path = "test-snapshot-artifact/trunk/test-snapshot-artifact-module-a";
354 assertEquals( TEST_SCM_CONN_BASE + path, metadata.getScm().getConnection() );
355 assertEquals( TEST_SCM_DEV_CONN_BASE + path, metadata.getScm().getDeveloperConnection() );
356 assertEquals( TEST_SCM_URL_BASE + path, metadata.getScm().getUrl() );
358 List<Dependency> dependencies = metadata.getDependencies();
359 assertEquals( 2, dependencies.size() );
360 assertDependency( dependencies.get( 0 ), "commons-io", "commons-io", "1.4" );
361 assertDependency( dependencies.get( 1 ), "junit", "junit", "3.8.1", "test" );
363 List<String> paths = new ArrayList<String>();
364 paths.add( "target/test-repositorys/com/example/test/test-snapshot-artifact-module-a" );
365 paths.add( "target/test-repositorys/com/example/test/test-snapshot-artifact-root" );
367 deleteTestArtifactWithParent( paths );
371 public void testGetProjectVersionMetadataWithParentSnapshotVersionAndSnapNotAllowed2()
374 copyTestArtifactWithParent( "target/test-classes/com/example/test/test-artifact-module-b",
375 "target/test-repository/com/example/test/test-artifact-module-b" );
377 ReadMetadataRequest readMetadataRequest =
378 new ReadMetadataRequest().repositoryId( TEST_REPO_ID ).namespace( "com.example.test" ).projectId(
379 "test-artifact-module-b" ).projectVersion( "1.0" );
381 ProjectVersionMetadata metadata = storage.readProjectVersionMetadata( readMetadataRequest );
383 MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet( MavenProjectFacet.FACET_ID );
384 assertEquals( "jar", facet.getPackaging() );
385 assertEquals( "com.example.test", facet.getParent().getGroupId() );
386 assertEquals( "test-snapshot-artifact-root", facet.getParent().getArtifactId() );
387 assertEquals( "1.1-SNAPSHOT", facet.getParent().getVersion() );
388 assertEquals( "test-artifact-module-b", facet.getArtifactId() );
389 assertEquals( "com.example.test", facet.getGroupId() );
390 assertNull( metadata.getCiManagement() );
391 assertNotNull( metadata.getDescription() );
393 checkApacheLicense( metadata );
395 assertEquals( "1.0", metadata.getId() );
396 assertEquals( "Test Artifact :: Module B", metadata.getName() );
397 String path = "test-snapshot-artifact/trunk/test-artifact-module-b";
398 assertEquals( TEST_SCM_CONN_BASE + path, metadata.getScm().getConnection() );
399 assertEquals( TEST_SCM_DEV_CONN_BASE + path, metadata.getScm().getDeveloperConnection() );
400 assertEquals( TEST_SCM_URL_BASE + path, metadata.getScm().getUrl() );
402 List<Dependency> dependencies = metadata.getDependencies();
403 assertEquals( 2, dependencies.size() );
404 assertDependency( dependencies.get( 0 ), "commons-io", "commons-io", "1.4" );
405 assertDependency( dependencies.get( 1 ), "junit", "junit", "3.8.1", "test" );
407 List<String> paths = new ArrayList<String>();
408 paths.add( "target/test-repository/com/example/test/test-artifact-module-b" );
409 paths.add( "target/test-repository/com/example/test/test-snapshot-artifact-root" );
411 deleteTestArtifactWithParent( paths );
413 // Tests for MRM-1411 - END
415 private void assertDependency( Dependency dependency, String groupId, String artifactId, String version )
417 assertDependency( dependency, groupId, artifactId, version, "compile" );
420 private void assertDependency( Dependency dependency, String groupId, String artifactId, String version,
423 assertEquals( artifactId, dependency.getArtifactId() );
424 assertEquals( "jar", dependency.getType() );
425 assertEquals( version, dependency.getVersion() );
426 assertEquals( groupId, dependency.getGroupId() );
427 assertEquals( scope, dependency.getScope() );
428 assertNull( dependency.getClassifier() );
429 assertNull( dependency.getSystemPath() );
432 private void assertArtifact( ArtifactMetadata artifact, String id, int size, String sha1, String md5 )
434 assertEquals( id, artifact.getId() );
435 assertEquals( md5, artifact.getMd5() );
436 assertEquals( sha1, artifact.getSha1() );
437 assertEquals( size, artifact.getSize() );
438 assertEquals( "org.codehaus.plexus", artifact.getNamespace() );
439 assertEquals( "plexus-spring", artifact.getProject() );
440 assertEquals( "1.2", artifact.getVersion() );
441 assertEquals( TEST_REPO_ID, artifact.getRepositoryId() );
444 private void assertMailingList( MailingList mailingList, String name, String archive, String post, String subscribe,
445 String unsubscribe, List<String> otherArchives, boolean allowPost )
447 assertEquals( archive, mailingList.getMainArchiveUrl() );
450 assertEquals( post, mailingList.getPostAddress() );
454 assertNull( mailingList.getPostAddress() );
456 assertEquals( subscribe, mailingList.getSubscribeAddress() );
457 assertEquals( unsubscribe, mailingList.getUnsubscribeAddress() );
458 assertEquals( name, mailingList.getName() );
459 assertEquals( otherArchives, mailingList.getOtherArchives() );
462 private void assertMailingList( String prefix, MailingList mailingList, String name, boolean allowPost,
465 List<String> otherArchives = new ArrayList<String>();
466 otherArchives.add( "http://www.mail-archive.com/" + prefix + "@archiva.apache.org" );
467 if ( nabbleUrl != null )
469 otherArchives.add( nabbleUrl );
471 otherArchives.add( "http://markmail.org/list/org.apache.archiva." + prefix );
472 assertMailingList( mailingList, name, "http://mail-archives.apache.org/mod_mbox/archiva-" + prefix + "/",
473 prefix + "@archiva.apache.org", prefix + "-subscribe@archiva.apache.org",
474 prefix + "-unsubscribe@archiva.apache.org", otherArchives, allowPost );
477 private void checkApacheLicense( ProjectVersionMetadata metadata )
479 assertEquals( Arrays.asList( new License( "The Apache Software License, Version 2.0",
480 "http://www.apache.org/licenses/LICENSE-2.0.txt" ) ),
481 metadata.getLicenses() );
484 private void checkOrganizationApache( ProjectVersionMetadata metadata )
486 assertEquals( "The Apache Software Foundation", metadata.getOrganization().getName() );
487 assertEquals( "http://www.apache.org/", metadata.getOrganization().getUrl() );
490 private void deleteTestArtifactWithParent( List<String> pathsToBeDeleted )
493 for ( String path : pathsToBeDeleted )
495 File dir = new File( FileUtil.getBasedir(), path );
496 FileUtils.deleteDirectory( dir );
498 assertFalse( dir.exists() );
500 File dest = new File( FileUtil.getBasedir(), "target/test-repository/com/example/test/test-artifact-module-a" );
502 new File( FileUtil.getBasedir(), "target/test-repository/com/example/test/test-artifact-parent" );
503 File rootPom = new File( FileUtil.getBasedir(), "target/test-repository/com/example/test/test-artifact-root" );
505 FileUtils.deleteDirectory( dest );
506 FileUtils.deleteDirectory( parentPom );
507 FileUtils.deleteDirectory( rootPom );
509 assertFalse( dest.exists() );
510 assertFalse( parentPom.exists() );
511 assertFalse( rootPom.exists() );
514 private File copyTestArtifactWithParent( String srcPath, String destPath )
517 File src = new File( FileUtil.getBasedir(), srcPath );
518 File dest = new File( FileUtil.getBasedir(), destPath );
520 FileUtils.copyDirectory( src, dest );
521 assertTrue( dest.exists() );