1 package org.apache.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
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
27 import org.apache.commons.io.FileUtils;
28 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
29 import org.apache.maven.archiva.configuration.Configuration;
30 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
31 import org.apache.maven.archiva.indexer.search.SearchResultHit;
32 import org.apache.maven.archiva.indexer.search.SearchResultLimits;
33 import org.apache.maven.archiva.indexer.search.SearchResults;
34 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
35 import org.easymock.MockControl;
36 import org.sonatype.nexus.index.ArtifactContext;
37 import org.sonatype.nexus.index.ArtifactContextProducer;
38 import org.sonatype.nexus.index.NexusIndexer;
39 import org.sonatype.nexus.index.context.IndexingContext;
40 import org.sonatype.nexus.index.context.UnsupportedExistingLuceneIndexException;
41 import org.sonatype.nexus.index.creator.IndexerEngine;
43 public class NexusRepositorySearchTest
44 extends PlexusInSpringTestCase
46 private RepositorySearch search;
48 private ArchivaConfiguration archivaConfig;
50 private NexusIndexer indexer;
52 private IndexingContext context;
54 private IndexerEngine indexerEngine;
56 private ArtifactContextProducer artifactContextProducer;
58 private MockControl archivaConfigControl;
60 private Configuration config;
62 private final static String TEST_REPO_1 = "nexus-search-test-repo";
64 private final static String TEST_REPO_2 = "nexus-search-test-repo-2";
67 protected void setUp()
72 indexer = (NexusIndexer) lookup( NexusIndexer.class );
74 archivaConfigControl = MockControl.createControl( ArchivaConfiguration.class );
76 archivaConfig = (ArchivaConfiguration) archivaConfigControl.getMock();
78 search = new NexusRepositorySearch( indexer, archivaConfig );
80 indexerEngine = (IndexerEngine) lookup( IndexerEngine.class );
82 artifactContextProducer = (ArtifactContextProducer) lookup( ArtifactContextProducer.class );
84 config = new Configuration();
85 config.addManagedRepository( createRepositoryConfig( TEST_REPO_1 ) );
87 List<File> files = new ArrayList<File>();
88 files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 +
89 "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
90 files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 +
91 "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" ) );
92 files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 +
93 "/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ) );
95 createIndex( TEST_REPO_1, files );
98 private ManagedRepositoryConfiguration createRepositoryConfig( String repository )
100 ManagedRepositoryConfiguration repositoryConfig = new ManagedRepositoryConfiguration();
101 repositoryConfig.setId( repository );
102 repositoryConfig.setLocation( getBasedir() + "/target/test-classes/" + repository );
103 repositoryConfig.setLayout( "default" );
104 repositoryConfig.setName( repository );
105 repositoryConfig.setScanned( true );
106 repositoryConfig.setSnapshots( false );
107 repositoryConfig.setReleases( true );
109 return repositoryConfig;
113 protected void tearDown()
116 FileUtils.deleteDirectory( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + "/.indexer" ) );
117 assertFalse( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + "/.indexer" ).exists() );
122 private void createIndex( String repository, List<File> filesToBeIndexed )
123 throws IOException, UnsupportedExistingLuceneIndexException
126 indexer.addIndexingContext( repository, repository, new File( getBasedir(), "/target/test-classes/" +
127 repository ), new File( getBasedir(), "/target/test-classes/" + repository + "/.indexer" ), null, null,
128 NexusIndexer.FULL_INDEX );
129 context.setSearchable( true );
131 indexerEngine.beginIndexing( context );
133 for ( File artifactFile : filesToBeIndexed )
135 ArtifactContext ac = artifactContextProducer.getArtifactContext( context, artifactFile );
136 indexerEngine.index( context, ac );
139 indexerEngine.endIndexing( context );
140 indexer.removeIndexingContext( context, false );
142 assertTrue( new File( getBasedir(), "/target/test-classes/" + repository + "/.indexer" ).exists() );
145 public void testQuickSearch()
148 List<String> selectedRepos = new ArrayList<String>();
149 selectedRepos.add( TEST_REPO_1 );
152 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
154 archivaConfigControl.replay();
156 SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
158 archivaConfigControl.verify();
160 assertNotNull( results );
161 assertEquals( 1, results.getTotalHits() );
163 SearchResultHit hit = results.getHits().get( 0 );
164 assertEquals( "org.apache.archiva", hit.getGroupId() );
165 assertEquals( "archiva-search", hit.getArtifactId() );
166 assertEquals( "1.0", hit.getVersions().get( 0 ) );
168 archivaConfigControl.reset();
171 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
173 archivaConfigControl.replay();
175 results = search.search( "user", selectedRepos, "org.apache.archiva", null, null );
177 archivaConfigControl.verify();
179 assertNotNull( results );
180 assertEquals( 2, results.getTotalHits() );
182 //TODO: search for class & package names
185 public void testQuickSearchWithPagination()
188 List<String> selectedRepos = new ArrayList<String>();
189 selectedRepos.add( TEST_REPO_1 );
192 SearchResultLimits limits = new SearchResultLimits( 0 );
193 limits.setPageSize( 1 );
195 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
197 archivaConfigControl.replay();
199 SearchResults results = search.search( "user", selectedRepos, "org", limits, null );
201 archivaConfigControl.verify();
203 assertNotNull( results );
204 assertEquals( 1, results.getTotalHits() );
206 archivaConfigControl.reset();
209 limits = new SearchResultLimits( 1 );
210 limits.setPageSize( 1 );
212 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
214 archivaConfigControl.replay();
216 results = search.search( "user", selectedRepos, "org", limits, null );
218 archivaConfigControl.verify();
220 assertNotNull( results );
221 assertEquals( 1, results.getTotalHits() );
224 public void testArtifactFoundInMultipleRepositories()
227 List<File> files = new ArrayList<File>();
228 files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 +
229 "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
230 files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 +
231 "/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar" ) );
232 createIndex( TEST_REPO_2, files );
234 List<String> selectedRepos = new ArrayList<String>();
235 selectedRepos.add( TEST_REPO_1 );
236 selectedRepos.add( TEST_REPO_2 );
238 config.addManagedRepository( createRepositoryConfig( TEST_REPO_2 ) );
240 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 2 );
242 archivaConfigControl.replay();
244 SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
246 archivaConfigControl.verify();
248 assertNotNull( results );
249 assertEquals( 1, results.getTotalHits() );
251 SearchResultHit hit = results.getHits().get( 0 );
252 assertEquals( "org.apache.archiva", hit.getGroupId() );
253 assertEquals( "archiva-search", hit.getArtifactId() );
254 assertEquals( 2, hit.getVersions().size() );
255 assertTrue( hit.getVersions().contains( "1.0" ) );
256 assertTrue( hit.getVersions().contains( "1.1" ) );
258 archivaConfigControl.reset();
260 FileUtils.deleteDirectory( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ) );
261 assertFalse( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ).exists() );
263 // TODO: [BROWSE] in artifact info from browse, display all the repositories where the artifact is found
266 public void testNoMatchFound()
269 List<String> selectedRepos = new ArrayList<String>();
270 selectedRepos.add( TEST_REPO_1 );
272 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
274 archivaConfigControl.replay();
276 SearchResults results = search.search( "user", selectedRepos, "dfghdfkweriuasndsaie", null, null );
278 archivaConfigControl.verify();
280 assertNotNull( results );
281 assertEquals( 0, results.getTotalHits() );
284 public void testNoIndexFound()
287 List<String> selectedRepos = new ArrayList<String>();
288 selectedRepos.add( "non-existing-repo" );
290 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
292 archivaConfigControl.replay();
294 SearchResults results = search.search( "user", selectedRepos, "org.apache.archiva", null, null );
295 assertNotNull( results );
296 assertEquals( 0, results.getTotalHits() );
298 archivaConfigControl.verify();
301 public void testSearchWithinSearchResults()
304 List<String> selectedRepos = new ArrayList<String>();
305 selectedRepos.add( TEST_REPO_1 );
307 List<String> previousSearchTerms = new ArrayList<String>();
308 previousSearchTerms.add( "archiva-test" );
310 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
312 archivaConfigControl.replay();
314 SearchResults results = search.search( "user", selectedRepos, "1.0", null, previousSearchTerms );
316 archivaConfigControl.verify();
318 assertNotNull( results );
319 assertEquals( 1, results.getTotalHits() );
321 SearchResultHit hit = results.getHits().get( 0 );
322 assertEquals( "org.apache.archiva", hit.getGroupId() );
323 assertEquals( "archiva-test", hit.getArtifactId() );
324 assertEquals( 1, hit.getVersions().size() );
325 assertEquals( "1.0", hit.getVersions().get( 0 ) );
328 public void testAdvancedSearch()