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;
24 import org.apache.archiva.metadata.generic.GenericMetadataFacet;
25 import org.apache.archiva.metadata.model.ArtifactMetadata;
26 import org.apache.archiva.metadata.model.Dependency;
27 import org.apache.archiva.metadata.model.MailingList;
28 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
29 import org.apache.archiva.metadata.model.ProjectVersionReference;
30 import org.apache.archiva.metadata.repository.memory.TestMetadataResolver;
31 import org.apache.archiva.metadata.repository.storage.maven2.MavenArtifactFacet;
32 import org.apache.maven.archiva.common.utils.VersionUtil;
33 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
34 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
35 import org.apache.maven.archiva.repository.RepositoryContentFactory;
36 import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent;
37 import org.easymock.MockControl;
38 import org.easymock.classextension.MockClassControl;
40 import java.util.Arrays;
41 import java.util.Collections;
42 import java.util.List;
45 public class ShowArtifactActionTest
46 extends AbstractActionTestCase
48 private static final String ACTION_HINT = "showArtifactAction";
50 private static final String TEST_VERSION = "version";
52 private static final String TEST_SNAPSHOT_VERSION = "1.0-SNAPSHOT";
54 private static final String TEST_TS_SNAPSHOT_VERSION = "1.0-20091120.111111-1";
56 private static final String OTHER_TEST_REPO = "first-repo";
58 private ShowArtifactAction action;
60 private static final List<ArtifactMetadata> TEST_SNAPSHOT_ARTIFACTS = Arrays.asList( createArtifact(
61 TEST_TS_SNAPSHOT_VERSION ), createArtifact( "1.0-20091120.222222-2", "20091120.222222", 2 ), createArtifact(
62 "1.0-20091123.333333-3", "20091123.333333", 3 ) );
64 private static final long TEST_SIZE = 12345L;
66 private static final String TEST_TYPE = "jar";
68 public void testInstantiation()
70 assertFalse( action == lookup( Action.class, ACTION_HINT ) );
73 public void testGetArtifactUniqueRelease()
75 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
76 createProjectModel( TEST_VERSION ) );
78 setActionParameters();
80 String result = action.artifact();
82 assertActionSuccess( action, result );
84 assertActionParameters( action );
85 ProjectVersionMetadata model = action.getModel();
86 assertDefaultModel( model );
88 assertEquals( TEST_REPO, action.getRepositoryId() );
90 assertNull( action.getDependees() );
91 assertNull( action.getDependencies() );
92 assertNull( action.getMailingLists() );
93 assertTrue( action.getArtifacts().isEmpty() );
96 public void testGetArtifactUniqueSnapshot()
98 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
99 createProjectModel( TEST_SNAPSHOT_VERSION ) );
100 metadataResolver.setArtifacts( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_SNAPSHOT_VERSION,
101 TEST_SNAPSHOT_ARTIFACTS );
103 action.setGroupId( TEST_GROUP_ID );
104 action.setArtifactId( TEST_ARTIFACT_ID );
105 action.setVersion( TEST_SNAPSHOT_VERSION );
107 String result = action.artifact();
109 assertActionSuccess( action, result );
111 assertEquals( TEST_GROUP_ID, action.getGroupId() );
112 assertEquals( TEST_ARTIFACT_ID, action.getArtifactId() );
113 assertEquals( TEST_SNAPSHOT_VERSION, action.getVersion() );
114 ProjectVersionMetadata model = action.getModel();
115 assertDefaultModel( model, TEST_SNAPSHOT_VERSION );
117 assertEquals( TEST_REPO, action.getRepositoryId() );
119 assertArtifacts( TEST_SNAPSHOT_ARTIFACTS, action.getArtifacts() );
121 assertNull( action.getDependees() );
122 assertNull( action.getDependencies() );
123 assertNull( action.getMailingLists() );
126 public void testGetArtifactUniqueSnapshotTimestamped()
128 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
129 createProjectModel( TEST_SNAPSHOT_VERSION ) );
130 metadataResolver.setArtifacts( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_SNAPSHOT_VERSION,
131 TEST_SNAPSHOT_ARTIFACTS );
133 action.setGroupId( TEST_GROUP_ID );
134 action.setArtifactId( TEST_ARTIFACT_ID );
135 action.setVersion( TEST_TS_SNAPSHOT_VERSION );
137 String result = action.artifact();
138 assertError( result );
139 assertNoOutputFields();
142 public void testGetMissingProject()
144 setActionParameters();
146 String result = action.artifact();
147 assertError( result );
149 assertActionParameters( action );
150 assertNoOutputFields();
153 public void testGetArtifactNoObservableRepos()
155 setObservableRepos( Collections.<String>emptyList() );
157 setActionParameters();
159 String result = action.artifact();
161 // Actually, it'd be better to have an error:
162 assertError( result );
163 assertActionParameters( action );
164 assertNoOutputFields();
167 public void testGetArtifactNotInObservableRepos()
169 metadataResolver.setProjectVersion( OTHER_TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
170 createProjectModel( TEST_VERSION ) );
172 setActionParameters();
174 String result = action.artifact();
175 assertError( result );
177 assertActionParameters( action );
178 assertNoOutputFields();
181 public void testGetArtifactOnlySeenInSecondObservableRepo()
183 setObservableRepos( Arrays.asList( OTHER_TEST_REPO, TEST_REPO ) );
184 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
185 createProjectModel( TEST_VERSION ) );
187 setActionParameters();
189 String result = action.artifact();
191 assertActionSuccess( action, result );
193 assertActionParameters( action );
194 ProjectVersionMetadata model = action.getModel();
195 assertDefaultModel( model );
197 assertEquals( TEST_REPO, action.getRepositoryId() );
199 assertNull( action.getDependees() );
200 assertNull( action.getDependencies() );
201 assertNull( action.getMailingLists() );
202 assertTrue( action.getArtifacts().isEmpty() );
205 public void testGetArtifactSeenInBothObservableRepo()
207 setObservableRepos( Arrays.asList( TEST_REPO, OTHER_TEST_REPO ) );
208 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
209 createProjectModel( TEST_VERSION ) );
210 metadataResolver.setProjectVersion( OTHER_TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
211 createProjectModel( TEST_VERSION ) );
213 setActionParameters();
215 String result = action.artifact();
217 assertActionSuccess( action, result );
219 assertActionParameters( action );
220 ProjectVersionMetadata model = action.getModel();
221 assertDefaultModel( model );
223 assertEquals( TEST_REPO, action.getRepositoryId() );
225 assertNull( action.getDependees() );
226 assertNull( action.getDependencies() );
227 assertNull( action.getMailingLists() );
228 assertTrue( action.getArtifacts().isEmpty() );
231 public void testGetArtifactCanOnlyObserveInOneOfTwoRepos()
233 setObservableRepos( Arrays.asList( TEST_REPO ) );
234 metadataResolver.setProjectVersion( OTHER_TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
235 createProjectModel( TEST_VERSION ) );
236 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
237 createProjectModel( TEST_VERSION ) );
239 setActionParameters();
241 String result = action.artifact();
243 assertActionSuccess( action, result );
245 assertActionParameters( action );
246 ProjectVersionMetadata model = action.getModel();
247 assertDefaultModel( model );
249 assertEquals( TEST_REPO, action.getRepositoryId() );
251 assertNull( action.getDependees() );
252 assertNull( action.getDependencies() );
253 assertNull( action.getMailingLists() );
254 assertTrue( action.getArtifacts().isEmpty() );
257 public void testGetArtifactNoMavenFacet()
259 ProjectVersionMetadata versionMetadata = new ProjectVersionMetadata();
260 versionMetadata.setId( TEST_VERSION );
261 versionMetadata.setUrl( TEST_URL );
262 versionMetadata.setName( TEST_NAME );
263 versionMetadata.setDescription( TEST_DESCRIPTION );
265 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID, versionMetadata );
267 setActionParameters();
269 String result = action.artifact();
271 assertActionSuccess( action, result );
273 assertActionParameters( action );
274 ProjectVersionMetadata model = action.getModel();
275 assertEquals( TEST_VERSION, model.getVersion() );
276 assertEquals( TEST_URL, model.getUrl() );
277 assertEquals( TEST_NAME, model.getName() );
278 assertEquals( TEST_DESCRIPTION, model.getDescription() );
280 assertEquals( TEST_REPO, action.getRepositoryId() );
282 assertNull( action.getDependees() );
283 assertNull( action.getDependencies() );
284 assertNull( action.getMailingLists() );
285 assertTrue( action.getArtifacts().isEmpty() );
288 public void testGetMailingLists()
290 ProjectVersionMetadata versionMetadata = createProjectModel( TEST_VERSION );
291 MailingList ml1 = createMailingList( "Users List", "users" );
292 MailingList ml2 = createMailingList( "Developers List", "dev" );
293 versionMetadata.setMailingLists( Arrays.asList( ml1, ml2 ) );
294 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID, versionMetadata );
296 setActionParameters();
298 String result = action.mailingLists();
300 assertActionSuccess( action, result );
302 assertActionParameters( action );
303 ProjectVersionMetadata model = action.getModel();
304 assertDefaultModel( model );
306 assertNotNull( action.getMailingLists() );
307 assertMailingList( action.getMailingLists().get( 0 ), "Users List", "users" );
308 assertMailingList( action.getMailingLists().get( 1 ), "Developers List", "dev" );
310 assertEquals( TEST_REPO, action.getRepositoryId() );
311 assertNull( action.getDependees() );
312 assertNull( action.getDependencies() );
313 assertTrue( action.getArtifacts().isEmpty() );
316 public void testGetDependencies()
318 ProjectVersionMetadata versionMetadata = createProjectModel( TEST_VERSION );
319 Dependency dependency1 = createDependencyBasic( "artifactId1" );
320 Dependency dependency2 = createDependencyExtended( "artifactId2" );
321 versionMetadata.setDependencies( Arrays.asList( dependency1, dependency2 ) );
322 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID, versionMetadata );
324 setActionParameters();
326 String result = action.dependencies();
328 assertActionSuccess( action, result );
330 assertActionParameters( action );
331 ProjectVersionMetadata model = action.getModel();
332 assertDefaultModel( model );
334 assertNotNull( action.getDependencies() );
335 assertDependencyBasic( action.getDependencies().get( 0 ), "artifactId1" );
336 assertDependencyExtended( action.getDependencies().get( 1 ), "artifactId2" );
338 assertEquals( TEST_REPO, action.getRepositoryId() );
339 assertNull( action.getDependees() );
340 assertNull( action.getMailingLists() );
341 assertTrue( action.getArtifacts().isEmpty() );
344 public void testGetDependees()
346 ProjectVersionMetadata versionMetadata = createProjectModel( TEST_VERSION );
347 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID, versionMetadata );
348 ProjectVersionReference dependee1 = createReference( "artifactId1" );
349 ProjectVersionReference dependee2 = createReference( "artifactId2" );
350 metadataResolver.setProjectReferences( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION,
351 Arrays.asList( dependee1, dependee2 ) );
353 setActionParameters();
355 String result = action.dependees();
357 assertActionSuccess( action, result );
359 assertActionParameters( action );
360 ProjectVersionMetadata model = action.getModel();
361 assertDefaultModel( model );
363 assertNotNull( action.getDependees() );
364 assertCoordinate( action.getDependees().get( 0 ), "artifactId1" );
365 assertCoordinate( action.getDependees().get( 1 ), "artifactId2" );
367 assertEquals( TEST_REPO, action.getRepositoryId() );
368 assertNull( action.getDependencies() );
369 assertNull( action.getMailingLists() );
370 assertTrue( action.getArtifacts().isEmpty() );
373 public void testGetProjectMetadata()
375 ProjectVersionMetadata versionMetadata = createProjectModel( TEST_VERSION );
377 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID, versionMetadata );
379 setActionParameters();
381 String result = action.projectMetadata();
383 assertActionSuccess( action, result );
385 assertActionParameters( action );
387 Map<String, String> genericMetadata = action.getGenericMetadata();
388 assertNotNull( genericMetadata.get( TEST_GENERIC_METADATA_PROPERTY_NAME ) );
389 assertEquals( genericMetadata.get( TEST_GENERIC_METADATA_PROPERTY_NAME ), TEST_GENERIC_METADATA_PROPERTY_VALUE );
391 assertEquals( TEST_REPO, action.getRepositoryId() );
392 assertNotNull( action.getModel() );
393 assertNull( action.getDependees() );
394 assertNull( action.getDependencies() );
395 assertNull( action.getMailingLists() );
396 assertTrue( action.getArtifacts().isEmpty() );
399 private void assertArtifacts( List<ArtifactMetadata> expectedArtifacts,
400 Map<String, List<ShowArtifactAction.ArtifactDownloadInfo>> artifactMap )
402 // assuming only one of each version at this point
403 assertEquals( expectedArtifacts.size(), artifactMap.size() );
404 for ( ArtifactMetadata artifact : expectedArtifacts )
406 assertTrue( artifactMap.containsKey( artifact.getVersion() ) );
407 List<ShowArtifactAction.ArtifactDownloadInfo> list = artifactMap.get( artifact.getVersion() );
408 ShowArtifactAction.ArtifactDownloadInfo actual = list.get( 0 );
409 assertEquals( artifact.getNamespace(), actual.getNamespace() );
410 assertEquals( artifact.getId(), actual.getId() );
411 assertEquals( artifact.getProject(), actual.getProject() );
412 assertEquals( artifact.getRepositoryId(), actual.getRepositoryId() );
413 assertEquals( artifact.getVersion(), actual.getVersion() );
414 assertEquals( TEST_TYPE, actual.getType() );
415 assertEquals( "12.06 K", actual.getSize() );
416 assertEquals( artifact.getNamespace() + "/" + artifact.getProject() + "/" + TEST_SNAPSHOT_VERSION + "/" +
417 artifact.getId(), actual.getPath() );
421 private static ArtifactMetadata createArtifact( String version )
423 return createArtifact( version, null, 0 );
426 private static ArtifactMetadata createArtifact( String version, String timestamp, int buildNumber )
428 ArtifactMetadata metadata = new ArtifactMetadata();
429 metadata.setProject( TEST_ARTIFACT_ID );
430 metadata.setId( TEST_ARTIFACT_ID + "-" + version + ".jar" );
431 metadata.setNamespace( TEST_GROUP_ID );
432 metadata.setRepositoryId( TEST_REPO );
433 metadata.setSize( TEST_SIZE );
434 metadata.setProjectVersion( VersionUtil.getBaseVersion( version ) );
435 metadata.setVersion( version );
437 MavenArtifactFacet facet = new MavenArtifactFacet();
438 facet.setType( "jar" );
439 facet.setTimestamp( timestamp );
440 facet.setBuildNumber( buildNumber );
441 metadata.addFacet( facet );
446 private ProjectVersionReference createReference( String projectId )
448 ProjectVersionReference reference = new ProjectVersionReference();
449 reference.setNamespace( "groupId" );
450 reference.setProjectId( projectId );
451 reference.setProjectVersion( "version" );
452 reference.setReferenceType( ProjectVersionReference.ReferenceType.DEPENDENCY );
456 private void assertCoordinate( ProjectVersionReference dependee, String artifactId )
458 assertEquals( artifactId, dependee.getProjectId() );
459 assertEquals( "groupId", dependee.getNamespace() );
460 assertEquals( "version", dependee.getProjectVersion() );
463 private void assertDependencyBasic( Dependency dependency, String artifactId )
465 assertEquals( artifactId, dependency.getArtifactId() );
466 assertEquals( "groupId", dependency.getGroupId() );
467 assertEquals( "version", dependency.getVersion() );
470 private void assertDependencyExtended( Dependency dependency, String artifactId )
472 assertDependencyBasic( dependency, artifactId );
473 assertEquals( true, dependency.isOptional() );
474 assertEquals( "classifier", dependency.getClassifier() );
475 assertEquals( "type", dependency.getType() );
476 assertEquals( "scope", dependency.getScope() );
477 assertEquals( "systemPath", dependency.getSystemPath() );
480 private Dependency createDependencyExtended( String artifactId )
482 Dependency dependency = createDependencyBasic( artifactId );
483 dependency.setClassifier( "classifier" );
484 dependency.setOptional( true );
485 dependency.setScope( "scope" );
486 dependency.setSystemPath( "systemPath" );
487 dependency.setType( "type" );
491 private Dependency createDependencyBasic( String artifactId )
493 Dependency dependency = new Dependency();
494 dependency.setArtifactId( artifactId );
495 dependency.setGroupId( "groupId" );
496 dependency.setVersion( "version" );
500 private void assertMailingList( MailingList mailingList, String name, String prefix )
502 assertEquals( name, mailingList.getName() );
503 assertEquals( prefix + "-post@", mailingList.getPostAddress() );
504 assertEquals( prefix + "-subscribe@", mailingList.getSubscribeAddress() );
505 assertEquals( prefix + "-unsubscribe@", mailingList.getUnsubscribeAddress() );
506 assertEquals( prefix + "-archive-url", mailingList.getMainArchiveUrl() );
507 assertEquals( Arrays.asList( "other-" + prefix + "-archive-url-1", "other-" + prefix + "-archive-url-2" ),
508 mailingList.getOtherArchives() );
511 private MailingList createMailingList( String name, String prefix )
513 MailingList ml1 = new MailingList();
515 ml1.setPostAddress( prefix + "-post@" );
516 ml1.setSubscribeAddress( prefix + "-subscribe@" );
517 ml1.setUnsubscribeAddress( prefix + "-unsubscribe@" );
518 ml1.setMainArchiveUrl( prefix + "-archive-url" );
519 ml1.setOtherArchives(
520 Arrays.asList( "other-" + prefix + "-archive-url-1", "other-" + prefix + "-archive-url-2" ) );
524 private void assertNoOutputFields()
526 assertNull( action.getModel() );
527 assertNull( action.getDependees() );
528 assertNull( action.getDependencies() );
529 assertNull( action.getMailingLists() );
530 assertTrue( action.getArtifacts().isEmpty() );
533 private void assertError( String result )
535 assertEquals( Action.ERROR, result );
536 assertEquals( 1, action.getActionErrors().size() );
539 private void assertDefaultModel( ProjectVersionMetadata model )
541 assertDefaultModel( model, TEST_VERSION );
544 private void setActionParameters()
546 action.setGroupId( TEST_GROUP_ID );
547 action.setArtifactId( TEST_ARTIFACT_ID );
548 action.setVersion( TEST_VERSION );
551 private void assertActionParameters( ShowArtifactAction action )
553 assertEquals( TEST_GROUP_ID, action.getGroupId() );
554 assertEquals( TEST_ARTIFACT_ID, action.getArtifactId() );
555 assertEquals( TEST_VERSION, action.getVersion() );
558 private void assertActionSuccess( ShowArtifactAction action, String result )
560 assertEquals( Action.SUCCESS, result );
561 assertTrue( action.getActionErrors().isEmpty() );
562 assertTrue( action.getActionMessages().isEmpty() );
565 protected void setUp()
569 action = (ShowArtifactAction) lookup( Action.class, ACTION_HINT );
570 metadataResolver = (TestMetadataResolver) action.getMetadataResolver();
571 MockControl control = MockClassControl.createControl( RepositoryContentFactory.class );
572 RepositoryContentFactory factory = (RepositoryContentFactory) control.getMock();
573 action.setRepositoryFactory( factory );
575 ManagedRepositoryConfiguration config = new ManagedRepositoryConfiguration();
576 config.setId( TEST_REPO );
577 config.setLocation( getTestFile( "target/test-repo" ).getAbsolutePath() );
578 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
579 content.setRepository( config );
580 factory.getManagedRepositoryContent( TEST_REPO );
581 control.setDefaultReturnValue( content );