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;
26 import com.opensymphony.xwork2.Action;
27 import org.apache.archiva.metadata.model.Dependency;
28 import org.apache.archiva.metadata.model.MailingList;
29 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
30 import org.apache.archiva.metadata.model.ProjectVersionReference;
31 import org.apache.archiva.metadata.repository.memory.TestMetadataResolver;
33 public class ShowArtifactActionTest
34 extends AbstractActionTestCase
36 private static final String ACTION_HINT = "showArtifactAction";
38 private static final String TEST_VERSION = "version";
40 private static final String TEST_SNAPSHOT_VERSION = "1.0-SNAPSHOT";
42 private static final String TEST_TS_SNAPSHOT_VERSION = "1.0-20091120.111111-1";
44 private static final List<String> ALL_TEST_SNAPSHOT_VERSIONS =
45 Arrays.asList( TEST_TS_SNAPSHOT_VERSION, "1.0-20091120.222222-2", "1.0-20091123.333333-3" );
47 private static final String OTHER_TEST_REPO = "first-repo";
49 private ShowArtifactAction action;
51 public void testInstantiation()
53 assertFalse( action == lookup( Action.class, ACTION_HINT ) );
56 public void testGetArtifactUniqueRelease()
58 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
59 createProjectModel( TEST_VERSION ) );
61 setActionParameters();
63 String result = action.artifact();
65 assertActionSuccess( action, result );
67 assertActionParameters( action );
68 ProjectVersionMetadata model = action.getModel();
69 assertDefaultModel( model );
71 assertEquals( TEST_REPO, action.getRepositoryId() );
73 assertNull( action.getDependees() );
74 assertNull( action.getDependencies() );
75 assertNull( action.getMailingLists() );
76 assertTrue( action.getSnapshotVersions().isEmpty() );
79 public void testGetArtifactUniqueSnapshot()
81 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
82 createProjectModel( TEST_SNAPSHOT_VERSION ) );
83 metadataResolver.setArtifactVersions( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_SNAPSHOT_VERSION,
84 ALL_TEST_SNAPSHOT_VERSIONS );
86 action.setGroupId( TEST_GROUP_ID );
87 action.setArtifactId( TEST_ARTIFACT_ID );
88 action.setVersion( TEST_SNAPSHOT_VERSION );
90 String result = action.artifact();
92 assertActionSuccess( action, result );
94 assertEquals( TEST_GROUP_ID, action.getGroupId() );
95 assertEquals( TEST_ARTIFACT_ID, action.getArtifactId() );
96 assertEquals( TEST_SNAPSHOT_VERSION, action.getVersion() );
97 ProjectVersionMetadata model = action.getModel();
98 assertDefaultModel( model, TEST_SNAPSHOT_VERSION );
100 assertEquals( TEST_REPO, action.getRepositoryId() );
102 assertEquals( ALL_TEST_SNAPSHOT_VERSIONS, action.getSnapshotVersions() );
104 assertNull( action.getDependees() );
105 assertNull( action.getDependencies() );
106 assertNull( action.getMailingLists() );
109 public void testGetArtifactUniqueSnapshotTimestamped()
111 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
112 createProjectModel( TEST_TS_SNAPSHOT_VERSION ) );
113 metadataResolver.setArtifactVersions( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_TS_SNAPSHOT_VERSION,
114 ALL_TEST_SNAPSHOT_VERSIONS );
116 action.setGroupId( TEST_GROUP_ID );
117 action.setArtifactId( TEST_ARTIFACT_ID );
118 action.setVersion( TEST_TS_SNAPSHOT_VERSION );
120 String result = action.artifact();
122 assertActionSuccess( action, result );
124 assertEquals( TEST_GROUP_ID, action.getGroupId() );
125 assertEquals( TEST_ARTIFACT_ID, action.getArtifactId() );
126 assertEquals( TEST_TS_SNAPSHOT_VERSION, action.getVersion() );
127 ProjectVersionMetadata model = action.getModel();
128 assertDefaultModel( model, TEST_TS_SNAPSHOT_VERSION );
130 assertEquals( TEST_REPO, action.getRepositoryId() );
132 assertEquals( Arrays.asList( ALL_TEST_SNAPSHOT_VERSIONS.get( 1 ), ALL_TEST_SNAPSHOT_VERSIONS.get( 2 ) ),
133 action.getSnapshotVersions() );
135 assertNull( action.getDependees() );
136 assertNull( action.getDependencies() );
137 assertNull( action.getMailingLists() );
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.getSnapshotVersions().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.getSnapshotVersions().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.getSnapshotVersions().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.getSnapshotVersions().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 assertNull( action.getRepositoryId() );
309 assertNull( action.getDependees() );
310 assertNull( action.getDependencies() );
311 assertNull( action.getSnapshotVersions() );
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 assertNull( action.getRepositoryId() );
337 assertNull( action.getDependees() );
338 assertNull( action.getMailingLists() );
339 assertNull( action.getSnapshotVersions() );
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 assertNull( action.getRepositoryId() );
366 assertNull( action.getDependencies() );
367 assertNull( action.getMailingLists() );
368 assertNull( action.getSnapshotVersions() );
371 private ProjectVersionReference createReference( String projectId )
373 ProjectVersionReference reference = new ProjectVersionReference();
374 reference.setNamespace( "groupId" );
375 reference.setProjectId( projectId );
376 reference.setProjectVersion( "version" );
377 reference.setReferenceType( ProjectVersionReference.ReferenceType.DEPENDENCY );
381 private void assertCoordinate( ProjectVersionReference dependee, String artifactId )
383 assertEquals( artifactId, dependee.getProjectId() );
384 assertEquals( "groupId", dependee.getNamespace() );
385 assertEquals( "version", dependee.getProjectVersion() );
388 private void assertDependencyBasic( Dependency dependency, String artifactId )
390 assertEquals( artifactId, dependency.getArtifactId() );
391 assertEquals( "groupId", dependency.getGroupId() );
392 assertEquals( "version", dependency.getVersion() );
395 private void assertDependencyExtended( Dependency dependency, String artifactId )
397 assertDependencyBasic( dependency, artifactId );
398 assertEquals( true, dependency.isOptional() );
399 assertEquals( "classifier", dependency.getClassifier() );
400 assertEquals( "type", dependency.getType() );
401 assertEquals( "scope", dependency.getScope() );
402 assertEquals( "systemPath", dependency.getSystemPath() );
405 private Dependency createDependencyExtended( String artifactId )
407 Dependency dependency = createDependencyBasic( artifactId );
408 dependency.setClassifier( "classifier" );
409 dependency.setOptional( true );
410 dependency.setScope( "scope" );
411 dependency.setSystemPath( "systemPath" );
412 dependency.setType( "type" );
416 private Dependency createDependencyBasic( String artifactId )
418 Dependency dependency = new Dependency();
419 dependency.setArtifactId( artifactId );
420 dependency.setGroupId( "groupId" );
421 dependency.setVersion( "version" );
425 private void assertMailingList( MailingList mailingList, String name, String prefix )
427 assertEquals( name, mailingList.getName() );
428 assertEquals( prefix + "-post@", mailingList.getPostAddress() );
429 assertEquals( prefix + "-subscribe@", mailingList.getSubscribeAddress() );
430 assertEquals( prefix + "-unsubscribe@", mailingList.getUnsubscribeAddress() );
431 assertEquals( prefix + "-archive-url", mailingList.getMainArchiveUrl() );
432 assertEquals( Arrays.asList( "other-" + prefix + "-archive-url-1", "other-" + prefix + "-archive-url-2" ),
433 mailingList.getOtherArchives() );
436 private MailingList createMailingList( String name, String prefix )
438 MailingList ml1 = new MailingList();
440 ml1.setPostAddress( prefix + "-post@" );
441 ml1.setSubscribeAddress( prefix + "-subscribe@" );
442 ml1.setUnsubscribeAddress( prefix + "-unsubscribe@" );
443 ml1.setMainArchiveUrl( prefix + "-archive-url" );
444 ml1.setOtherArchives(
445 Arrays.asList( "other-" + prefix + "-archive-url-1", "other-" + prefix + "-archive-url-2" ) );
449 private void assertNoOutputFields()
451 assertNull( action.getModel() );
452 assertNull( action.getDependees() );
453 assertNull( action.getDependencies() );
454 assertNull( action.getMailingLists() );
455 assertTrue( action.getSnapshotVersions().isEmpty() );
458 private void assertError( String result )
460 assertEquals( Action.ERROR, result );
461 assertEquals( 1, action.getActionErrors().size() );
464 private void assertDefaultModel( ProjectVersionMetadata model )
466 assertDefaultModel( model, TEST_VERSION );
469 private void setActionParameters()
471 action.setGroupId( TEST_GROUP_ID );
472 action.setArtifactId( TEST_ARTIFACT_ID );
473 action.setVersion( TEST_VERSION );
476 private void assertActionParameters( ShowArtifactAction action )
478 assertEquals( TEST_GROUP_ID, action.getGroupId() );
479 assertEquals( TEST_ARTIFACT_ID, action.getArtifactId() );
480 assertEquals( TEST_VERSION, action.getVersion() );
483 private void assertActionSuccess( ShowArtifactAction action, String result )
485 assertEquals( Action.SUCCESS, result );
486 assertTrue( action.getActionErrors().isEmpty() );
487 assertTrue( action.getActionMessages().isEmpty() );
490 protected void setUp()
494 action = (ShowArtifactAction) lookup( Action.class, ACTION_HINT );
495 metadataResolver = (TestMetadataResolver) action.getMetadataResolver();