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.ProjectVersionMetadata;
28 import org.apache.archiva.metadata.repository.memory.TestMetadataResolver;
29 import org.apache.maven.archiva.database.ArchivaDAO;
30 import org.apache.maven.archiva.database.ArchivaDatabaseException;
31 import org.apache.maven.archiva.database.ArtifactDAO;
32 import org.apache.maven.archiva.database.ProjectModelDAO;
33 import org.apache.maven.archiva.database.browsing.BrowsingResults;
34 import org.apache.maven.archiva.database.constraints.ArtifactsRelatedConstraint;
35 import org.apache.maven.archiva.model.ArchivaArtifact;
36 import org.apache.maven.archiva.model.ArchivaArtifactModel;
37 import org.apache.maven.archiva.model.ArchivaProjectModel;
38 import org.apache.maven.archiva.model.CiManagement;
39 import org.apache.maven.archiva.model.IssueManagement;
40 import org.apache.maven.archiva.model.License;
41 import org.apache.maven.archiva.model.Organization;
42 import org.apache.maven.archiva.model.Scm;
43 import org.apache.maven.archiva.model.VersionedReference;
44 import org.apache.maven.archiva.web.action.admin.repositories.ArchivaDAOStub;
45 import org.easymock.MockControl;
47 public class BrowseActionTest
48 extends AbstractActionTestCase
50 private static final String ACTION_HINT = "browseAction";
52 private BrowseAction action;
54 private ArchivaDAOStub archivaDao;
56 private static final List<String> GROUPS =
57 Arrays.asList( "org.apache.archiva", "commons-lang", "org.apache.maven", "com.sun", "com.oracle",
60 public void testInstantiation()
62 assertFalse( action == lookup( Action.class, ACTION_HINT ) );
65 public void testBrowse()
67 metadataResolver.setNamespaces( GROUPS );
69 String result = action.browse();
70 assertSuccessResult( result );
72 BrowsingResults results = action.getResults();
73 assertNotNull( results );
74 assertEquals( Arrays.asList( TEST_REPO ), results.getSelectedRepositoryIds() );
75 assertEquals( Arrays.asList( "com", "commons-lang", "org.apache", "repeat.repeat" ), results.getGroupIds() );
76 assertNull( results.getArtifacts() );
77 assertNull( results.getSelectedArtifactId() );
78 assertNull( results.getSelectedGroupId() );
79 assertNull( results.getVersions() );
81 assertNull( action.getGroupId() );
82 assertNull( action.getArtifactId() );
83 assertNull( action.getRepositoryId() );
84 assertNull( action.getSharedModel() );
87 public void testBrowseNoObservableRepos()
89 setObservableRepos( Collections.<String>emptyList() );
91 String result = action.browse();
92 assertNoAccessResult( result );
94 assertNoOutputVariables();
97 public void testBrowseGroupNoObservableRepos()
99 setObservableRepos( Collections.<String>emptyList() );
100 String selectedGroupId = "org";
102 action.setGroupId( selectedGroupId );
103 String result = action.browseGroup();
104 assertNoAccessResult( result );
106 assertEquals( selectedGroupId, action.getGroupId() );
107 assertNull( action.getResults() );
108 assertNull( action.getArtifactId() );
109 assertNull( action.getRepositoryId() );
110 assertNull( action.getSharedModel() );
113 public void testBrowseArtifactNoObservableRepos()
115 setObservableRepos( Collections.<String>emptyList() );
116 String selectedGroupId = "org.apache";
117 String selectedArtifactId = "apache";
119 action.setGroupId( selectedGroupId );
120 action.setArtifactId( selectedArtifactId );
121 String result = action.browseArtifact();
122 assertNoAccessResult( result );
124 assertEquals( selectedGroupId, action.getGroupId() );
125 assertEquals( selectedArtifactId, action.getArtifactId() );
126 assertNull( action.getResults() );
127 assertNull( action.getRepositoryId() );
128 assertNull( action.getSharedModel() );
131 public void testBrowseGroupNoGroupId()
133 String result = action.browseGroup();
134 assertErrorResult( result );
135 assertNoOutputVariables();
138 public void testBrowseGroupNoArtifacts()
140 String selectedGroupId = "org";
141 List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache.maven" );
143 metadataResolver.setNamespaces( groups );
144 action.setGroupId( selectedGroupId );
145 String result = action.browseGroup();
146 assertSuccessResult( result );
148 BrowsingResults results = action.getResults();
149 assertNotNull( results );
150 assertEquals( Arrays.asList( TEST_REPO ), results.getSelectedRepositoryIds() );
151 assertEquals( Collections.singletonList( "org.apache" ), results.getGroupIds() );
152 assertEquals( Collections.<String>emptyList(), results.getArtifacts() );
153 assertNull( results.getSelectedArtifactId() );
154 assertEquals( selectedGroupId, results.getSelectedGroupId() );
155 assertNull( results.getVersions() );
157 assertEquals( selectedGroupId, action.getGroupId() );
158 assertNull( action.getArtifactId() );
159 assertNull( action.getRepositoryId() );
160 assertNull( action.getSharedModel() );
163 public void testBrowseGroupWithArtifacts()
165 String artifacts = "apache";
166 String selectedGroupId = "org.apache";
167 List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache.maven" );
169 metadataResolver.setNamespaces( groups );
170 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, artifacts, new ProjectVersionMetadata() );
171 action.setGroupId( selectedGroupId );
172 String result = action.browseGroup();
173 assertSuccessResult( result );
175 BrowsingResults results = action.getResults();
176 assertNotNull( results );
177 assertEquals( Arrays.asList( TEST_REPO ), results.getSelectedRepositoryIds() );
178 assertEquals( groups, results.getGroupIds() );
179 assertEquals( Collections.singletonList( artifacts ), results.getArtifacts() );
180 assertNull( results.getSelectedArtifactId() );
181 assertEquals( selectedGroupId, results.getSelectedGroupId() );
182 assertNull( results.getVersions() );
184 assertEquals( selectedGroupId, action.getGroupId() );
185 assertNull( action.getArtifactId() );
186 assertNull( action.getRepositoryId() );
187 assertNull( action.getSharedModel() );
190 public void testBrowseWithCollapsedGroupsAndArtifacts()
192 List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache" );
194 metadataResolver.setNamespaces( groups );
195 // add an artifact in the tree to make sure "single" is not collapsed
196 metadataResolver.setProjectVersion( TEST_REPO, "org.apache", "apache", new ProjectVersionMetadata() );
198 String result = action.browse();
199 assertSuccessResult( result );
201 BrowsingResults results = action.getResults();
202 assertNotNull( results );
203 assertEquals( Arrays.asList( TEST_REPO ), results.getSelectedRepositoryIds() );
204 assertEquals( Collections.singletonList( "org.apache" ), results.getGroupIds() );
205 assertNull( results.getArtifacts() );
206 assertNull( results.getSelectedArtifactId() );
207 assertNull( results.getSelectedGroupId() );
208 assertNull( results.getVersions() );
210 assertNull( action.getGroupId() );
211 assertNull( action.getArtifactId() );
212 assertNull( action.getRepositoryId() );
213 assertNull( action.getSharedModel() );
216 public void testBrowseGroupWithCollapsedGroupsAndArtifacts()
218 String artifacts = "apache";
219 String selectedGroupId = "org.apache";
220 List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache" );
222 metadataResolver.setNamespaces( groups );
223 // add an artifact in the tree to make sure "single" is not collapsed
224 metadataResolver.setProjectVersion( TEST_REPO, "org.apache", "apache", new ProjectVersionMetadata() );
226 action.setGroupId( selectedGroupId );
227 String result = action.browseGroup();
228 assertSuccessResult( result );
230 BrowsingResults results = action.getResults();
231 assertNotNull( results );
232 assertEquals( Arrays.asList( TEST_REPO ), results.getSelectedRepositoryIds() );
233 assertEquals( Collections.singletonList( "org.apache.archiva" ), results.getGroupIds() );
234 assertEquals( Collections.singletonList( artifacts ), results.getArtifacts() );
235 assertNull( results.getSelectedArtifactId() );
236 assertEquals( selectedGroupId, results.getSelectedGroupId() );
237 assertNull( results.getVersions() );
239 assertEquals( selectedGroupId, action.getGroupId() );
240 assertNull( action.getArtifactId() );
241 assertNull( action.getRepositoryId() );
242 assertNull( action.getSharedModel() );
245 public void testBrowseArtifactNoGroupId()
247 String selectedArtifactId = "apache";
249 action.setArtifactId( selectedArtifactId );
250 String result = action.browseArtifact();
251 assertErrorResult( result );
253 assertNull( action.getResults() );
254 assertNull( action.getGroupId() );
255 assertEquals( selectedArtifactId, action.getArtifactId() );
256 assertNull( action.getRepositoryId() );
257 assertNull( action.getSharedModel() );
260 public void testBrowseArtifactNoArtifactId()
262 String selectedGroupId = "org.apache";
264 action.setGroupId( selectedGroupId );
265 String result = action.browseArtifact();
266 assertErrorResult( result );
268 assertNull( action.getResults() );
269 assertEquals( selectedGroupId, action.getGroupId() );
270 assertNull( action.getArtifactId() );
271 assertNull( action.getRepositoryId() );
272 assertNull( action.getSharedModel() );
275 public void testBrowseArtifact()
276 throws ArchivaDatabaseException
278 String selectedGroupId = "org.apache";
279 String selectedArtifactId = "apache";
281 List<String> versions = Arrays.asList( "1", "2", "3", "4" );
282 archivaDao.setVersions( versions );
283 MockControl artifactDaoMockControl = createArtifactDaoMock( selectedGroupId, selectedArtifactId, versions );
284 MockControl projectDaoMockControl = createProjectDaoMock(
285 Arrays.asList( createProjectModel( selectedGroupId, selectedArtifactId, "1" ),
286 createProjectModel( selectedGroupId, selectedArtifactId, "2" ),
287 createProjectModel( selectedGroupId, selectedArtifactId, "3" ),
288 createProjectModel( selectedGroupId, selectedArtifactId, "4" ) ) );
290 action.setGroupId( selectedGroupId );
291 action.setArtifactId( selectedArtifactId );
292 String result = action.browseArtifact();
293 assertSuccessResult( result );
295 artifactDaoMockControl.verify();
296 projectDaoMockControl.verify();
298 assertEquals( selectedGroupId, action.getGroupId() );
299 assertEquals( selectedArtifactId, action.getArtifactId() );
300 assertNull( action.getRepositoryId() );
302 BrowsingResults results = action.getResults();
303 assertNotNull( results );
304 assertEquals( Arrays.asList( TEST_REPO ), results.getSelectedRepositoryIds() );
305 assertNull( results.getGroupIds() );
306 assertNull( results.getArtifacts() );
307 assertEquals( selectedGroupId, results.getSelectedGroupId() );
308 assertEquals( selectedArtifactId, results.getSelectedArtifactId() );
309 assertEquals( versions, results.getVersions() );
311 ArchivaProjectModel model = action.getSharedModel();
312 assertDefaultModel( model, selectedGroupId, selectedArtifactId, null );
315 public void testBrowseArtifactWithSnapshots()
316 throws ArchivaDatabaseException
318 String selectedGroupId = "org.apache";
319 String selectedArtifactId = "apache";
321 List<String> versions = Arrays.asList( "1", "2", "3", "4-SNAPSHOT", "4", "5-SNAPSHOT" );
322 archivaDao.setVersions( versions );
323 MockControl artifactDaoMockControl = createArtifactDaoMock( selectedGroupId, selectedArtifactId, versions );
324 MockControl projectDaoMockControl = createProjectDaoMock(
325 Arrays.asList( createProjectModel( selectedGroupId, selectedArtifactId, "1" ),
326 createProjectModel( selectedGroupId, selectedArtifactId, "2" ),
327 createProjectModel( selectedGroupId, selectedArtifactId, "3" ),
328 createProjectModel( selectedGroupId, selectedArtifactId, "4-SNAPSHOT" ),
329 createProjectModel( selectedGroupId, selectedArtifactId, "4" ),
330 createProjectModel( selectedGroupId, selectedArtifactId, "5-SNAPSHOT" ) ) );
332 action.setGroupId( selectedGroupId );
333 action.setArtifactId( selectedArtifactId );
334 String result = action.browseArtifact();
335 assertSuccessResult( result );
337 artifactDaoMockControl.verify();
338 projectDaoMockControl.verify();
340 assertEquals( selectedGroupId, action.getGroupId() );
341 assertEquals( selectedArtifactId, action.getArtifactId() );
342 assertNull( action.getRepositoryId() );
344 BrowsingResults results = action.getResults();
345 assertNotNull( results );
346 assertEquals( Arrays.asList( TEST_REPO ), results.getSelectedRepositoryIds() );
347 assertNull( results.getGroupIds() );
348 assertNull( results.getArtifacts() );
349 assertEquals( selectedGroupId, results.getSelectedGroupId() );
350 assertEquals( selectedArtifactId, results.getSelectedArtifactId() );
351 assertEquals( versions, results.getVersions() );
353 ArchivaProjectModel model = action.getSharedModel();
354 assertDefaultModel( model, selectedGroupId, selectedArtifactId, null );
357 // TODO: test with restricted observable repos
358 // not currently relevant since it is controlled at the DefaultRepositoryBrowsing level
359 // TODO: current behaviour is to ignore values that differ between models - instead, pick the latest and use that.
360 // Need to update the tests to verify this as models are currently the same
362 private void assertNoAccessResult( String result )
364 assertEquals( GlobalResults.ACCESS_TO_NO_REPOS, result );
365 assertEquals( 0, action.getActionErrors().size() );
366 assertEquals( 0, action.getActionMessages().size() );
369 private void assertSuccessResult( String result )
371 assertEquals( Action.SUCCESS, result );
372 assertEquals( 0, action.getActionErrors().size() );
373 assertEquals( 0, action.getActionMessages().size() );
376 private void assertErrorResult( String result )
378 assertEquals( Action.ERROR, result );
379 assertEquals( 1, action.getActionErrors().size() );
380 assertEquals( 0, action.getActionMessages().size() );
383 private void assertNoOutputVariables()
385 assertNull( action.getResults() );
386 assertNull( action.getGroupId() );
387 assertNull( action.getArtifactId() );
388 assertNull( action.getRepositoryId() );
389 assertNull( action.getSharedModel() );
392 private MockControl createArtifactDaoMock( String groupId, String artifactId, List<String> versions )
393 throws ArchivaDatabaseException
395 // testing deeper than normal with the mocks as we intend to replace RepositoryBrowsing, not just the database
396 // underneath it - those sections will be adjusted with a mock content repository later
397 MockControl control = MockControl.createNiceControl( ArtifactDAO.class );
398 ArtifactDAO dao = (ArtifactDAO) control.getMock();
399 archivaDao.setArtifactDao( dao );
401 for ( String v : versions )
403 ArtifactsRelatedConstraint c = new ArtifactsRelatedConstraint( groupId, artifactId, v );
404 dao.queryArtifacts( c );
405 control.setReturnValue( Collections.singletonList( createArtifact( groupId, artifactId, v ) ) );
412 private ArchivaArtifact createArtifact( String groupId, String artifactId, String version )
414 return createArtifact( groupId, artifactId, version, TEST_REPO );
417 private ArchivaArtifact createArtifact( String groupId, String artifactId, String version, String repoId )
419 ArchivaArtifactModel model = new ArchivaArtifactModel();
420 model.setGroupId( groupId );
421 model.setArtifactId( artifactId );
422 model.setVersion( version );
423 model.setRepositoryId( repoId );
424 return new ArchivaArtifact( model );
427 private MockControl createProjectDaoMock( List<ArchivaProjectModel> projects )
428 throws ArchivaDatabaseException
430 MockControl control = MockControl.createNiceControl( ProjectModelDAO.class );
431 ProjectModelDAO dao = (ProjectModelDAO) control.getMock();
432 archivaDao.setProjectDao( dao );
434 for ( ArchivaProjectModel project : projects )
436 control.expectAndReturn(
437 dao.getProjectModel( project.getGroupId(), project.getArtifactId(), project.getVersion() ), project );
444 protected void setUp()
448 action = (BrowseAction) lookup( Action.class, ACTION_HINT );
449 archivaDao = (ArchivaDAOStub) lookup( ArchivaDAO.class, "jdo" );
450 metadataResolver = (TestMetadataResolver) action.getMetadataResolver();
453 protected ArchivaProjectModel createProjectModel( String groupId, String artifactId, String version )
455 ArchivaProjectModel model = new ArchivaProjectModel();
456 model.setGroupId( groupId );
457 model.setArtifactId( artifactId );
458 model.setVersion( version );
459 model.setPackaging( TEST_PACKAGING );
460 model.setUrl( TEST_URL );
461 model.setName( TEST_NAME );
462 model.setDescription( TEST_DESCRIPTION );
463 VersionedReference parent = new VersionedReference();
464 parent.setGroupId( TEST_PARENT_GROUP_ID );
465 parent.setArtifactId( TEST_PARENT_ARTIFACT_ID );
466 parent.setVersion( TEST_PARENT_VERSION );
467 model.setParentProject( parent );
468 CiManagement ci = new CiManagement();
469 ci.setSystem( TEST_CI_SYSTEM );
470 ci.setUrl( TEST_CI_URL );
471 model.setCiManagement( ci );
472 IssueManagement issue = new IssueManagement();
473 issue.setSystem( TEST_ISSUE_SYSTEM );
474 issue.setUrl( TEST_ISSUE_URL );
475 model.setIssueManagement( issue );
476 Organization org = new Organization();
477 org.setName( TEST_ORGANIZATION_NAME );
478 org.setUrl( TEST_ORGANIZATION_URL );
479 model.setOrganization( org );
480 License l = new License();
481 l.setName( TEST_LICENSE_NAME );
482 l.setUrl( TEST_LICENSE_URL );
483 model.addLicense( l );
485 l.setName( TEST_LICENSE_NAME_2 );
486 l.setUrl( TEST_LICENSE_URL_2 );
487 model.addLicense( l );
489 scm.setConnection( TEST_SCM_CONNECTION );
490 scm.setDeveloperConnection( TEST_SCM_DEV_CONNECTION );
491 scm.setUrl( TEST_SCM_URL );