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.lucene.search.Hits;
23 import org.apache.lucene.search.MatchAllDocsQuery;
24 import org.apache.lucene.search.Query;
25 import org.apache.lucene.search.Searcher;
26 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
27 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
28 import org.apache.maven.archiva.indexer.MockConfiguration;
29 import org.apache.maven.archiva.indexer.RepositoryContentIndex;
30 import org.apache.maven.archiva.indexer.RepositoryContentIndexFactory;
31 import org.apache.maven.archiva.indexer.bytecode.BytecodeRecord;
32 import org.apache.maven.archiva.indexer.filecontent.FileContentRecord;
33 import org.apache.maven.archiva.indexer.hashcodes.HashcodesRecord;
34 import org.codehaus.plexus.PlexusTestCase;
35 import org.codehaus.plexus.util.FileUtils;
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 PlexusTestCase
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" );
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" );
162 public void testSearchInvalidTerm()
165 CrossRepositorySearch search = lookupCrossRepositorySearch();
167 String expectedRepos[] = new String[] {
171 String expectedResults[] = new String[] {
175 assertSearchResults( expectedRepos, expectedResults, search, "monosodium" );
178 private void assertSearchResults( String expectedRepos[], String expectedResults[], CrossRepositorySearch search, String term )
181 SearchResultLimits limits = new SearchResultLimits( 0 );
182 limits.setPageSize( 20 );
184 List<String> selectedRepos = new ArrayList<String>();
185 selectedRepos.addAll( Arrays.asList( expectedRepos ) );
187 SearchResults results = search.searchForTerm( "guest", selectedRepos, term, limits );
189 assertNotNull( "Search Results should not be null.", results );
190 assertEquals( "Repository Hits", expectedRepos.length, results.getRepositories().size() );
191 // TODO: test the repository ids returned.
193 assertEquals( "Search Result Hits", expectedResults.length, results.getHits().size() );
194 // TODO: test the order of hits.
195 // TODO: test the value of the hits.
198 protected ManagedRepositoryConfiguration createRepository( String id, String name, File location )
200 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
202 repo.setName( name );
203 repo.setLocation( location.getAbsolutePath() );