1 package org.apache.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.google.common.collect.Lists;
23 import com.opensymphony.xwork2.Action;
24 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
25 import org.apache.archiva.metadata.repository.RepositorySession;
26 import org.apache.archiva.webtest.memory.TestMetadataResolver;
27 import org.apache.archiva.webtest.memory.TestRepositorySessionFactory;
28 import org.apache.archiva.scheduler.indexing.DefaultDownloadRemoteIndexScheduler;
30 import java.util.Arrays;
31 import java.util.Collections;
32 import java.util.List;
34 import static org.mockito.Mockito.mock;
35 import static org.mockito.Mockito.when;
37 public class BrowseActionTest
38 extends AbstractActionTestCase
40 private static final String ACTION_HINT = "browseAction";
42 private BrowseAction action;
44 private static final List<String> GROUPS =
45 Arrays.asList( "org.apache.archiva", "commons-lang", "org.apache.maven", "com.sun", "com.oracle",
48 private static final String OTHER_TEST_REPO = "other-repo";
50 protected void setUp()
54 setObservableRepos( Lists.<String>newArrayList( "test-repo" ) );
55 action = (BrowseAction) getActionProxy( "/browse.action" ).getAction();
56 metadataResolver = new TestMetadataResolver();
57 RepositorySession repositorySession = mock( RepositorySession.class );
58 when( repositorySession.getResolver() ).thenReturn( metadataResolver );
59 TestRepositorySessionFactory factory =
60 applicationContext.getBean( "repositorySessionFactory#test", TestRepositorySessionFactory.class );
61 factory.setRepositorySession( repositorySession );
64 protected void tearDown()
68 applicationContext.getBean( DefaultDownloadRemoteIndexScheduler.class ).shutdown();
69 setObservableRepos( Lists.<String>newArrayList( "test-repo" ) );
72 public void testInstantiation()
74 assertFalse( action == getActionProxy( "/browse.action" ).getAction() );
77 public void testBrowse()
80 metadataResolver.setNamespaces( TEST_REPO, GROUPS );
82 String result = action.browse();
83 assertSuccessResult( result );
85 assertEquals( Arrays.asList( "com", "commons-lang", "org.apache", "repeat.repeat" ), action.getNamespaces() );
86 assertNull( action.getProjectIds() );
87 assertNull( action.getProjectVersions() );
89 assertNull( action.getGroupId() );
90 assertNull( action.getArtifactId() );
91 assertNull( action.getRepositoryId() );
92 assertNull( action.getSharedModel() );
95 public void testBrowseNoObservableRepos()
98 setObservableRepos( Collections.<String>emptyList() );
100 String result = action.browse();
101 assertNoAccessResult( result );
103 assertNoOutputVariables();
106 public void testBrowseGroupNoObservableRepos()
109 setObservableRepos( Collections.<String>emptyList() );
110 String selectedGroupId = "org";
112 action.setGroupId( selectedGroupId );
113 String result = action.browseGroup();
114 assertNoAccessResult( result );
116 assertEquals( selectedGroupId, action.getGroupId() );
117 assertNull( action.getNamespaces() );
118 assertNull( action.getProjectIds() );
119 assertNull( action.getProjectVersions() );
120 assertNull( action.getArtifactId() );
121 assertNull( action.getRepositoryId() );
122 assertNull( action.getSharedModel() );
125 public void testBrowseArtifactNoObservableRepos()
128 setObservableRepos( Collections.<String>emptyList() );
129 String selectedGroupId = "org.apache";
130 String selectedArtifactId = "apache";
132 action.setGroupId( selectedGroupId );
133 action.setArtifactId( selectedArtifactId );
134 String result = action.browseArtifact();
135 assertNoAccessResult( result );
137 assertEquals( selectedGroupId, action.getGroupId() );
138 assertEquals( selectedArtifactId, action.getArtifactId() );
139 assertNull( action.getNamespaces() );
140 assertNull( action.getProjectIds() );
141 assertNull( action.getProjectVersions() );
142 assertNull( action.getRepositoryId() );
143 assertNull( action.getSharedModel() );
146 public void testBrowseGroupNoGroupId()
149 String result = action.browseGroup();
150 assertErrorResult( result );
151 assertNoOutputVariables();
154 public void testBrowseGroupNoArtifacts()
157 String selectedGroupId = "org";
158 List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache.maven" );
160 metadataResolver.setNamespaces( TEST_REPO, groups );
161 action.setGroupId( selectedGroupId );
162 String result = action.browseGroup();
163 assertSuccessResult( result );
165 assertEquals( Collections.singletonList( "org.apache" ), action.getNamespaces() );
166 assertEquals( Collections.<String>emptyList(), action.getProjectIds() );
167 assertNull( action.getProjectVersions() );
169 assertEquals( selectedGroupId, action.getGroupId() );
170 assertNull( action.getArtifactId() );
171 assertNull( action.getRepositoryId() );
172 assertNull( action.getSharedModel() );
175 public void testBrowseGroupWithArtifacts()
178 String artifacts = "apache";
179 String selectedGroupId = "org.apache";
180 List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache.maven" );
182 metadataResolver.setNamespaces( TEST_REPO, groups );
183 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, artifacts, new ProjectVersionMetadata() );
184 action.setGroupId( selectedGroupId );
185 String result = action.browseGroup();
186 assertSuccessResult( result );
188 assertEquals( groups, action.getNamespaces() );
189 assertEquals( Collections.singletonList( artifacts ), action.getProjectIds() );
190 assertNull( action.getProjectVersions() );
192 assertEquals( selectedGroupId, action.getGroupId() );
193 assertNull( action.getArtifactId() );
194 assertNull( action.getRepositoryId() );
195 assertNull( action.getSharedModel() );
198 public void testBrowseWithCollapsedGroupsAndArtifacts()
201 List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache" );
203 metadataResolver.setNamespaces( TEST_REPO, groups );
204 // add an artifact in the tree to make sure "single" is not collapsed
205 metadataResolver.setProjectVersion( TEST_REPO, "org.apache", "apache", new ProjectVersionMetadata() );
207 String result = action.browse();
208 assertSuccessResult( result );
210 assertEquals( Collections.singletonList( "org.apache" ), action.getNamespaces() );
211 assertNull( action.getProjectIds() );
212 assertNull( action.getProjectVersions() );
214 assertNull( action.getGroupId() );
215 assertNull( action.getArtifactId() );
216 assertNull( action.getRepositoryId() );
217 assertNull( action.getSharedModel() );
220 public void testBrowseWithCollapsedGroupsAndArtifactsAcrossRepositories()
223 setObservableRepos( Arrays.asList( TEST_REPO, OTHER_TEST_REPO ) );
225 metadataResolver.setNamespaces( TEST_REPO, Arrays.asList( "org.apache.archiva", "org.apache" ) );
226 metadataResolver.setNamespaces( OTHER_TEST_REPO, Arrays.asList( "org.codehaus.plexus", "org.codehaus" ) );
228 // add an artifact in the tree to make sure "single" is not collapsed
229 metadataResolver.setProjectVersion( TEST_REPO, "org.apache", "apache", new ProjectVersionMetadata() );
231 String result = action.browse();
232 assertSuccessResult( result );
234 assertEquals( Collections.singletonList( "org" ), action.getNamespaces() );
235 assertNull( action.getProjectIds() );
236 assertNull( action.getProjectVersions() );
238 assertNull( action.getGroupId() );
239 assertNull( action.getArtifactId() );
240 assertNull( action.getRepositoryId() );
241 assertNull( action.getSharedModel() );
244 public void testBrowseGroupWithCollapsedGroupsAndArtifacts()
247 String artifacts = "apache";
248 String selectedGroupId = "org.apache";
249 List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache" );
251 metadataResolver.setNamespaces( TEST_REPO, groups );
252 // add an artifact in the tree to make sure "single" is not collapsed
253 metadataResolver.setProjectVersion( TEST_REPO, "org.apache", "apache", new ProjectVersionMetadata() );
255 action.setGroupId( selectedGroupId );
256 String result = action.browseGroup();
257 assertSuccessResult( result );
259 assertEquals( Collections.singletonList( "org.apache.archiva" ), action.getNamespaces() );
260 assertEquals( Collections.singletonList( artifacts ), action.getProjectIds() );
261 assertNull( action.getProjectVersions() );
263 assertEquals( selectedGroupId, action.getGroupId() );
264 assertNull( action.getArtifactId() );
265 assertNull( action.getRepositoryId() );
266 assertNull( action.getSharedModel() );
269 public void testBrowseArtifactNoGroupId()
272 String selectedArtifactId = "apache";
274 action.setArtifactId( selectedArtifactId );
275 String result = action.browseArtifact();
276 assertErrorResult( result );
278 assertNull( action.getNamespaces() );
279 assertNull( action.getProjectIds() );
280 assertNull( action.getProjectVersions() );
281 assertNull( action.getGroupId() );
282 assertEquals( selectedArtifactId, action.getArtifactId() );
283 assertNull( action.getRepositoryId() );
284 assertNull( action.getSharedModel() );
287 public void testBrowseArtifactNoArtifactId()
290 String selectedGroupId = "org.apache";
292 action.setGroupId( selectedGroupId );
293 String result = action.browseArtifact();
294 assertErrorResult( result );
296 assertNull( action.getNamespaces() );
297 assertNull( action.getProjectIds() );
298 assertNull( action.getProjectVersions() );
299 assertEquals( selectedGroupId, action.getGroupId() );
300 assertNull( action.getArtifactId() );
301 assertNull( action.getRepositoryId() );
302 assertNull( action.getSharedModel() );
305 public void testBrowseArtifact()
309 String selectedGroupId = "org.apache";
310 String selectedArtifactId = "apache";
312 List<String> versions = Arrays.asList( "1", "2", "3", "4" );
313 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
314 createProjectModel( selectedGroupId, selectedArtifactId, "1" ) );
315 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
316 createProjectModel( selectedGroupId, selectedArtifactId, "2" ) );
317 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
318 createProjectModel( selectedGroupId, selectedArtifactId, "3" ) );
319 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
320 createProjectModel( selectedGroupId, selectedArtifactId, "4" ) );
322 action.setGroupId( selectedGroupId );
323 action.setArtifactId( selectedArtifactId );
324 String result = action.browseArtifact();
325 assertSuccessResult( result );
327 assertEquals( selectedGroupId, action.getGroupId() );
328 assertEquals( selectedArtifactId, action.getArtifactId() );
329 assertNull( action.getRepositoryId() );
331 assertNull( action.getNamespaces() );
332 assertNull( action.getProjectIds() );
333 assertEquals( versions, action.getProjectVersions() );
335 ProjectVersionMetadata model = action.getSharedModel();
336 assertDefaultModel( model, selectedGroupId, selectedArtifactId, null );
339 public void testBrowseArtifactWithSnapshots()
343 String selectedGroupId = "org.apache";
344 String selectedArtifactId = "apache";
346 List<String> versions = Arrays.asList( "1", "2", "3", "4-SNAPSHOT", "4", "5-SNAPSHOT" );
347 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
348 createProjectModel( selectedGroupId, selectedArtifactId, "1" ) );
349 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
350 createProjectModel( selectedGroupId, selectedArtifactId, "2" ) );
351 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
352 createProjectModel( selectedGroupId, selectedArtifactId, "3" ) );
353 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
354 createProjectModel( selectedGroupId, selectedArtifactId, "4-SNAPSHOT" ) );
355 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
356 createProjectModel( selectedGroupId, selectedArtifactId, "4" ) );
357 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
358 createProjectModel( selectedGroupId, selectedArtifactId, "5-SNAPSHOT" ) );
360 action.setGroupId( selectedGroupId );
361 action.setArtifactId( selectedArtifactId );
362 String result = action.browseArtifact();
363 assertSuccessResult( result );
365 assertEquals( selectedGroupId, action.getGroupId() );
366 assertEquals( selectedArtifactId, action.getArtifactId() );
367 assertNull( action.getRepositoryId() );
369 assertNull( action.getNamespaces() );
370 assertNull( action.getProjectIds() );
371 assertEquals( versions, action.getProjectVersions() );
373 ProjectVersionMetadata model = action.getSharedModel();
374 assertDefaultModel( model, selectedGroupId, selectedArtifactId, null );
377 // TODO: test with restricted observable repos
378 // TODO: current behaviour is to ignore values that differ between models - instead, pick the latest and use that.
379 // Need to update the tests to verify this as models are currently the same
381 private void assertNoAccessResult( String result )
383 assertEquals( GlobalResults.ACCESS_TO_NO_REPOS, result );
384 assertEquals( 0, action.getActionErrors().size() );
385 assertEquals( 0, action.getActionMessages().size() );
388 private void assertSuccessResult( String result )
390 assertEquals( Action.SUCCESS, result );
391 assertEquals( 0, action.getActionErrors().size() );
392 assertEquals( 0, action.getActionMessages().size() );
395 private void assertErrorResult( String result )
397 assertEquals( Action.ERROR, result );
398 assertEquals( 1, action.getActionErrors().size() );
399 assertEquals( 0, action.getActionMessages().size() );
402 private void assertNoOutputVariables()
404 assertNull( action.getNamespaces() );
405 assertNull( action.getProjectIds() );
406 assertNull( action.getProjectVersions() );
407 assertNull( action.getGroupId() );
408 assertNull( action.getArtifactId() );
409 assertNull( action.getRepositoryId() );
410 assertNull( action.getSharedModel() );