]> source.dussan.org Git - archiva.git/blob
2deb6f1c2e4ef9a4be2c9f5be9c0b94bd3a888cb
[archiva.git] /
1 package org.apache.archiva.web.xmlrpc.services;
2
3 /*
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
11  *
12  *   http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21  
22
23 import java.io.File;
24 import java.util.ArrayList;
25 import java.util.Date;
26 import java.util.List;
27
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;
68
69 import sun.security.action.GetLongAction;
70
71 /**
72  * SearchServiceImplTest
73  * 
74  * @version $Id: SearchServiceImplTest.java
75  */
76 public class SearchServiceImplTest
77     extends PlexusInSpringTestCase
78 {
79     private SearchService searchService;
80     
81     private MockControl userReposControl;
82     
83     private XmlRpcUserRepositories userRepos;
84     
85     private MockControl crossRepoSearchControl;
86     
87     private CrossRepositorySearch crossRepoSearch;
88     
89     private MockControl archivaDAOControl;
90     
91     private ArchivaDAO archivaDAO;
92     
93     private MockControl artifactDAOControl;
94     
95     private ArtifactDAO artifactDAO;
96     
97     private MockControl repoBrowsingControl;
98     
99     private RepositoryBrowsing repoBrowsing;
100         
101     public void setUp()
102         throws Exception
103     {
104         userReposControl = MockClassControl.createControl( XmlRpcUserRepositories.class );
105         userRepos = ( XmlRpcUserRepositories ) userReposControl.getMock();
106         
107         crossRepoSearchControl = MockControl.createControl( CrossRepositorySearch.class );
108         crossRepoSearch = ( CrossRepositorySearch ) crossRepoSearchControl.getMock();
109         
110         archivaDAOControl = MockControl.createControl( ArchivaDAO.class );
111         archivaDAO = ( ArchivaDAO ) archivaDAOControl.getMock();
112         
113         repoBrowsingControl = MockControl.createControl( RepositoryBrowsing.class );
114         repoBrowsing = ( RepositoryBrowsing ) repoBrowsingControl.getMock();
115         
116         searchService = new SearchServiceImpl( userRepos, crossRepoSearch, archivaDAO, repoBrowsing );
117         
118         artifactDAOControl = MockControl.createControl( ArtifactDAO.class );
119         artifactDAO = ( ArtifactDAO ) artifactDAOControl.getMock();
120     }
121     
122     /*
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
130      */
131  
132  /* quick search */
133     
134     public void testQuickSearchArtifactBytecodeSearch()
135         throws Exception
136     {
137         // 1. check whether bytecode search or ordinary search
138         // 2. get observable repos
139         // 3. convert results to a list of Artifact objects
140         
141         List<String> observableRepoIds = new ArrayList<String>();
142         observableRepoIds.add( "repo1.mirror" );
143         observableRepoIds.add( "public.releases" );
144         
145         userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
146         
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 );
151         
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" );
157                 
158         results.addHit( record );
159         
160         SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
161         
162         crossRepoSearchControl.expectAndDefaultReturn( 
163                    crossRepoSearch.searchForBytecode( "", observableRepoIds, "MyClassName", limits ), results );
164         
165         archivaDAOControl.expectAndReturn( archivaDAO.getArtifactDAO(), artifactDAO );
166         artifactDAOControl.expectAndReturn( artifactDAO.getArtifact( "org.apache.archiva", "archiva-test", "1.0", "", "pom" ), artifact );
167         
168         userReposControl.replay();
169         crossRepoSearchControl.replay();
170         archivaDAOControl.replay();
171         artifactDAOControl.replay();
172         
173         List<Artifact> artifacts = searchService.quickSearch( "bytecode:MyClassName" );
174         
175         userReposControl.verify();
176         crossRepoSearchControl.verify();
177         archivaDAOControl.verify();
178         artifactDAOControl.verify();
179         
180         assertNotNull( artifacts );
181         assertEquals( 1, artifacts.size() );        
182     }
183     
184     public void testQuickSearchArtifactRegularSearch()
185         throws Exception
186     {
187         List<String> observableRepoIds = new ArrayList<String>();
188         observableRepoIds.add( "repo1.mirror" );
189         observableRepoIds.add( "public.releases" );
190         
191         userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
192         
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 );
197         
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" );
203                 
204         results.addHit( record );
205         
206         SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
207         
208         crossRepoSearchControl.expectAndDefaultReturn( 
209                    crossRepoSearch.searchForTerm( "", observableRepoIds, "archiva", limits ), results );
210         
211         archivaDAOControl.expectAndReturn( archivaDAO.getArtifactDAO(), artifactDAO );
212         artifactDAOControl.expectAndReturn( artifactDAO.getArtifact( "org.apache.archiva", "archiva-test", "1.0", "", "pom" ), artifact );
213         
214         userReposControl.replay();
215         crossRepoSearchControl.replay();
216         archivaDAOControl.replay();
217         artifactDAOControl.replay();
218         
219         List<Artifact> artifacts = searchService.quickSearch( "archiva" );
220         
221         userReposControl.verify();
222         crossRepoSearchControl.verify();  
223         archivaDAOControl.verify();
224         artifactDAOControl.verify();
225         
226         assertNotNull( artifacts );
227         assertEquals( 1, artifacts.size() );        
228     }
229     
230 /* query artifact by checksum */
231     
232     public void testGetArtifactByChecksum()
233         throws Exception
234     {
235         Date whenGathered = new Date();
236         
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 );
242         
243         archivaDAOControl.expectAndReturn( archivaDAO.getArtifactDAO(), artifactDAO );
244         artifactDAO.queryArtifacts( constraint );
245         artifactDAOControl.setMatcher( MockControl.ALWAYS_MATCHER );
246         artifactDAOControl.setReturnValue( artifacts );
247         
248         archivaDAOControl.replay();
249         artifactDAOControl.replay();
250         
251         List<Artifact> results = searchService.getArtifactByChecksum( "a1b2c3aksjhdasfkdasasd" );
252         
253         archivaDAOControl.verify();
254         artifactDAOControl.verify();
255         
256         assertNotNull( results );
257         assertEquals( 1, results.size() );
258     }
259     
260 /* query artifact versions */
261     
262     public void testGetArtifactVersionsArtifactExists()
263         throws Exception
264     {
265         Date whenGathered = new Date();
266         
267         List<String> observableRepoIds = new ArrayList<String>();
268         observableRepoIds.add( "repo1.mirror" );
269         observableRepoIds.add( "public.releases" );
270         
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" );
278         
279         BrowsingResults results = new BrowsingResults( "org.apache.archiva", "archiva-test" );
280         results.setSelectedRepositoryIds( observableRepoIds );
281         results.setVersions( versions );
282         
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 );
287         
288         archivaArtifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", versions.get( 1 ), "", "pom" );
289         archivaArtifact.getModel().setWhenGathered( whenGathered );
290         archivaArtifacts.add( archivaArtifact );
291         
292         archivaArtifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", versions.get( 2 ), "", "pom" );
293         archivaArtifact.getModel().setWhenGathered( whenGathered );
294         archivaArtifacts.add( archivaArtifact );
295         
296         archivaArtifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", versions.get( 3 ), "", "pom" );
297         archivaArtifact.getModel().setWhenGathered( whenGathered );
298         archivaArtifacts.add( archivaArtifact );
299         
300         archivaArtifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", versions.get( 4 ), "", "pom" );
301         archivaArtifact.getModel().setWhenGathered( whenGathered );
302         archivaArtifacts.add( archivaArtifact );
303         
304         archivaArtifact = new ArchivaArtifact( "org.apache.archiva", "archiva-test", versions.get( 5 ), "", "pom" );
305         archivaArtifact.getModel().setWhenGathered( whenGathered );
306         archivaArtifacts.add( archivaArtifact );
307         
308         userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
309         repoBrowsingControl.expectAndReturn( repoBrowsing.selectArtifactId( "", observableRepoIds, "org.apache.archiva", "archiva-test" ), results );
310         archivaDAOControl.expectAndReturn( archivaDAO.getArtifactDAO(), artifactDAO );
311         
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 ) );
318         
319         userReposControl.replay();
320         repoBrowsingControl.replay();
321         archivaDAOControl.replay();
322         artifactDAOControl.replay();
323         
324         List<Artifact> artifacts = searchService.getArtifactVersions( "org.apache.archiva", "archiva-test" );
325         
326         userReposControl.verify();
327         repoBrowsingControl.verify();
328         archivaDAOControl.verify();
329         artifactDAOControl.verify();
330         
331         assertNotNull( artifacts );
332         assertEquals( 6, artifacts.size() );
333     }
334     
335 /* query artifact versions since a given date */
336     
337     public void testGetArtifactVersionsByDateArtifactExists()
338         throws Exception
339     {
340     
341     }
342     
343     public void testGetArtifactVersionsByDateArtifactDoesNotExist()
344         throws Exception
345     {
346     
347     }
348     
349 /* query artifact dependencies */
350     
351     public void testGetDependenciesArtifactExists()
352         throws Exception
353     {   
354         List<String> observableRepoIds = new ArrayList<String>();
355         observableRepoIds.add( "repo1.mirror" );
356         observableRepoIds.add( "public.releases" );
357         
358         ArchivaProjectModel model = new ArchivaProjectModel();
359         model.setGroupId( "org.apache.archiva" );
360         model.setArtifactId( "archiva-test" );
361         model.setVersion( "1.0" );
362         
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" );
367         
368         model.addDependency( dependency );
369         
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" );
375         
376         model.addDependency( dependency );
377         
378         userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );        
379         repoBrowsingControl.expectAndReturn( 
380                  repoBrowsing.selectVersion( "", observableRepoIds, "org.apache.archiva", "archiva-test", "1.0" ), model );
381         
382         repoBrowsingControl.replay(); 
383         userReposControl.replay();
384         
385         List<Dependency> dependencies = searchService.getDependencies( "org.apache.archiva", "archiva-test", "1.0" );
386         
387         repoBrowsingControl.verify();
388         userReposControl.verify();
389         
390         assertNotNull( dependencies );
391         assertEquals( 2, dependencies.size() );
392     }
393     
394     public void testGetDependenciesArtifactDoesNotExist()
395         throws Exception
396     {
397         List<String> observableRepoIds = new ArrayList<String>();
398         observableRepoIds.add( "repo1.mirror" );
399         observableRepoIds.add( "public.releases" );
400         
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." ) );
404         
405         userReposControl.replay();
406         repoBrowsingControl.replay();
407         
408         try
409         {
410             List<Dependency> dependencies = searchService.getDependencies( "org.apache.archiva", "archiva-test", "1.0" );
411             fail( "An exception should have been thrown." );
412         }
413         catch ( Exception e )
414         {
415             assertEquals( "Artifact does not exist.", e.getMessage() );
416         }
417         
418         userReposControl.verify();
419         repoBrowsingControl.verify();
420     }
421     
422 /* get dependency tree */
423     
424     public void testGetDependencyTreeArtifactExists()
425         throws Exception
426     {
427         
428     }
429     
430     public void testGetDependencyTreeArtifactDoesNotExist()
431         throws Exception
432     {
433     
434     }
435     
436 /* get dependees */
437     
438     public void testGetDependees()
439         throws Exception
440     {
441         Date date = new Date();
442         List<String> observableRepoIds = new ArrayList<String>();
443         observableRepoIds.add( "repo1.mirror" );
444         observableRepoIds.add( "public.releases" );
445         
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 );
453         
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 );
460         
461         userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
462         repoBrowsingControl.expectAndReturn( repoBrowsing.getUsedBy( "", observableRepoIds, "org.apache.archiva", "archiva-test", "1.0" ), dependeeModels );
463         
464         repoBrowsingControl.replay(); 
465         userReposControl.replay();
466
467         List<Artifact> dependees = searchService.getDependees( "org.apache.archiva", "archiva-test", "1.0" );
468         
469         repoBrowsingControl.verify();
470         userReposControl.verify();
471         
472         assertNotNull( dependees );
473         assertEquals( 2, dependees.size() );
474     }
475     
476     /*public void testGetDependeesArtifactDoesNotExist()
477         throws Exception
478     {
479         Date date = new Date();
480         List<String> observableRepoIds = new ArrayList<String>();
481         observableRepoIds.add( "repo1.mirror" );
482         observableRepoIds.add( "public.releases" );
483         
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 );
491         
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 );
498         
499         userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
500         repoBrowsingControl.expectAndReturn( repoBrowsing.getUsedBy( "", observableRepoIds, "org.apache.archiva", "archiva-test", "1.0" ), dependeeModels );
501         
502         repoBrowsingControl.replay(); 
503         userReposControl.replay();
504
505         try
506         {
507             List<Artifact> dependees = searchService.getDependees( "org.apache.archiva", "archiva-test", "1.0" );
508             fail( "An exception should have been thrown." );
509         }
510         catch ( Exception e )
511         {
512             assertEquals( "Artifact does not exist." )
513         }
514         
515         repoBrowsingControl.verify();
516         userReposControl.verify();
517         
518         assertNotNull( dependees );
519         assertEquals( 2, dependees.size() );
520     }*/
521 }