1 package org.apache.maven.archiva.indexer.search;
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
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;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.List;
44 * DefaultCrossRepositorySearchTest
48 public class DefaultCrossRepositorySearchTest
49 extends PlexusInSpringTestCase
51 private static final String TEST_DEFAULT_REPOSITORY_NAME = "Test Default Repository";
53 private static final String TEST_DEFAULT_REPO_ID = "testDefaultRepo";
56 protected void setUp()
61 RepositoryContentIndexFactory indexFactory =
62 (RepositoryContentIndexFactory) lookup( RepositoryContentIndexFactory.class
63 .getName(), "lucene" );
65 File repoDir = new File( getBasedir(), "src/test/managed-repository" );
67 assertTrue( "Default Test Repository should exist.", repoDir.exists() && repoDir.isDirectory() );
69 ManagedRepositoryConfiguration repository = createRepository( TEST_DEFAULT_REPO_ID, TEST_DEFAULT_REPOSITORY_NAME, repoDir );
71 File indexLocation = new File( "target/index-crossrepo-" + getName() + "/" );
73 MockConfiguration config = (MockConfiguration) lookup( ArchivaConfiguration.class.getName(), "mock" );
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 );
82 if ( indexLocation.exists() )
84 FileUtils.deleteDirectory( indexLocation );
87 config.getConfiguration().addManagedRepository( repoConfig );
89 // Create the (empty) indexes.
90 RepositoryContentIndex indexHashcode = indexFactory.createHashcodeIndex( repository );
91 RepositoryContentIndex indexBytecode = indexFactory.createBytecodeIndex( repository );
92 RepositoryContentIndex indexContents = indexFactory.createFileContentIndex( repository );
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() );
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() );
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() );
111 private void assertRecordCount( RepositoryContentIndex index, int expectedCount )
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() );
120 private CrossRepositorySearch lookupCrossRepositorySearch()
123 CrossRepositorySearch search =
124 (CrossRepositorySearch) lookup( CrossRepositorySearch.class.getName(), "default" );
125 assertNotNull( "CrossRepositorySearch:default should not be null.", search );
129 public void testSearchTerm_Org()
132 CrossRepositorySearch search = lookupCrossRepositorySearch();
134 String expectedRepos[] = new String[] {
138 String expectedResults[] = new String[] {
139 "org","org2","org3","org4","org5","org6","org7"
142 assertSearchResults( expectedRepos, expectedResults, search, "org", null, false );
145 public void testSearchInvalidTerm()
148 CrossRepositorySearch search = lookupCrossRepositorySearch();
150 String expectedRepos[] = new String[] {
154 String expectedResults[] = new String[] {
158 assertSearchResults( expectedRepos, expectedResults, search, "monosodium", null, false );
161 public void testSearchForClassesAndPackages()
164 CrossRepositorySearch search = lookupCrossRepositorySearch();
166 String expectedRepos[] = new String[] {
170 String expectedResults[] = new String[] {
171 "archiva-common-1.0.jar"
174 // class with packagename search
175 assertSearchResults( expectedRepos, expectedResults, search,
176 "org.apache.maven.archiva.common.utils.BaseFile", null, true );
178 assertSearchResults( expectedRepos, expectedResults, search,
179 "BaseFile", null, true );
181 String expectedMethodSearchResults[] = new String[] {
182 "continuum-webapp-1.0.3-SNAPSHOT.war"
186 assertSearchResults( expectedRepos, expectedMethodSearchResults, search,
187 "org.apache.maven.continuum.web.action.BuildDefinitionAction.isBuildFresh", null, true );
190 public void testExecuteFilteredSearch()
193 CrossRepositorySearch search = lookupCrossRepositorySearch();
195 String expectedRepos[] = new String[] { TEST_DEFAULT_REPO_ID };
197 String expectedResults[] = new String[] { "org1", "org2", "org3", "org4", "org5", "org6", "org7", "org8" };
199 String secondExpectedResults[] = new String[] { "continuum-webapp" };
201 String thirdExpectedResults[] = new String[] { "archiva-common" };
203 // search for groupId
204 assertFilteredSearchResults( expectedRepos, expectedResults, search, "org", null, null, null, 30 );
206 // search for groupId and artifactId
207 assertFilteredSearchResults( expectedRepos, secondExpectedResults, search, "org.apache.maven",
208 "continuum-webapp", null, null, 30 );
210 // search for groupId , artifactId and version
211 assertFilteredSearchResults( expectedRepos, thirdExpectedResults, search, "org.apache.maven.archiva",
212 "archiva-common", "1.0", null, 30 );
215 // MRM-981 - artifactIds with numeric characters aren't found in advanced search
216 public void testFilteredSearchArtifactIdHasNumericChar()
219 CrossRepositorySearch search = lookupCrossRepositorySearch();
221 String expectedRepos[] = new String[] { TEST_DEFAULT_REPO_ID };
223 String expectedResults[] = new String[] { "a-common5" };
225 assertFilteredSearchResults( expectedRepos, expectedResults, search, null, "a-common5", null, null, 30 );
227 assertFilteredSearchResults( expectedRepos, expectedResults, search, "a", "a-common5", null, null, 30 );
229 assertFilteredSearchResults( expectedRepos, expectedResults, search, "a", "a-common5", "1.0", null, 30 );
231 assertFilteredSearchResults( expectedRepos, expectedResults, search, "a", "a-common5", "1.0", "ACommonTestFile", 30 );
233 assertFilteredSearchResults( expectedRepos, expectedResults, search, "a", "a-common5", "1.0", "a.common5.package.", 30 );
235 String noHitsExpectedResults[] = new String[] {};
237 assertFilteredSearchResults( expectedRepos, noHitsExpectedResults, search, "org.apache.maven.archiva",
238 "a-common5", null, null, 30 );
241 private void assertFilteredSearchResults ( String expectedRepos[], String expectedResults[], CrossRepositorySearch search,
242 String groupId, String artifactId, String version, String className , int rowCount )
244 SearchResultLimits limits = new SearchResultLimits( 0 );
245 limits.setPageSize( rowCount );
247 List<String> selectedRepos = new ArrayList<String>();
248 selectedRepos.addAll( Arrays.asList( expectedRepos ) );
250 SearchResults results = null;
252 results = search.executeFilteredSearch( "guest" , selectedRepos, groupId, artifactId, version, className, limits );
254 assertNotNull( "Search Results should not be null.", results );
255 assertEquals( "Repository Hits", expectedRepos.length, results.getRepositories().size() );
256 assertEquals( expectedRepos.length, 1);
257 assertEquals( TEST_DEFAULT_REPO_ID , selectedRepos.get( 0 ) );
258 assertEquals( "Search Result Hits", expectedResults.length, results.getHits().size() );
261 private void assertSearchResults( String expectedRepos[], String expectedResults[], CrossRepositorySearch search,
262 String term, List<String> previousSearchTerms, boolean bytecode )
265 SearchResultLimits limits = new SearchResultLimits( 0 );
266 limits.setPageSize( 20 );
268 List<String> selectedRepos = new ArrayList<String>();
269 selectedRepos.addAll( Arrays.asList( expectedRepos ) );
271 SearchResults results = null;
273 if( previousSearchTerms == null )
277 results = search.searchForBytecode( "guest", selectedRepos, term, limits );
281 results = search.searchForTerm( "guest", selectedRepos, term, limits );
286 results = search.searchForTerm( "guest", selectedRepos, term, limits, previousSearchTerms );
290 assertNotNull( "Search Results should not be null.", results );
291 assertEquals( "Repository Hits", expectedRepos.length, results.getRepositories().size() );
293 // TODO: test the repository ids returned.
295 assertEquals( "Search Result Hits", expectedResults.length, results.getHits().size() );
296 // TODO: test the order of hits.
297 // TODO: test the value of the hits.
300 protected ManagedRepositoryConfiguration createRepository( String id, String name, File location )
302 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
304 repo.setName( name );
305 repo.setLocation( location.getAbsolutePath() );