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.maven.archiva.database.ArchivaDAO;
28 import org.apache.maven.archiva.database.ArchivaDatabaseException;
29 import org.apache.maven.archiva.database.ArtifactDAO;
30 import org.apache.maven.archiva.database.ProjectModelDAO;
31 import org.apache.maven.archiva.database.constraints.ArtifactsRelatedConstraint;
32 import org.apache.maven.archiva.model.ArchivaArtifact;
33 import org.apache.maven.archiva.model.ArchivaArtifactModel;
34 import org.apache.maven.archiva.model.ArchivaProjectModel;
35 import org.apache.maven.archiva.model.CiManagement;
36 import org.apache.maven.archiva.model.IssueManagement;
37 import org.apache.maven.archiva.model.License;
38 import org.apache.maven.archiva.model.Organization;
39 import org.apache.maven.archiva.model.Scm;
40 import org.apache.maven.archiva.model.VersionedReference;
41 import org.apache.maven.archiva.security.UserRepositories;
42 import org.apache.maven.archiva.security.UserRepositoriesStub;
43 import org.apache.maven.archiva.web.action.admin.repositories.ArchivaDAOStub;
44 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
45 import org.easymock.MockControl;
47 public class ShowArtifactActionTest
48 extends PlexusInSpringTestCase
50 private static final String ACTION_HINT = "showArtifactAction";
52 private static final String TEST_GROUP_ID = "groupId";
54 private static final String TEST_ARTIFACT_ID = "artifactId";
56 private static final String TEST_VERSION = "version";
58 private static final String TEST_PACKAGING = "packaging";
60 private static final String TEST_ISSUE_URL = "http://jira.codehaus.org/browse/MRM";
62 private static final String TEST_ISSUE_SYSTEM = "jira";
64 private static final String TEST_CI_SYSTEM = "continuum";
66 private static final String TEST_CI_URL = "http://vmbuild.apache.org/";
68 private static final String TEST_URL = "url";
70 private static final String TEST_NAME = "name";
72 private static final String TEST_DESCRIPTION = "description";
74 private static final String TEST_PARENT_GROUP_ID = "parentGroupId";
76 private static final String TEST_PARENT_ARTIFACT_ID = "parentArtifactId";
78 private static final String TEST_PARENT_VERSION = "parentVersion";
80 private static final String TEST_ORGANIZATION_NAME = "organizationName";
82 private static final String TEST_ORGANIZATION_URL = "organizationUrl";
84 private static final String TEST_LICENSE_URL = "licenseUrl";
86 private static final String TEST_LICENSE_NAME = "licenseName";
88 private static final String TEST_LICENSE_URL_2 = "licenseUrl_2";
90 private static final String TEST_LICENSE_NAME_2 = "licenseName_2";
92 private static final String TEST_REPO = "test-repo";
94 private static final String TEST_SCM_CONNECTION = "scmConnection";
96 private static final String TEST_SCM_DEV_CONNECTION = "scmDevConnection";
98 private static final String TEST_SCM_URL = "scmUrl";
100 private ShowArtifactAction action;
102 private static final String TEST_SNAPSHOT_VERSION = "1.0-SNAPSHOT";
104 private static final String TEST_TS_SNAPSHOT_VERSION = "1.0-20091120.111111-1";
106 private ArchivaDAOStub archivaDao;
108 private static final List<String> ALL_TEST_SNAPSHOT_VERSIONS =
109 Arrays.asList( TEST_TS_SNAPSHOT_VERSION, "1.0-20091120.222222-2", "1.0-20091123.333333-3" );
111 private static final String OTHER_TEST_REPO = "first-repo";
113 public void testInstantiation()
115 assertFalse( action == lookup( Action.class, ACTION_HINT ) );
118 public void testGetArtifactUniqueRelease()
119 throws ArchivaDatabaseException
121 List<ArchivaArtifact> artifacts =
122 Collections.singletonList( createArtifact( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION ) );
123 MockControl artifactDaoMockControl = createArtifactDaoMock( artifacts, 2 );
124 MockControl projectDaoMockControl =
125 createProjectDaoMock( createProjectModel( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION ) );
127 setActionParameters();
129 String result = action.artifact();
131 assertActionSuccess( action, result );
133 artifactDaoMockControl.verify();
134 projectDaoMockControl.verify();
136 assertActionParameters( action );
137 ArchivaProjectModel model = action.getModel();
138 assertDefaultModel( model );
140 assertEquals( TEST_REPO, action.getRepositoryId() );
142 assertNull( action.getDependees() );
143 assertNull( action.getDependencies() );
144 assertNull( action.getMailingLists() );
145 assertNull( action.getSnapshotVersions() );
148 public void testGetArtifactUniqueSnapshot()
149 throws ArchivaDatabaseException
151 List<ArchivaArtifact> artifacts =
152 Collections.singletonList( createArtifact( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_SNAPSHOT_VERSION ) );
153 MockControl artifactDaoMockControl = createArtifactDaoMock( artifacts, TEST_SNAPSHOT_VERSION, 2 );
154 MockControl projectDaoMockControl =
155 createProjectDaoMock( createProjectModel( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_SNAPSHOT_VERSION ) );
156 archivaDao.setVersions( ALL_TEST_SNAPSHOT_VERSIONS );
158 action.setGroupId( TEST_GROUP_ID );
159 action.setArtifactId( TEST_ARTIFACT_ID );
160 action.setVersion( TEST_SNAPSHOT_VERSION );
162 String result = action.artifact();
164 assertActionSuccess( action, result );
166 artifactDaoMockControl.verify();
167 projectDaoMockControl.verify();
169 assertEquals( TEST_GROUP_ID, action.getGroupId() );
170 assertEquals( TEST_ARTIFACT_ID, action.getArtifactId() );
171 assertEquals( TEST_SNAPSHOT_VERSION, action.getVersion() );
172 ArchivaProjectModel model = action.getModel();
173 assertDefaultModel( model, TEST_SNAPSHOT_VERSION );
175 assertEquals( TEST_REPO, action.getRepositoryId() );
177 assertEquals( ALL_TEST_SNAPSHOT_VERSIONS, action.getSnapshotVersions() );
179 assertNull( action.getDependees() );
180 assertNull( action.getDependencies() );
181 assertNull( action.getMailingLists() );
184 public void testGetArtifactUniqueSnapshotTimestamped()
185 throws ArchivaDatabaseException
187 List<ArchivaArtifact> artifacts =
188 Collections.singletonList( createArtifact( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_TS_SNAPSHOT_VERSION ) );
189 MockControl artifactDaoMockControl = createArtifactDaoMock( artifacts, TEST_TS_SNAPSHOT_VERSION, 2 );
190 MockControl projectDaoMockControl =
191 createProjectDaoMock( createProjectModel( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_TS_SNAPSHOT_VERSION ) );
192 archivaDao.setVersions( ALL_TEST_SNAPSHOT_VERSIONS );
194 action.setGroupId( TEST_GROUP_ID );
195 action.setArtifactId( TEST_ARTIFACT_ID );
196 action.setVersion( TEST_TS_SNAPSHOT_VERSION );
198 String result = action.artifact();
200 assertActionSuccess( action, result );
202 artifactDaoMockControl.verify();
203 projectDaoMockControl.verify();
205 assertEquals( TEST_GROUP_ID, action.getGroupId() );
206 assertEquals( TEST_ARTIFACT_ID, action.getArtifactId() );
207 assertEquals( TEST_TS_SNAPSHOT_VERSION, action.getVersion() );
208 ArchivaProjectModel model = action.getModel();
209 assertDefaultModel( model, TEST_TS_SNAPSHOT_VERSION );
211 assertEquals( TEST_REPO, action.getRepositoryId() );
213 assertEquals( Arrays.asList( ALL_TEST_SNAPSHOT_VERSIONS.get( 1 ), ALL_TEST_SNAPSHOT_VERSIONS.get( 2 ) ),
214 action.getSnapshotVersions() );
216 assertNull( action.getDependees() );
217 assertNull( action.getDependencies() );
218 assertNull( action.getMailingLists() );
221 public void testGetMissingProject()
222 throws ArchivaDatabaseException
224 MockControl artifactDaoMockControl = createArtifactDaoMock( Collections.<ArchivaArtifact>emptyList(), 1 );
226 setActionParameters();
228 String result = action.artifact();
229 assertError( result );
231 artifactDaoMockControl.verify();
233 assertActionParameters( action );
234 assertNoOutputFields();
237 public void testGetArtifactNoObservableRepos()
238 throws ArchivaDatabaseException
240 setObservableRepos( Collections.<String>emptyList() );
242 setActionParameters();
248 // Actually, it'd be better to have an error:
249 // assertError( result );
250 // assertActionParameters( action );
251 // assertNoOutputFields();
254 catch ( ArchivaDatabaseException e )
260 public void testGetArtifactNotInObservableRepos()
261 throws ArchivaDatabaseException
263 List<ArchivaArtifact> artifacts = Collections.singletonList(
264 createArtifact( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION, OTHER_TEST_REPO ) );
265 MockControl artifactDaoMockControl = createArtifactDaoMock( artifacts, 1 );
267 setActionParameters();
269 String result = action.artifact();
270 assertError( result );
272 artifactDaoMockControl.verify();
274 assertActionParameters( action );
275 assertNoOutputFields();
278 public void testGetArtifactOnlySeenInSecondObservableRepo()
279 throws ArchivaDatabaseException
281 setObservableRepos( Arrays.asList( OTHER_TEST_REPO, TEST_REPO ) );
282 List<ArchivaArtifact> artifacts =
283 Collections.singletonList( createArtifact( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION ) );
284 MockControl artifactDaoMockControl = createArtifactDaoMock( artifacts, 2 );
285 MockControl projectDaoMockControl =
286 createProjectDaoMock( createProjectModel( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION ) );
288 setActionParameters();
290 String result = action.artifact();
292 assertActionSuccess( action, result );
294 artifactDaoMockControl.verify();
295 projectDaoMockControl.verify();
297 assertActionParameters( action );
298 ArchivaProjectModel model = action.getModel();
299 assertDefaultModel( model );
301 assertEquals( TEST_REPO, action.getRepositoryId() );
303 assertNull( action.getDependees() );
304 assertNull( action.getDependencies() );
305 assertNull( action.getMailingLists() );
306 assertNull( action.getSnapshotVersions() );
309 public void testGetArtifactSeenInBothObservableRepo()
310 throws ArchivaDatabaseException
312 setObservableRepos( Arrays.asList( OTHER_TEST_REPO, TEST_REPO ) );
313 List<ArchivaArtifact> artifacts =
314 Arrays.asList( createArtifact( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION ),
315 createArtifact( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION, OTHER_TEST_REPO ) );
316 MockControl artifactDaoMockControl = createArtifactDaoMock( artifacts, 2 );
317 MockControl projectDaoMockControl =
318 createProjectDaoMock( createProjectModel( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION ) );
320 setActionParameters();
322 String result = action.artifact();
324 assertActionSuccess( action, result );
326 artifactDaoMockControl.verify();
327 projectDaoMockControl.verify();
329 assertActionParameters( action );
330 ArchivaProjectModel model = action.getModel();
331 assertDefaultModel( model );
333 assertEquals( TEST_REPO, action.getRepositoryId() );
335 assertNull( action.getDependees() );
336 assertNull( action.getDependencies() );
337 assertNull( action.getMailingLists() );
338 assertNull( action.getSnapshotVersions() );
341 public void testGetArtifactCanOnlyObserveInOneOfTwoRepos()
342 throws ArchivaDatabaseException
344 setObservableRepos( Arrays.asList( TEST_REPO ) );
345 List<ArchivaArtifact> artifacts =
346 Arrays.asList( createArtifact( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION, OTHER_TEST_REPO ),
347 createArtifact( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION ) );
348 MockControl artifactDaoMockControl = createArtifactDaoMock( artifacts, 2 );
349 MockControl projectDaoMockControl =
350 createProjectDaoMock( createProjectModel( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION ) );
352 setActionParameters();
354 String result = action.artifact();
356 assertActionSuccess( action, result );
358 artifactDaoMockControl.verify();
359 projectDaoMockControl.verify();
361 assertActionParameters( action );
362 ArchivaProjectModel model = action.getModel();
363 assertDefaultModel( model );
365 assertEquals( TEST_REPO, action.getRepositoryId() );
367 assertNull( action.getDependees() );
368 assertNull( action.getDependencies() );
369 assertNull( action.getMailingLists() );
370 assertNull( action.getSnapshotVersions() );
373 private void assertNoOutputFields()
375 assertNull( action.getModel() );
376 assertNull( action.getDependees() );
377 assertNull( action.getDependencies() );
378 assertNull( action.getMailingLists() );
379 assertNull( action.getSnapshotVersions() );
382 private void assertError( String result )
384 assertEquals( Action.ERROR, result );
385 assertEquals( 1, action.getActionErrors().size() );
388 private void setObservableRepos( List<String> repoIds )
390 UserRepositoriesStub repos = (UserRepositoriesStub) lookup( UserRepositories.class );
391 repos.setObservableRepositoryIds( repoIds );
394 private void assertDefaultModel( ArchivaProjectModel model )
396 assertDefaultModel( model, TEST_VERSION );
399 private void assertDefaultModel( ArchivaProjectModel model, String version )
401 assertEquals( TEST_GROUP_ID, model.getGroupId() );
402 assertEquals( TEST_ARTIFACT_ID, model.getArtifactId() );
403 assertEquals( version, model.getVersion() );
404 assertEquals( TEST_PACKAGING, model.getPackaging() );
405 assertEquals( TEST_URL, model.getUrl() );
406 assertEquals( TEST_NAME, model.getName() );
407 assertEquals( TEST_DESCRIPTION, model.getDescription() );
408 assertEquals( TEST_ORGANIZATION_NAME, model.getOrganization().getName() );
409 assertEquals( TEST_ORGANIZATION_URL, model.getOrganization().getUrl() );
410 assertEquals( 2, model.getLicenses().size() );
411 License l = model.getLicenses().get( 0 );
412 assertEquals( TEST_LICENSE_NAME, l.getName() );
413 assertEquals( TEST_LICENSE_URL, l.getUrl() );
414 l = model.getLicenses().get( 1 );
415 assertEquals( TEST_LICENSE_NAME_2, l.getName() );
416 assertEquals( TEST_LICENSE_URL_2, l.getUrl() );
417 assertEquals( TEST_PARENT_GROUP_ID, model.getParentProject().getGroupId() );
418 assertEquals( TEST_PARENT_ARTIFACT_ID, model.getParentProject().getArtifactId() );
419 assertEquals( TEST_PARENT_VERSION, model.getParentProject().getVersion() );
420 assertEquals( TEST_ISSUE_SYSTEM, model.getIssueManagement().getSystem() );
421 assertEquals( TEST_ISSUE_URL, model.getIssueManagement().getUrl() );
422 assertEquals( TEST_CI_SYSTEM, model.getCiManagement().getSystem() );
423 assertEquals( TEST_CI_URL, model.getCiManagement().getUrl() );
424 assertEquals( TEST_SCM_CONNECTION, model.getScm().getConnection() );
425 assertEquals( TEST_SCM_DEV_CONNECTION, model.getScm().getDeveloperConnection() );
426 assertEquals( TEST_SCM_URL, model.getScm().getUrl() );
429 private void setActionParameters()
431 action.setGroupId( TEST_GROUP_ID );
432 action.setArtifactId( TEST_ARTIFACT_ID );
433 action.setVersion( TEST_VERSION );
436 private void assertActionParameters( ShowArtifactAction action )
438 assertEquals( TEST_GROUP_ID, action.getGroupId() );
439 assertEquals( TEST_ARTIFACT_ID, action.getArtifactId() );
440 assertEquals( TEST_VERSION, action.getVersion() );
443 private void assertActionSuccess( ShowArtifactAction action, String result )
445 assertEquals( Action.SUCCESS, result );
446 assertTrue( action.getActionErrors().isEmpty() );
447 assertTrue( action.getActionMessages().isEmpty() );
450 private ArchivaProjectModel createProjectModel( String groupId, String artifactId, String version )
452 ArchivaProjectModel model = new ArchivaProjectModel();
453 model.setGroupId( groupId );
454 model.setArtifactId( artifactId );
455 model.setVersion( version );
456 model.setPackaging( TEST_PACKAGING );
457 model.setUrl( TEST_URL );
458 model.setName( TEST_NAME );
459 model.setDescription( TEST_DESCRIPTION );
460 VersionedReference parent = new VersionedReference();
461 parent.setGroupId( TEST_PARENT_GROUP_ID );
462 parent.setArtifactId( TEST_PARENT_ARTIFACT_ID );
463 parent.setVersion( TEST_PARENT_VERSION );
464 model.setParentProject( parent );
465 CiManagement ci = new CiManagement();
466 ci.setSystem( TEST_CI_SYSTEM );
467 ci.setUrl( TEST_CI_URL );
468 model.setCiManagement( ci );
469 IssueManagement issue = new IssueManagement();
470 issue.setSystem( TEST_ISSUE_SYSTEM );
471 issue.setUrl( TEST_ISSUE_URL );
472 model.setIssueManagement( issue );
473 Organization org = new Organization();
474 org.setName( TEST_ORGANIZATION_NAME );
475 org.setUrl( TEST_ORGANIZATION_URL );
476 model.setOrganization( org );
477 License l = new License();
478 l.setName( TEST_LICENSE_NAME );
479 l.setUrl( TEST_LICENSE_URL );
480 model.addLicense( l );
482 l.setName( TEST_LICENSE_NAME_2 );
483 l.setUrl( TEST_LICENSE_URL_2 );
484 model.addLicense( l );
486 scm.setConnection( TEST_SCM_CONNECTION );
487 scm.setDeveloperConnection( TEST_SCM_DEV_CONNECTION );
488 scm.setUrl( TEST_SCM_URL );
493 private MockControl createArtifactDaoMock( List<ArchivaArtifact> artifacts, int count )
494 throws ArchivaDatabaseException
496 return createArtifactDaoMock( artifacts, TEST_VERSION, count );
499 private MockControl createArtifactDaoMock( List<ArchivaArtifact> artifacts, String version, int count )
500 throws ArchivaDatabaseException
502 // testing deeper than normal with the mocks as we intend to replace RepositoryBrowsing, not just the database
503 // underneath it - those sections will be adjusted with a mock content repository later
504 MockControl control = MockControl.createNiceControl( ArtifactDAO.class );
505 ArtifactDAO dao = (ArtifactDAO) control.getMock();
506 archivaDao.setArtifactDao( dao );
508 ArtifactsRelatedConstraint c = new ArtifactsRelatedConstraint( TEST_GROUP_ID, TEST_ARTIFACT_ID, version );
509 dao.queryArtifacts( c );
510 control.setReturnValue( artifacts, count );
516 private MockControl createProjectDaoMock( ArchivaProjectModel project )
517 throws ArchivaDatabaseException
519 MockControl control = MockControl.createNiceControl( ProjectModelDAO.class );
520 ProjectModelDAO dao = (ProjectModelDAO) control.getMock();
521 archivaDao.setProjectDao( dao );
523 control.expectAndReturn(
524 dao.getProjectModel( project.getGroupId(), project.getArtifactId(), project.getVersion() ), project );
530 private ArchivaArtifact createArtifact( String groupId, String artifactId, String version )
532 return createArtifact( groupId, artifactId, version, TEST_REPO );
535 private ArchivaArtifact createArtifact( String groupId, String artifactId, String version, String repoId )
537 ArchivaArtifactModel model = new ArchivaArtifactModel();
538 model.setGroupId( groupId );
539 model.setArtifactId( artifactId );
540 model.setVersion( version );
541 model.setRepositoryId( repoId );
542 return new ArchivaArtifact( model );
545 protected void setUp()
549 action = (ShowArtifactAction) lookup( Action.class, ACTION_HINT );
550 archivaDao = (ArchivaDAOStub) lookup( ArchivaDAO.class, "jdo" );