]> source.dussan.org Git - archiva.git/blob
71c872d671651759bb70c5ff82f2b01c53f28a54
[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     private void assertSearchResults( String expectedRepos[], String expectedResults[], CrossRepositorySearch search,
246                                       String term, List<String> previousSearchTerms, boolean bytecode )
247         throws Exception
248     {
249         SearchResultLimits limits = new SearchResultLimits( 0 );
250         limits.setPageSize( 20 );
251         
252         List<String> selectedRepos = new ArrayList<String>();
253         selectedRepos.addAll( Arrays.asList( expectedRepos ) );
254         
255         SearchResults results = null;
256         if( previousSearchTerms == null )
257         {
258             if( bytecode )
259             {
260                 results = search.searchForBytecode( "guest", selectedRepos, term, limits );
261             }
262             else
263             {
264                 results = search.searchForTerm( "guest", selectedRepos, term, limits );
265             }
266         }
267         else
268         {
269             results = search.searchForTerm( "guest", selectedRepos, term, limits, previousSearchTerms );
270         }
271         
272         assertNotNull( "Search Results should not be null.", results );
273         assertEquals( "Repository Hits", expectedRepos.length, results.getRepositories().size() );
274         // TODO: test the repository ids returned.
275
276         assertEquals( "Search Result Hits", expectedResults.length, results.getHits().size() );
277         // TODO: test the order of hits.
278         // TODO: test the value of the hits.
279     }
280
281     protected ManagedRepositoryConfiguration createRepository( String id, String name, File location )
282     {
283         ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
284         repo.setId( id );
285         repo.setName( name );
286         repo.setLocation( location.getAbsolutePath() );
287         return repo;
288     }
289 }