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;
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 =
38 Arrays.asList( "org.apache.archiva", "commons-lang", "org.apache.maven", "com.sun", "com.oracle",
41 public void testInstantiation()
43 assertFalse( action == lookup( Action.class, ACTION_HINT ) );
46 public void testBrowse()
48 metadataResolver.setNamespaces( GROUPS );
50 String result = action.browse();
51 assertSuccessResult( result );
53 assertEquals( Arrays.asList( "com", "commons-lang", "org.apache", "repeat.repeat" ), action.getNamespaces() );
54 assertNull( action.getProjectIds() );
55 assertNull( action.getProjectVersions() );
57 assertNull( action.getGroupId() );
58 assertNull( action.getArtifactId() );
59 assertNull( action.getRepositoryId() );
60 assertNull( action.getSharedModel() );
63 public void testBrowseNoObservableRepos()
65 setObservableRepos( Collections.<String>emptyList() );
67 String result = action.browse();
68 assertNoAccessResult( result );
70 assertNoOutputVariables();
73 public void testBrowseGroupNoObservableRepos()
75 setObservableRepos( Collections.<String>emptyList() );
76 String selectedGroupId = "org";
78 action.setGroupId( selectedGroupId );
79 String result = action.browseGroup();
80 assertNoAccessResult( result );
82 assertEquals( selectedGroupId, action.getGroupId() );
83 assertNull( action.getNamespaces() );
84 assertNull( action.getProjectIds() );
85 assertNull( action.getProjectVersions() );
86 assertNull( action.getArtifactId() );
87 assertNull( action.getRepositoryId() );
88 assertNull( action.getSharedModel() );
91 public void testBrowseArtifactNoObservableRepos()
93 setObservableRepos( Collections.<String>emptyList() );
94 String selectedGroupId = "org.apache";
95 String selectedArtifactId = "apache";
97 action.setGroupId( selectedGroupId );
98 action.setArtifactId( selectedArtifactId );
99 String result = action.browseArtifact();
100 assertNoAccessResult( result );
102 assertEquals( selectedGroupId, action.getGroupId() );
103 assertEquals( selectedArtifactId, action.getArtifactId() );
104 assertNull( action.getNamespaces() );
105 assertNull( action.getProjectIds() );
106 assertNull( action.getProjectVersions() );
107 assertNull( action.getRepositoryId() );
108 assertNull( action.getSharedModel() );
111 public void testBrowseGroupNoGroupId()
113 String result = action.browseGroup();
114 assertErrorResult( result );
115 assertNoOutputVariables();
118 public void testBrowseGroupNoArtifacts()
120 String selectedGroupId = "org";
121 List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache.maven" );
123 metadataResolver.setNamespaces( groups );
124 action.setGroupId( selectedGroupId );
125 String result = action.browseGroup();
126 assertSuccessResult( result );
128 assertEquals( Collections.singletonList( "org.apache" ), action.getNamespaces() );
129 assertEquals( Collections.<String>emptyList(), action.getProjectIds() );
130 assertNull( action.getProjectVersions() );
132 assertEquals( selectedGroupId, action.getGroupId() );
133 assertNull( action.getArtifactId() );
134 assertNull( action.getRepositoryId() );
135 assertNull( action.getSharedModel() );
138 public void testBrowseGroupWithArtifacts()
140 String artifacts = "apache";
141 String selectedGroupId = "org.apache";
142 List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache.maven" );
144 metadataResolver.setNamespaces( groups );
145 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, artifacts, new ProjectVersionMetadata() );
146 action.setGroupId( selectedGroupId );
147 String result = action.browseGroup();
148 assertSuccessResult( result );
150 assertEquals( groups, action.getNamespaces() );
151 assertEquals( Collections.singletonList( artifacts ), action.getProjectIds() );
152 assertNull( action.getProjectVersions() );
154 assertEquals( selectedGroupId, action.getGroupId() );
155 assertNull( action.getArtifactId() );
156 assertNull( action.getRepositoryId() );
157 assertNull( action.getSharedModel() );
160 public void testBrowseWithCollapsedGroupsAndArtifacts()
162 List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache" );
164 metadataResolver.setNamespaces( groups );
165 // add an artifact in the tree to make sure "single" is not collapsed
166 metadataResolver.setProjectVersion( TEST_REPO, "org.apache", "apache", new ProjectVersionMetadata() );
168 String result = action.browse();
169 assertSuccessResult( result );
171 assertEquals( Collections.singletonList( "org.apache" ), action.getNamespaces() );
172 assertNull( action.getProjectIds() );
173 assertNull( action.getProjectVersions() );
175 assertNull( action.getGroupId() );
176 assertNull( action.getArtifactId() );
177 assertNull( action.getRepositoryId() );
178 assertNull( action.getSharedModel() );
181 public void testBrowseGroupWithCollapsedGroupsAndArtifacts()
183 String artifacts = "apache";
184 String selectedGroupId = "org.apache";
185 List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache" );
187 metadataResolver.setNamespaces( groups );
188 // add an artifact in the tree to make sure "single" is not collapsed
189 metadataResolver.setProjectVersion( TEST_REPO, "org.apache", "apache", new ProjectVersionMetadata() );
191 action.setGroupId( selectedGroupId );
192 String result = action.browseGroup();
193 assertSuccessResult( result );
195 assertEquals( Collections.singletonList( "org.apache.archiva" ), action.getNamespaces() );
196 assertEquals( Collections.singletonList( artifacts ), action.getProjectIds() );
197 assertNull( action.getProjectVersions() );
199 assertEquals( selectedGroupId, action.getGroupId() );
200 assertNull( action.getArtifactId() );
201 assertNull( action.getRepositoryId() );
202 assertNull( action.getSharedModel() );
205 public void testBrowseArtifactNoGroupId()
207 String selectedArtifactId = "apache";
209 action.setArtifactId( selectedArtifactId );
210 String result = action.browseArtifact();
211 assertErrorResult( result );
213 assertNull( action.getNamespaces() );
214 assertNull( action.getProjectIds() );
215 assertNull( action.getProjectVersions() );
216 assertNull( action.getGroupId() );
217 assertEquals( selectedArtifactId, action.getArtifactId() );
218 assertNull( action.getRepositoryId() );
219 assertNull( action.getSharedModel() );
222 public void testBrowseArtifactNoArtifactId()
224 String selectedGroupId = "org.apache";
226 action.setGroupId( selectedGroupId );
227 String result = action.browseArtifact();
228 assertErrorResult( result );
230 assertNull( action.getNamespaces() );
231 assertNull( action.getProjectIds() );
232 assertNull( action.getProjectVersions() );
233 assertEquals( selectedGroupId, action.getGroupId() );
234 assertNull( action.getArtifactId() );
235 assertNull( action.getRepositoryId() );
236 assertNull( action.getSharedModel() );
239 public void testBrowseArtifact()
241 String selectedGroupId = "org.apache";
242 String selectedArtifactId = "apache";
244 List<String> versions = Arrays.asList( "1", "2", "3", "4" );
245 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
246 createProjectModel( selectedGroupId, selectedArtifactId, "1" ) );
247 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
248 createProjectModel( selectedGroupId, selectedArtifactId, "2" ) );
249 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
250 createProjectModel( selectedGroupId, selectedArtifactId, "3" ) );
251 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
252 createProjectModel( selectedGroupId, selectedArtifactId, "4" ) );
254 action.setGroupId( selectedGroupId );
255 action.setArtifactId( selectedArtifactId );
256 String result = action.browseArtifact();
257 assertSuccessResult( result );
259 assertEquals( selectedGroupId, action.getGroupId() );
260 assertEquals( selectedArtifactId, action.getArtifactId() );
261 assertNull( action.getRepositoryId() );
263 assertNull( action.getNamespaces() );
264 assertNull( action.getProjectIds() );
265 assertEquals( versions, action.getProjectVersions() );
267 ProjectVersionMetadata model = action.getSharedModel();
268 assertDefaultModel( model, selectedGroupId, selectedArtifactId, null );
271 public void testBrowseArtifactWithSnapshots()
273 String selectedGroupId = "org.apache";
274 String selectedArtifactId = "apache";
276 List<String> versions = Arrays.asList( "1", "2", "3", "4-SNAPSHOT", "4", "5-SNAPSHOT" );
277 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
278 createProjectModel( selectedGroupId, selectedArtifactId, "1" ) );
279 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
280 createProjectModel( selectedGroupId, selectedArtifactId, "2" ) );
281 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
282 createProjectModel( selectedGroupId, selectedArtifactId, "3" ) );
283 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
284 createProjectModel( selectedGroupId, selectedArtifactId, "4-SNAPSHOT" ) );
285 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
286 createProjectModel( selectedGroupId, selectedArtifactId, "4" ) );
287 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
288 createProjectModel( selectedGroupId, selectedArtifactId, "5-SNAPSHOT" ) );
290 action.setGroupId( selectedGroupId );
291 action.setArtifactId( selectedArtifactId );
292 String result = action.browseArtifact();
293 assertSuccessResult( result );
295 assertEquals( selectedGroupId, action.getGroupId() );
296 assertEquals( selectedArtifactId, action.getArtifactId() );
297 assertNull( action.getRepositoryId() );
299 assertNull( action.getNamespaces() );
300 assertNull( action.getProjectIds() );
301 assertEquals( versions, action.getProjectVersions() );
303 ProjectVersionMetadata model = action.getSharedModel();
304 assertDefaultModel( model, selectedGroupId, selectedArtifactId, null );
307 // TODO: test with restricted observable repos
308 // TODO: current behaviour is to ignore values that differ between models - instead, pick the latest and use that.
309 // Need to update the tests to verify this as models are currently the same
311 private void assertNoAccessResult( String result )
313 assertEquals( GlobalResults.ACCESS_TO_NO_REPOS, result );
314 assertEquals( 0, action.getActionErrors().size() );
315 assertEquals( 0, action.getActionMessages().size() );
318 private void assertSuccessResult( String result )
320 assertEquals( Action.SUCCESS, result );
321 assertEquals( 0, action.getActionErrors().size() );
322 assertEquals( 0, action.getActionMessages().size() );
325 private void assertErrorResult( String result )
327 assertEquals( Action.ERROR, result );
328 assertEquals( 1, action.getActionErrors().size() );
329 assertEquals( 0, action.getActionMessages().size() );
332 private void assertNoOutputVariables()
334 assertNull( action.getNamespaces() );
335 assertNull( action.getProjectIds() );
336 assertNull( action.getProjectVersions() );
337 assertNull( action.getGroupId() );
338 assertNull( action.getArtifactId() );
339 assertNull( action.getRepositoryId() );
340 assertNull( action.getSharedModel() );
343 protected void setUp()
347 action = (BrowseAction) lookup( Action.class, ACTION_HINT );
348 metadataResolver = (TestMetadataResolver) action.getMetadataResolver();