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.CiManagement;
28 import org.apache.archiva.metadata.model.IssueManagement;
29 import org.apache.archiva.metadata.model.License;
30 import org.apache.archiva.metadata.model.Organization;
31 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
32 import org.apache.archiva.metadata.model.Scm;
33 import org.apache.archiva.metadata.repository.memory.TestMetadataResolver;
34 import org.apache.archiva.metadata.repository.storage.maven2.MavenProjectFacet;
35 import org.apache.archiva.metadata.repository.storage.maven2.MavenProjectParent;
36 import org.apache.maven.archiva.model.ArchivaProjectModel;
37 import org.apache.maven.archiva.security.UserRepositories;
38 import org.apache.maven.archiva.security.UserRepositoriesStub;
39 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
41 public class ShowArtifactActionTest
42 extends PlexusInSpringTestCase
44 private static final String ACTION_HINT = "showArtifactAction";
46 private static final String TEST_GROUP_ID = "groupId";
48 private static final String TEST_ARTIFACT_ID = "artifactId";
50 private static final String TEST_VERSION = "version";
52 private static final String TEST_PACKAGING = "packaging";
54 private static final String TEST_ISSUE_URL = "http://jira.codehaus.org/browse/MRM";
56 private static final String TEST_ISSUE_SYSTEM = "jira";
58 private static final String TEST_CI_SYSTEM = "continuum";
60 private static final String TEST_CI_URL = "http://vmbuild.apache.org/";
62 private static final String TEST_URL = "url";
64 private static final String TEST_NAME = "name";
66 private static final String TEST_DESCRIPTION = "description";
68 private static final String TEST_PARENT_GROUP_ID = "parentGroupId";
70 private static final String TEST_PARENT_ARTIFACT_ID = "parentArtifactId";
72 private static final String TEST_PARENT_VERSION = "parentVersion";
74 private static final String TEST_ORGANIZATION_NAME = "organizationName";
76 private static final String TEST_ORGANIZATION_URL = "organizationUrl";
78 private static final String TEST_LICENSE_URL = "licenseUrl";
80 private static final String TEST_LICENSE_NAME = "licenseName";
82 private static final String TEST_LICENSE_URL_2 = "licenseUrl_2";
84 private static final String TEST_LICENSE_NAME_2 = "licenseName_2";
86 private static final String TEST_REPO = "test-repo";
88 private static final String TEST_SCM_CONNECTION = "scmConnection";
90 private static final String TEST_SCM_DEV_CONNECTION = "scmDevConnection";
92 private static final String TEST_SCM_URL = "scmUrl";
94 private static final String TEST_SNAPSHOT_VERSION = "1.0-SNAPSHOT";
96 private static final String TEST_TS_SNAPSHOT_VERSION = "1.0-20091120.111111-1";
98 private static final List<String> ALL_TEST_SNAPSHOT_VERSIONS =
99 Arrays.asList( TEST_TS_SNAPSHOT_VERSION, "1.0-20091120.222222-2", "1.0-20091123.333333-3" );
101 private static final String OTHER_TEST_REPO = "first-repo";
103 private ShowArtifactAction action;
105 private TestMetadataResolver metadataResolver;
107 public void testInstantiation()
109 assertFalse( action == lookup( Action.class, ACTION_HINT ) );
112 public void testGetArtifactUniqueRelease()
114 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
115 createProjectModel( TEST_VERSION ) );
117 setActionParameters();
119 String result = action.artifact();
121 assertActionSuccess( action, result );
123 assertActionParameters( action );
124 ArchivaProjectModel model = action.getModel();
125 assertDefaultModel( model );
127 assertEquals( TEST_REPO, action.getRepositoryId() );
129 assertNull( action.getDependees() );
130 assertNull( action.getDependencies() );
131 assertNull( action.getMailingLists() );
132 assertTrue( action.getSnapshotVersions().isEmpty() );
135 public void testGetArtifactUniqueSnapshot()
137 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
138 createProjectModel( TEST_SNAPSHOT_VERSION ) );
139 metadataResolver.setArtifactVersions( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_SNAPSHOT_VERSION,
140 ALL_TEST_SNAPSHOT_VERSIONS );
142 action.setGroupId( TEST_GROUP_ID );
143 action.setArtifactId( TEST_ARTIFACT_ID );
144 action.setVersion( TEST_SNAPSHOT_VERSION );
146 String result = action.artifact();
148 assertActionSuccess( action, result );
150 assertEquals( TEST_GROUP_ID, action.getGroupId() );
151 assertEquals( TEST_ARTIFACT_ID, action.getArtifactId() );
152 assertEquals( TEST_SNAPSHOT_VERSION, action.getVersion() );
153 ArchivaProjectModel model = action.getModel();
154 assertDefaultModel( model, TEST_SNAPSHOT_VERSION );
156 assertEquals( TEST_REPO, action.getRepositoryId() );
158 assertEquals( ALL_TEST_SNAPSHOT_VERSIONS, action.getSnapshotVersions() );
160 assertNull( action.getDependees() );
161 assertNull( action.getDependencies() );
162 assertNull( action.getMailingLists() );
165 public void testGetArtifactUniqueSnapshotTimestamped()
167 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
168 createProjectModel( TEST_TS_SNAPSHOT_VERSION ) );
169 metadataResolver.setArtifactVersions( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_TS_SNAPSHOT_VERSION,
170 ALL_TEST_SNAPSHOT_VERSIONS );
172 action.setGroupId( TEST_GROUP_ID );
173 action.setArtifactId( TEST_ARTIFACT_ID );
174 action.setVersion( TEST_TS_SNAPSHOT_VERSION );
176 String result = action.artifact();
178 assertActionSuccess( action, result );
180 assertEquals( TEST_GROUP_ID, action.getGroupId() );
181 assertEquals( TEST_ARTIFACT_ID, action.getArtifactId() );
182 assertEquals( TEST_TS_SNAPSHOT_VERSION, action.getVersion() );
183 ArchivaProjectModel model = action.getModel();
184 assertDefaultModel( model, TEST_TS_SNAPSHOT_VERSION );
186 assertEquals( TEST_REPO, action.getRepositoryId() );
188 assertEquals( Arrays.asList( ALL_TEST_SNAPSHOT_VERSIONS.get( 1 ), ALL_TEST_SNAPSHOT_VERSIONS.get( 2 ) ),
189 action.getSnapshotVersions() );
191 assertNull( action.getDependees() );
192 assertNull( action.getDependencies() );
193 assertNull( action.getMailingLists() );
196 public void testGetMissingProject()
198 setActionParameters();
200 String result = action.artifact();
201 assertError( result );
203 assertActionParameters( action );
204 assertNoOutputFields();
207 public void testGetArtifactNoObservableRepos()
209 setObservableRepos( Collections.<String>emptyList() );
211 setActionParameters();
213 String result = action.artifact();
215 // Actually, it'd be better to have an error:
216 assertError( result );
217 assertActionParameters( action );
218 assertNoOutputFields();
221 public void testGetArtifactNotInObservableRepos()
223 metadataResolver.setProjectVersion( OTHER_TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
224 createProjectModel( TEST_VERSION ) );
226 setActionParameters();
228 String result = action.artifact();
229 assertError( result );
231 assertActionParameters( action );
232 assertNoOutputFields();
235 public void testGetArtifactOnlySeenInSecondObservableRepo()
237 setObservableRepos( Arrays.asList( OTHER_TEST_REPO, TEST_REPO ) );
238 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
239 createProjectModel( TEST_VERSION ) );
241 setActionParameters();
243 String result = action.artifact();
245 assertActionSuccess( action, result );
247 assertActionParameters( action );
248 ArchivaProjectModel model = action.getModel();
249 assertDefaultModel( model );
251 assertEquals( TEST_REPO, action.getRepositoryId() );
253 assertNull( action.getDependees() );
254 assertNull( action.getDependencies() );
255 assertNull( action.getMailingLists() );
256 assertTrue( action.getSnapshotVersions().isEmpty() );
259 public void testGetArtifactSeenInBothObservableRepo()
261 setObservableRepos( Arrays.asList( TEST_REPO, OTHER_TEST_REPO ) );
262 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
263 createProjectModel( TEST_VERSION ) );
264 metadataResolver.setProjectVersion( OTHER_TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
265 createProjectModel( TEST_VERSION ) );
267 setActionParameters();
269 String result = action.artifact();
271 assertActionSuccess( action, result );
273 assertActionParameters( action );
274 ArchivaProjectModel model = action.getModel();
275 assertDefaultModel( model );
277 assertEquals( TEST_REPO, action.getRepositoryId() );
279 assertNull( action.getDependees() );
280 assertNull( action.getDependencies() );
281 assertNull( action.getMailingLists() );
282 assertTrue( action.getSnapshotVersions().isEmpty() );
285 public void testGetArtifactCanOnlyObserveInOneOfTwoRepos()
287 setObservableRepos( Arrays.asList( TEST_REPO ) );
288 metadataResolver.setProjectVersion( OTHER_TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
289 createProjectModel( TEST_VERSION ) );
290 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID,
291 createProjectModel( TEST_VERSION ) );
293 setActionParameters();
295 String result = action.artifact();
297 assertActionSuccess( action, result );
299 assertActionParameters( action );
300 ArchivaProjectModel model = action.getModel();
301 assertDefaultModel( model );
303 assertEquals( TEST_REPO, action.getRepositoryId() );
305 assertNull( action.getDependees() );
306 assertNull( action.getDependencies() );
307 assertNull( action.getMailingLists() );
308 assertTrue( action.getSnapshotVersions().isEmpty() );
311 public void testGetArtifactNoMavenFacet()
313 ProjectVersionMetadata versionMetadata = new ProjectVersionMetadata();
314 versionMetadata.setId( TEST_VERSION );
315 versionMetadata.setUrl( TEST_URL );
316 versionMetadata.setName( TEST_NAME );
317 versionMetadata.setDescription( TEST_DESCRIPTION );
319 metadataResolver.setProjectVersion( TEST_REPO, TEST_GROUP_ID, TEST_ARTIFACT_ID, versionMetadata );
321 setActionParameters();
323 String result = action.artifact();
325 assertActionSuccess( action, result );
327 assertActionParameters( action );
328 ArchivaProjectModel model = action.getModel();
329 assertEquals( TEST_VERSION, model.getVersion() );
330 assertEquals( TEST_URL, model.getUrl() );
331 assertEquals( TEST_NAME, model.getName() );
332 assertEquals( TEST_DESCRIPTION, model.getDescription() );
334 assertEquals( TEST_REPO, action.getRepositoryId() );
336 assertNull( action.getDependees() );
337 assertNull( action.getDependencies() );
338 assertNull( action.getMailingLists() );
339 assertTrue( action.getSnapshotVersions().isEmpty() );
342 private void assertNoOutputFields()
344 assertNull( action.getModel() );
345 assertNull( action.getDependees() );
346 assertNull( action.getDependencies() );
347 assertNull( action.getMailingLists() );
348 assertTrue( action.getSnapshotVersions().isEmpty() );
351 private void assertError( String result )
353 assertEquals( Action.ERROR, result );
354 assertEquals( 1, action.getActionErrors().size() );
357 private void setObservableRepos( List<String> repoIds )
359 UserRepositoriesStub repos = (UserRepositoriesStub) lookup( UserRepositories.class );
360 repos.setObservableRepositoryIds( repoIds );
363 private void assertDefaultModel( ArchivaProjectModel model )
365 assertDefaultModel( model, TEST_VERSION );
368 private void assertDefaultModel( ArchivaProjectModel model, String version )
370 assertEquals( TEST_GROUP_ID, model.getGroupId() );
371 assertEquals( TEST_ARTIFACT_ID, model.getArtifactId() );
372 assertEquals( version, model.getVersion() );
373 assertEquals( TEST_URL, model.getUrl() );
374 assertEquals( TEST_NAME, model.getName() );
375 assertEquals( TEST_DESCRIPTION, model.getDescription() );
376 assertEquals( TEST_ORGANIZATION_NAME, model.getOrganization().getName() );
377 assertEquals( TEST_ORGANIZATION_URL, model.getOrganization().getUrl() );
378 assertEquals( 2, model.getLicenses().size() );
379 org.apache.maven.archiva.model.License l = model.getLicenses().get( 0 );
380 assertEquals( TEST_LICENSE_NAME, l.getName() );
381 assertEquals( TEST_LICENSE_URL, l.getUrl() );
382 l = model.getLicenses().get( 1 );
383 assertEquals( TEST_LICENSE_NAME_2, l.getName() );
384 assertEquals( TEST_LICENSE_URL_2, l.getUrl() );
385 assertEquals( TEST_ISSUE_SYSTEM, model.getIssueManagement().getSystem() );
386 assertEquals( TEST_ISSUE_URL, model.getIssueManagement().getUrl() );
387 assertEquals( TEST_CI_SYSTEM, model.getCiManagement().getSystem() );
388 assertEquals( TEST_CI_URL, model.getCiManagement().getUrl() );
389 assertEquals( TEST_SCM_CONNECTION, model.getScm().getConnection() );
390 assertEquals( TEST_SCM_DEV_CONNECTION, model.getScm().getDeveloperConnection() );
391 assertEquals( TEST_SCM_URL, model.getScm().getUrl() );
393 assertEquals( TEST_PACKAGING, model.getPackaging() );
394 assertEquals( TEST_PARENT_GROUP_ID, model.getParentProject().getGroupId() );
395 assertEquals( TEST_PARENT_ARTIFACT_ID, model.getParentProject().getArtifactId() );
396 assertEquals( TEST_PARENT_VERSION, model.getParentProject().getVersion() );
399 private void setActionParameters()
401 action.setGroupId( TEST_GROUP_ID );
402 action.setArtifactId( TEST_ARTIFACT_ID );
403 action.setVersion( TEST_VERSION );
406 private void assertActionParameters( ShowArtifactAction action )
408 assertEquals( TEST_GROUP_ID, action.getGroupId() );
409 assertEquals( TEST_ARTIFACT_ID, action.getArtifactId() );
410 assertEquals( TEST_VERSION, action.getVersion() );
413 private void assertActionSuccess( ShowArtifactAction action, String result )
415 assertEquals( Action.SUCCESS, result );
416 assertTrue( action.getActionErrors().isEmpty() );
417 assertTrue( action.getActionMessages().isEmpty() );
420 private ProjectVersionMetadata createProjectModel( String version )
422 ProjectVersionMetadata model = new ProjectVersionMetadata();
423 model.setId( version );
424 model.setUrl( TEST_URL );
425 model.setName( TEST_NAME );
426 model.setDescription( TEST_DESCRIPTION );
427 CiManagement ci = new CiManagement();
428 ci.setSystem( TEST_CI_SYSTEM );
429 ci.setUrl( TEST_CI_URL );
430 model.setCiManagement( ci );
431 IssueManagement issue = new IssueManagement();
432 issue.setSystem( TEST_ISSUE_SYSTEM );
433 issue.setUrl( TEST_ISSUE_URL );
434 model.setIssueManagement( issue );
435 Organization organization = new Organization();
436 organization.setName( TEST_ORGANIZATION_NAME );
437 organization.setUrl( TEST_ORGANIZATION_URL );
438 model.setOrganization( organization );
439 License l = new License();
440 l.setName( TEST_LICENSE_NAME );
441 l.setUrl( TEST_LICENSE_URL );
442 model.addLicense( l );
444 l.setName( TEST_LICENSE_NAME_2 );
445 l.setUrl( TEST_LICENSE_URL_2 );
446 model.addLicense( l );
448 scm.setConnection( TEST_SCM_CONNECTION );
449 scm.setDeveloperConnection( TEST_SCM_DEV_CONNECTION );
450 scm.setUrl( TEST_SCM_URL );
453 MavenProjectFacet mavenProjectFacet = new MavenProjectFacet();
454 mavenProjectFacet.setGroupId( TEST_GROUP_ID );
455 mavenProjectFacet.setArtifactId( TEST_ARTIFACT_ID );
456 mavenProjectFacet.setPackaging( TEST_PACKAGING );
457 MavenProjectParent parent = new MavenProjectParent();
458 parent.setGroupId( TEST_PARENT_GROUP_ID );
459 parent.setArtifactId( TEST_PARENT_ARTIFACT_ID );
460 parent.setVersion( TEST_PARENT_VERSION );
461 mavenProjectFacet.setParent( parent );
462 model.addFacet( mavenProjectFacet );
466 protected void setUp()
470 action = (ShowArtifactAction) lookup( Action.class, ACTION_HINT );
471 metadataResolver = (TestMetadataResolver) action.getMetadataResolver();