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