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.configuration.ManagedRepositoryConfiguration;
31 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
32 import org.apache.maven.archiva.repository.RepositoryContentFactory;
33 import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent;
34 import org.easymock.MockControl;
35 import org.easymock.classextension.MockClassControl;
37 import java.util.Arrays;
38 import java.util.Collections;
39 import java.util.List;
42 public class ShowArtifactActionTest
43 extends AbstractActionTestCase
45 private static final String ACTION_HINT = "showArtifactAction";
47 private static final String TEST_VERSION = "version";
49 private static final String TEST_SNAPSHOT_VERSION = "1.0-SNAPSHOT";
51 private static final String TEST_TS_SNAPSHOT_VERSION = "1.0-20091120.111111-1";
53 private static final String OTHER_TEST_REPO = "first-repo";
55 private ShowArtifactAction action;
57 private static final List<ArtifactMetadata> TEST_SNAPSHOT_ARTIFACTS = Arrays.asList( createArtifact(
58 TEST_TS_SNAPSHOT_VERSION ), createArtifact( "1.0-20091120.222222-2", "20091120.222222", 2 ), createArtifact(
59 "1.0-20091123.333333-3", "20091123.333333", 3 ) );
61 private static final long TEST_SIZE = 12345L;
63 private static final String TEST_TYPE = "jar";
65 public void testInstantiation()
67 assertFalse( action == lookup( Action.class, ACTION_HINT ) );
70 public void testGetArtifactUniqueRelease()
72 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
73 createProjectModel( TEST_VERSION ) );
75 setActionParameters();
77 String result = action.artifact();
79 assertActionSuccess( action, result );
81 assertActionParameters( action );
82 ProjectVersionMetadata model = action.getModel();
83 assertDefaultModel( model );
85 assertEquals( TEST_REPO, action.getRepositoryId() );
87 assertNull( action.getDependees() );
88 assertNull( action.getDependencies() );
89 assertNull( action.getMailingLists() );
90 assertTrue( action.getArtifacts().isEmpty() );
93 public void testGetArtifactUniqueSnapshot()
95 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
96 createProjectModel( TEST_SNAPSHOT_VERSION ) );
97 metadataResolver.setArtifacts( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_SNAPSHOT_VERSION,
98 TEST_SNAPSHOT_ARTIFACTS );
100 action.setGroupId( TEST_GROUP_ID );
101 action.setArtifactId( TEST_ARTIFACT_ID );
102 action.setVersion( TEST_SNAPSHOT_VERSION );
104 String result = action.artifact();
106 assertActionSuccess( action, result );
108 assertEquals( TEST_GROUP_ID, action.getGroupId() );
109 assertEquals( TEST_ARTIFACT_ID, action.getArtifactId() );
110 assertEquals( TEST_SNAPSHOT_VERSION, action.getVersion() );
111 ProjectVersionMetadata model = action.getModel();
112 assertDefaultModel( model, TEST_SNAPSHOT_VERSION );
114 assertEquals( TEST_REPO, action.getRepositoryId() );
116 assertArtifacts( TEST_SNAPSHOT_ARTIFACTS, action.getArtifacts() );
118 assertNull( action.getDependees() );
119 assertNull( action.getDependencies() );
120 assertNull( action.getMailingLists() );
123 public void testGetArtifactUniqueSnapshotTimestamped()
125 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
126 createProjectModel( TEST_SNAPSHOT_VERSION ) );
127 metadataResolver.setArtifacts( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_SNAPSHOT_VERSION,
128 TEST_SNAPSHOT_ARTIFACTS );
130 action.setGroupId( TEST_GROUP_ID );
131 action.setArtifactId( TEST_ARTIFACT_ID );
132 action.setVersion( TEST_TS_SNAPSHOT_VERSION );
134 String result = action.artifact();
135 assertError( result );
136 assertNoOutputFields();
139 public void testGetMissingProject()
141 setActionParameters();
143 String result = action.artifact();
144 assertError( result );
146 assertActionParameters( action );
147 assertNoOutputFields();
150 public void testGetArtifactNoObservableRepos()
152 setObservableRepos( Collections.<String>emptyList() );
154 setActionParameters();
156 String result = action.artifact();
158 // Actually, it'd be better to have an error:
159 assertError( result );
160 assertActionParameters( action );
161 assertNoOutputFields();
164 public void testGetArtifactNotInObservableRepos()
166 metadataResolver.setProjectVersion( OTHER_TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
167 createProjectModel( TEST_VERSION ) );
169 setActionParameters();
171 String result = action.artifact();
172 assertError( result );
174 assertActionParameters( action );
175 assertNoOutputFields();
178 public void testGetArtifactOnlySeenInSecondObservableRepo()
180 setObservableRepos( Arrays.asList( OTHER_TEST_REPO, TEST_REPO ) );
181 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
182 createProjectModel( TEST_VERSION ) );
184 setActionParameters();
186 String result = action.artifact();
188 assertActionSuccess( action, result );
190 assertActionParameters( action );
191 ProjectVersionMetadata model = action.getModel();
192 assertDefaultModel( model );
194 assertEquals( TEST_REPO, action.getRepositoryId() );
196 assertNull( action.getDependees() );
197 assertNull( action.getDependencies() );
198 assertNull( action.getMailingLists() );
199 assertTrue( action.getArtifacts().isEmpty() );
202 public void testGetArtifactSeenInBothObservableRepo()
204 setObservableRepos( Arrays.asList( TEST_REPO, OTHER_TEST_REPO ) );
205 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
206 createProjectModel( TEST_VERSION ) );
207 metadataResolver.setProjectVersion( OTHER_TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
208 createProjectModel( TEST_VERSION ) );
210 setActionParameters();
212 String result = action.artifact();
214 assertActionSuccess( action, result );
216 assertActionParameters( action );
217 ProjectVersionMetadata model = action.getModel();
218 assertDefaultModel( model );
220 assertEquals( TEST_REPO, action.getRepositoryId() );
222 assertNull( action.getDependees() );
223 assertNull( action.getDependencies() );
224 assertNull( action.getMailingLists() );
225 assertTrue( action.getArtifacts().isEmpty() );
228 public void testGetArtifactCanOnlyObserveInOneOfTwoRepos()
230 setObservableRepos( Arrays.asList( TEST_REPO ) );
231 metadataResolver.setProjectVersion( OTHER_TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
232 createProjectModel( TEST_VERSION ) );
233 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
234 createProjectModel( TEST_VERSION ) );
236 setActionParameters();
238 String result = action.artifact();
240 assertActionSuccess( action, result );
242 assertActionParameters( action );
243 ProjectVersionMetadata model = action.getModel();
244 assertDefaultModel( model );
246 assertEquals( TEST_REPO, action.getRepositoryId() );
248 assertNull( action.getDependees() );
249 assertNull( action.getDependencies() );
250 assertNull( action.getMailingLists() );
251 assertTrue( action.getArtifacts().isEmpty() );
254 public void testGetArtifactNoMavenFacet()
256 ProjectVersionMetadata versionMetadata = new ProjectVersionMetadata();
257 versionMetadata.setId( TEST_VERSION );
258 versionMetadata.setUrl( TEST_URL );
259 versionMetadata.setName( TEST_NAME );
260 versionMetadata.setDescription( TEST_DESCRIPTION );
262 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID, versionMetadata );
264 setActionParameters();
266 String result = action.artifact();
268 assertActionSuccess( action, result );
270 assertActionParameters( action );
271 ProjectVersionMetadata model = action.getModel();
272 assertEquals( TEST_VERSION, model.getVersion() );
273 assertEquals( TEST_URL, model.getUrl() );
274 assertEquals( TEST_NAME, model.getName() );
275 assertEquals( TEST_DESCRIPTION, model.getDescription() );
277 assertEquals( TEST_REPO, action.getRepositoryId() );
279 assertNull( action.getDependees() );
280 assertNull( action.getDependencies() );
281 assertNull( action.getMailingLists() );
282 assertTrue( action.getArtifacts().isEmpty() );
285 public void testGetMailingLists()
287 ProjectVersionMetadata versionMetadata = createProjectModel( TEST_VERSION );
288 MailingList ml1 = createMailingList( "Users List", "users" );
289 MailingList ml2 = createMailingList( "Developers List", "dev" );
290 versionMetadata.setMailingLists( Arrays.asList( ml1, ml2 ) );
291 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID, versionMetadata );
293 setActionParameters();
295 String result = action.mailingLists();
297 assertActionSuccess( action, result );
299 assertActionParameters( action );
300 ProjectVersionMetadata model = action.getModel();
301 assertDefaultModel( model );
303 assertNotNull( action.getMailingLists() );
304 assertMailingList( action.getMailingLists().get( 0 ), "Users List", "users" );
305 assertMailingList( action.getMailingLists().get( 1 ), "Developers List", "dev" );
307 assertEquals( TEST_REPO, action.getRepositoryId() );
308 assertNull( action.getDependees() );
309 assertNull( action.getDependencies() );
310 assertTrue( action.getArtifacts().isEmpty() );
313 public void testGetDependencies()
315 ProjectVersionMetadata versionMetadata = createProjectModel( TEST_VERSION );
316 Dependency dependency1 = createDependencyBasic( "artifactId1" );
317 Dependency dependency2 = createDependencyExtended( "artifactId2" );
318 versionMetadata.setDependencies( Arrays.asList( dependency1, dependency2 ) );
319 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID, versionMetadata );
321 setActionParameters();
323 String result = action.dependencies();
325 assertActionSuccess( action, result );
327 assertActionParameters( action );
328 ProjectVersionMetadata model = action.getModel();
329 assertDefaultModel( model );
331 assertNotNull( action.getDependencies() );
332 assertDependencyBasic( action.getDependencies().get( 0 ), "artifactId1" );
333 assertDependencyExtended( action.getDependencies().get( 1 ), "artifactId2" );
335 assertEquals( TEST_REPO, action.getRepositoryId() );
336 assertNull( action.getDependees() );
337 assertNull( action.getMailingLists() );
338 assertTrue( action.getArtifacts().isEmpty() );
341 public void testGetDependees()
343 ProjectVersionMetadata versionMetadata = createProjectModel( TEST_VERSION );
344 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID, versionMetadata );
345 ProjectVersionReference dependee1 = createReference( "artifactId1" );
346 ProjectVersionReference dependee2 = createReference( "artifactId2" );
347 metadataResolver.setProjectReferences( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION,
348 Arrays.asList( dependee1, dependee2 ) );
350 setActionParameters();
352 String result = action.dependees();
354 assertActionSuccess( action, result );
356 assertActionParameters( action );
357 ProjectVersionMetadata model = action.getModel();
358 assertDefaultModel( model );
360 assertNotNull( action.getDependees() );
361 assertCoordinate( action.getDependees().get( 0 ), "artifactId1" );
362 assertCoordinate( action.getDependees().get( 1 ), "artifactId2" );
364 assertEquals( TEST_REPO, action.getRepositoryId() );
365 assertNull( action.getDependencies() );
366 assertNull( action.getMailingLists() );
367 assertTrue( action.getArtifacts().isEmpty() );
370 private void assertArtifacts( List<ArtifactMetadata> expectedArtifacts,
371 Map<String, List<ShowArtifactAction.ArtifactDownloadInfo>> artifactMap )
373 // assuming only one of each version at this point
374 assertEquals( expectedArtifacts.size(), artifactMap.size() );
375 for ( ArtifactMetadata artifact : expectedArtifacts )
377 assertTrue( artifactMap.containsKey( artifact.getVersion() ) );
378 List<ShowArtifactAction.ArtifactDownloadInfo> list = artifactMap.get( artifact.getVersion() );
379 ShowArtifactAction.ArtifactDownloadInfo actual = list.get( 0 );
380 assertEquals( artifact.getNamespace(), actual.getNamespace() );
381 assertEquals( artifact.getId(), actual.getId() );
382 assertEquals( artifact.getProject(), actual.getProject() );
383 assertEquals( artifact.getRepositoryId(), actual.getRepositoryId() );
384 assertEquals( artifact.getVersion(), actual.getVersion() );
385 assertEquals( TEST_TYPE, actual.getType() );
386 assertEquals( "12.06 K", actual.getSize() );
387 assertEquals( artifact.getNamespace() + "/" + artifact.getProject() + "/" + TEST_SNAPSHOT_VERSION + "/" +
388 artifact.getId(), actual.getPath() );
392 private static ArtifactMetadata createArtifact( String version )
394 return createArtifact( version, null, 0 );
397 private static ArtifactMetadata createArtifact( String version, String timestamp, int buildNumber )
399 ArtifactMetadata metadata = new ArtifactMetadata();
400 metadata.setProject( TEST_ARTIFACT_ID );
401 metadata.setId( TEST_ARTIFACT_ID + "-" + version + ".jar" );
402 metadata.setNamespace( TEST_GROUP_ID );
403 metadata.setRepositoryId( TEST_REPO );
404 metadata.setSize( TEST_SIZE );
405 metadata.setVersion( version );
407 MavenArtifactFacet facet = new MavenArtifactFacet();
408 facet.setType( "jar" );
409 facet.setTimestamp( timestamp );
410 facet.setBuildNumber( buildNumber );
411 metadata.addFacet( facet );
416 private ProjectVersionReference createReference( String projectId )
418 ProjectVersionReference reference = new ProjectVersionReference();
419 reference.setNamespace( "groupId" );
420 reference.setProjectId( projectId );
421 reference.setProjectVersion( "version" );
422 reference.setReferenceType( ProjectVersionReference.ReferenceType.DEPENDENCY );
426 private void assertCoordinate( ProjectVersionReference dependee, String artifactId )
428 assertEquals( artifactId, dependee.getProjectId() );
429 assertEquals( "groupId", dependee.getNamespace() );
430 assertEquals( "version", dependee.getProjectVersion() );
433 private void assertDependencyBasic( Dependency dependency, String artifactId )
435 assertEquals( artifactId, dependency.getArtifactId() );
436 assertEquals( "groupId", dependency.getGroupId() );
437 assertEquals( "version", dependency.getVersion() );
440 private void assertDependencyExtended( Dependency dependency, String artifactId )
442 assertDependencyBasic( dependency, artifactId );
443 assertEquals( true, dependency.isOptional() );
444 assertEquals( "classifier", dependency.getClassifier() );
445 assertEquals( "type", dependency.getType() );
446 assertEquals( "scope", dependency.getScope() );
447 assertEquals( "systemPath", dependency.getSystemPath() );
450 private Dependency createDependencyExtended( String artifactId )
452 Dependency dependency = createDependencyBasic( artifactId );
453 dependency.setClassifier( "classifier" );
454 dependency.setOptional( true );
455 dependency.setScope( "scope" );
456 dependency.setSystemPath( "systemPath" );
457 dependency.setType( "type" );
461 private Dependency createDependencyBasic( String artifactId )
463 Dependency dependency = new Dependency();
464 dependency.setArtifactId( artifactId );
465 dependency.setGroupId( "groupId" );
466 dependency.setVersion( "version" );
470 private void assertMailingList( MailingList mailingList, String name, String prefix )
472 assertEquals( name, mailingList.getName() );
473 assertEquals( prefix + "-post@", mailingList.getPostAddress() );
474 assertEquals( prefix + "-subscribe@", mailingList.getSubscribeAddress() );
475 assertEquals( prefix + "-unsubscribe@", mailingList.getUnsubscribeAddress() );
476 assertEquals( prefix + "-archive-url", mailingList.getMainArchiveUrl() );
477 assertEquals( Arrays.asList( "other-" + prefix + "-archive-url-1", "other-" + prefix + "-archive-url-2" ),
478 mailingList.getOtherArchives() );
481 private MailingList createMailingList( String name, String prefix )
483 MailingList ml1 = new MailingList();
485 ml1.setPostAddress( prefix + "-post@" );
486 ml1.setSubscribeAddress( prefix + "-subscribe@" );
487 ml1.setUnsubscribeAddress( prefix + "-unsubscribe@" );
488 ml1.setMainArchiveUrl( prefix + "-archive-url" );
489 ml1.setOtherArchives(
490 Arrays.asList( "other-" + prefix + "-archive-url-1", "other-" + prefix + "-archive-url-2" ) );
494 private void assertNoOutputFields()
496 assertNull( action.getModel() );
497 assertNull( action.getDependees() );
498 assertNull( action.getDependencies() );
499 assertNull( action.getMailingLists() );
500 assertTrue( action.getArtifacts().isEmpty() );
503 private void assertError( String result )
505 assertEquals( Action.ERROR, result );
506 assertEquals( 1, action.getActionErrors().size() );
509 private void assertDefaultModel( ProjectVersionMetadata model )
511 assertDefaultModel( model, TEST_VERSION );
514 private void setActionParameters()
516 action.setGroupId( TEST_GROUP_ID );
517 action.setArtifactId( TEST_ARTIFACT_ID );
518 action.setVersion( TEST_VERSION );
521 private void assertActionParameters( ShowArtifactAction action )
523 assertEquals( TEST_GROUP_ID, action.getGroupId() );
524 assertEquals( TEST_ARTIFACT_ID, action.getArtifactId() );
525 assertEquals( TEST_VERSION, action.getVersion() );
528 private void assertActionSuccess( ShowArtifactAction action, String result )
530 assertEquals( Action.SUCCESS, result );
531 assertTrue( action.getActionErrors().isEmpty() );
532 assertTrue( action.getActionMessages().isEmpty() );
535 protected void setUp()
539 action = (ShowArtifactAction) lookup( Action.class, ACTION_HINT );
540 metadataResolver = (TestMetadataResolver) action.getMetadataResolver();
541 MockControl control = MockClassControl.createControl( RepositoryContentFactory.class );
542 RepositoryContentFactory factory = (RepositoryContentFactory) control.getMock();
543 action.setRepositoryFactory( factory );
545 ManagedRepositoryConfiguration config = new ManagedRepositoryConfiguration();
546 config.setId( TEST_REPO );
547 config.setLocation( getTestFile( "target/test-repo" ).getAbsolutePath() );
548 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
549 content.setRepository( config );
550 factory.getManagedRepositoryContent( TEST_REPO );
551 control.setDefaultReturnValue( content );