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 com.opensymphony.xwork2.Action;
23 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
24 import org.apache.archiva.metadata.repository.RepositorySession;
25 import org.apache.archiva.metadata.repository.memory.TestMetadataResolver;
26 import org.apache.archiva.metadata.repository.memory.TestRepositorySessionFactory;
28 import java.util.Arrays;
29 import java.util.Collections;
30 import java.util.List;
32 import static org.mockito.Mockito.mock;
33 import static org.mockito.Mockito.when;
35 public class BrowseActionTest
36 extends AbstractActionTestCase
38 private static final String ACTION_HINT = "browseAction";
40 private BrowseAction action;
42 private static final List<String> GROUPS =
43 Arrays.asList( "org.apache.archiva", "commons-lang", "org.apache.maven", "com.sun", "com.oracle",
46 private static final String OTHER_TEST_REPO = "other-repo";
48 public void testInstantiation()
50 assertFalse( action == (BrowseAction) getActionProxy( "/browse.action" ).getAction() );
53 public void testBrowse()
56 metadataResolver.setNamespaces( TEST_REPO, GROUPS );
58 String result = action.browse();
59 assertSuccessResult( result );
61 assertEquals( Arrays.asList( "com", "commons-lang", "org.apache", "repeat.repeat" ), action.getNamespaces() );
62 assertNull( action.getProjectIds() );
63 assertNull( action.getProjectVersions() );
65 assertNull( action.getGroupId() );
66 assertNull( action.getArtifactId() );
67 assertNull( action.getRepositoryId() );
68 assertNull( action.getSharedModel() );
71 public void testBrowseNoObservableRepos()
74 setObservableRepos( Collections.<String>emptyList() );
76 String result = action.browse();
77 assertNoAccessResult( result );
79 assertNoOutputVariables();
82 public void testBrowseGroupNoObservableRepos()
85 setObservableRepos( Collections.<String>emptyList() );
86 String selectedGroupId = "org";
88 action.setGroupId( selectedGroupId );
89 String result = action.browseGroup();
90 assertNoAccessResult( result );
92 assertEquals( selectedGroupId, action.getGroupId() );
93 assertNull( action.getNamespaces() );
94 assertNull( action.getProjectIds() );
95 assertNull( action.getProjectVersions() );
96 assertNull( action.getArtifactId() );
97 assertNull( action.getRepositoryId() );
98 assertNull( action.getSharedModel() );
101 public void testBrowseArtifactNoObservableRepos()
104 setObservableRepos( Collections.<String>emptyList() );
105 String selectedGroupId = "org.apache";
106 String selectedArtifactId = "apache";
108 action.setGroupId( selectedGroupId );
109 action.setArtifactId( selectedArtifactId );
110 String result = action.browseArtifact();
111 assertNoAccessResult( result );
113 assertEquals( selectedGroupId, action.getGroupId() );
114 assertEquals( selectedArtifactId, action.getArtifactId() );
115 assertNull( action.getNamespaces() );
116 assertNull( action.getProjectIds() );
117 assertNull( action.getProjectVersions() );
118 assertNull( action.getRepositoryId() );
119 assertNull( action.getSharedModel() );
122 public void testBrowseGroupNoGroupId()
125 String result = action.browseGroup();
126 assertErrorResult( result );
127 assertNoOutputVariables();
130 public void testBrowseGroupNoArtifacts()
133 String selectedGroupId = "org";
134 List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache.maven" );
136 metadataResolver.setNamespaces( TEST_REPO, groups );
137 action.setGroupId( selectedGroupId );
138 String result = action.browseGroup();
139 assertSuccessResult( result );
141 assertEquals( Collections.singletonList( "org.apache" ), action.getNamespaces() );
142 assertEquals( Collections.<String>emptyList(), action.getProjectIds() );
143 assertNull( action.getProjectVersions() );
145 assertEquals( selectedGroupId, action.getGroupId() );
146 assertNull( action.getArtifactId() );
147 assertNull( action.getRepositoryId() );
148 assertNull( action.getSharedModel() );
151 public void testBrowseGroupWithArtifacts()
154 String artifacts = "apache";
155 String selectedGroupId = "org.apache";
156 List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache.maven" );
158 metadataResolver.setNamespaces( TEST_REPO, groups );
159 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, artifacts, new ProjectVersionMetadata() );
160 action.setGroupId( selectedGroupId );
161 String result = action.browseGroup();
162 assertSuccessResult( result );
164 assertEquals( groups, action.getNamespaces() );
165 assertEquals( Collections.singletonList( artifacts ), action.getProjectIds() );
166 assertNull( action.getProjectVersions() );
168 assertEquals( selectedGroupId, action.getGroupId() );
169 assertNull( action.getArtifactId() );
170 assertNull( action.getRepositoryId() );
171 assertNull( action.getSharedModel() );
174 public void testBrowseWithCollapsedGroupsAndArtifacts()
177 List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache" );
179 metadataResolver.setNamespaces( TEST_REPO, groups );
180 // add an artifact in the tree to make sure "single" is not collapsed
181 metadataResolver.setProjectVersion( TEST_REPO, "org.apache", "apache", new ProjectVersionMetadata() );
183 String result = action.browse();
184 assertSuccessResult( result );
186 assertEquals( Collections.singletonList( "org.apache" ), action.getNamespaces() );
187 assertNull( action.getProjectIds() );
188 assertNull( action.getProjectVersions() );
190 assertNull( action.getGroupId() );
191 assertNull( action.getArtifactId() );
192 assertNull( action.getRepositoryId() );
193 assertNull( action.getSharedModel() );
196 public void testBrowseWithCollapsedGroupsAndArtifactsAcrossRepositories()
199 setObservableRepos( Arrays.asList( TEST_REPO, OTHER_TEST_REPO ) );
201 metadataResolver.setNamespaces( TEST_REPO, Arrays.asList( "org.apache.archiva", "org.apache" ) );
202 metadataResolver.setNamespaces( OTHER_TEST_REPO, Arrays.asList( "org.codehaus.plexus", "org.codehaus" ) );
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" ), 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 testBrowseGroupWithCollapsedGroupsAndArtifacts()
223 String artifacts = "apache";
224 String selectedGroupId = "org.apache";
225 List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache" );
227 metadataResolver.setNamespaces( TEST_REPO, groups );
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 action.setGroupId( selectedGroupId );
232 String result = action.browseGroup();
233 assertSuccessResult( result );
235 assertEquals( Collections.singletonList( "org.apache.archiva" ), action.getNamespaces() );
236 assertEquals( Collections.singletonList( artifacts ), action.getProjectIds() );
237 assertNull( action.getProjectVersions() );
239 assertEquals( selectedGroupId, action.getGroupId() );
240 assertNull( action.getArtifactId() );
241 assertNull( action.getRepositoryId() );
242 assertNull( action.getSharedModel() );
245 public void testBrowseArtifactNoGroupId()
248 String selectedArtifactId = "apache";
250 action.setArtifactId( selectedArtifactId );
251 String result = action.browseArtifact();
252 assertErrorResult( result );
254 assertNull( action.getNamespaces() );
255 assertNull( action.getProjectIds() );
256 assertNull( action.getProjectVersions() );
257 assertNull( action.getGroupId() );
258 assertEquals( selectedArtifactId, action.getArtifactId() );
259 assertNull( action.getRepositoryId() );
260 assertNull( action.getSharedModel() );
263 public void testBrowseArtifactNoArtifactId()
266 String selectedGroupId = "org.apache";
268 action.setGroupId( selectedGroupId );
269 String result = action.browseArtifact();
270 assertErrorResult( result );
272 assertNull( action.getNamespaces() );
273 assertNull( action.getProjectIds() );
274 assertNull( action.getProjectVersions() );
275 assertEquals( selectedGroupId, action.getGroupId() );
276 assertNull( action.getArtifactId() );
277 assertNull( action.getRepositoryId() );
278 assertNull( action.getSharedModel() );
281 public void testBrowseArtifact()
285 String selectedGroupId = "org.apache";
286 String selectedArtifactId = "apache";
288 List<String> versions = Arrays.asList( "1", "2", "3", "4" );
289 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
290 createProjectModel( selectedGroupId, selectedArtifactId, "1" ) );
291 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
292 createProjectModel( selectedGroupId, selectedArtifactId, "2" ) );
293 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
294 createProjectModel( selectedGroupId, selectedArtifactId, "3" ) );
295 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
296 createProjectModel( selectedGroupId, selectedArtifactId, "4" ) );
298 action.setGroupId( selectedGroupId );
299 action.setArtifactId( selectedArtifactId );
300 String result = action.browseArtifact();
301 assertSuccessResult( result );
303 assertEquals( selectedGroupId, action.getGroupId() );
304 assertEquals( selectedArtifactId, action.getArtifactId() );
305 assertNull( action.getRepositoryId() );
307 assertNull( action.getNamespaces() );
308 assertNull( action.getProjectIds() );
309 assertEquals( versions, action.getProjectVersions() );
311 ProjectVersionMetadata model = action.getSharedModel();
312 assertDefaultModel( model, selectedGroupId, selectedArtifactId, null );
315 public void testBrowseArtifactWithSnapshots()
319 String selectedGroupId = "org.apache";
320 String selectedArtifactId = "apache";
322 List<String> versions = Arrays.asList( "1", "2", "3", "4-SNAPSHOT", "4", "5-SNAPSHOT" );
323 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
324 createProjectModel( selectedGroupId, selectedArtifactId, "1" ) );
325 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
326 createProjectModel( selectedGroupId, selectedArtifactId, "2" ) );
327 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
328 createProjectModel( selectedGroupId, selectedArtifactId, "3" ) );
329 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
330 createProjectModel( selectedGroupId, selectedArtifactId, "4-SNAPSHOT" ) );
331 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
332 createProjectModel( selectedGroupId, selectedArtifactId, "4" ) );
333 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
334 createProjectModel( selectedGroupId, selectedArtifactId, "5-SNAPSHOT" ) );
336 action.setGroupId( selectedGroupId );
337 action.setArtifactId( selectedArtifactId );
338 String result = action.browseArtifact();
339 assertSuccessResult( result );
341 assertEquals( selectedGroupId, action.getGroupId() );
342 assertEquals( selectedArtifactId, action.getArtifactId() );
343 assertNull( action.getRepositoryId() );
345 assertNull( action.getNamespaces() );
346 assertNull( action.getProjectIds() );
347 assertEquals( versions, action.getProjectVersions() );
349 ProjectVersionMetadata model = action.getSharedModel();
350 assertDefaultModel( model, selectedGroupId, selectedArtifactId, null );
353 // TODO: test with restricted observable repos
354 // TODO: current behaviour is to ignore values that differ between models - instead, pick the latest and use that.
355 // Need to update the tests to verify this as models are currently the same
357 private void assertNoAccessResult( String result )
359 assertEquals( GlobalResults.ACCESS_TO_NO_REPOS, result );
360 assertEquals( 0, action.getActionErrors().size() );
361 assertEquals( 0, action.getActionMessages().size() );
364 private void assertSuccessResult( String result )
366 assertEquals( Action.SUCCESS, result );
367 assertEquals( 0, action.getActionErrors().size() );
368 assertEquals( 0, action.getActionMessages().size() );
371 private void assertErrorResult( String result )
373 assertEquals( Action.ERROR, result );
374 assertEquals( 1, action.getActionErrors().size() );
375 assertEquals( 0, action.getActionMessages().size() );
378 private void assertNoOutputVariables()
380 assertNull( action.getNamespaces() );
381 assertNull( action.getProjectIds() );
382 assertNull( action.getProjectVersions() );
383 assertNull( action.getGroupId() );
384 assertNull( action.getArtifactId() );
385 assertNull( action.getRepositoryId() );
386 assertNull( action.getSharedModel() );
389 protected void setUp()
393 //action = (BrowseAction) lookup( Action.class, ACTION_HINT );
394 action = (BrowseAction) getActionProxy( "/browse.action" ).getAction();
395 metadataResolver = new TestMetadataResolver();
396 RepositorySession repositorySession = mock( RepositorySession.class );
397 when( repositorySession.getResolver() ).thenReturn( metadataResolver );
398 TestRepositorySessionFactory factory =
399 applicationContext.getBean( "repositorySessionFactory#test", TestRepositorySessionFactory.class );
400 factory.setRepositorySession( repositorySession );