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.MetadataResolverException;
29 import org.apache.archiva.metadata.repository.memory.TestMetadataResolver;
31 public class BrowseActionTest
32 extends AbstractActionTestCase
34 private static final String ACTION_HINT = "browseAction";
36 private BrowseAction action;
38 private static final List<String> GROUPS =
39 Arrays.asList( "org.apache.archiva", "commons-lang", "org.apache.maven", "com.sun", "com.oracle",
42 public void testInstantiation()
44 assertFalse( action == lookup( Action.class, ACTION_HINT ) );
47 public void testBrowse()
49 metadataResolver.setNamespaces( GROUPS );
51 String result = action.browse();
52 assertSuccessResult( result );
54 assertEquals( Arrays.asList( "com", "commons-lang", "org.apache", "repeat.repeat" ), action.getNamespaces() );
55 assertNull( action.getProjectIds() );
56 assertNull( action.getProjectVersions() );
58 assertNull( action.getGroupId() );
59 assertNull( action.getArtifactId() );
60 assertNull( action.getRepositoryId() );
61 assertNull( action.getSharedModel() );
64 public void testBrowseNoObservableRepos()
66 setObservableRepos( Collections.<String>emptyList() );
68 String result = action.browse();
69 assertNoAccessResult( result );
71 assertNoOutputVariables();
74 public void testBrowseGroupNoObservableRepos()
76 setObservableRepos( Collections.<String>emptyList() );
77 String selectedGroupId = "org";
79 action.setGroupId( selectedGroupId );
80 String result = action.browseGroup();
81 assertNoAccessResult( result );
83 assertEquals( selectedGroupId, action.getGroupId() );
84 assertNull( action.getNamespaces() );
85 assertNull( action.getProjectIds() );
86 assertNull( action.getProjectVersions() );
87 assertNull( action.getArtifactId() );
88 assertNull( action.getRepositoryId() );
89 assertNull( action.getSharedModel() );
92 public void testBrowseArtifactNoObservableRepos()
93 throws MetadataResolverException
95 setObservableRepos( Collections.<String>emptyList() );
96 String selectedGroupId = "org.apache";
97 String selectedArtifactId = "apache";
99 action.setGroupId( selectedGroupId );
100 action.setArtifactId( selectedArtifactId );
101 String result = action.browseArtifact();
102 assertNoAccessResult( result );
104 assertEquals( selectedGroupId, action.getGroupId() );
105 assertEquals( selectedArtifactId, action.getArtifactId() );
106 assertNull( action.getNamespaces() );
107 assertNull( action.getProjectIds() );
108 assertNull( action.getProjectVersions() );
109 assertNull( action.getRepositoryId() );
110 assertNull( action.getSharedModel() );
113 public void testBrowseGroupNoGroupId()
115 String result = action.browseGroup();
116 assertErrorResult( result );
117 assertNoOutputVariables();
120 public void testBrowseGroupNoArtifacts()
122 String selectedGroupId = "org";
123 List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache.maven" );
125 metadataResolver.setNamespaces( groups );
126 action.setGroupId( selectedGroupId );
127 String result = action.browseGroup();
128 assertSuccessResult( result );
130 assertEquals( Collections.singletonList( "org.apache" ), action.getNamespaces() );
131 assertEquals( Collections.<String>emptyList(), action.getProjectIds() );
132 assertNull( action.getProjectVersions() );
134 assertEquals( selectedGroupId, action.getGroupId() );
135 assertNull( action.getArtifactId() );
136 assertNull( action.getRepositoryId() );
137 assertNull( action.getSharedModel() );
140 public void testBrowseGroupWithArtifacts()
142 String artifacts = "apache";
143 String selectedGroupId = "org.apache";
144 List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache.maven" );
146 metadataResolver.setNamespaces( groups );
147 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, artifacts, new ProjectVersionMetadata() );
148 action.setGroupId( selectedGroupId );
149 String result = action.browseGroup();
150 assertSuccessResult( result );
152 assertEquals( groups, action.getNamespaces() );
153 assertEquals( Collections.singletonList( artifacts ), action.getProjectIds() );
154 assertNull( action.getProjectVersions() );
156 assertEquals( selectedGroupId, action.getGroupId() );
157 assertNull( action.getArtifactId() );
158 assertNull( action.getRepositoryId() );
159 assertNull( action.getSharedModel() );
162 public void testBrowseWithCollapsedGroupsAndArtifacts()
164 List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache" );
166 metadataResolver.setNamespaces( groups );
167 // add an artifact in the tree to make sure "single" is not collapsed
168 metadataResolver.setProjectVersion( TEST_REPO, "org.apache", "apache", new ProjectVersionMetadata() );
170 String result = action.browse();
171 assertSuccessResult( result );
173 assertEquals( Collections.singletonList( "org.apache" ), action.getNamespaces() );
174 assertNull( action.getProjectIds() );
175 assertNull( action.getProjectVersions() );
177 assertNull( action.getGroupId() );
178 assertNull( action.getArtifactId() );
179 assertNull( action.getRepositoryId() );
180 assertNull( action.getSharedModel() );
183 public void testBrowseGroupWithCollapsedGroupsAndArtifacts()
185 String artifacts = "apache";
186 String selectedGroupId = "org.apache";
187 List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache" );
189 metadataResolver.setNamespaces( groups );
190 // add an artifact in the tree to make sure "single" is not collapsed
191 metadataResolver.setProjectVersion( TEST_REPO, "org.apache", "apache", new ProjectVersionMetadata() );
193 action.setGroupId( selectedGroupId );
194 String result = action.browseGroup();
195 assertSuccessResult( result );
197 assertEquals( Collections.singletonList( "org.apache.archiva" ), action.getNamespaces() );
198 assertEquals( Collections.singletonList( artifacts ), action.getProjectIds() );
199 assertNull( action.getProjectVersions() );
201 assertEquals( selectedGroupId, action.getGroupId() );
202 assertNull( action.getArtifactId() );
203 assertNull( action.getRepositoryId() );
204 assertNull( action.getSharedModel() );
207 public void testBrowseArtifactNoGroupId()
208 throws MetadataResolverException
210 String selectedArtifactId = "apache";
212 action.setArtifactId( selectedArtifactId );
213 String result = action.browseArtifact();
214 assertErrorResult( result );
216 assertNull( action.getNamespaces() );
217 assertNull( action.getProjectIds() );
218 assertNull( action.getProjectVersions() );
219 assertNull( action.getGroupId() );
220 assertEquals( selectedArtifactId, action.getArtifactId() );
221 assertNull( action.getRepositoryId() );
222 assertNull( action.getSharedModel() );
225 public void testBrowseArtifactNoArtifactId()
226 throws MetadataResolverException
228 String selectedGroupId = "org.apache";
230 action.setGroupId( selectedGroupId );
231 String result = action.browseArtifact();
232 assertErrorResult( result );
234 assertNull( action.getNamespaces() );
235 assertNull( action.getProjectIds() );
236 assertNull( action.getProjectVersions() );
237 assertEquals( selectedGroupId, action.getGroupId() );
238 assertNull( action.getArtifactId() );
239 assertNull( action.getRepositoryId() );
240 assertNull( action.getSharedModel() );
243 public void testBrowseArtifact()
244 throws MetadataResolverException
246 String selectedGroupId = "org.apache";
247 String selectedArtifactId = "apache";
249 List<String> versions = Arrays.asList( "1", "2", "3", "4" );
250 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
251 createProjectModel( selectedGroupId, selectedArtifactId, "1" ) );
252 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
253 createProjectModel( selectedGroupId, selectedArtifactId, "2" ) );
254 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
255 createProjectModel( selectedGroupId, selectedArtifactId, "3" ) );
256 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
257 createProjectModel( selectedGroupId, selectedArtifactId, "4" ) );
259 action.setGroupId( selectedGroupId );
260 action.setArtifactId( selectedArtifactId );
261 String result = action.browseArtifact();
262 assertSuccessResult( result );
264 assertEquals( selectedGroupId, action.getGroupId() );
265 assertEquals( selectedArtifactId, action.getArtifactId() );
266 assertNull( action.getRepositoryId() );
268 assertNull( action.getNamespaces() );
269 assertNull( action.getProjectIds() );
270 assertEquals( versions, action.getProjectVersions() );
272 ProjectVersionMetadata model = action.getSharedModel();
273 assertDefaultModel( model, selectedGroupId, selectedArtifactId, null );
276 public void testBrowseArtifactWithSnapshots()
277 throws MetadataResolverException
279 String selectedGroupId = "org.apache";
280 String selectedArtifactId = "apache";
282 List<String> versions = Arrays.asList( "1", "2", "3", "4-SNAPSHOT", "4", "5-SNAPSHOT" );
283 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
284 createProjectModel( selectedGroupId, selectedArtifactId, "1" ) );
285 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
286 createProjectModel( selectedGroupId, selectedArtifactId, "2" ) );
287 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
288 createProjectModel( selectedGroupId, selectedArtifactId, "3" ) );
289 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
290 createProjectModel( selectedGroupId, selectedArtifactId, "4-SNAPSHOT" ) );
291 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
292 createProjectModel( selectedGroupId, selectedArtifactId, "4" ) );
293 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
294 createProjectModel( selectedGroupId, selectedArtifactId, "5-SNAPSHOT" ) );
296 action.setGroupId( selectedGroupId );
297 action.setArtifactId( selectedArtifactId );
298 String result = action.browseArtifact();
299 assertSuccessResult( result );
301 assertEquals( selectedGroupId, action.getGroupId() );
302 assertEquals( selectedArtifactId, action.getArtifactId() );
303 assertNull( action.getRepositoryId() );
305 assertNull( action.getNamespaces() );
306 assertNull( action.getProjectIds() );
307 assertEquals( versions, action.getProjectVersions() );
309 ProjectVersionMetadata model = action.getSharedModel();
310 assertDefaultModel( model, selectedGroupId, selectedArtifactId, null );
313 // TODO: test with restricted observable repos
314 // TODO: current behaviour is to ignore values that differ between models - instead, pick the latest and use that.
315 // Need to update the tests to verify this as models are currently the same
317 private void assertNoAccessResult( String result )
319 assertEquals( GlobalResults.ACCESS_TO_NO_REPOS, result );
320 assertEquals( 0, action.getActionErrors().size() );
321 assertEquals( 0, action.getActionMessages().size() );
324 private void assertSuccessResult( String result )
326 assertEquals( Action.SUCCESS, result );
327 assertEquals( 0, action.getActionErrors().size() );
328 assertEquals( 0, action.getActionMessages().size() );
331 private void assertErrorResult( String result )
333 assertEquals( Action.ERROR, result );
334 assertEquals( 1, action.getActionErrors().size() );
335 assertEquals( 0, action.getActionMessages().size() );
338 private void assertNoOutputVariables()
340 assertNull( action.getNamespaces() );
341 assertNull( action.getProjectIds() );
342 assertNull( action.getProjectVersions() );
343 assertNull( action.getGroupId() );
344 assertNull( action.getArtifactId() );
345 assertNull( action.getRepositoryId() );
346 assertNull( action.getSharedModel() );
349 protected void setUp()
353 action = (BrowseAction) lookup( Action.class, ACTION_HINT );
354 metadataResolver = (TestMetadataResolver) action.getMetadataResolver();