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.indexer.search.SearchResultHit;
29 import org.apache.archiva.indexer.search.SearchResultLimits;
30 import org.apache.archiva.indexer.search.SearchResults;
31 import org.apache.archiva.indexer.util.SearchUtil;
32 import org.apache.archiva.web.xmlrpc.api.SearchService;
33 import org.apache.archiva.web.xmlrpc.api.beans.Artifact;
34 import org.apache.archiva.web.xmlrpc.api.beans.Dependency;
35 import org.apache.archiva.web.xmlrpc.security.XmlRpcUserRepositories;
36 import org.apache.maven.archiva.database.ArchivaDAO;
37 import org.apache.maven.archiva.database.ArtifactDAO;
38 import org.apache.maven.archiva.database.ObjectNotFoundException;
39 import org.apache.maven.archiva.database.browsing.BrowsingResults;
40 import org.apache.maven.archiva.database.browsing.RepositoryBrowsing;
41 import org.apache.maven.archiva.database.constraints.ArtifactsByChecksumConstraint;
42 import org.apache.maven.archiva.model.ArchivaArtifact;
43 import org.apache.maven.archiva.model.ArchivaProjectModel;
44 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
45 import org.easymock.MockControl;
46 import org.easymock.classextension.MockClassControl;
49 * SearchServiceImplTest
51 * @version $Id: SearchServiceImplTest.java
53 public class SearchServiceImplTest
54 extends PlexusInSpringTestCase
56 private SearchService searchService;
58 private MockControl userReposControl;
60 private XmlRpcUserRepositories userRepos;
62 private MockControl searchControl;
64 private RepositorySearch search;
66 private MockControl archivaDAOControl;
68 private ArchivaDAO archivaDAO;
70 private MockControl artifactDAOControl;
72 private ArtifactDAO artifactDAO;
74 private MockControl repoBrowsingControl;
76 private RepositoryBrowsing repoBrowsing;
82 userReposControl = MockClassControl.createControl( XmlRpcUserRepositories.class );
83 userRepos = ( XmlRpcUserRepositories ) userReposControl.getMock();
85 archivaDAOControl = MockControl.createControl( ArchivaDAO.class );
86 archivaDAO = ( ArchivaDAO ) archivaDAOControl.getMock();
88 repoBrowsingControl = MockControl.createControl( RepositoryBrowsing.class );
89 repoBrowsing = ( RepositoryBrowsing ) repoBrowsingControl.getMock();
91 searchControl = MockControl.createControl( RepositorySearch.class );
92 search = ( RepositorySearch ) searchControl.getMock();
94 searchService = new SearchServiceImpl( userRepos, archivaDAO, repoBrowsing, search );
96 artifactDAOControl = MockControl.createControl( ArtifactDAO.class );
97 artifactDAO = ( ArtifactDAO ) artifactDAOControl.getMock();
101 * quick/general text search which returns a list of artifacts
102 * query for an artifact based on a checksum
103 * query for all available versions of an artifact, sorted in version significance order
104 * query for all available versions of an artifact since a given date
105 * query for an artifact's direct dependencies
106 * query for an artifact's dependency tree (as with mvn dependency:tree - no duplicates should be included)
107 * query for all artifacts that depend on a given artifact
112 // public void testQuickSearchArtifactBytecodeSearch()
115 // // 1. check whether bytecode search or ordinary search
116 // // 2. get observable repos
117 // // 3. convert results to a list of Artifact objects
119 // List<String> observableRepoIds = new ArrayList<String>();
120 // observableRepoIds.add( "repo1.mirror" );
121 // observableRepoIds.add( "public.releases" );
123 // userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
125 // Date whenGathered = new Date();
126 // SearchResults results = new SearchResults();
127 // ArchivaArtifact artifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", "1.0", "", "jar", "public.releases" );
128 // artifact.getModel().setWhenGathered( whenGathered );
130 // SearchResultHit resultHit = new SearchResultHit();
131 // resultHit.setArtifact(artifact);
132 // resultHit.setRepositoryId("repo1.mirror");
134 // results.addHit(SearchUtil.getHitId(artifact.getGroupId(), artifact.getArtifactId()), resultHit);
136 // SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
138 // searchControl.expectAndDefaultReturn( search.search( "", observableRepoIds, "MyClassName", limits, null ), results );
140 // archivaDAOControl.expectAndReturn( archivaDAO.getArtifactDAO(), artifactDAO );
141 // artifactDAOControl.expectAndReturn( artifactDAO.getArtifact( "org.apache.archiva", "archiva-test", "1.0", "", "pom", "public.releases" ), artifact );
143 // userReposControl.replay();
144 // searchControl.replay();
145 // archivaDAOControl.replay();
146 // artifactDAOControl.replay();
148 // List<Artifact> artifacts = searchService.quickSearch( "bytecode:MyClassName" );
150 // userReposControl.verify();
151 // searchControl.verify();
152 // archivaDAOControl.verify();
153 // artifactDAOControl.verify();
155 // assertNotNull( artifacts );
156 // assertEquals( 1, artifacts.size() );
159 // public void testQuickSearchArtifactRegularSearch()
162 // List<String> observableRepoIds = new ArrayList<String>();
163 // observableRepoIds.add( "repo1.mirror" );
164 // observableRepoIds.add( "public.releases" );
166 // userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
168 // Date whenGathered = new Date();
169 // SearchResults results = new SearchResults();
170 // ArchivaArtifact artifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", "1.0", "", "jar", "public.releases" );
171 // artifact.getModel().setWhenGathered( whenGathered );
174 // SearchResultHit resultHit = new SearchResultHit();
175 // resultHit.setArtifact(artifact);
176 // resultHit.setRepositoryId("repo1.mirror");
177 // results.addHit(SearchUtil.getHitId(artifact.getGroupId(), artifact.getArtifactId()), resultHit);
179 // SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
181 // searchControl.expectAndDefaultReturn( search.search( "", observableRepoIds, "archiva", limits, null ), results );
183 // archivaDAOControl.expectAndReturn( archivaDAO.getArtifactDAO(), artifactDAO );
184 // artifactDAOControl.expectAndReturn( artifactDAO.getArtifact( "org.apache.archiva", "archiva-test", "1.0", "", "pom", "public.releases" ), artifact );
186 // userReposControl.replay();
187 // searchControl.replay();
188 // archivaDAOControl.replay();
189 // artifactDAOControl.replay();
191 // List<Artifact> artifacts = searchService.quickSearch( "archiva" );
193 // userReposControl.verify();
194 // searchControl.verify();
195 // archivaDAOControl.verify();
196 // artifactDAOControl.verify();
198 // assertNotNull( artifacts );
199 // assertEquals( 1, artifacts.size() );
202 /* query artifact by checksum */
204 public void testGetArtifactByChecksum()
207 Date whenGathered = new Date();
209 ArtifactsByChecksumConstraint constraint = new ArtifactsByChecksumConstraint( "a1b2c3aksjhdasfkdasasd" );
210 List<ArchivaArtifact> artifacts = new ArrayList<ArchivaArtifact>();
211 ArchivaArtifact artifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", "1.0", "", "jar", "test-repo" );
212 artifact.getModel().setWhenGathered( whenGathered );
213 artifacts.add( artifact );
215 archivaDAOControl.expectAndReturn( archivaDAO.getArtifactDAO(), artifactDAO );
216 artifactDAO.queryArtifacts( constraint );
217 artifactDAOControl.setMatcher( MockControl.ALWAYS_MATCHER );
218 artifactDAOControl.setReturnValue( artifacts );
220 archivaDAOControl.replay();
221 artifactDAOControl.replay();
223 List<Artifact> results = searchService.getArtifactByChecksum( "a1b2c3aksjhdasfkdasasd" );
225 archivaDAOControl.verify();
226 artifactDAOControl.verify();
228 assertNotNull( results );
229 assertEquals( 1, results.size() );
232 /* query artifact versions */
234 public void testGetArtifactVersionsArtifactExists()
237 Date whenGathered = new Date();
239 List<String> observableRepoIds = new ArrayList<String>();
240 observableRepoIds.add( "repo1.mirror" );
241 observableRepoIds.add( "public.releases" );
243 List<String> versions = new ArrayList<String>();
244 versions.add( "1.0" );
245 versions.add( "1.1-beta-1" );
246 versions.add( "1.1-beta-2" );
247 versions.add( "1.1" );
248 versions.add( "1.2" );
249 versions.add( "1.2.1-SNAPSHOT" );
251 BrowsingResults results = new BrowsingResults( "org.apache.archiva", "archiva-test" );
252 results.setSelectedRepositoryIds( observableRepoIds );
253 results.setVersions( versions );
255 List<ArchivaArtifact> archivaArtifacts = new ArrayList<ArchivaArtifact>();
256 ArchivaArtifact archivaArtifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", versions.get( 0 ), "", "pom", "repo1.mirror" );
257 archivaArtifact.getModel().setWhenGathered( whenGathered );
258 archivaArtifacts.add( archivaArtifact );
260 archivaArtifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", versions.get( 1 ), "", "pom", "public.releases" );
261 archivaArtifact.getModel().setWhenGathered( whenGathered );
262 archivaArtifacts.add( archivaArtifact );
264 archivaArtifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", versions.get( 2 ), "", "pom", "repo1.mirror" );
265 archivaArtifact.getModel().setWhenGathered( whenGathered );
266 archivaArtifacts.add( archivaArtifact );
268 archivaArtifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", versions.get( 3 ), "", "pom", "public.releases" );
269 archivaArtifact.getModel().setWhenGathered( whenGathered );
270 archivaArtifacts.add( archivaArtifact );
272 archivaArtifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", versions.get( 4 ), "", "pom", "repo1.mirror" );
273 archivaArtifact.getModel().setWhenGathered( whenGathered );
274 archivaArtifacts.add( archivaArtifact );
276 archivaArtifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", versions.get( 5 ), "", "pom", "public.releases" );
277 archivaArtifact.getModel().setWhenGathered( whenGathered );
278 archivaArtifacts.add( archivaArtifact );
280 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
281 repoBrowsingControl.expectAndReturn( repoBrowsing.selectArtifactId( "", observableRepoIds, "org.apache.archiva", "archiva-test" ), results );
282 archivaDAOControl.expectAndReturn( archivaDAO.getArtifactDAO(), artifactDAO );
284 artifactDAOControl.expectAndDefaultReturn( artifactDAO.getArtifact( "org.apache.archiva", "archiva-test", versions.get( 0 ), "", "pom", "repo1.mirror" ), archivaArtifacts.get( 0 ) );
285 artifactDAOControl.expectAndDefaultReturn( artifactDAO.getArtifact( "org.apache.archiva", "archiva-test", versions.get( 1 ), "", "pom", "public.releases" ), archivaArtifacts.get( 1 ) );
286 artifactDAOControl.expectAndDefaultReturn( artifactDAO.getArtifact( "org.apache.archiva", "archiva-test", versions.get( 2 ), "", "pom", "repo1.mirror" ), archivaArtifacts.get( 2 ) );
287 artifactDAOControl.expectAndDefaultReturn( artifactDAO.getArtifact( "org.apache.archiva", "archiva-test", versions.get( 3 ), "", "pom", "public.releases" ), archivaArtifacts.get( 3 ) );
288 artifactDAOControl.expectAndDefaultReturn( artifactDAO.getArtifact( "org.apache.archiva", "archiva-test", versions.get( 4 ), "", "pom", "repo1.mirror" ), archivaArtifacts.get( 4 ) );
289 artifactDAOControl.expectAndDefaultReturn( artifactDAO.getArtifact( "org.apache.archiva", "archiva-test", versions.get( 5 ), "", "pom", "public.releases" ), archivaArtifacts.get( 5 ) );
291 userReposControl.replay();
292 repoBrowsingControl.replay();
293 archivaDAOControl.replay();
294 artifactDAOControl.replay();
296 List<Artifact> artifacts = searchService.getArtifactVersions( "org.apache.archiva", "archiva-test" );
298 userReposControl.verify();
299 repoBrowsingControl.verify();
300 archivaDAOControl.verify();
301 artifactDAOControl.verify();
303 assertNotNull( artifacts );
304 assertEquals( 6, artifacts.size() );
307 /* query artifact versions since a given date */
309 public void testGetArtifactVersionsByDateArtifactExists()
315 public void testGetArtifactVersionsByDateArtifactDoesNotExist()
321 /* query artifact dependencies */
323 public void testGetDependenciesArtifactExists()
326 List<String> observableRepoIds = new ArrayList<String>();
327 observableRepoIds.add( "repo1.mirror" );
328 observableRepoIds.add( "public.releases" );
330 ArchivaProjectModel model = new ArchivaProjectModel();
331 model.setGroupId( "org.apache.archiva" );
332 model.setArtifactId( "archiva-test" );
333 model.setVersion( "1.0" );
335 org.apache.maven.archiva.model.Dependency dependency = new org.apache.maven.archiva.model.Dependency();
336 dependency.setGroupId( "commons-logging" );
337 dependency.setArtifactId( "commons-logging" );
338 dependency.setVersion( "2.0" );
340 model.addDependency( dependency );
342 dependency = new org.apache.maven.archiva.model.Dependency();
343 dependency.setGroupId( "junit" );
344 dependency.setArtifactId( "junit" );
345 dependency.setVersion( "2.4" );
346 dependency.setScope( "test" );
348 model.addDependency( dependency );
350 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
351 repoBrowsingControl.expectAndReturn(
352 repoBrowsing.selectVersion( "", observableRepoIds, "org.apache.archiva", "archiva-test", "1.0" ), model );
354 repoBrowsingControl.replay();
355 userReposControl.replay();
357 List<Dependency> dependencies = searchService.getDependencies( "org.apache.archiva", "archiva-test", "1.0" );
359 repoBrowsingControl.verify();
360 userReposControl.verify();
362 assertNotNull( dependencies );
363 assertEquals( 2, dependencies.size() );
366 public void testGetDependenciesArtifactDoesNotExist()
369 List<String> observableRepoIds = new ArrayList<String>();
370 observableRepoIds.add( "repo1.mirror" );
371 observableRepoIds.add( "public.releases" );
373 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
374 repoBrowsingControl.expectAndThrow(
375 repoBrowsing.selectVersion( "", observableRepoIds, "org.apache.archiva", "archiva-test", "1.0" ), new ObjectNotFoundException( "Artifact does not exist." ) );
377 userReposControl.replay();
378 repoBrowsingControl.replay();
382 List<Dependency> dependencies = searchService.getDependencies( "org.apache.archiva", "archiva-test", "1.0" );
383 fail( "An exception should have been thrown." );
385 catch ( Exception e )
387 assertEquals( "Artifact does not exist.", e.getMessage() );
390 userReposControl.verify();
391 repoBrowsingControl.verify();
394 /* get dependency tree */
396 public void testGetDependencyTreeArtifactExists()
402 public void testGetDependencyTreeArtifactDoesNotExist()
410 public void testGetDependees()
413 Date date = new Date();
414 List<String> observableRepoIds = new ArrayList<String>();
415 observableRepoIds.add( "repo1.mirror" );
416 observableRepoIds.add( "public.releases" );
418 List dependeeModels = new ArrayList();
419 ArchivaProjectModel dependeeModel = new ArchivaProjectModel();
420 dependeeModel.setGroupId( "org.apache.archiva" );
421 dependeeModel.setArtifactId( "archiva-dependee-one" );
422 dependeeModel.setVersion( "1.0" );
423 dependeeModel.setWhenIndexed( date );
424 dependeeModels.add( dependeeModel );
426 dependeeModel = new ArchivaProjectModel();
427 dependeeModel.setGroupId( "org.apache.archiva" );
428 dependeeModel.setArtifactId( "archiva-dependee-two" );
429 dependeeModel.setVersion( "1.0" );
430 dependeeModel.setWhenIndexed( date );
431 dependeeModels.add( dependeeModel );
433 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
434 repoBrowsingControl.expectAndReturn( repoBrowsing.getUsedBy( "", observableRepoIds, "org.apache.archiva", "archiva-test", "1.0" ), dependeeModels );
436 repoBrowsingControl.replay();
437 userReposControl.replay();
439 List<Artifact> dependees = searchService.getDependees( "org.apache.archiva", "archiva-test", "1.0" );
441 repoBrowsingControl.verify();
442 userReposControl.verify();
444 assertNotNull( dependees );
445 assertEquals( 2, dependees.size() );
448 /*public void testGetDependeesArtifactDoesNotExist()
451 Date date = new Date();
452 List<String> observableRepoIds = new ArrayList<String>();
453 observableRepoIds.add( "repo1.mirror" );
454 observableRepoIds.add( "public.releases" );
456 List dependeeModels = new ArrayList();
457 ArchivaProjectModel dependeeModel = new ArchivaProjectModel();
458 dependeeModel.setGroupId( "org.apache.archiva" );
459 dependeeModel.setArtifactId( "archiva-dependee-one" );
460 dependeeModel.setVersion( "1.0" );
461 dependeeModel.setWhenIndexed( date );
462 dependeeModels.add( dependeeModel );
464 dependeeModel = new ArchivaProjectModel();
465 dependeeModel.setGroupId( "org.apache.archiva" );
466 dependeeModel.setArtifactId( "archiva-dependee-two" );
467 dependeeModel.setVersion( "1.0" );
468 dependeeModel.setWhenIndexed( date );
469 dependeeModels.add( dependeeModel );
471 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
472 repoBrowsingControl.expectAndReturn( repoBrowsing.getUsedBy( "", observableRepoIds, "org.apache.archiva", "archiva-test", "1.0" ), dependeeModels );
474 repoBrowsingControl.replay();
475 userReposControl.replay();
479 List<Artifact> dependees = searchService.getDependees( "org.apache.archiva", "archiva-test", "1.0" );
480 fail( "An exception should have been thrown." );
482 catch ( Exception e )
484 assertEquals( "Artifact does not exist." )
487 repoBrowsingControl.verify();
488 userReposControl.verify();
490 assertNotNull( dependees );
491 assertEquals( 2, dependees.size() );