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.getHits().size() );
205 assertEquals( 2, results.getTotalHits() );
206 assertEquals( limits, results.getLimits() );
208 archivaConfigControl.reset();
211 limits = new SearchResultLimits( 1 );
212 limits.setPageSize( 1 );
214 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
216 archivaConfigControl.replay();
218 results = search.search( "user", selectedRepos, "org", limits, null );
220 archivaConfigControl.verify();
222 assertNotNull( results );
223 assertEquals( 1, results.getHits().size() );
224 assertEquals( 2, results.getTotalHits() );
225 assertEquals( limits, results.getLimits() );
228 public void testArtifactFoundInMultipleRepositories()
231 List<File> files = new ArrayList<File>();
232 files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 +
233 "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
234 files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 +
235 "/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar" ) );
236 createIndex( TEST_REPO_2, files );
238 List<String> selectedRepos = new ArrayList<String>();
239 selectedRepos.add( TEST_REPO_1 );
240 selectedRepos.add( TEST_REPO_2 );
242 config.addManagedRepository( createRepositoryConfig( TEST_REPO_2 ) );
244 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 2 );
246 archivaConfigControl.replay();
248 SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
250 archivaConfigControl.verify();
252 assertNotNull( results );
253 assertEquals( 1, results.getTotalHits() );
255 SearchResultHit hit = results.getHits().get( 0 );
256 assertEquals( "org.apache.archiva", hit.getGroupId() );
257 assertEquals( "archiva-search", hit.getArtifactId() );
258 assertEquals( 2, hit.getVersions().size() );
259 assertTrue( hit.getVersions().contains( "1.0" ) );
260 assertTrue( hit.getVersions().contains( "1.1" ) );
262 archivaConfigControl.reset();
264 FileUtils.deleteDirectory( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ) );
265 assertFalse( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ).exists() );
267 // TODO: [BROWSE] in artifact info from browse, display all the repositories where the artifact is found
270 public void testNoMatchFound()
273 List<String> selectedRepos = new ArrayList<String>();
274 selectedRepos.add( TEST_REPO_1 );
276 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
278 archivaConfigControl.replay();
280 SearchResults results = search.search( "user", selectedRepos, "dfghdfkweriuasndsaie", null, null );
282 archivaConfigControl.verify();
284 assertNotNull( results );
285 assertEquals( 0, results.getTotalHits() );
288 public void testNoIndexFound()
291 List<String> selectedRepos = new ArrayList<String>();
292 selectedRepos.add( "non-existing-repo" );
294 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
296 archivaConfigControl.replay();
298 SearchResults results = search.search( "user", selectedRepos, "org.apache.archiva", null, null );
299 assertNotNull( results );
300 assertEquals( 0, results.getTotalHits() );
302 archivaConfigControl.verify();
305 public void testSearchWithinSearchResults()
308 List<String> selectedRepos = new ArrayList<String>();
309 selectedRepos.add( TEST_REPO_1 );
311 List<String> previousSearchTerms = new ArrayList<String>();
312 previousSearchTerms.add( "archiva-test" );
314 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
316 archivaConfigControl.replay();
318 SearchResults results = search.search( "user", selectedRepos, "1.0", null, previousSearchTerms );
320 archivaConfigControl.verify();
322 assertNotNull( results );
323 assertEquals( 1, results.getTotalHits() );
325 SearchResultHit hit = results.getHits().get( 0 );
326 assertEquals( "org.apache.archiva", hit.getGroupId() );
327 assertEquals( "archiva-test", hit.getArtifactId() );
328 assertEquals( 1, hit.getVersions().size() );
329 assertEquals( "1.0", hit.getVersions().get( 0 ) );
332 public void testAdvancedSearch()
335 List<File> files = new ArrayList<File>();
336 files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 +
337 "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
338 files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 +
339 "/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar" ) );
340 createIndex( TEST_REPO_2, files );
342 List<String> selectedRepos = new ArrayList<String>();
343 selectedRepos.add( TEST_REPO_1 );
344 selectedRepos.add( TEST_REPO_2 );
346 config.addManagedRepository( createRepositoryConfig( TEST_REPO_2 ) );
348 SearchFields searchFields = new SearchFields();
349 searchFields.setGroupId( "org.apache.archiva" );
350 searchFields.setVersion( "1.0" );
351 searchFields.setRepositories( selectedRepos );
353 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 2 );
355 archivaConfigControl.replay();
357 SearchResults results = search.search( "user", searchFields, null );
359 archivaConfigControl.verify();
361 assertNotNull( results );
362 assertEquals( 2, results.getTotalHits() );
364 FileUtils.deleteDirectory( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ) );
365 assertFalse( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ).exists() );
368 public void testAdvancedSearchWithPagination()
371 List<File> files = new ArrayList<File>();
372 files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 +
373 "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
374 files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 +
375 "/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar" ) );
376 createIndex( TEST_REPO_2, files );
378 List<String> selectedRepos = new ArrayList<String>();
379 selectedRepos.add( TEST_REPO_1 );
380 selectedRepos.add( TEST_REPO_2 );
382 config.addManagedRepository( createRepositoryConfig( TEST_REPO_2 ) );
384 SearchFields searchFields = new SearchFields();
385 searchFields.setGroupId( "org.apache.archiva" );
386 searchFields.setVersion( "1.0" );
387 searchFields.setRepositories( selectedRepos );
391 SearchResultLimits limits = new SearchResultLimits( 0 );
392 limits.setPageSize( 1 );
394 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 2 );
396 archivaConfigControl.replay();
398 SearchResults results = search.search( "user", searchFields, limits );
400 archivaConfigControl.verify();
402 assertNotNull( results );
403 assertEquals( 2, results.getTotalHits() );
404 assertEquals( 1, results.getHits().size() );
407 archivaConfigControl.reset();
409 limits = new SearchResultLimits( 1 );
410 limits.setPageSize( 1 );
412 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 2 );
414 archivaConfigControl.replay();
416 results = search.search( "user", searchFields, limits );
418 archivaConfigControl.verify();
420 assertNotNull( results );
421 assertEquals( 2, results.getTotalHits() );
422 assertEquals( 1, results.getHits().size() );
424 FileUtils.deleteDirectory( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ) );
425 assertFalse( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ).exists() );
428 // TODO: add test when an existing index already exists