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 private void assertNoOutputFields()
313 assertNull( action.getModel() );
314 assertNull( action.getDependees() );
315 assertNull( action.getDependencies() );
316 assertNull( action.getMailingLists() );
317 assertTrue( action.getSnapshotVersions().isEmpty() );
320 private void assertError( String result )
322 assertEquals( Action.ERROR, result );
323 assertEquals( 1, action.getActionErrors().size() );
326 private void setObservableRepos( List<String> repoIds )
328 UserRepositoriesStub repos = (UserRepositoriesStub) lookup( UserRepositories.class );
329 repos.setObservableRepositoryIds( repoIds );
332 private void assertDefaultModel( ArchivaProjectModel model )
334 assertDefaultModel( model, TEST_VERSION );
337 private void assertDefaultModel( ArchivaProjectModel model, String version )
339 assertEquals( TEST_GROUP_ID, model.getGroupId() );
340 assertEquals( TEST_ARTIFACT_ID, model.getArtifactId() );
341 assertEquals( version, model.getVersion() );
342 assertEquals( TEST_URL, model.getUrl() );
343 assertEquals( TEST_NAME, model.getName() );
344 assertEquals( TEST_DESCRIPTION, model.getDescription() );
345 assertEquals( TEST_ORGANIZATION_NAME, model.getOrganization().getName() );
346 assertEquals( TEST_ORGANIZATION_URL, model.getOrganization().getUrl() );
347 assertEquals( 2, model.getLicenses().size() );
348 org.apache.maven.archiva.model.License l = model.getLicenses().get( 0 );
349 assertEquals( TEST_LICENSE_NAME, l.getName() );
350 assertEquals( TEST_LICENSE_URL, l.getUrl() );
351 l = model.getLicenses().get( 1 );
352 assertEquals( TEST_LICENSE_NAME_2, l.getName() );
353 assertEquals( TEST_LICENSE_URL_2, l.getUrl() );
354 assertEquals( TEST_ISSUE_SYSTEM, model.getIssueManagement().getSystem() );
355 assertEquals( TEST_ISSUE_URL, model.getIssueManagement().getUrl() );
356 assertEquals( TEST_CI_SYSTEM, model.getCiManagement().getSystem() );
357 assertEquals( TEST_CI_URL, model.getCiManagement().getUrl() );
358 assertEquals( TEST_SCM_CONNECTION, model.getScm().getConnection() );
359 assertEquals( TEST_SCM_DEV_CONNECTION, model.getScm().getDeveloperConnection() );
360 assertEquals( TEST_SCM_URL, model.getScm().getUrl() );
362 assertEquals( TEST_PACKAGING, model.getPackaging() );
363 assertEquals( TEST_PARENT_GROUP_ID, model.getParentProject().getGroupId() );
364 assertEquals( TEST_PARENT_ARTIFACT_ID, model.getParentProject().getArtifactId() );
365 assertEquals( TEST_PARENT_VERSION, model.getParentProject().getVersion() );
368 private void setActionParameters()
370 action.setGroupId( TEST_GROUP_ID );
371 action.setArtifactId( TEST_ARTIFACT_ID );
372 action.setVersion( TEST_VERSION );
375 private void assertActionParameters( ShowArtifactAction action )
377 assertEquals( TEST_GROUP_ID, action.getGroupId() );
378 assertEquals( TEST_ARTIFACT_ID, action.getArtifactId() );
379 assertEquals( TEST_VERSION, action.getVersion() );
382 private void assertActionSuccess( ShowArtifactAction action, String result )
384 assertEquals( Action.SUCCESS, result );
385 assertTrue( action.getActionErrors().isEmpty() );
386 assertTrue( action.getActionMessages().isEmpty() );
389 private ProjectVersionMetadata createProjectModel( String version )
391 ProjectVersionMetadata model = new ProjectVersionMetadata();
392 model.setId( version );
393 model.setUrl( TEST_URL );
394 model.setName( TEST_NAME );
395 model.setDescription( TEST_DESCRIPTION );
396 CiManagement ci = new CiManagement();
397 ci.setSystem( TEST_CI_SYSTEM );
398 ci.setUrl( TEST_CI_URL );
399 model.setCiManagement( ci );
400 IssueManagement issue = new IssueManagement();
401 issue.setSystem( TEST_ISSUE_SYSTEM );
402 issue.setUrl( TEST_ISSUE_URL );
403 model.setIssueManagement( issue );
404 Organization organization = new Organization();
405 organization.setName( TEST_ORGANIZATION_NAME );
406 organization.setUrl( TEST_ORGANIZATION_URL );
407 model.setOrganization( organization );
408 License l = new License();
409 l.setName( TEST_LICENSE_NAME );
410 l.setUrl( TEST_LICENSE_URL );
411 model.addLicense( l );
413 l.setName( TEST_LICENSE_NAME_2 );
414 l.setUrl( TEST_LICENSE_URL_2 );
415 model.addLicense( l );
417 scm.setConnection( TEST_SCM_CONNECTION );
418 scm.setDeveloperConnection( TEST_SCM_DEV_CONNECTION );
419 scm.setUrl( TEST_SCM_URL );
422 MavenProjectFacet mavenProjectFacet = new MavenProjectFacet();
423 mavenProjectFacet.setGroupId( TEST_GROUP_ID );
424 mavenProjectFacet.setArtifactId( TEST_ARTIFACT_ID );
425 mavenProjectFacet.setPackaging( TEST_PACKAGING );
426 MavenProjectParent parent = new MavenProjectParent();
427 parent.setGroupId( TEST_PARENT_GROUP_ID );
428 parent.setArtifactId( TEST_PARENT_ARTIFACT_ID );
429 parent.setVersion( TEST_PARENT_VERSION );
430 mavenProjectFacet.setParent( parent );
431 model.addFacet( mavenProjectFacet );
435 protected void setUp()
439 action = (ShowArtifactAction) lookup( Action.class, ACTION_HINT );
440 metadataResolver = (TestMetadataResolver) action.getMetadataResolver();