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 java.util.Arrays;
23 import java.util.Collections;
24 import java.util.List;
27 import com.opensymphony.xwork2.Action;
28 import org.apache.archiva.metadata.model.ArtifactMetadata;
29 import org.apache.archiva.metadata.model.Dependency;
30 import org.apache.archiva.metadata.model.MailingList;
31 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
32 import org.apache.archiva.metadata.model.ProjectVersionReference;
33 import org.apache.archiva.metadata.repository.memory.TestMetadataResolver;
34 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
35 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
36 import org.apache.maven.archiva.repository.RepositoryContentFactory;
37 import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent;
38 import org.easymock.MockControl;
39 import org.easymock.classextension.MockClassControl;
41 public class ShowArtifactActionTest
42 extends AbstractActionTestCase
44 private static final String ACTION_HINT = "showArtifactAction";
46 private static final String TEST_VERSION = "version";
48 private static final String TEST_SNAPSHOT_VERSION = "1.0-SNAPSHOT";
50 private static final String TEST_TS_SNAPSHOT_VERSION = "1.0-20091120.111111-1";
52 private static final String OTHER_TEST_REPO = "first-repo";
54 private ShowArtifactAction action;
56 private static final List<ArtifactMetadata> TEST_SNAPSHOT_ARTIFACTS =
57 Arrays.asList( createArtifact( TEST_TS_SNAPSHOT_VERSION ), createArtifact( "1.0-20091120.222222-2" ),
58 createArtifact( "1.0-20091123.333333-3" ) );
60 private static final long TEST_SIZE = 12345L;
62 private static final String TEST_TYPE = "jar";
64 public void testInstantiation()
66 assertFalse( action == lookup( Action.class, ACTION_HINT ) );
69 public void testGetArtifactUniqueRelease()
71 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
72 createProjectModel( TEST_VERSION ) );
74 setActionParameters();
76 String result = action.artifact();
78 assertActionSuccess( action, result );
80 assertActionParameters( action );
81 ProjectVersionMetadata model = action.getModel();
82 assertDefaultModel( model );
84 assertEquals( TEST_REPO, action.getRepositoryId() );
86 assertNull( action.getDependees() );
87 assertNull( action.getDependencies() );
88 assertNull( action.getMailingLists() );
89 assertTrue( action.getArtifacts().isEmpty() );
92 public void testGetArtifactUniqueSnapshot()
94 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
95 createProjectModel( TEST_SNAPSHOT_VERSION ) );
96 metadataResolver.setArtifacts( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_SNAPSHOT_VERSION,
97 TEST_SNAPSHOT_ARTIFACTS );
99 action.setGroupId( TEST_GROUP_ID );
100 action.setArtifactId( TEST_ARTIFACT_ID );
101 action.setVersion( TEST_SNAPSHOT_VERSION );
103 String result = action.artifact();
105 assertActionSuccess( action, result );
107 assertEquals( TEST_GROUP_ID, action.getGroupId() );
108 assertEquals( TEST_ARTIFACT_ID, action.getArtifactId() );
109 assertEquals( TEST_SNAPSHOT_VERSION, action.getVersion() );
110 ProjectVersionMetadata model = action.getModel();
111 assertDefaultModel( model, TEST_SNAPSHOT_VERSION );
113 assertEquals( TEST_REPO, action.getRepositoryId() );
115 assertArtifacts( TEST_SNAPSHOT_ARTIFACTS, action.getArtifacts() );
117 assertNull( action.getDependees() );
118 assertNull( action.getDependencies() );
119 assertNull( action.getMailingLists() );
122 public void testGetArtifactUniqueSnapshotTimestamped()
124 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
125 createProjectModel( TEST_SNAPSHOT_VERSION ) );
126 metadataResolver.setArtifacts( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_SNAPSHOT_VERSION,
127 TEST_SNAPSHOT_ARTIFACTS );
129 action.setGroupId( TEST_GROUP_ID );
130 action.setArtifactId( TEST_ARTIFACT_ID );
131 action.setVersion( TEST_TS_SNAPSHOT_VERSION );
133 String result = action.artifact();
134 assertError( result );
135 assertNoOutputFields();
138 public void testGetMissingProject()
140 setActionParameters();
142 String result = action.artifact();
143 assertError( result );
145 assertActionParameters( action );
146 assertNoOutputFields();
149 public void testGetArtifactNoObservableRepos()
151 setObservableRepos( Collections.<String>emptyList() );
153 setActionParameters();
155 String result = action.artifact();
157 // Actually, it'd be better to have an error:
158 assertError( result );
159 assertActionParameters( action );
160 assertNoOutputFields();
163 public void testGetArtifactNotInObservableRepos()
165 metadataResolver.setProjectVersion( OTHER_TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
166 createProjectModel( TEST_VERSION ) );
168 setActionParameters();
170 String result = action.artifact();
171 assertError( result );
173 assertActionParameters( action );
174 assertNoOutputFields();
177 public void testGetArtifactOnlySeenInSecondObservableRepo()
179 setObservableRepos( Arrays.asList( OTHER_TEST_REPO, TEST_REPO ) );
180 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
181 createProjectModel( TEST_VERSION ) );
183 setActionParameters();
185 String result = action.artifact();
187 assertActionSuccess( action, result );
189 assertActionParameters( action );
190 ProjectVersionMetadata model = action.getModel();
191 assertDefaultModel( model );
193 assertEquals( TEST_REPO, action.getRepositoryId() );
195 assertNull( action.getDependees() );
196 assertNull( action.getDependencies() );
197 assertNull( action.getMailingLists() );
198 assertTrue( action.getArtifacts().isEmpty() );
201 public void testGetArtifactSeenInBothObservableRepo()
203 setObservableRepos( Arrays.asList( TEST_REPO, OTHER_TEST_REPO ) );
204 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
205 createProjectModel( TEST_VERSION ) );
206 metadataResolver.setProjectVersion( OTHER_TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
207 createProjectModel( TEST_VERSION ) );
209 setActionParameters();
211 String result = action.artifact();
213 assertActionSuccess( action, result );
215 assertActionParameters( action );
216 ProjectVersionMetadata model = action.getModel();
217 assertDefaultModel( model );
219 assertEquals( TEST_REPO, action.getRepositoryId() );
221 assertNull( action.getDependees() );
222 assertNull( action.getDependencies() );
223 assertNull( action.getMailingLists() );
224 assertTrue( action.getArtifacts().isEmpty() );
227 public void testGetArtifactCanOnlyObserveInOneOfTwoRepos()
229 setObservableRepos( Arrays.asList( TEST_REPO ) );
230 metadataResolver.setProjectVersion( OTHER_TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
231 createProjectModel( TEST_VERSION ) );
232 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
233 createProjectModel( TEST_VERSION ) );
235 setActionParameters();
237 String result = action.artifact();
239 assertActionSuccess( action, result );
241 assertActionParameters( action );
242 ProjectVersionMetadata model = action.getModel();
243 assertDefaultModel( model );
245 assertEquals( TEST_REPO, action.getRepositoryId() );
247 assertNull( action.getDependees() );
248 assertNull( action.getDependencies() );
249 assertNull( action.getMailingLists() );
250 assertTrue( action.getArtifacts().isEmpty() );
253 public void testGetArtifactNoMavenFacet()
255 ProjectVersionMetadata versionMetadata = new ProjectVersionMetadata();
256 versionMetadata.setId( TEST_VERSION );
257 versionMetadata.setUrl( TEST_URL );
258 versionMetadata.setName( TEST_NAME );
259 versionMetadata.setDescription( TEST_DESCRIPTION );
261 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID, versionMetadata );
263 setActionParameters();
265 String result = action.artifact();
267 assertActionSuccess( action, result );
269 assertActionParameters( action );
270 ProjectVersionMetadata model = action.getModel();
271 assertEquals( TEST_VERSION, model.getVersion() );
272 assertEquals( TEST_URL, model.getUrl() );
273 assertEquals( TEST_NAME, model.getName() );
274 assertEquals( TEST_DESCRIPTION, model.getDescription() );
276 assertEquals( TEST_REPO, action.getRepositoryId() );
278 assertNull( action.getDependees() );
279 assertNull( action.getDependencies() );
280 assertNull( action.getMailingLists() );
281 assertTrue( action.getArtifacts().isEmpty() );
284 public void testGetMailingLists()
286 ProjectVersionMetadata versionMetadata = createProjectModel( TEST_VERSION );
287 MailingList ml1 = createMailingList( "Users List", "users" );
288 MailingList ml2 = createMailingList( "Developers List", "dev" );
289 versionMetadata.setMailingLists( Arrays.asList( ml1, ml2 ) );
290 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID, versionMetadata );
292 setActionParameters();
294 String result = action.mailingLists();
296 assertActionSuccess( action, result );
298 assertActionParameters( action );
299 ProjectVersionMetadata model = action.getModel();
300 assertDefaultModel( model );
302 assertNotNull( action.getMailingLists() );
303 assertMailingList( action.getMailingLists().get( 0 ), "Users List", "users" );
304 assertMailingList( action.getMailingLists().get( 1 ), "Developers List", "dev" );
306 assertEquals( TEST_REPO, action.getRepositoryId() );
307 assertNull( action.getDependees() );
308 assertNull( action.getDependencies() );
309 assertTrue( action.getArtifacts().isEmpty() );
312 public void testGetDependencies()
314 ProjectVersionMetadata versionMetadata = createProjectModel( TEST_VERSION );
315 Dependency dependency1 = createDependencyBasic( "artifactId1" );
316 Dependency dependency2 = createDependencyExtended( "artifactId2" );
317 versionMetadata.setDependencies( Arrays.asList( dependency1, dependency2 ) );
318 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID, versionMetadata );
320 setActionParameters();
322 String result = action.dependencies();
324 assertActionSuccess( action, result );
326 assertActionParameters( action );
327 ProjectVersionMetadata model = action.getModel();
328 assertDefaultModel( model );
330 assertNotNull( action.getDependencies() );
331 assertDependencyBasic( action.getDependencies().get( 0 ), "artifactId1" );
332 assertDependencyExtended( action.getDependencies().get( 1 ), "artifactId2" );
334 assertEquals( TEST_REPO, action.getRepositoryId() );
335 assertNull( action.getDependees() );
336 assertNull( action.getMailingLists() );
337 assertTrue( action.getArtifacts().isEmpty() );
340 public void testGetDependees()
342 ProjectVersionMetadata versionMetadata = createProjectModel( TEST_VERSION );
343 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID, versionMetadata );
344 ProjectVersionReference dependee1 = createReference( "artifactId1" );
345 ProjectVersionReference dependee2 = createReference( "artifactId2" );
346 metadataResolver.setProjectReferences( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION,
347 Arrays.asList( dependee1, dependee2 ) );
349 setActionParameters();
351 String result = action.dependees();
353 assertActionSuccess( action, result );
355 assertActionParameters( action );
356 ProjectVersionMetadata model = action.getModel();
357 assertDefaultModel( model );
359 assertNotNull( action.getDependees() );
360 assertCoordinate( action.getDependees().get( 0 ), "artifactId1" );
361 assertCoordinate( action.getDependees().get( 1 ), "artifactId2" );
363 assertEquals( TEST_REPO, action.getRepositoryId() );
364 assertNull( action.getDependencies() );
365 assertNull( action.getMailingLists() );
366 assertTrue( action.getArtifacts().isEmpty() );
369 private void assertArtifacts( List<ArtifactMetadata> expectedArtifacts,
370 Map<String, List<ShowArtifactAction.ArtifactDownloadInfo>> artifactMap )
372 // assuming only one of each version at this point
373 assertEquals( expectedArtifacts.size(), artifactMap.size() );
374 for ( ArtifactMetadata artifact : expectedArtifacts )
376 assertTrue( artifactMap.containsKey( artifact.getVersion() ) );
377 List<ShowArtifactAction.ArtifactDownloadInfo> list = artifactMap.get( artifact.getVersion() );
378 ShowArtifactAction.ArtifactDownloadInfo actual = list.get( 0 );
379 assertEquals( artifact.getNamespace(), actual.getNamespace() );
380 assertEquals( artifact.getId(), actual.getId() );
381 assertEquals( artifact.getProject(), actual.getProject() );
382 assertEquals( artifact.getRepositoryId(), actual.getRepositoryId() );
383 assertEquals( artifact.getSize(), actual.getSize() );
384 assertEquals( artifact.getVersion(), actual.getVersion() );
385 assertEquals( TEST_TYPE, actual.getType() );
386 assertEquals( TEST_SIZE, actual.getSize() );
387 assertEquals( artifact.getNamespace() + "/" + artifact.getProject() + "/" + TEST_SNAPSHOT_VERSION + "/" +
388 artifact.getId(), actual.getPath() );
392 private static ArtifactMetadata createArtifact( String version )
394 ArtifactMetadata metadata = new ArtifactMetadata();
395 metadata.setProject( TEST_ARTIFACT_ID );
396 metadata.setId( TEST_ARTIFACT_ID + "-" + version + ".jar" );
397 metadata.setNamespace( TEST_GROUP_ID );
398 metadata.setRepositoryId( TEST_REPO );
399 metadata.setSize( TEST_SIZE );
400 metadata.setVersion( version );
404 private ProjectVersionReference createReference( String projectId )
406 ProjectVersionReference reference = new ProjectVersionReference();
407 reference.setNamespace( "groupId" );
408 reference.setProjectId( projectId );
409 reference.setProjectVersion( "version" );
410 reference.setReferenceType( ProjectVersionReference.ReferenceType.DEPENDENCY );
414 private void assertCoordinate( ProjectVersionReference dependee, String artifactId )
416 assertEquals( artifactId, dependee.getProjectId() );
417 assertEquals( "groupId", dependee.getNamespace() );
418 assertEquals( "version", dependee.getProjectVersion() );
421 private void assertDependencyBasic( Dependency dependency, String artifactId )
423 assertEquals( artifactId, dependency.getArtifactId() );
424 assertEquals( "groupId", dependency.getGroupId() );
425 assertEquals( "version", dependency.getVersion() );
428 private void assertDependencyExtended( Dependency dependency, String artifactId )
430 assertDependencyBasic( dependency, artifactId );
431 assertEquals( true, dependency.isOptional() );
432 assertEquals( "classifier", dependency.getClassifier() );
433 assertEquals( "type", dependency.getType() );
434 assertEquals( "scope", dependency.getScope() );
435 assertEquals( "systemPath", dependency.getSystemPath() );
438 private Dependency createDependencyExtended( String artifactId )
440 Dependency dependency = createDependencyBasic( artifactId );
441 dependency.setClassifier( "classifier" );
442 dependency.setOptional( true );
443 dependency.setScope( "scope" );
444 dependency.setSystemPath( "systemPath" );
445 dependency.setType( "type" );
449 private Dependency createDependencyBasic( String artifactId )
451 Dependency dependency = new Dependency();
452 dependency.setArtifactId( artifactId );
453 dependency.setGroupId( "groupId" );
454 dependency.setVersion( "version" );
458 private void assertMailingList( MailingList mailingList, String name, String prefix )
460 assertEquals( name, mailingList.getName() );
461 assertEquals( prefix + "-post@", mailingList.getPostAddress() );
462 assertEquals( prefix + "-subscribe@", mailingList.getSubscribeAddress() );
463 assertEquals( prefix + "-unsubscribe@", mailingList.getUnsubscribeAddress() );
464 assertEquals( prefix + "-archive-url", mailingList.getMainArchiveUrl() );
465 assertEquals( Arrays.asList( "other-" + prefix + "-archive-url-1", "other-" + prefix + "-archive-url-2" ),
466 mailingList.getOtherArchives() );
469 private MailingList createMailingList( String name, String prefix )
471 MailingList ml1 = new MailingList();
473 ml1.setPostAddress( prefix + "-post@" );
474 ml1.setSubscribeAddress( prefix + "-subscribe@" );
475 ml1.setUnsubscribeAddress( prefix + "-unsubscribe@" );
476 ml1.setMainArchiveUrl( prefix + "-archive-url" );
477 ml1.setOtherArchives(
478 Arrays.asList( "other-" + prefix + "-archive-url-1", "other-" + prefix + "-archive-url-2" ) );
482 private void assertNoOutputFields()
484 assertNull( action.getModel() );
485 assertNull( action.getDependees() );
486 assertNull( action.getDependencies() );
487 assertNull( action.getMailingLists() );
488 assertTrue( action.getArtifacts().isEmpty() );
491 private void assertError( String result )
493 assertEquals( Action.ERROR, result );
494 assertEquals( 1, action.getActionErrors().size() );
497 private void assertDefaultModel( ProjectVersionMetadata model )
499 assertDefaultModel( model, TEST_VERSION );
502 private void setActionParameters()
504 action.setGroupId( TEST_GROUP_ID );
505 action.setArtifactId( TEST_ARTIFACT_ID );
506 action.setVersion( TEST_VERSION );
509 private void assertActionParameters( ShowArtifactAction action )
511 assertEquals( TEST_GROUP_ID, action.getGroupId() );
512 assertEquals( TEST_ARTIFACT_ID, action.getArtifactId() );
513 assertEquals( TEST_VERSION, action.getVersion() );
516 private void assertActionSuccess( ShowArtifactAction action, String result )
518 assertEquals( Action.SUCCESS, result );
519 assertTrue( action.getActionErrors().isEmpty() );
520 assertTrue( action.getActionMessages().isEmpty() );
523 protected void setUp()
527 action = (ShowArtifactAction) lookup( Action.class, ACTION_HINT );
528 metadataResolver = (TestMetadataResolver) action.getMetadataResolver();
529 MockControl control = MockClassControl.createControl( RepositoryContentFactory.class );
530 RepositoryContentFactory factory = (RepositoryContentFactory) control.getMock();
531 action.setRepositoryFactory( factory );
533 ManagedRepositoryConfiguration config = new ManagedRepositoryConfiguration();
534 config.setId( TEST_REPO );
535 config.setLocation( getTestFile( "target/test-repo" ).getAbsolutePath() );
536 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
537 content.setRepository( config );
538 factory.getManagedRepositoryContent( TEST_REPO );
539 control.setDefaultReturnValue( content );