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.memory.TestMetadataResolver;
26 import java.util.Arrays;
27 import java.util.Collections;
28 import java.util.List;
30 public class BrowseActionTest
31 extends AbstractActionTestCase
33 private static final String ACTION_HINT = "browseAction";
35 private BrowseAction action;
37 private static final List<String> GROUPS = Arrays.asList( "org.apache.archiva", "commons-lang", "org.apache.maven",
38 "com.sun", "com.oracle", "repeat.repeat" );
40 private static final String OTHER_TEST_REPO = "other-repo";
42 public void testInstantiation()
44 assertFalse( action == lookup( Action.class, ACTION_HINT ) );
47 public void testBrowse()
50 metadataResolver.setNamespaces( TEST_REPO, GROUPS );
52 String result = action.browse();
53 assertSuccessResult( result );
55 assertEquals( Arrays.asList( "com", "commons-lang", "org.apache", "repeat.repeat" ), action.getNamespaces() );
56 assertNull( action.getProjectIds() );
57 assertNull( action.getProjectVersions() );
59 assertNull( action.getGroupId() );
60 assertNull( action.getArtifactId() );
61 assertNull( action.getRepositoryId() );
62 assertNull( action.getSharedModel() );
65 public void testBrowseNoObservableRepos()
68 setObservableRepos( Collections.<String>emptyList() );
70 String result = action.browse();
71 assertNoAccessResult( result );
73 assertNoOutputVariables();
76 public void testBrowseGroupNoObservableRepos()
79 setObservableRepos( Collections.<String>emptyList() );
80 String selectedGroupId = "org";
82 action.setGroupId( selectedGroupId );
83 String result = action.browseGroup();
84 assertNoAccessResult( result );
86 assertEquals( selectedGroupId, action.getGroupId() );
87 assertNull( action.getNamespaces() );
88 assertNull( action.getProjectIds() );
89 assertNull( action.getProjectVersions() );
90 assertNull( action.getArtifactId() );
91 assertNull( action.getRepositoryId() );
92 assertNull( action.getSharedModel() );
95 public void testBrowseArtifactNoObservableRepos()
98 setObservableRepos( Collections.<String>emptyList() );
99 String selectedGroupId = "org.apache";
100 String selectedArtifactId = "apache";
102 action.setGroupId( selectedGroupId );
103 action.setArtifactId( selectedArtifactId );
104 String result = action.browseArtifact();
105 assertNoAccessResult( result );
107 assertEquals( selectedGroupId, action.getGroupId() );
108 assertEquals( selectedArtifactId, action.getArtifactId() );
109 assertNull( action.getNamespaces() );
110 assertNull( action.getProjectIds() );
111 assertNull( action.getProjectVersions() );
112 assertNull( action.getRepositoryId() );
113 assertNull( action.getSharedModel() );
116 public void testBrowseGroupNoGroupId()
119 String result = action.browseGroup();
120 assertErrorResult( result );
121 assertNoOutputVariables();
124 public void testBrowseGroupNoArtifacts()
127 String selectedGroupId = "org";
128 List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache.maven" );
130 metadataResolver.setNamespaces( TEST_REPO, groups );
131 action.setGroupId( selectedGroupId );
132 String result = action.browseGroup();
133 assertSuccessResult( result );
135 assertEquals( Collections.singletonList( "org.apache" ), action.getNamespaces() );
136 assertEquals( Collections.<String>emptyList(), action.getProjectIds() );
137 assertNull( action.getProjectVersions() );
139 assertEquals( selectedGroupId, action.getGroupId() );
140 assertNull( action.getArtifactId() );
141 assertNull( action.getRepositoryId() );
142 assertNull( action.getSharedModel() );
145 public void testBrowseGroupWithArtifacts()
148 String artifacts = "apache";
149 String selectedGroupId = "org.apache";
150 List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache.maven" );
152 metadataResolver.setNamespaces( TEST_REPO, groups );
153 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, artifacts, new ProjectVersionMetadata() );
154 action.setGroupId( selectedGroupId );
155 String result = action.browseGroup();
156 assertSuccessResult( result );
158 assertEquals( groups, action.getNamespaces() );
159 assertEquals( Collections.singletonList( artifacts ), action.getProjectIds() );
160 assertNull( action.getProjectVersions() );
162 assertEquals( selectedGroupId, action.getGroupId() );
163 assertNull( action.getArtifactId() );
164 assertNull( action.getRepositoryId() );
165 assertNull( action.getSharedModel() );
168 public void testBrowseWithCollapsedGroupsAndArtifacts()
171 List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache" );
173 metadataResolver.setNamespaces( TEST_REPO, groups );
174 // add an artifact in the tree to make sure "single" is not collapsed
175 metadataResolver.setProjectVersion( TEST_REPO, "org.apache", "apache", new ProjectVersionMetadata() );
177 String result = action.browse();
178 assertSuccessResult( result );
180 assertEquals( Collections.singletonList( "org.apache" ), action.getNamespaces() );
181 assertNull( action.getProjectIds() );
182 assertNull( action.getProjectVersions() );
184 assertNull( action.getGroupId() );
185 assertNull( action.getArtifactId() );
186 assertNull( action.getRepositoryId() );
187 assertNull( action.getSharedModel() );
190 public void testBrowseWithCollapsedGroupsAndArtifactsAcrossRepositories()
193 setObservableRepos( Arrays.asList( TEST_REPO, OTHER_TEST_REPO ) );
195 metadataResolver.setNamespaces( TEST_REPO, Arrays.asList( "org.apache.archiva", "org.apache" ) );
196 metadataResolver.setNamespaces( OTHER_TEST_REPO, Arrays.asList( "org.codehaus.plexus", "org.codehaus" ) );
198 // add an artifact in the tree to make sure "single" is not collapsed
199 metadataResolver.setProjectVersion( TEST_REPO, "org.apache", "apache", new ProjectVersionMetadata() );
201 String result = action.browse();
202 assertSuccessResult( result );
204 assertEquals( Collections.singletonList( "org" ), action.getNamespaces() );
205 assertNull( action.getProjectIds() );
206 assertNull( action.getProjectVersions() );
208 assertNull( action.getGroupId() );
209 assertNull( action.getArtifactId() );
210 assertNull( action.getRepositoryId() );
211 assertNull( action.getSharedModel() );
214 public void testBrowseGroupWithCollapsedGroupsAndArtifacts()
217 String artifacts = "apache";
218 String selectedGroupId = "org.apache";
219 List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache" );
221 metadataResolver.setNamespaces( TEST_REPO, groups );
222 // add an artifact in the tree to make sure "single" is not collapsed
223 metadataResolver.setProjectVersion( TEST_REPO, "org.apache", "apache", new ProjectVersionMetadata() );
225 action.setGroupId( selectedGroupId );
226 String result = action.browseGroup();
227 assertSuccessResult( result );
229 assertEquals( Collections.singletonList( "org.apache.archiva" ), action.getNamespaces() );
230 assertEquals( Collections.singletonList( artifacts ), action.getProjectIds() );
231 assertNull( action.getProjectVersions() );
233 assertEquals( selectedGroupId, action.getGroupId() );
234 assertNull( action.getArtifactId() );
235 assertNull( action.getRepositoryId() );
236 assertNull( action.getSharedModel() );
239 public void testBrowseArtifactNoGroupId()
242 String selectedArtifactId = "apache";
244 action.setArtifactId( selectedArtifactId );
245 String result = action.browseArtifact();
246 assertErrorResult( result );
248 assertNull( action.getNamespaces() );
249 assertNull( action.getProjectIds() );
250 assertNull( action.getProjectVersions() );
251 assertNull( action.getGroupId() );
252 assertEquals( selectedArtifactId, action.getArtifactId() );
253 assertNull( action.getRepositoryId() );
254 assertNull( action.getSharedModel() );
257 public void testBrowseArtifactNoArtifactId()
260 String selectedGroupId = "org.apache";
262 action.setGroupId( selectedGroupId );
263 String result = action.browseArtifact();
264 assertErrorResult( result );
266 assertNull( action.getNamespaces() );
267 assertNull( action.getProjectIds() );
268 assertNull( action.getProjectVersions() );
269 assertEquals( selectedGroupId, action.getGroupId() );
270 assertNull( action.getArtifactId() );
271 assertNull( action.getRepositoryId() );
272 assertNull( action.getSharedModel() );
275 public void testBrowseArtifact()
279 String selectedGroupId = "org.apache";
280 String selectedArtifactId = "apache";
282 List<String> versions = Arrays.asList( "1", "2", "3", "4" );
283 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId, createProjectModel(
284 selectedGroupId, selectedArtifactId, "1" ) );
285 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId, createProjectModel(
286 selectedGroupId, selectedArtifactId, "2" ) );
287 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId, createProjectModel(
288 selectedGroupId, selectedArtifactId, "3" ) );
289 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId, createProjectModel(
290 selectedGroupId, selectedArtifactId, "4" ) );
292 action.setGroupId( selectedGroupId );
293 action.setArtifactId( selectedArtifactId );
294 String result = action.browseArtifact();
295 assertSuccessResult( result );
297 assertEquals( selectedGroupId, action.getGroupId() );
298 assertEquals( selectedArtifactId, action.getArtifactId() );
299 assertNull( action.getRepositoryId() );
301 assertNull( action.getNamespaces() );
302 assertNull( action.getProjectIds() );
303 assertEquals( versions, action.getProjectVersions() );
305 ProjectVersionMetadata model = action.getSharedModel();
306 assertDefaultModel( model, selectedGroupId, selectedArtifactId, null );
309 public void testBrowseArtifactWithSnapshots()
313 String selectedGroupId = "org.apache";
314 String selectedArtifactId = "apache";
316 List<String> versions = Arrays.asList( "1", "2", "3", "4-SNAPSHOT", "4", "5-SNAPSHOT" );
317 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId, createProjectModel(
318 selectedGroupId, selectedArtifactId, "1" ) );
319 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId, createProjectModel(
320 selectedGroupId, selectedArtifactId, "2" ) );
321 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId, createProjectModel(
322 selectedGroupId, selectedArtifactId, "3" ) );
323 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId, createProjectModel(
324 selectedGroupId, selectedArtifactId, "4-SNAPSHOT" ) );
325 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId, createProjectModel(
326 selectedGroupId, selectedArtifactId, "4" ) );
327 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId, createProjectModel(
328 selectedGroupId, selectedArtifactId, "5-SNAPSHOT" ) );
330 action.setGroupId( selectedGroupId );
331 action.setArtifactId( selectedArtifactId );
332 String result = action.browseArtifact();
333 assertSuccessResult( result );
335 assertEquals( selectedGroupId, action.getGroupId() );
336 assertEquals( selectedArtifactId, action.getArtifactId() );
337 assertNull( action.getRepositoryId() );
339 assertNull( action.getNamespaces() );
340 assertNull( action.getProjectIds() );
341 assertEquals( versions, action.getProjectVersions() );
343 ProjectVersionMetadata model = action.getSharedModel();
344 assertDefaultModel( model, selectedGroupId, selectedArtifactId, null );
347 // TODO: test with restricted observable repos
348 // TODO: current behaviour is to ignore values that differ between models - instead, pick the latest and use that.
349 // Need to update the tests to verify this as models are currently the same
351 private void assertNoAccessResult( String result )
353 assertEquals( GlobalResults.ACCESS_TO_NO_REPOS, result );
354 assertEquals( 0, action.getActionErrors().size() );
355 assertEquals( 0, action.getActionMessages().size() );
358 private void assertSuccessResult( String result )
360 assertEquals( Action.SUCCESS, result );
361 assertEquals( 0, action.getActionErrors().size() );
362 assertEquals( 0, action.getActionMessages().size() );
365 private void assertErrorResult( String result )
367 assertEquals( Action.ERROR, result );
368 assertEquals( 1, action.getActionErrors().size() );
369 assertEquals( 0, action.getActionMessages().size() );
372 private void assertNoOutputVariables()
374 assertNull( action.getNamespaces() );
375 assertNull( action.getProjectIds() );
376 assertNull( action.getProjectVersions() );
377 assertNull( action.getGroupId() );
378 assertNull( action.getArtifactId() );
379 assertNull( action.getRepositoryId() );
380 assertNull( action.getSharedModel() );
383 protected void setUp()
387 action = (BrowseAction) lookup( Action.class, ACTION_HINT );
388 metadataResolver = (TestMetadataResolver) action.getMetadataResolver();