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
46 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
49 public class DefaultCrossRepositorySearchTest
50 extends PlexusInSpringTestCase
52 private static final String TEST_DEFAULT_REPOSITORY_NAME = "Test Default Repository";
54 private static final String TEST_DEFAULT_REPO_ID = "testDefaultRepo";
57 protected void setUp()
62 RepositoryContentIndexFactory indexFactory =
63 (RepositoryContentIndexFactory) lookup( RepositoryContentIndexFactory.class
64 .getName(), "lucene" );
66 File repoDir = new File( getBasedir(), "src/test/managed-repository" );
68 assertTrue( "Default Test Repository should exist.", repoDir.exists() && repoDir.isDirectory() );
70 ManagedRepositoryConfiguration repository = createRepository( TEST_DEFAULT_REPO_ID, TEST_DEFAULT_REPOSITORY_NAME, repoDir );
72 File indexLocation = new File( "target/index-crossrepo-" + getName() + "/" );
74 MockConfiguration config = (MockConfiguration) lookup( ArchivaConfiguration.class.getName(), "mock" );
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 );
83 if ( indexLocation.exists() )
85 FileUtils.deleteDirectory( indexLocation );
88 config.getConfiguration().addManagedRepository( repoConfig );
90 // Create the (empty) indexes.
91 RepositoryContentIndex indexHashcode = indexFactory.createHashcodeIndex( repository );
92 RepositoryContentIndex indexBytecode = indexFactory.createBytecodeIndex( repository );
93 RepositoryContentIndex indexContents = indexFactory.createFileContentIndex( repository );
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() );
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() );
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() );
112 private void assertRecordCount( RepositoryContentIndex index, int expectedCount )
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() );
121 private CrossRepositorySearch lookupCrossRepositorySearch()
124 CrossRepositorySearch search =
125 (CrossRepositorySearch) lookup( CrossRepositorySearch.class.getName(), "default" );
126 assertNotNull( "CrossRepositorySearch:default should not be null.", search );
130 public void testSearchTerm_Org()
133 CrossRepositorySearch search = lookupCrossRepositorySearch();
135 String expectedRepos[] = new String[] {
139 String expectedResults[] = new String[] {
140 "org","org2","org3","org4","org5","org6","org7"
143 assertSearchResults( expectedRepos, expectedResults, search, "org", null );
146 public void testSearchTerm_Junit()
149 CrossRepositorySearch search = lookupCrossRepositorySearch();
151 String expectedRepos[] = new String[] {
155 String expectedResults[] = new String[] {
156 "junit","junit2","junit3"
159 assertSearchResults( expectedRepos, expectedResults, search, "junit", null );
162 public void testSearchInvalidTerm()
165 CrossRepositorySearch search = lookupCrossRepositorySearch();
167 String expectedRepos[] = new String[] {
171 String expectedResults[] = new String[] {
175 assertSearchResults( expectedRepos, expectedResults, search, "monosodium", null );
178 public void testSearchWithinSearchResults()
181 CrossRepositorySearch search = lookupCrossRepositorySearch();
183 String expectedRepos[] = new String[] {
187 String expectedResults[] = new String[] {
188 "org","org2","org3","org4","org5","org6","org7"
192 assertSearchResults( expectedRepos, expectedResults, search, "org", null );
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"
204 assertSearchResults( expectedRepos, secondSearchExpectedResults, search, "org.apache.maven.archiva.record",
205 previousSearchTerms );
207 previousSearchTerms.add( "org.apache.maven.archiva.record" );
208 String thirdSearchExpectedResults[] = new String[] {
209 "junit", "junit2", "junit3"
213 assertSearchResults( expectedRepos, thirdSearchExpectedResults, search, "junit", previousSearchTerms );
216 private void assertSearchResults( String expectedRepos[], String expectedResults[], CrossRepositorySearch search,
217 String term, List<String> previousSearchTerms )
220 SearchResultLimits limits = new SearchResultLimits( 0 );
221 limits.setPageSize( 20 );
223 List<String> selectedRepos = new ArrayList<String>();
224 selectedRepos.addAll( Arrays.asList( expectedRepos ) );
226 SearchResults results = null;
227 if( previousSearchTerms == null )
229 results = search.searchForTerm( "guest", selectedRepos, term, limits );
233 results = search.searchForTerm( "guest", selectedRepos, term, limits, previousSearchTerms );
236 assertNotNull( "Search Results should not be null.", results );
237 assertEquals( "Repository Hits", expectedRepos.length, results.getRepositories().size() );
238 // TODO: test the repository ids returned.
240 assertEquals( "Search Result Hits", expectedResults.length, results.getHits().size() );
241 // TODO: test the order of hits.
242 // TODO: test the value of the hits.
245 protected ManagedRepositoryConfiguration createRepository( String id, String name, File location )
247 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
249 repo.setName( name );
250 repo.setLocation( location.getAbsolutePath() );