]> source.dussan.org Git - archiva.git/blob
005e3e7194af755e9889f1173d717c99dbaf3970
[archiva.git] /
1 package org.apache.maven.archiva.indexer.search;
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 import org.apache.commons.io.FileUtils;
23 import org.apache.lucene.search.Hits;
24 import org.apache.lucene.search.MatchAllDocsQuery;
25 import org.apache.lucene.search.Query;
26 import org.apache.lucene.search.Searcher;
27 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
28 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
29 import org.apache.maven.archiva.indexer.MockConfiguration;
30 import org.apache.maven.archiva.indexer.RepositoryContentIndex;
31 import org.apache.maven.archiva.indexer.RepositoryContentIndexFactory;
32 import org.apache.maven.archiva.indexer.bytecode.BytecodeRecord;
33 import org.apache.maven.archiva.indexer.filecontent.FileContentRecord;
34 import org.apache.maven.archiva.indexer.hashcodes.HashcodesRecord;
35 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
36
37 import java.io.File;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.List;
41 import java.util.Map;
42
43 /**
44  * DefaultCrossRepositorySearchTest
45  *
46  * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
47  * @version $Id$
48  */
49 public class DefaultCrossRepositorySearchTest    
50     extends PlexusInSpringTestCase
51 {
52     private static final String TEST_DEFAULT_REPOSITORY_NAME = "Test Default Repository";
53
54     private static final String TEST_DEFAULT_REPO_ID = "testDefaultRepo";
55
56     @Override
57     protected void setUp()
58         throws Exception
59     {
60         super.setUp();
61
62         RepositoryContentIndexFactory indexFactory =
63             (RepositoryContentIndexFactory) lookup( RepositoryContentIndexFactory.class
64                .getName(), "lucene" );
65
66         File repoDir = new File( getBasedir(), "src/test/managed-repository" );
67
68         assertTrue( "Default Test Repository should exist.", repoDir.exists() && repoDir.isDirectory() );
69
70         ManagedRepositoryConfiguration repository = createRepository( TEST_DEFAULT_REPO_ID, TEST_DEFAULT_REPOSITORY_NAME, repoDir );
71
72         File indexLocation = new File( "target/index-crossrepo-" + getName() + "/" );
73
74         MockConfiguration config = (MockConfiguration) lookup( ArchivaConfiguration.class.getName(), "mock" );
75
76         ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
77         repoConfig.setId( TEST_DEFAULT_REPO_ID );
78         repoConfig.setName( TEST_DEFAULT_REPOSITORY_NAME );
79         repoConfig.setLocation( repoDir.getAbsolutePath() );
80         repoConfig.setIndexDir( indexLocation.getAbsolutePath() );
81         repoConfig.setScanned( true );
82
83         if ( indexLocation.exists() )
84         {
85             FileUtils.deleteDirectory( indexLocation );
86         }
87
88         config.getConfiguration().addManagedRepository( repoConfig );
89
90         // Create the (empty) indexes.
91         RepositoryContentIndex indexHashcode = indexFactory.createHashcodeIndex( repository );
92         RepositoryContentIndex indexBytecode = indexFactory.createBytecodeIndex( repository );
93         RepositoryContentIndex indexContents = indexFactory.createFileContentIndex( repository );
94
95         // Now populate them.
96         Map<String, HashcodesRecord> hashcodesMap = new HashcodesIndexPopulator().populate( new File( getBasedir() ) );
97         indexHashcode.indexRecords( hashcodesMap.values() );
98         assertEquals( "Hashcode Key Count", hashcodesMap.size(), indexHashcode.getAllRecordKeys().size() );
99         assertRecordCount( indexHashcode, hashcodesMap.size() );
100
101         Map<String, BytecodeRecord> bytecodeMap = new BytecodeIndexPopulator().populate( new File( getBasedir() ) );
102         indexBytecode.indexRecords( bytecodeMap.values() );
103         assertEquals( "Bytecode Key Count", bytecodeMap.size(), indexBytecode.getAllRecordKeys().size() );
104         assertRecordCount( indexBytecode, bytecodeMap.size() );
105
106         Map<String, FileContentRecord> contentMap = new FileContentIndexPopulator().populate( new File( getBasedir() ) );
107         indexContents.indexRecords( contentMap.values() );
108         assertEquals( "File Content Key Count", contentMap.size(), indexContents.getAllRecordKeys().size() );
109         assertRecordCount( indexContents, contentMap.size() );
110     }
111
112     private void assertRecordCount( RepositoryContentIndex index, int expectedCount )
113         throws Exception
114     {
115         Query query = new MatchAllDocsQuery();
116         Searcher searcher = (Searcher) index.getSearchable();
117         Hits hits = searcher.search( query );
118         assertEquals( "Expected Record Count for " + index.getId(), expectedCount, hits.length() );
119     }
120
121     private CrossRepositorySearch lookupCrossRepositorySearch()
122         throws Exception
123     {
124         CrossRepositorySearch search =
125             (CrossRepositorySearch) lookup( CrossRepositorySearch.class.getName(), "default" );
126         assertNotNull( "CrossRepositorySearch:default should not be null.", search );
127         return search;
128     }
129
130     public void testSearchTerm_Org()
131         throws Exception
132     {        
133         CrossRepositorySearch search = lookupCrossRepositorySearch();
134
135         String expectedRepos[] = new String[] {
136             TEST_DEFAULT_REPO_ID
137         };
138         
139         String expectedResults[] = new String[] { 
140             "org","org2","org3","org4","org5","org6","org7"
141         };
142         
143         assertSearchResults( expectedRepos, expectedResults, search, "org", null, false );
144     }
145
146     public void testSearchTerm_Junit()
147         throws Exception
148     {        
149         CrossRepositorySearch search = lookupCrossRepositorySearch();
150         
151         String expectedRepos[] = new String[] {
152             TEST_DEFAULT_REPO_ID
153         };
154         
155         String expectedResults[] = new String[] { 
156             "junit","junit2","junit3"
157         };
158         
159         assertSearchResults( expectedRepos, expectedResults, search, "junit", null, false );
160     }
161
162     public void testSearchInvalidTerm()
163         throws Exception
164     {        
165         CrossRepositorySearch search = lookupCrossRepositorySearch();
166
167         String expectedRepos[] = new String[] {
168             TEST_DEFAULT_REPO_ID
169         };
170         
171         String expectedResults[] = new String[] { 
172             // Nothing.
173         };
174         
175         assertSearchResults( expectedRepos, expectedResults, search, "monosodium", null, false );
176     }
177     
178     public void testSearchWithinSearchResults()
179         throws Exception
180     {        
181         CrossRepositorySearch search = lookupCrossRepositorySearch();
182
183         String expectedRepos[] = new String[] {
184             TEST_DEFAULT_REPO_ID
185         };
186         
187         String expectedResults[] = new String[] { 
188             "org","org2","org3","org4","org5","org6","org7"
189         };
190         
191         // first search
192         assertSearchResults( expectedRepos, expectedResults, search, "org", null, false );
193         
194         List<String> previousSearchTerms = new ArrayList<String>();
195         previousSearchTerms.add( "org" );        
196         String secondSearchExpectedResults[] = new String[] { 
197             "org.apache.maven.archiva.record", "org.apache.maven.archiva.record2",
198                 "org.apache.maven.archiva.record3", "org.apache.maven.archiva.record4",
199                 "org.apache.maven.archiva.record5", "org.apache.maven.archiva.record6",
200                 "org.apache.maven.archiva.record7" 
201         };
202         
203         //second search
204         assertSearchResults( expectedRepos, secondSearchExpectedResults, search, "org.apache.maven.archiva.record",
205                              previousSearchTerms, false );
206         
207         previousSearchTerms.add( "org.apache.maven.archiva.record" );
208         String thirdSearchExpectedResults[] = new String[] { 
209             "junit", "junit2", "junit3"
210         };
211         
212         //third search
213         assertSearchResults( expectedRepos, thirdSearchExpectedResults, search, "junit", previousSearchTerms, false );        
214     }
215     
216     public void testSearchForClassesAndPackages()
217         throws Exception
218     {                
219         CrossRepositorySearch search = lookupCrossRepositorySearch();
220
221         String expectedRepos[] = new String[] {
222             TEST_DEFAULT_REPO_ID
223         };
224                 
225         String expectedResults[] = new String[] { 
226             "archiva-common-1.0.jar"
227         };
228         
229         // class with packagename search
230         assertSearchResults( expectedRepos, expectedResults, search, 
231                              "org.apache.maven.archiva.common.utils.BaseFile", null, true );
232         // class name search
233         assertSearchResults( expectedRepos, expectedResults, search, 
234                              "BaseFile", null, true );
235                 
236         String expectedMethodSearchResults[] = new String[] { 
237             "continuum-webapp-1.0.3-SNAPSHOT.war"
238         };
239         
240         // method search
241         assertSearchResults( expectedRepos, expectedMethodSearchResults, search,
242                              "org.apache.maven.continuum.web.action.BuildDefinitionAction.isBuildFresh", null, true );        
243     }
244     
245     public void testExecuteFilteredSearch()
246         throws Exception
247     {
248         CrossRepositorySearch search = lookupCrossRepositorySearch();
249
250         String expectedRepos[] = new String[] { TEST_DEFAULT_REPO_ID };
251
252         String expectedResults[] = new String[] { "org1", "org2", "org3", "org4", "org5", "org6", "org7", "org8" };
253
254         String secondExpectedResults[] = new String[] { "continuum-webapp" };
255
256         String thirdExpectedResults[] = new String[] { "archiva-common" };
257
258         // search for groupId
259         assertFilteredSearchResults( expectedRepos, expectedResults, search, "org", null, null, null, 30 );
260
261         // search for groupId and artifactId
262         assertFilteredSearchResults( expectedRepos, secondExpectedResults, search, "org.apache.maven",
263                                      "continuum-webapp", null, null, 30 );
264
265         // search for groupId , artifactId and version
266         assertFilteredSearchResults( expectedRepos, thirdExpectedResults, search, "org.apache.maven.archiva",
267                                      "archiva-common", "1.0", null, 30 );
268     }
269     
270     private void assertFilteredSearchResults ( String expectedRepos[], String expectedResults[], CrossRepositorySearch search, 
271                                                String groupId, String artifactId, String version, String className , int rowCount )
272     {
273         SearchResultLimits limits = new SearchResultLimits( 0 );
274         limits.setPageSize(  rowCount );
275         
276         List<String> selectedRepos = new ArrayList<String>();
277         selectedRepos.addAll( Arrays.asList( expectedRepos ) );
278         
279         SearchResults results = null;
280         
281         results = search.executeFilteredSearch( "guest" , selectedRepos, groupId, artifactId, version, className, limits );
282         
283         assertNotNull( "Search Results should not be null.", results );
284         assertEquals( "Repository Hits", expectedRepos.length, results.getRepositories().size() );
285         assertEquals( expectedRepos.length, 1);
286         assertEquals( TEST_DEFAULT_REPO_ID , selectedRepos.get( 0 ) );
287         assertEquals( "Search Result Hits", expectedResults.length, results.getHits().size() );
288     }
289     
290     private void assertSearchResults( String expectedRepos[], String expectedResults[], CrossRepositorySearch search,
291                                       String term, List<String> previousSearchTerms, boolean bytecode )
292         throws Exception
293     {
294         SearchResultLimits limits = new SearchResultLimits( 0 );
295         limits.setPageSize( 20 );
296         
297         List<String> selectedRepos = new ArrayList<String>();
298         selectedRepos.addAll( Arrays.asList( expectedRepos ) );
299        
300         SearchResults results = null;
301
302         if( previousSearchTerms == null )
303             {
304                 if( bytecode )
305             {
306                 results = search.searchForBytecode( "guest", selectedRepos, term, limits );
307             }
308             else
309             {
310                 results = search.searchForTerm( "guest", selectedRepos, term, limits );
311             }
312         }
313         else
314         {
315             results = search.searchForTerm( "guest", selectedRepos, term, limits, previousSearchTerms );
316         }
317
318         
319         assertNotNull( "Search Results should not be null.", results );
320         assertEquals( "Repository Hits", expectedRepos.length, results.getRepositories().size() );
321         
322         // TODO: test the repository ids returned.
323
324         assertEquals( "Search Result Hits", expectedResults.length, results.getHits().size() );
325         // TODO: test the order of hits.
326         // TODO: test the value of the hits.
327     }
328     
329     protected ManagedRepositoryConfiguration createRepository( String id, String name, File location )
330     {
331         ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
332         repo.setId( id );
333         repo.setName( name );
334         repo.setLocation( location.getAbsolutePath() );
335         return repo;
336     }
337 }