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
23 import java.util.ArrayList;
24 import java.util.Date;
25 import java.util.List;
27 import org.apache.archiva.indexer.search.RepositorySearch;
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.XmlRpcUserRepositories;
32 import org.apache.maven.archiva.database.ArchivaDAO;
33 import org.apache.maven.archiva.database.ArtifactDAO;
34 import org.apache.maven.archiva.database.ObjectNotFoundException;
35 import org.apache.maven.archiva.database.browsing.BrowsingResults;
36 import org.apache.maven.archiva.database.browsing.RepositoryBrowsing;
37 import org.apache.maven.archiva.database.constraints.ArtifactsByChecksumConstraint;
38 import org.apache.maven.archiva.model.ArchivaArtifact;
39 import org.apache.maven.archiva.model.ArchivaProjectModel;
40 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
41 import org.easymock.MockControl;
42 import org.easymock.classextension.MockClassControl;
45 * SearchServiceImplTest
47 * @version $Id: SearchServiceImplTest.java
49 public class SearchServiceImplTest
50 extends PlexusInSpringTestCase
52 private SearchService searchService;
54 private MockControl userReposControl;
56 private XmlRpcUserRepositories userRepos;
58 private MockControl searchControl;
60 private RepositorySearch search;
62 private MockControl archivaDAOControl;
64 private ArchivaDAO archivaDAO;
66 private MockControl artifactDAOControl;
68 private ArtifactDAO artifactDAO;
70 private MockControl repoBrowsingControl;
72 private RepositoryBrowsing repoBrowsing;
78 userReposControl = MockClassControl.createControl( XmlRpcUserRepositories.class );
79 userRepos = ( XmlRpcUserRepositories ) userReposControl.getMock();
81 archivaDAOControl = MockControl.createControl( ArchivaDAO.class );
82 archivaDAO = ( ArchivaDAO ) archivaDAOControl.getMock();
84 repoBrowsingControl = MockControl.createControl( RepositoryBrowsing.class );
85 repoBrowsing = ( RepositoryBrowsing ) repoBrowsingControl.getMock();
87 searchControl = MockControl.createControl( RepositorySearch.class );
88 search = ( RepositorySearch ) searchControl.getMock();
90 searchService = new SearchServiceImpl( userRepos, archivaDAO, repoBrowsing, search );
92 artifactDAOControl = MockControl.createControl( ArtifactDAO.class );
93 artifactDAO = ( ArtifactDAO ) artifactDAOControl.getMock();
97 * quick/general text search which returns a list of artifacts
98 * query for an artifact based on a checksum
99 * query for all available versions of an artifact, sorted in version significance order
100 * query for all available versions of an artifact since a given date
101 * query for an artifact's direct dependencies
102 * query for an artifact's dependency tree (as with mvn dependency:tree - no duplicates should be included)
103 * query for all artifacts that depend on a given artifact
108 // public void testQuickSearchArtifactBytecodeSearch()
111 // // 1. check whether bytecode search or ordinary search
112 // // 2. get observable repos
113 // // 3. convert results to a list of Artifact objects
115 // List<String> observableRepoIds = new ArrayList<String>();
116 // observableRepoIds.add( "repo1.mirror" );
117 // observableRepoIds.add( "public.releases" );
119 // userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
121 // Date whenGathered = new Date();
122 // SearchResults results = new SearchResults();
123 // ArchivaArtifact artifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", "1.0", "", "jar", "public.releases" );
124 // artifact.getModel().setWhenGathered( whenGathered );
126 // SearchResultHit resultHit = new SearchResultHit();
127 // resultHit.setArtifact(artifact);
128 // resultHit.setRepositoryId("repo1.mirror");
130 // results.addHit(SearchUtil.getHitId(artifact.getGroupId(), artifact.getArtifactId()), resultHit);
132 // SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
134 // searchControl.expectAndDefaultReturn( search.search( "", observableRepoIds, "MyClassName", limits, null ), results );
136 // archivaDAOControl.expectAndReturn( archivaDAO.getArtifactDAO(), artifactDAO );
137 // artifactDAOControl.expectAndReturn( artifactDAO.getArtifact( "org.apache.archiva", "archiva-test", "1.0", "", "pom", "public.releases" ), artifact );
139 // userReposControl.replay();
140 // searchControl.replay();
141 // archivaDAOControl.replay();
142 // artifactDAOControl.replay();
144 // List<Artifact> artifacts = searchService.quickSearch( "bytecode:MyClassName" );
146 // userReposControl.verify();
147 // searchControl.verify();
148 // archivaDAOControl.verify();
149 // artifactDAOControl.verify();
151 // assertNotNull( artifacts );
152 // assertEquals( 1, artifacts.size() );
155 // public void testQuickSearchArtifactRegularSearch()
158 // List<String> observableRepoIds = new ArrayList<String>();
159 // observableRepoIds.add( "repo1.mirror" );
160 // observableRepoIds.add( "public.releases" );
162 // userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
164 // Date whenGathered = new Date();
165 // SearchResults results = new SearchResults();
166 // ArchivaArtifact artifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", "1.0", "", "jar", "public.releases" );
167 // artifact.getModel().setWhenGathered( whenGathered );
170 // SearchResultHit resultHit = new SearchResultHit();
171 // resultHit.setArtifact(artifact);
172 // resultHit.setRepositoryId("repo1.mirror");
173 // results.addHit(SearchUtil.getHitId(artifact.getGroupId(), artifact.getArtifactId()), resultHit);
175 // SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
177 // searchControl.expectAndDefaultReturn( search.search( "", observableRepoIds, "archiva", limits, null ), results );
179 // archivaDAOControl.expectAndReturn( archivaDAO.getArtifactDAO(), artifactDAO );
180 // artifactDAOControl.expectAndReturn( artifactDAO.getArtifact( "org.apache.archiva", "archiva-test", "1.0", "", "pom", "public.releases" ), artifact );
182 // userReposControl.replay();
183 // searchControl.replay();
184 // archivaDAOControl.replay();
185 // artifactDAOControl.replay();
187 // List<Artifact> artifacts = searchService.quickSearch( "archiva" );
189 // userReposControl.verify();
190 // searchControl.verify();
191 // archivaDAOControl.verify();
192 // artifactDAOControl.verify();
194 // assertNotNull( artifacts );
195 // assertEquals( 1, artifacts.size() );
198 /* query artifact by checksum */
200 public void testGetArtifactByChecksum()
203 Date whenGathered = new Date();
205 ArtifactsByChecksumConstraint constraint = new ArtifactsByChecksumConstraint( "a1b2c3aksjhdasfkdasasd" );
206 List<ArchivaArtifact> artifacts = new ArrayList<ArchivaArtifact>();
207 ArchivaArtifact artifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", "1.0", "", "jar", "test-repo" );
208 artifact.getModel().setWhenGathered( whenGathered );
209 artifacts.add( artifact );
211 archivaDAOControl.expectAndReturn( archivaDAO.getArtifactDAO(), artifactDAO );
212 artifactDAO.queryArtifacts( constraint );
213 artifactDAOControl.setMatcher( MockControl.ALWAYS_MATCHER );
214 artifactDAOControl.setReturnValue( artifacts );
216 archivaDAOControl.replay();
217 artifactDAOControl.replay();
219 List<Artifact> results = searchService.getArtifactByChecksum( "a1b2c3aksjhdasfkdasasd" );
221 archivaDAOControl.verify();
222 artifactDAOControl.verify();
224 assertNotNull( results );
225 assertEquals( 1, results.size() );
228 /* query artifact versions */
230 public void testGetArtifactVersionsArtifactExists()
233 Date whenGathered = new Date();
235 List<String> observableRepoIds = new ArrayList<String>();
236 observableRepoIds.add( "repo1.mirror" );
237 observableRepoIds.add( "public.releases" );
239 List<String> versions = new ArrayList<String>();
240 versions.add( "1.0" );
241 versions.add( "1.1-beta-1" );
242 versions.add( "1.1-beta-2" );
243 versions.add( "1.1" );
244 versions.add( "1.2" );
245 versions.add( "1.2.1-SNAPSHOT" );
247 BrowsingResults results = new BrowsingResults( "org.apache.archiva", "archiva-test" );
248 results.setSelectedRepositoryIds( observableRepoIds );
249 results.setVersions( versions );
251 List<ArchivaArtifact> archivaArtifacts = new ArrayList<ArchivaArtifact>();
252 ArchivaArtifact archivaArtifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", versions.get( 0 ), "", "pom", "repo1.mirror" );
253 archivaArtifact.getModel().setWhenGathered( whenGathered );
254 archivaArtifacts.add( archivaArtifact );
256 archivaArtifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", versions.get( 1 ), "", "pom", "public.releases" );
257 archivaArtifact.getModel().setWhenGathered( whenGathered );
258 archivaArtifacts.add( archivaArtifact );
260 archivaArtifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", versions.get( 2 ), "", "pom", "repo1.mirror" );
261 archivaArtifact.getModel().setWhenGathered( whenGathered );
262 archivaArtifacts.add( archivaArtifact );
264 archivaArtifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", versions.get( 3 ), "", "pom", "public.releases" );
265 archivaArtifact.getModel().setWhenGathered( whenGathered );
266 archivaArtifacts.add( archivaArtifact );
268 archivaArtifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", versions.get( 4 ), "", "pom", "repo1.mirror" );
269 archivaArtifact.getModel().setWhenGathered( whenGathered );
270 archivaArtifacts.add( archivaArtifact );
272 archivaArtifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", versions.get( 5 ), "", "pom", "public.releases" );
273 archivaArtifact.getModel().setWhenGathered( whenGathered );
274 archivaArtifacts.add( archivaArtifact );
276 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
277 repoBrowsingControl.expectAndReturn( repoBrowsing.selectArtifactId( "", observableRepoIds, "org.apache.archiva", "archiva-test" ), results );
278 archivaDAOControl.expectAndReturn( archivaDAO.getArtifactDAO(), artifactDAO );
280 artifactDAOControl.expectAndDefaultReturn( artifactDAO.getArtifact( "org.apache.archiva", "archiva-test", versions.get( 0 ), "", "pom", "repo1.mirror" ), archivaArtifacts.get( 0 ) );
281 artifactDAOControl.expectAndDefaultReturn( artifactDAO.getArtifact( "org.apache.archiva", "archiva-test", versions.get( 1 ), "", "pom", "public.releases" ), archivaArtifacts.get( 1 ) );
282 artifactDAOControl.expectAndDefaultReturn( artifactDAO.getArtifact( "org.apache.archiva", "archiva-test", versions.get( 2 ), "", "pom", "repo1.mirror" ), archivaArtifacts.get( 2 ) );
283 artifactDAOControl.expectAndDefaultReturn( artifactDAO.getArtifact( "org.apache.archiva", "archiva-test", versions.get( 3 ), "", "pom", "public.releases" ), archivaArtifacts.get( 3 ) );
284 artifactDAOControl.expectAndDefaultReturn( artifactDAO.getArtifact( "org.apache.archiva", "archiva-test", versions.get( 4 ), "", "pom", "repo1.mirror" ), archivaArtifacts.get( 4 ) );
285 artifactDAOControl.expectAndDefaultReturn( artifactDAO.getArtifact( "org.apache.archiva", "archiva-test", versions.get( 5 ), "", "pom", "public.releases" ), archivaArtifacts.get( 5 ) );
287 userReposControl.replay();
288 repoBrowsingControl.replay();
289 archivaDAOControl.replay();
290 artifactDAOControl.replay();
292 List<Artifact> artifacts = searchService.getArtifactVersions( "org.apache.archiva", "archiva-test" );
294 userReposControl.verify();
295 repoBrowsingControl.verify();
296 archivaDAOControl.verify();
297 artifactDAOControl.verify();
299 assertNotNull( artifacts );
300 assertEquals( 6, artifacts.size() );
303 /* query artifact versions since a given date */
305 public void testGetArtifactVersionsByDateArtifactExists()
311 public void testGetArtifactVersionsByDateArtifactDoesNotExist()
317 /* query artifact dependencies */
319 public void testGetDependenciesArtifactExists()
322 List<String> observableRepoIds = new ArrayList<String>();
323 observableRepoIds.add( "repo1.mirror" );
324 observableRepoIds.add( "public.releases" );
326 ArchivaProjectModel model = new ArchivaProjectModel();
327 model.setGroupId( "org.apache.archiva" );
328 model.setArtifactId( "archiva-test" );
329 model.setVersion( "1.0" );
331 org.apache.maven.archiva.model.Dependency dependency = new org.apache.maven.archiva.model.Dependency();
332 dependency.setGroupId( "commons-logging" );
333 dependency.setArtifactId( "commons-logging" );
334 dependency.setVersion( "2.0" );
336 model.addDependency( dependency );
338 dependency = new org.apache.maven.archiva.model.Dependency();
339 dependency.setGroupId( "junit" );
340 dependency.setArtifactId( "junit" );
341 dependency.setVersion( "2.4" );
342 dependency.setScope( "test" );
344 model.addDependency( dependency );
346 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
347 repoBrowsingControl.expectAndReturn(
348 repoBrowsing.selectVersion( "", observableRepoIds, "org.apache.archiva", "archiva-test", "1.0" ), model );
350 repoBrowsingControl.replay();
351 userReposControl.replay();
353 List<Dependency> dependencies = searchService.getDependencies( "org.apache.archiva", "archiva-test", "1.0" );
355 repoBrowsingControl.verify();
356 userReposControl.verify();
358 assertNotNull( dependencies );
359 assertEquals( 2, dependencies.size() );
362 public void testGetDependenciesArtifactDoesNotExist()
365 List<String> observableRepoIds = new ArrayList<String>();
366 observableRepoIds.add( "repo1.mirror" );
367 observableRepoIds.add( "public.releases" );
369 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
370 repoBrowsingControl.expectAndThrow(
371 repoBrowsing.selectVersion( "", observableRepoIds, "org.apache.archiva", "archiva-test", "1.0" ), new ObjectNotFoundException( "Artifact does not exist." ) );
373 userReposControl.replay();
374 repoBrowsingControl.replay();
378 searchService.getDependencies( "org.apache.archiva", "archiva-test", "1.0" );
379 fail( "An exception should have been thrown." );
381 catch ( Exception e )
383 assertEquals( "Artifact does not exist.", e.getMessage() );
386 userReposControl.verify();
387 repoBrowsingControl.verify();
390 /* get dependency tree */
392 public void testGetDependencyTreeArtifactExists()
398 public void testGetDependencyTreeArtifactDoesNotExist()
406 public void testGetDependees()
409 Date date = new Date();
410 List<String> observableRepoIds = new ArrayList<String>();
411 observableRepoIds.add( "repo1.mirror" );
412 observableRepoIds.add( "public.releases" );
414 List<ArchivaProjectModel> dependeeModels = new ArrayList<ArchivaProjectModel>();
415 ArchivaProjectModel dependeeModel = new ArchivaProjectModel();
416 dependeeModel.setGroupId( "org.apache.archiva" );
417 dependeeModel.setArtifactId( "archiva-dependee-one" );
418 dependeeModel.setVersion( "1.0" );
419 dependeeModel.setWhenIndexed( date );
420 dependeeModels.add( dependeeModel );
422 dependeeModel = new ArchivaProjectModel();
423 dependeeModel.setGroupId( "org.apache.archiva" );
424 dependeeModel.setArtifactId( "archiva-dependee-two" );
425 dependeeModel.setVersion( "1.0" );
426 dependeeModel.setWhenIndexed( date );
427 dependeeModels.add( dependeeModel );
429 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
430 repoBrowsingControl.expectAndReturn( repoBrowsing.getUsedBy( "", observableRepoIds, "org.apache.archiva", "archiva-test", "1.0" ), dependeeModels );
432 repoBrowsingControl.replay();
433 userReposControl.replay();
435 List<Artifact> dependees = searchService.getDependees( "org.apache.archiva", "archiva-test", "1.0" );
437 repoBrowsingControl.verify();
438 userReposControl.verify();
440 assertNotNull( dependees );
441 assertEquals( 2, dependees.size() );
444 /*public void testGetDependeesArtifactDoesNotExist()
447 Date date = new Date();
448 List<String> observableRepoIds = new ArrayList<String>();
449 observableRepoIds.add( "repo1.mirror" );
450 observableRepoIds.add( "public.releases" );
452 List dependeeModels = new ArrayList();
453 ArchivaProjectModel dependeeModel = new ArchivaProjectModel();
454 dependeeModel.setGroupId( "org.apache.archiva" );
455 dependeeModel.setArtifactId( "archiva-dependee-one" );
456 dependeeModel.setVersion( "1.0" );
457 dependeeModel.setWhenIndexed( date );
458 dependeeModels.add( dependeeModel );
460 dependeeModel = new ArchivaProjectModel();
461 dependeeModel.setGroupId( "org.apache.archiva" );
462 dependeeModel.setArtifactId( "archiva-dependee-two" );
463 dependeeModel.setVersion( "1.0" );
464 dependeeModel.setWhenIndexed( date );
465 dependeeModels.add( dependeeModel );
467 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
468 repoBrowsingControl.expectAndReturn( repoBrowsing.getUsedBy( "", observableRepoIds, "org.apache.archiva", "archiva-test", "1.0" ), dependeeModels );
470 repoBrowsingControl.replay();
471 userReposControl.replay();
475 List<Artifact> dependees = searchService.getDependees( "org.apache.archiva", "archiva-test", "1.0" );
476 fail( "An exception should have been thrown." );
478 catch ( Exception e )
480 assertEquals( "Artifact does not exist." )
483 repoBrowsingControl.verify();
484 userReposControl.verify();
486 assertNotNull( dependees );
487 assertEquals( 2, dependees.size() );