1 package org.apache.archiva.web.xmlrpc.services;
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
24 import java.util.ArrayList;
25 import java.util.Date;
26 import java.util.List;
28 import org.apache.archiva.web.xmlrpc.api.SearchService;
29 import org.apache.archiva.web.xmlrpc.api.beans.Artifact;
30 import org.apache.archiva.web.xmlrpc.api.beans.Dependency;
31 import org.apache.archiva.web.xmlrpc.security.XmlRpcAuthenticator;
32 import org.apache.archiva.web.xmlrpc.security.XmlRpcUserRepositories;
33 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
34 import org.apache.maven.archiva.configuration.Configuration;
35 import org.apache.maven.archiva.configuration.FileTypes;
36 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
37 import org.apache.maven.archiva.database.ArchivaDAO;
38 import org.apache.maven.archiva.database.ArtifactDAO;
39 import org.apache.maven.archiva.database.ObjectNotFoundException;
40 import org.apache.maven.archiva.database.browsing.BrowsingResults;
41 import org.apache.maven.archiva.database.browsing.RepositoryBrowsing;
42 import org.apache.maven.archiva.database.constraints.ArtifactsByChecksumConstraint;
43 import org.apache.maven.archiva.indexer.filecontent.FileContentRecord;
44 import org.apache.maven.archiva.indexer.lucene.LuceneRepositoryContentRecord;
45 import org.apache.maven.archiva.indexer.search.CrossRepositorySearch;
46 import org.apache.maven.archiva.indexer.search.SearchResultLimits;
47 import org.apache.maven.archiva.indexer.search.SearchResults;
48 import org.apache.maven.archiva.model.ArchivaArtifact;
49 import org.apache.maven.archiva.model.ArchivaArtifactModel;
50 import org.apache.maven.archiva.model.ArchivaProjectModel;
51 import org.apache.maven.archiva.model.ArtifactReference;
52 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
53 import org.apache.maven.archiva.repository.RepositoryContentFactory;
54 import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent;
55 import org.apache.maven.archiva.repository.content.PathParser;
56 import org.apache.maven.archiva.security.ArchivaRoleConstants;
57 import org.apache.xmlrpc.XmlRpcRequest;
58 import org.apache.xmlrpc.common.XmlRpcHttpRequestConfigImpl;
59 import org.codehaus.plexus.redback.role.RoleManager;
60 import org.codehaus.plexus.redback.system.SecuritySystem;
61 import org.codehaus.plexus.redback.users.User;
62 import org.codehaus.plexus.redback.users.UserManager;
63 import org.codehaus.plexus.redback.users.UserNotFoundException;
64 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
65 import org.easymock.ArgumentsMatcher;
66 import org.easymock.MockControl;
67 import org.easymock.classextension.MockClassControl;
69 import sun.security.action.GetLongAction;
72 * SearchServiceImplTest
74 * @version $Id: SearchServiceImplTest.java
76 public class SearchServiceImplTest
77 extends PlexusInSpringTestCase
79 private SearchService searchService;
81 private MockControl userReposControl;
83 private XmlRpcUserRepositories userRepos;
85 private MockControl crossRepoSearchControl;
87 private CrossRepositorySearch crossRepoSearch;
89 private MockControl archivaDAOControl;
91 private ArchivaDAO archivaDAO;
93 private MockControl artifactDAOControl;
95 private ArtifactDAO artifactDAO;
97 private MockControl repoBrowsingControl;
99 private RepositoryBrowsing repoBrowsing;
104 userReposControl = MockClassControl.createControl( XmlRpcUserRepositories.class );
105 userRepos = ( XmlRpcUserRepositories ) userReposControl.getMock();
107 crossRepoSearchControl = MockControl.createControl( CrossRepositorySearch.class );
108 crossRepoSearch = ( CrossRepositorySearch ) crossRepoSearchControl.getMock();
110 archivaDAOControl = MockControl.createControl( ArchivaDAO.class );
111 archivaDAO = ( ArchivaDAO ) archivaDAOControl.getMock();
113 repoBrowsingControl = MockControl.createControl( RepositoryBrowsing.class );
114 repoBrowsing = ( RepositoryBrowsing ) repoBrowsingControl.getMock();
116 searchService = new SearchServiceImpl( userRepos, crossRepoSearch, archivaDAO, repoBrowsing );
118 artifactDAOControl = MockControl.createControl( ArtifactDAO.class );
119 artifactDAO = ( ArtifactDAO ) artifactDAOControl.getMock();
123 * quick/general text search which returns a list of artifacts
124 * query for an artifact based on a checksum
125 * query for all available versions of an artifact, sorted in version significance order
126 * query for all available versions of an artifact since a given date
127 * query for an artifact's direct dependencies
128 * query for an artifact's dependency tree (as with mvn dependency:tree - no duplicates should be included)
129 * query for all artifacts that depend on a given artifact
134 public void testQuickSearchArtifactBytecodeSearch()
137 // 1. check whether bytecode search or ordinary search
138 // 2. get observable repos
139 // 3. convert results to a list of Artifact objects
141 List<String> observableRepoIds = new ArrayList<String>();
142 observableRepoIds.add( "repo1.mirror" );
143 observableRepoIds.add( "public.releases" );
145 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
147 Date whenGathered = new Date();
148 SearchResults results = new SearchResults();
149 ArchivaArtifact artifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", "1.0", "", "jar" );
150 artifact.getModel().setWhenGathered( whenGathered );
152 FileContentRecord record = new FileContentRecord();
153 record.setRepositoryId( "repo1.mirror" );
154 record.setArtifact( artifact );
155 record.setContents( "org.apache.archiva:archiva-test:1.0:jar org.apache.archiva.test.MyClassName" );
156 record.setFilename( "archiva-test-1.0.jar" );
158 results.addHit( record );
160 SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
162 crossRepoSearchControl.expectAndDefaultReturn(
163 crossRepoSearch.searchForBytecode( "", observableRepoIds, "MyClassName", limits ), results );
165 archivaDAOControl.expectAndReturn( archivaDAO.getArtifactDAO(), artifactDAO );
166 artifactDAOControl.expectAndReturn( artifactDAO.getArtifact( "org.apache.archiva", "archiva-test", "1.0", "", "pom" ), artifact );
168 userReposControl.replay();
169 crossRepoSearchControl.replay();
170 archivaDAOControl.replay();
171 artifactDAOControl.replay();
173 List<Artifact> artifacts = searchService.quickSearch( "bytecode:MyClassName" );
175 userReposControl.verify();
176 crossRepoSearchControl.verify();
177 archivaDAOControl.verify();
178 artifactDAOControl.verify();
180 assertNotNull( artifacts );
181 assertEquals( 1, artifacts.size() );
184 public void testQuickSearchArtifactRegularSearch()
187 List<String> observableRepoIds = new ArrayList<String>();
188 observableRepoIds.add( "repo1.mirror" );
189 observableRepoIds.add( "public.releases" );
191 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
193 Date whenGathered = new Date();
194 SearchResults results = new SearchResults();
195 ArchivaArtifact artifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", "1.0", "", "jar" );
196 artifact.getModel().setWhenGathered( whenGathered );
198 FileContentRecord record = new FileContentRecord();
199 record.setRepositoryId( "repo1.mirror" );
200 record.setArtifact( artifact );
201 record.setContents( "org.apache.archiva:archiva-test:1.0:jar" );
202 record.setFilename( "archiva-test-1.0.jar" );
204 results.addHit( record );
206 SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
208 crossRepoSearchControl.expectAndDefaultReturn(
209 crossRepoSearch.searchForTerm( "", observableRepoIds, "archiva", limits ), results );
211 archivaDAOControl.expectAndReturn( archivaDAO.getArtifactDAO(), artifactDAO );
212 artifactDAOControl.expectAndReturn( artifactDAO.getArtifact( "org.apache.archiva", "archiva-test", "1.0", "", "pom" ), artifact );
214 userReposControl.replay();
215 crossRepoSearchControl.replay();
216 archivaDAOControl.replay();
217 artifactDAOControl.replay();
219 List<Artifact> artifacts = searchService.quickSearch( "archiva" );
221 userReposControl.verify();
222 crossRepoSearchControl.verify();
223 archivaDAOControl.verify();
224 artifactDAOControl.verify();
226 assertNotNull( artifacts );
227 assertEquals( 1, artifacts.size() );
230 /* query artifact by checksum */
232 public void testGetArtifactByChecksum()
235 Date whenGathered = new Date();
237 ArtifactsByChecksumConstraint constraint = new ArtifactsByChecksumConstraint( "a1b2c3aksjhdasfkdasasd" );
238 List<ArchivaArtifact> artifacts = new ArrayList<ArchivaArtifact>();
239 ArchivaArtifact artifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", "1.0", "", "jar" );
240 artifact.getModel().setWhenGathered( whenGathered );
241 artifacts.add( artifact );
243 archivaDAOControl.expectAndReturn( archivaDAO.getArtifactDAO(), artifactDAO );
244 artifactDAO.queryArtifacts( constraint );
245 artifactDAOControl.setMatcher( MockControl.ALWAYS_MATCHER );
246 artifactDAOControl.setReturnValue( artifacts );
248 archivaDAOControl.replay();
249 artifactDAOControl.replay();
251 List<Artifact> results = searchService.getArtifactByChecksum( "a1b2c3aksjhdasfkdasasd" );
253 archivaDAOControl.verify();
254 artifactDAOControl.verify();
256 assertNotNull( results );
257 assertEquals( 1, results.size() );
260 /* query artifact versions */
262 public void testGetArtifactVersionsArtifactExists()
265 Date whenGathered = new Date();
267 List<String> observableRepoIds = new ArrayList<String>();
268 observableRepoIds.add( "repo1.mirror" );
269 observableRepoIds.add( "public.releases" );
271 List<String> versions = new ArrayList<String>();
272 versions.add( "1.0" );
273 versions.add( "1.1-beta-1" );
274 versions.add( "1.1-beta-2" );
275 versions.add( "1.1" );
276 versions.add( "1.2" );
277 versions.add( "1.2.1-SNAPSHOT" );
279 BrowsingResults results = new BrowsingResults( "org.apache.archiva", "archiva-test" );
280 results.setSelectedRepositoryIds( observableRepoIds );
281 results.setVersions( versions );
283 List<ArchivaArtifact> archivaArtifacts = new ArrayList<ArchivaArtifact>();
284 ArchivaArtifact archivaArtifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", versions.get( 0 ), "", "pom" );
285 archivaArtifact.getModel().setWhenGathered( whenGathered );
286 archivaArtifacts.add( archivaArtifact );
288 archivaArtifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", versions.get( 1 ), "", "pom" );
289 archivaArtifact.getModel().setWhenGathered( whenGathered );
290 archivaArtifacts.add( archivaArtifact );
292 archivaArtifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", versions.get( 2 ), "", "pom" );
293 archivaArtifact.getModel().setWhenGathered( whenGathered );
294 archivaArtifacts.add( archivaArtifact );
296 archivaArtifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", versions.get( 3 ), "", "pom" );
297 archivaArtifact.getModel().setWhenGathered( whenGathered );
298 archivaArtifacts.add( archivaArtifact );
300 archivaArtifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", versions.get( 4 ), "", "pom" );
301 archivaArtifact.getModel().setWhenGathered( whenGathered );
302 archivaArtifacts.add( archivaArtifact );
304 archivaArtifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", versions.get( 5 ), "", "pom" );
305 archivaArtifact.getModel().setWhenGathered( whenGathered );
306 archivaArtifacts.add( archivaArtifact );
308 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
309 repoBrowsingControl.expectAndReturn( repoBrowsing.selectArtifactId( "", observableRepoIds, "org.apache.archiva", "archiva-test" ), results );
310 archivaDAOControl.expectAndReturn( archivaDAO.getArtifactDAO(), artifactDAO );
312 artifactDAOControl.expectAndDefaultReturn( artifactDAO.getArtifact( "org.apache.archiva", "archiva-test", versions.get( 0 ), "", "pom" ), archivaArtifacts.get( 0 ) );
313 artifactDAOControl.expectAndDefaultReturn( artifactDAO.getArtifact( "org.apache.archiva", "archiva-test", versions.get( 1 ), "", "pom" ), archivaArtifacts.get( 1 ) );
314 artifactDAOControl.expectAndDefaultReturn( artifactDAO.getArtifact( "org.apache.archiva", "archiva-test", versions.get( 2 ), "", "pom" ), archivaArtifacts.get( 2 ) );
315 artifactDAOControl.expectAndDefaultReturn( artifactDAO.getArtifact( "org.apache.archiva", "archiva-test", versions.get( 3 ), "", "pom" ), archivaArtifacts.get( 3 ) );
316 artifactDAOControl.expectAndDefaultReturn( artifactDAO.getArtifact( "org.apache.archiva", "archiva-test", versions.get( 4 ), "", "pom" ), archivaArtifacts.get( 4 ) );
317 artifactDAOControl.expectAndDefaultReturn( artifactDAO.getArtifact( "org.apache.archiva", "archiva-test", versions.get( 5 ), "", "pom" ), archivaArtifacts.get( 5 ) );
319 userReposControl.replay();
320 repoBrowsingControl.replay();
321 archivaDAOControl.replay();
322 artifactDAOControl.replay();
324 List<Artifact> artifacts = searchService.getArtifactVersions( "org.apache.archiva", "archiva-test" );
326 userReposControl.verify();
327 repoBrowsingControl.verify();
328 archivaDAOControl.verify();
329 artifactDAOControl.verify();
331 assertNotNull( artifacts );
332 assertEquals( 6, artifacts.size() );
335 /* query artifact versions since a given date */
337 public void testGetArtifactVersionsByDateArtifactExists()
343 public void testGetArtifactVersionsByDateArtifactDoesNotExist()
349 /* query artifact dependencies */
351 public void testGetDependenciesArtifactExists()
354 List<String> observableRepoIds = new ArrayList<String>();
355 observableRepoIds.add( "repo1.mirror" );
356 observableRepoIds.add( "public.releases" );
358 ArchivaProjectModel model = new ArchivaProjectModel();
359 model.setGroupId( "org.apache.archiva" );
360 model.setArtifactId( "archiva-test" );
361 model.setVersion( "1.0" );
363 org.apache.maven.archiva.model.Dependency dependency = new org.apache.maven.archiva.model.Dependency();
364 dependency.setGroupId( "commons-logging" );
365 dependency.setArtifactId( "commons-logging" );
366 dependency.setVersion( "2.0" );
368 model.addDependency( dependency );
370 dependency = new org.apache.maven.archiva.model.Dependency();
371 dependency.setGroupId( "junit" );
372 dependency.setArtifactId( "junit" );
373 dependency.setVersion( "2.4" );
374 dependency.setScope( "test" );
376 model.addDependency( dependency );
378 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
379 repoBrowsingControl.expectAndReturn(
380 repoBrowsing.selectVersion( "", observableRepoIds, "org.apache.archiva", "archiva-test", "1.0" ), model );
382 repoBrowsingControl.replay();
383 userReposControl.replay();
385 List<Dependency> dependencies = searchService.getDependencies( "org.apache.archiva", "archiva-test", "1.0" );
387 repoBrowsingControl.verify();
388 userReposControl.verify();
390 assertNotNull( dependencies );
391 assertEquals( 2, dependencies.size() );
394 public void testGetDependenciesArtifactDoesNotExist()
397 List<String> observableRepoIds = new ArrayList<String>();
398 observableRepoIds.add( "repo1.mirror" );
399 observableRepoIds.add( "public.releases" );
401 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
402 repoBrowsingControl.expectAndThrow(
403 repoBrowsing.selectVersion( "", observableRepoIds, "org.apache.archiva", "archiva-test", "1.0" ), new ObjectNotFoundException( "Artifact does not exist." ) );
405 userReposControl.replay();
406 repoBrowsingControl.replay();
410 List<Dependency> dependencies = searchService.getDependencies( "org.apache.archiva", "archiva-test", "1.0" );
411 fail( "An exception should have been thrown." );
413 catch ( Exception e )
415 assertEquals( "Artifact does not exist.", e.getMessage() );
418 userReposControl.verify();
419 repoBrowsingControl.verify();
422 /* get dependency tree */
424 public void testGetDependencyTreeArtifactExists()
430 public void testGetDependencyTreeArtifactDoesNotExist()
438 public void testGetDependees()
441 Date date = new Date();
442 List<String> observableRepoIds = new ArrayList<String>();
443 observableRepoIds.add( "repo1.mirror" );
444 observableRepoIds.add( "public.releases" );
446 List dependeeModels = new ArrayList();
447 ArchivaProjectModel dependeeModel = new ArchivaProjectModel();
448 dependeeModel.setGroupId( "org.apache.archiva" );
449 dependeeModel.setArtifactId( "archiva-dependee-one" );
450 dependeeModel.setVersion( "1.0" );
451 dependeeModel.setWhenIndexed( date );
452 dependeeModels.add( dependeeModel );
454 dependeeModel = new ArchivaProjectModel();
455 dependeeModel.setGroupId( "org.apache.archiva" );
456 dependeeModel.setArtifactId( "archiva-dependee-two" );
457 dependeeModel.setVersion( "1.0" );
458 dependeeModel.setWhenIndexed( date );
459 dependeeModels.add( dependeeModel );
461 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
462 repoBrowsingControl.expectAndReturn( repoBrowsing.getUsedBy( "", observableRepoIds, "org.apache.archiva", "archiva-test", "1.0" ), dependeeModels );
464 repoBrowsingControl.replay();
465 userReposControl.replay();
467 List<Artifact> dependees = searchService.getDependees( "org.apache.archiva", "archiva-test", "1.0" );
469 repoBrowsingControl.verify();
470 userReposControl.verify();
472 assertNotNull( dependees );
473 assertEquals( 2, dependees.size() );
476 /*public void testGetDependeesArtifactDoesNotExist()
479 Date date = new Date();
480 List<String> observableRepoIds = new ArrayList<String>();
481 observableRepoIds.add( "repo1.mirror" );
482 observableRepoIds.add( "public.releases" );
484 List dependeeModels = new ArrayList();
485 ArchivaProjectModel dependeeModel = new ArchivaProjectModel();
486 dependeeModel.setGroupId( "org.apache.archiva" );
487 dependeeModel.setArtifactId( "archiva-dependee-one" );
488 dependeeModel.setVersion( "1.0" );
489 dependeeModel.setWhenIndexed( date );
490 dependeeModels.add( dependeeModel );
492 dependeeModel = new ArchivaProjectModel();
493 dependeeModel.setGroupId( "org.apache.archiva" );
494 dependeeModel.setArtifactId( "archiva-dependee-two" );
495 dependeeModel.setVersion( "1.0" );
496 dependeeModel.setWhenIndexed( date );
497 dependeeModels.add( dependeeModel );
499 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
500 repoBrowsingControl.expectAndReturn( repoBrowsing.getUsedBy( "", observableRepoIds, "org.apache.archiva", "archiva-test", "1.0" ), dependeeModels );
502 repoBrowsingControl.replay();
503 userReposControl.replay();
507 List<Artifact> dependees = searchService.getDependees( "org.apache.archiva", "archiva-test", "1.0" );
508 fail( "An exception should have been thrown." );
510 catch ( Exception e )
512 assertEquals( "Artifact does not exist." )
515 repoBrowsingControl.verify();
516 userReposControl.verify();
518 assertNotNull( dependees );
519 assertEquals( 2, dependees.size() );