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.metadata.repository.memory.TestMetadataResolver;
27 import org.apache.archiva.metadata.repository.memory.TestRepositorySessionFactory;
28 import org.apache.archiva.scheduler.indexing.DefaultDownloadRemoteIndexScheduler;
29 import org.springframework.context.support.GenericApplicationContext;
31 import java.util.Arrays;
32 import java.util.Collections;
33 import java.util.List;
35 import static org.mockito.Mockito.mock;
36 import static org.mockito.Mockito.when;
38 public class BrowseActionTest
39 extends AbstractActionTestCase
41 private static final String ACTION_HINT = "browseAction";
43 private BrowseAction action;
45 private static final List<String> GROUPS =
46 Arrays.asList( "org.apache.archiva", "commons-lang", "org.apache.maven", "com.sun", "com.oracle",
49 private static final String OTHER_TEST_REPO = "other-repo";
51 protected void setUp()
55 setObservableRepos( Lists.<String>newArrayList( "test-repo" ) );
56 action = (BrowseAction) getActionProxy( "/browse.action" ).getAction();
57 metadataResolver = new TestMetadataResolver();
58 RepositorySession repositorySession = mock( RepositorySession.class );
59 when( repositorySession.getResolver() ).thenReturn( metadataResolver );
60 TestRepositorySessionFactory factory =
61 applicationContext.getBean( "repositorySessionFactory#test", TestRepositorySessionFactory.class );
62 factory.setRepositorySession( repositorySession );
65 protected void tearDown()
69 applicationContext.getBean( DefaultDownloadRemoteIndexScheduler.class ).shutdown();
70 setObservableRepos( Lists.<String>newArrayList( "test-repo" ) );
73 public void testInstantiation()
75 assertFalse( action == getActionProxy( "/browse.action" ).getAction() );
78 public void testBrowse()
81 metadataResolver.setNamespaces( TEST_REPO, GROUPS );
83 String result = action.browse();
84 assertSuccessResult( result );
86 assertEquals( Arrays.asList( "com", "commons-lang", "org.apache", "repeat.repeat" ), action.getNamespaces() );
87 assertNull( action.getProjectIds() );
88 assertNull( action.getProjectVersions() );
90 assertNull( action.getGroupId() );
91 assertNull( action.getArtifactId() );
92 assertNull( action.getRepositoryId() );
93 assertNull( action.getSharedModel() );
96 public void testBrowseNoObservableRepos()
99 setObservableRepos( Collections.<String>emptyList() );
101 String result = action.browse();
102 assertNoAccessResult( result );
104 assertNoOutputVariables();
107 public void testBrowseGroupNoObservableRepos()
110 setObservableRepos( Collections.<String>emptyList() );
111 String selectedGroupId = "org";
113 action.setGroupId( selectedGroupId );
114 String result = action.browseGroup();
115 assertNoAccessResult( result );
117 assertEquals( selectedGroupId, action.getGroupId() );
118 assertNull( action.getNamespaces() );
119 assertNull( action.getProjectIds() );
120 assertNull( action.getProjectVersions() );
121 assertNull( action.getArtifactId() );
122 assertNull( action.getRepositoryId() );
123 assertNull( action.getSharedModel() );
126 public void testBrowseArtifactNoObservableRepos()
129 setObservableRepos( Collections.<String>emptyList() );
130 String selectedGroupId = "org.apache";
131 String selectedArtifactId = "apache";
133 action.setGroupId( selectedGroupId );
134 action.setArtifactId( selectedArtifactId );
135 String result = action.browseArtifact();
136 assertNoAccessResult( result );
138 assertEquals( selectedGroupId, action.getGroupId() );
139 assertEquals( selectedArtifactId, action.getArtifactId() );
140 assertNull( action.getNamespaces() );
141 assertNull( action.getProjectIds() );
142 assertNull( action.getProjectVersions() );
143 assertNull( action.getRepositoryId() );
144 assertNull( action.getSharedModel() );
147 public void testBrowseGroupNoGroupId()
150 String result = action.browseGroup();
151 assertErrorResult( result );
152 assertNoOutputVariables();
155 public void testBrowseGroupNoArtifacts()
158 String selectedGroupId = "org";
159 List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache.maven" );
161 metadataResolver.setNamespaces( TEST_REPO, groups );
162 action.setGroupId( selectedGroupId );
163 String result = action.browseGroup();
164 assertSuccessResult( result );
166 assertEquals( Collections.singletonList( "org.apache" ), action.getNamespaces() );
167 assertEquals( Collections.<String>emptyList(), action.getProjectIds() );
168 assertNull( action.getProjectVersions() );
170 assertEquals( selectedGroupId, action.getGroupId() );
171 assertNull( action.getArtifactId() );
172 assertNull( action.getRepositoryId() );
173 assertNull( action.getSharedModel() );
176 public void testBrowseGroupWithArtifacts()
179 String artifacts = "apache";
180 String selectedGroupId = "org.apache";
181 List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache.maven" );
183 metadataResolver.setNamespaces( TEST_REPO, groups );
184 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, artifacts, new ProjectVersionMetadata() );
185 action.setGroupId( selectedGroupId );
186 String result = action.browseGroup();
187 assertSuccessResult( result );
189 assertEquals( groups, action.getNamespaces() );
190 assertEquals( Collections.singletonList( artifacts ), action.getProjectIds() );
191 assertNull( action.getProjectVersions() );
193 assertEquals( selectedGroupId, action.getGroupId() );
194 assertNull( action.getArtifactId() );
195 assertNull( action.getRepositoryId() );
196 assertNull( action.getSharedModel() );
199 public void testBrowseWithCollapsedGroupsAndArtifacts()
202 List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache" );
204 metadataResolver.setNamespaces( TEST_REPO, groups );
205 // add an artifact in the tree to make sure "single" is not collapsed
206 metadataResolver.setProjectVersion( TEST_REPO, "org.apache", "apache", new ProjectVersionMetadata() );
208 String result = action.browse();
209 assertSuccessResult( result );
211 assertEquals( Collections.singletonList( "org.apache" ), action.getNamespaces() );
212 assertNull( action.getProjectIds() );
213 assertNull( action.getProjectVersions() );
215 assertNull( action.getGroupId() );
216 assertNull( action.getArtifactId() );
217 assertNull( action.getRepositoryId() );
218 assertNull( action.getSharedModel() );
221 public void testBrowseWithCollapsedGroupsAndArtifactsAcrossRepositories()
224 setObservableRepos( Arrays.asList( TEST_REPO, OTHER_TEST_REPO ) );
226 metadataResolver.setNamespaces( TEST_REPO, Arrays.asList( "org.apache.archiva", "org.apache" ) );
227 metadataResolver.setNamespaces( OTHER_TEST_REPO, Arrays.asList( "org.codehaus.plexus", "org.codehaus" ) );
229 // add an artifact in the tree to make sure "single" is not collapsed
230 metadataResolver.setProjectVersion( TEST_REPO, "org.apache", "apache", new ProjectVersionMetadata() );
232 String result = action.browse();
233 assertSuccessResult( result );
235 assertEquals( Collections.singletonList( "org" ), action.getNamespaces() );
236 assertNull( action.getProjectIds() );
237 assertNull( action.getProjectVersions() );
239 assertNull( action.getGroupId() );
240 assertNull( action.getArtifactId() );
241 assertNull( action.getRepositoryId() );
242 assertNull( action.getSharedModel() );
245 public void testBrowseGroupWithCollapsedGroupsAndArtifacts()
248 String artifacts = "apache";
249 String selectedGroupId = "org.apache";
250 List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache" );
252 metadataResolver.setNamespaces( TEST_REPO, groups );
253 // add an artifact in the tree to make sure "single" is not collapsed
254 metadataResolver.setProjectVersion( TEST_REPO, "org.apache", "apache", new ProjectVersionMetadata() );
256 action.setGroupId( selectedGroupId );
257 String result = action.browseGroup();
258 assertSuccessResult( result );
260 assertEquals( Collections.singletonList( "org.apache.archiva" ), action.getNamespaces() );
261 assertEquals( Collections.singletonList( artifacts ), action.getProjectIds() );
262 assertNull( action.getProjectVersions() );
264 assertEquals( selectedGroupId, action.getGroupId() );
265 assertNull( action.getArtifactId() );
266 assertNull( action.getRepositoryId() );
267 assertNull( action.getSharedModel() );
270 public void testBrowseArtifactNoGroupId()
273 String selectedArtifactId = "apache";
275 action.setArtifactId( selectedArtifactId );
276 String result = action.browseArtifact();
277 assertErrorResult( result );
279 assertNull( action.getNamespaces() );
280 assertNull( action.getProjectIds() );
281 assertNull( action.getProjectVersions() );
282 assertNull( action.getGroupId() );
283 assertEquals( selectedArtifactId, action.getArtifactId() );
284 assertNull( action.getRepositoryId() );
285 assertNull( action.getSharedModel() );
288 public void testBrowseArtifactNoArtifactId()
291 String selectedGroupId = "org.apache";
293 action.setGroupId( selectedGroupId );
294 String result = action.browseArtifact();
295 assertErrorResult( result );
297 assertNull( action.getNamespaces() );
298 assertNull( action.getProjectIds() );
299 assertNull( action.getProjectVersions() );
300 assertEquals( selectedGroupId, action.getGroupId() );
301 assertNull( action.getArtifactId() );
302 assertNull( action.getRepositoryId() );
303 assertNull( action.getSharedModel() );
306 public void testBrowseArtifact()
310 String selectedGroupId = "org.apache";
311 String selectedArtifactId = "apache";
313 List<String> versions = Arrays.asList( "1", "2", "3", "4" );
314 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
315 createProjectModel( selectedGroupId, selectedArtifactId, "1" ) );
316 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
317 createProjectModel( selectedGroupId, selectedArtifactId, "2" ) );
318 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
319 createProjectModel( selectedGroupId, selectedArtifactId, "3" ) );
320 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
321 createProjectModel( selectedGroupId, selectedArtifactId, "4" ) );
323 action.setGroupId( selectedGroupId );
324 action.setArtifactId( selectedArtifactId );
325 String result = action.browseArtifact();
326 assertSuccessResult( result );
328 assertEquals( selectedGroupId, action.getGroupId() );
329 assertEquals( selectedArtifactId, action.getArtifactId() );
330 assertNull( action.getRepositoryId() );
332 assertNull( action.getNamespaces() );
333 assertNull( action.getProjectIds() );
334 assertEquals( versions, action.getProjectVersions() );
336 ProjectVersionMetadata model = action.getSharedModel();
337 assertDefaultModel( model, selectedGroupId, selectedArtifactId, null );
340 public void testBrowseArtifactWithSnapshots()
344 String selectedGroupId = "org.apache";
345 String selectedArtifactId = "apache";
347 List<String> versions = Arrays.asList( "1", "2", "3", "4-SNAPSHOT", "4", "5-SNAPSHOT" );
348 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
349 createProjectModel( selectedGroupId, selectedArtifactId, "1" ) );
350 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
351 createProjectModel( selectedGroupId, selectedArtifactId, "2" ) );
352 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
353 createProjectModel( selectedGroupId, selectedArtifactId, "3" ) );
354 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
355 createProjectModel( selectedGroupId, selectedArtifactId, "4-SNAPSHOT" ) );
356 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
357 createProjectModel( selectedGroupId, selectedArtifactId, "4" ) );
358 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
359 createProjectModel( selectedGroupId, selectedArtifactId, "5-SNAPSHOT" ) );
361 action.setGroupId( selectedGroupId );
362 action.setArtifactId( selectedArtifactId );
363 String result = action.browseArtifact();
364 assertSuccessResult( result );
366 assertEquals( selectedGroupId, action.getGroupId() );
367 assertEquals( selectedArtifactId, action.getArtifactId() );
368 assertNull( action.getRepositoryId() );
370 assertNull( action.getNamespaces() );
371 assertNull( action.getProjectIds() );
372 assertEquals( versions, action.getProjectVersions() );
374 ProjectVersionMetadata model = action.getSharedModel();
375 assertDefaultModel( model, selectedGroupId, selectedArtifactId, null );
378 // TODO: test with restricted observable repos
379 // TODO: current behaviour is to ignore values that differ between models - instead, pick the latest and use that.
380 // Need to update the tests to verify this as models are currently the same
382 private void assertNoAccessResult( String result )
384 assertEquals( GlobalResults.ACCESS_TO_NO_REPOS, result );
385 assertEquals( 0, action.getActionErrors().size() );
386 assertEquals( 0, action.getActionMessages().size() );
389 private void assertSuccessResult( String result )
391 assertEquals( Action.SUCCESS, result );
392 assertEquals( 0, action.getActionErrors().size() );
393 assertEquals( 0, action.getActionMessages().size() );
396 private void assertErrorResult( String result )
398 assertEquals( Action.ERROR, result );
399 assertEquals( 1, action.getActionErrors().size() );
400 assertEquals( 0, action.getActionMessages().size() );
403 private void assertNoOutputVariables()
405 assertNull( action.getNamespaces() );
406 assertNull( action.getProjectIds() );
407 assertNull( action.getProjectVersions() );
408 assertNull( action.getGroupId() );
409 assertNull( action.getArtifactId() );
410 assertNull( action.getRepositoryId() );
411 assertNull( action.getSharedModel() );