1 package org.apache.maven.archiva.web.action;
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 com.opensymphony.xwork2.Action;
23 import org.apache.archiva.metadata.model.ArtifactMetadata;
24 import org.apache.archiva.metadata.model.Dependency;
25 import org.apache.archiva.metadata.model.MailingList;
26 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
27 import org.apache.archiva.metadata.model.ProjectVersionReference;
28 import org.apache.archiva.metadata.repository.memory.TestMetadataResolver;
29 import org.apache.archiva.metadata.repository.storage.maven2.MavenArtifactFacet;
30 import org.apache.maven.archiva.common.utils.VersionUtil;
31 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
32 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
33 import org.apache.maven.archiva.repository.RepositoryContentFactory;
34 import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent;
35 import org.easymock.MockControl;
36 import org.easymock.classextension.MockClassControl;
38 import java.util.Arrays;
39 import java.util.Collections;
40 import java.util.List;
43 public class ShowArtifactActionTest
44 extends AbstractActionTestCase
46 private static final String ACTION_HINT = "showArtifactAction";
48 private static final String TEST_VERSION = "version";
50 private static final String TEST_SNAPSHOT_VERSION = "1.0-SNAPSHOT";
52 private static final String TEST_TS_SNAPSHOT_VERSION = "1.0-20091120.111111-1";
54 private static final String OTHER_TEST_REPO = "first-repo";
56 private ShowArtifactAction action;
58 private static final List<ArtifactMetadata> TEST_SNAPSHOT_ARTIFACTS = Arrays.asList( createArtifact(
59 TEST_TS_SNAPSHOT_VERSION ), createArtifact( "1.0-20091120.222222-2", "20091120.222222", 2 ), createArtifact(
60 "1.0-20091123.333333-3", "20091123.333333", 3 ) );
62 private static final long TEST_SIZE = 12345L;
64 private static final String TEST_TYPE = "jar";
66 public void testInstantiation()
68 assertFalse( action == lookup( Action.class, ACTION_HINT ) );
71 public void testGetArtifactUniqueRelease()
73 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
74 createProjectModel( TEST_VERSION ) );
76 setActionParameters();
78 String result = action.artifact();
80 assertActionSuccess( action, result );
82 assertActionParameters( action );
83 ProjectVersionMetadata model = action.getModel();
84 assertDefaultModel( model );
86 assertEquals( TEST_REPO, action.getRepositoryId() );
88 assertNull( action.getDependees() );
89 assertNull( action.getDependencies() );
90 assertNull( action.getMailingLists() );
91 assertTrue( action.getArtifacts().isEmpty() );
94 public void testGetArtifactUniqueSnapshot()
96 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
97 createProjectModel( TEST_SNAPSHOT_VERSION ) );
98 metadataResolver.setArtifacts( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_SNAPSHOT_VERSION,
99 TEST_SNAPSHOT_ARTIFACTS );
101 action.setGroupId( TEST_GROUP_ID );
102 action.setArtifactId( TEST_ARTIFACT_ID );
103 action.setVersion( TEST_SNAPSHOT_VERSION );
105 String result = action.artifact();
107 assertActionSuccess( action, result );
109 assertEquals( TEST_GROUP_ID, action.getGroupId() );
110 assertEquals( TEST_ARTIFACT_ID, action.getArtifactId() );
111 assertEquals( TEST_SNAPSHOT_VERSION, action.getVersion() );
112 ProjectVersionMetadata model = action.getModel();
113 assertDefaultModel( model, TEST_SNAPSHOT_VERSION );
115 assertEquals( TEST_REPO, action.getRepositoryId() );
117 assertArtifacts( TEST_SNAPSHOT_ARTIFACTS, action.getArtifacts() );
119 assertNull( action.getDependees() );
120 assertNull( action.getDependencies() );
121 assertNull( action.getMailingLists() );
124 public void testGetArtifactUniqueSnapshotTimestamped()
126 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
127 createProjectModel( TEST_SNAPSHOT_VERSION ) );
128 metadataResolver.setArtifacts( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_SNAPSHOT_VERSION,
129 TEST_SNAPSHOT_ARTIFACTS );
131 action.setGroupId( TEST_GROUP_ID );
132 action.setArtifactId( TEST_ARTIFACT_ID );
133 action.setVersion( TEST_TS_SNAPSHOT_VERSION );
135 String result = action.artifact();
136 assertError( result );
137 assertNoOutputFields();
140 public void testGetMissingProject()
142 setActionParameters();
144 String result = action.artifact();
145 assertError( result );
147 assertActionParameters( action );
148 assertNoOutputFields();
151 public void testGetArtifactNoObservableRepos()
153 setObservableRepos( Collections.<String>emptyList() );
155 setActionParameters();
157 String result = action.artifact();
159 // Actually, it'd be better to have an error:
160 assertError( result );
161 assertActionParameters( action );
162 assertNoOutputFields();
165 public void testGetArtifactNotInObservableRepos()
167 metadataResolver.setProjectVersion( OTHER_TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
168 createProjectModel( TEST_VERSION ) );
170 setActionParameters();
172 String result = action.artifact();
173 assertError( result );
175 assertActionParameters( action );
176 assertNoOutputFields();
179 public void testGetArtifactOnlySeenInSecondObservableRepo()
181 setObservableRepos( Arrays.asList( OTHER_TEST_REPO, TEST_REPO ) );
182 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
183 createProjectModel( TEST_VERSION ) );
185 setActionParameters();
187 String result = action.artifact();
189 assertActionSuccess( action, result );
191 assertActionParameters( action );
192 ProjectVersionMetadata model = action.getModel();
193 assertDefaultModel( model );
195 assertEquals( TEST_REPO, action.getRepositoryId() );
197 assertNull( action.getDependees() );
198 assertNull( action.getDependencies() );
199 assertNull( action.getMailingLists() );
200 assertTrue( action.getArtifacts().isEmpty() );
203 public void testGetArtifactSeenInBothObservableRepo()
205 setObservableRepos( Arrays.asList( TEST_REPO, OTHER_TEST_REPO ) );
206 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
207 createProjectModel( TEST_VERSION ) );
208 metadataResolver.setProjectVersion( OTHER_TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
209 createProjectModel( TEST_VERSION ) );
211 setActionParameters();
213 String result = action.artifact();
215 assertActionSuccess( action, result );
217 assertActionParameters( action );
218 ProjectVersionMetadata model = action.getModel();
219 assertDefaultModel( model );
221 assertEquals( TEST_REPO, action.getRepositoryId() );
223 assertNull( action.getDependees() );
224 assertNull( action.getDependencies() );
225 assertNull( action.getMailingLists() );
226 assertTrue( action.getArtifacts().isEmpty() );
229 public void testGetArtifactCanOnlyObserveInOneOfTwoRepos()
231 setObservableRepos( Arrays.asList( TEST_REPO ) );
232 metadataResolver.setProjectVersion( OTHER_TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
233 createProjectModel( TEST_VERSION ) );
234 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
235 createProjectModel( TEST_VERSION ) );
237 setActionParameters();
239 String result = action.artifact();
241 assertActionSuccess( action, result );
243 assertActionParameters( action );
244 ProjectVersionMetadata model = action.getModel();
245 assertDefaultModel( model );
247 assertEquals( TEST_REPO, action.getRepositoryId() );
249 assertNull( action.getDependees() );
250 assertNull( action.getDependencies() );
251 assertNull( action.getMailingLists() );
252 assertTrue( action.getArtifacts().isEmpty() );
255 public void testGetArtifactNoMavenFacet()
257 ProjectVersionMetadata versionMetadata = new ProjectVersionMetadata();
258 versionMetadata.setId( TEST_VERSION );
259 versionMetadata.setUrl( TEST_URL );
260 versionMetadata.setName( TEST_NAME );
261 versionMetadata.setDescription( TEST_DESCRIPTION );
263 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID, versionMetadata );
265 setActionParameters();
267 String result = action.artifact();
269 assertActionSuccess( action, result );
271 assertActionParameters( action );
272 ProjectVersionMetadata model = action.getModel();
273 assertEquals( TEST_VERSION, model.getVersion() );
274 assertEquals( TEST_URL, model.getUrl() );
275 assertEquals( TEST_NAME, model.getName() );
276 assertEquals( TEST_DESCRIPTION, model.getDescription() );
278 assertEquals( TEST_REPO, action.getRepositoryId() );
280 assertNull( action.getDependees() );
281 assertNull( action.getDependencies() );
282 assertNull( action.getMailingLists() );
283 assertTrue( action.getArtifacts().isEmpty() );
286 public void testGetMailingLists()
288 ProjectVersionMetadata versionMetadata = createProjectModel( TEST_VERSION );
289 MailingList ml1 = createMailingList( "Users List", "users" );
290 MailingList ml2 = createMailingList( "Developers List", "dev" );
291 versionMetadata.setMailingLists( Arrays.asList( ml1, ml2 ) );
292 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID, versionMetadata );
294 setActionParameters();
296 String result = action.mailingLists();
298 assertActionSuccess( action, result );
300 assertActionParameters( action );
301 ProjectVersionMetadata model = action.getModel();
302 assertDefaultModel( model );
304 assertNotNull( action.getMailingLists() );
305 assertMailingList( action.getMailingLists().get( 0 ), "Users List", "users" );
306 assertMailingList( action.getMailingLists().get( 1 ), "Developers List", "dev" );
308 assertEquals( TEST_REPO, action.getRepositoryId() );
309 assertNull( action.getDependees() );
310 assertNull( action.getDependencies() );
311 assertTrue( action.getArtifacts().isEmpty() );
314 public void testGetDependencies()
316 ProjectVersionMetadata versionMetadata = createProjectModel( TEST_VERSION );
317 Dependency dependency1 = createDependencyBasic( "artifactId1" );
318 Dependency dependency2 = createDependencyExtended( "artifactId2" );
319 versionMetadata.setDependencies( Arrays.asList( dependency1, dependency2 ) );
320 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID, versionMetadata );
322 setActionParameters();
324 String result = action.dependencies();
326 assertActionSuccess( action, result );
328 assertActionParameters( action );
329 ProjectVersionMetadata model = action.getModel();
330 assertDefaultModel( model );
332 assertNotNull( action.getDependencies() );
333 assertDependencyBasic( action.getDependencies().get( 0 ), "artifactId1" );
334 assertDependencyExtended( action.getDependencies().get( 1 ), "artifactId2" );
336 assertEquals( TEST_REPO, action.getRepositoryId() );
337 assertNull( action.getDependees() );
338 assertNull( action.getMailingLists() );
339 assertTrue( action.getArtifacts().isEmpty() );
342 public void testGetDependees()
344 ProjectVersionMetadata versionMetadata = createProjectModel( TEST_VERSION );
345 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID, versionMetadata );
346 ProjectVersionReference dependee1 = createReference( "artifactId1" );
347 ProjectVersionReference dependee2 = createReference( "artifactId2" );
348 metadataResolver.setProjectReferences( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION,
349 Arrays.asList( dependee1, dependee2 ) );
351 setActionParameters();
353 String result = action.dependees();
355 assertActionSuccess( action, result );
357 assertActionParameters( action );
358 ProjectVersionMetadata model = action.getModel();
359 assertDefaultModel( model );
361 assertNotNull( action.getDependees() );
362 assertCoordinate( action.getDependees().get( 0 ), "artifactId1" );
363 assertCoordinate( action.getDependees().get( 1 ), "artifactId2" );
365 assertEquals( TEST_REPO, action.getRepositoryId() );
366 assertNull( action.getDependencies() );
367 assertNull( action.getMailingLists() );
368 assertTrue( action.getArtifacts().isEmpty() );
371 private void assertArtifacts( List<ArtifactMetadata> expectedArtifacts,
372 Map<String, List<ShowArtifactAction.ArtifactDownloadInfo>> artifactMap )
374 // assuming only one of each version at this point
375 assertEquals( expectedArtifacts.size(), artifactMap.size() );
376 for ( ArtifactMetadata artifact : expectedArtifacts )
378 assertTrue( artifactMap.containsKey( artifact.getVersion() ) );
379 List<ShowArtifactAction.ArtifactDownloadInfo> list = artifactMap.get( artifact.getVersion() );
380 ShowArtifactAction.ArtifactDownloadInfo actual = list.get( 0 );
381 assertEquals( artifact.getNamespace(), actual.getNamespace() );
382 assertEquals( artifact.getId(), actual.getId() );
383 assertEquals( artifact.getProject(), actual.getProject() );
384 assertEquals( artifact.getRepositoryId(), actual.getRepositoryId() );
385 assertEquals( artifact.getVersion(), actual.getVersion() );
386 assertEquals( TEST_TYPE, actual.getType() );
387 assertEquals( "12.06 K", actual.getSize() );
388 assertEquals( artifact.getNamespace() + "/" + artifact.getProject() + "/" + TEST_SNAPSHOT_VERSION + "/" +
389 artifact.getId(), actual.getPath() );
393 private static ArtifactMetadata createArtifact( String version )
395 return createArtifact( version, null, 0 );
398 private static ArtifactMetadata createArtifact( String version, String timestamp, int buildNumber )
400 ArtifactMetadata metadata = new ArtifactMetadata();
401 metadata.setProject( TEST_ARTIFACT_ID );
402 metadata.setId( TEST_ARTIFACT_ID + "-" + version + ".jar" );
403 metadata.setNamespace( TEST_GROUP_ID );
404 metadata.setRepositoryId( TEST_REPO );
405 metadata.setSize( TEST_SIZE );
406 metadata.setProjectVersion( VersionUtil.getBaseVersion( version ) );
407 metadata.setVersion( version );
409 MavenArtifactFacet facet = new MavenArtifactFacet();
410 facet.setType( "jar" );
411 facet.setTimestamp( timestamp );
412 facet.setBuildNumber( buildNumber );
413 metadata.addFacet( facet );
418 private ProjectVersionReference createReference( String projectId )
420 ProjectVersionReference reference = new ProjectVersionReference();
421 reference.setNamespace( "groupId" );
422 reference.setProjectId( projectId );
423 reference.setProjectVersion( "version" );
424 reference.setReferenceType( ProjectVersionReference.ReferenceType.DEPENDENCY );
428 private void assertCoordinate( ProjectVersionReference dependee, String artifactId )
430 assertEquals( artifactId, dependee.getProjectId() );
431 assertEquals( "groupId", dependee.getNamespace() );
432 assertEquals( "version", dependee.getProjectVersion() );
435 private void assertDependencyBasic( Dependency dependency, String artifactId )
437 assertEquals( artifactId, dependency.getArtifactId() );
438 assertEquals( "groupId", dependency.getGroupId() );
439 assertEquals( "version", dependency.getVersion() );
442 private void assertDependencyExtended( Dependency dependency, String artifactId )
444 assertDependencyBasic( dependency, artifactId );
445 assertEquals( true, dependency.isOptional() );
446 assertEquals( "classifier", dependency.getClassifier() );
447 assertEquals( "type", dependency.getType() );
448 assertEquals( "scope", dependency.getScope() );
449 assertEquals( "systemPath", dependency.getSystemPath() );
452 private Dependency createDependencyExtended( String artifactId )
454 Dependency dependency = createDependencyBasic( artifactId );
455 dependency.setClassifier( "classifier" );
456 dependency.setOptional( true );
457 dependency.setScope( "scope" );
458 dependency.setSystemPath( "systemPath" );
459 dependency.setType( "type" );
463 private Dependency createDependencyBasic( String artifactId )
465 Dependency dependency = new Dependency();
466 dependency.setArtifactId( artifactId );
467 dependency.setGroupId( "groupId" );
468 dependency.setVersion( "version" );
472 private void assertMailingList( MailingList mailingList, String name, String prefix )
474 assertEquals( name, mailingList.getName() );
475 assertEquals( prefix + "-post@", mailingList.getPostAddress() );
476 assertEquals( prefix + "-subscribe@", mailingList.getSubscribeAddress() );
477 assertEquals( prefix + "-unsubscribe@", mailingList.getUnsubscribeAddress() );
478 assertEquals( prefix + "-archive-url", mailingList.getMainArchiveUrl() );
479 assertEquals( Arrays.asList( "other-" + prefix + "-archive-url-1", "other-" + prefix + "-archive-url-2" ),
480 mailingList.getOtherArchives() );
483 private MailingList createMailingList( String name, String prefix )
485 MailingList ml1 = new MailingList();
487 ml1.setPostAddress( prefix + "-post@" );
488 ml1.setSubscribeAddress( prefix + "-subscribe@" );
489 ml1.setUnsubscribeAddress( prefix + "-unsubscribe@" );
490 ml1.setMainArchiveUrl( prefix + "-archive-url" );
491 ml1.setOtherArchives(
492 Arrays.asList( "other-" + prefix + "-archive-url-1", "other-" + prefix + "-archive-url-2" ) );
496 private void assertNoOutputFields()
498 assertNull( action.getModel() );
499 assertNull( action.getDependees() );
500 assertNull( action.getDependencies() );
501 assertNull( action.getMailingLists() );
502 assertTrue( action.getArtifacts().isEmpty() );
505 private void assertError( String result )
507 assertEquals( Action.ERROR, result );
508 assertEquals( 1, action.getActionErrors().size() );
511 private void assertDefaultModel( ProjectVersionMetadata model )
513 assertDefaultModel( model, TEST_VERSION );
516 private void setActionParameters()
518 action.setGroupId( TEST_GROUP_ID );
519 action.setArtifactId( TEST_ARTIFACT_ID );
520 action.setVersion( TEST_VERSION );
523 private void assertActionParameters( ShowArtifactAction action )
525 assertEquals( TEST_GROUP_ID, action.getGroupId() );
526 assertEquals( TEST_ARTIFACT_ID, action.getArtifactId() );
527 assertEquals( TEST_VERSION, action.getVersion() );
530 private void assertActionSuccess( ShowArtifactAction action, String result )
532 assertEquals( Action.SUCCESS, result );
533 assertTrue( action.getActionErrors().isEmpty() );
534 assertTrue( action.getActionMessages().isEmpty() );
537 protected void setUp()
541 action = (ShowArtifactAction) lookup( Action.class, ACTION_HINT );
542 metadataResolver = (TestMetadataResolver) action.getMetadataResolver();
543 MockControl control = MockClassControl.createControl( RepositoryContentFactory.class );
544 RepositoryContentFactory factory = (RepositoryContentFactory) control.getMock();
545 action.setRepositoryFactory( factory );
547 ManagedRepositoryConfiguration config = new ManagedRepositoryConfiguration();
548 config.setId( TEST_REPO );
549 config.setLocation( getTestFile( "target/test-repo" ).getAbsolutePath() );
550 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
551 content.setRepository( config );
552 factory.getManagedRepositoryContent( TEST_REPO );
553 control.setDefaultReturnValue( content );