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() );
207 archivaConfigControl.reset();
210 limits = new SearchResultLimits( 1 );
211 limits.setPageSize( 1 );
213 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
215 archivaConfigControl.replay();
217 results = search.search( "user", selectedRepos, "org", limits, null );
219 archivaConfigControl.verify();
221 assertNotNull( results );
222 assertEquals( 1, results.getHits().size() );
223 assertEquals( 2, results.getTotalHits() );
226 public void testArtifactFoundInMultipleRepositories()
229 List<File> files = new ArrayList<File>();
230 files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 +
231 "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
232 files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 +
233 "/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar" ) );
234 createIndex( TEST_REPO_2, files );
236 List<String> selectedRepos = new ArrayList<String>();
237 selectedRepos.add( TEST_REPO_1 );
238 selectedRepos.add( TEST_REPO_2 );
240 config.addManagedRepository( createRepositoryConfig( TEST_REPO_2 ) );
242 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 2 );
244 archivaConfigControl.replay();
246 SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
248 archivaConfigControl.verify();
250 assertNotNull( results );
251 assertEquals( 1, results.getTotalHits() );
253 SearchResultHit hit = results.getHits().get( 0 );
254 assertEquals( "org.apache.archiva", hit.getGroupId() );
255 assertEquals( "archiva-search", hit.getArtifactId() );
256 assertEquals( 2, hit.getVersions().size() );
257 assertTrue( hit.getVersions().contains( "1.0" ) );
258 assertTrue( hit.getVersions().contains( "1.1" ) );
260 archivaConfigControl.reset();
262 FileUtils.deleteDirectory( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ) );
263 assertFalse( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ).exists() );
265 // TODO: [BROWSE] in artifact info from browse, display all the repositories where the artifact is found
268 public void testNoMatchFound()
271 List<String> selectedRepos = new ArrayList<String>();
272 selectedRepos.add( TEST_REPO_1 );
274 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
276 archivaConfigControl.replay();
278 SearchResults results = search.search( "user", selectedRepos, "dfghdfkweriuasndsaie", null, null );
280 archivaConfigControl.verify();
282 assertNotNull( results );
283 assertEquals( 0, results.getTotalHits() );
286 public void testNoIndexFound()
289 List<String> selectedRepos = new ArrayList<String>();
290 selectedRepos.add( "non-existing-repo" );
292 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
294 archivaConfigControl.replay();
296 SearchResults results = search.search( "user", selectedRepos, "org.apache.archiva", null, null );
297 assertNotNull( results );
298 assertEquals( 0, results.getTotalHits() );
300 archivaConfigControl.verify();
303 public void testSearchWithinSearchResults()
306 List<String> selectedRepos = new ArrayList<String>();
307 selectedRepos.add( TEST_REPO_1 );
309 List<String> previousSearchTerms = new ArrayList<String>();
310 previousSearchTerms.add( "archiva-test" );
312 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
314 archivaConfigControl.replay();
316 SearchResults results = search.search( "user", selectedRepos, "1.0", null, previousSearchTerms );
318 archivaConfigControl.verify();
320 assertNotNull( results );
321 assertEquals( 1, results.getTotalHits() );
323 SearchResultHit hit = results.getHits().get( 0 );
324 assertEquals( "org.apache.archiva", hit.getGroupId() );
325 assertEquals( "archiva-test", hit.getArtifactId() );
326 assertEquals( 1, hit.getVersions().size() );
327 assertEquals( "1.0", hit.getVersions().get( 0 ) );
330 public void testAdvancedSearch()
333 List<File> files = new ArrayList<File>();
334 files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 +
335 "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
336 files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 +
337 "/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar" ) );
338 createIndex( TEST_REPO_2, files );
340 List<String> selectedRepos = new ArrayList<String>();
341 selectedRepos.add( TEST_REPO_1 );
342 selectedRepos.add( TEST_REPO_2 );
344 config.addManagedRepository( createRepositoryConfig( TEST_REPO_2 ) );
346 SearchFields searchFields = new SearchFields();
347 searchFields.setGroupId( "org.apache.archiva" );
348 searchFields.setVersion( "1.0" );
349 searchFields.setRepositories( selectedRepos );
351 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 2 );
353 archivaConfigControl.replay();
355 SearchResults results = search.search( "user", searchFields, null );
357 archivaConfigControl.verify();
359 assertNotNull( results );
360 assertEquals( 2, results.getTotalHits() );
362 FileUtils.deleteDirectory( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ) );
363 assertFalse( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ).exists() );
366 public void testAdvancedSearchWithPagination()
369 List<File> files = new ArrayList<File>();
370 files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 +
371 "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
372 files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 +
373 "/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar" ) );
374 createIndex( TEST_REPO_2, files );
376 List<String> selectedRepos = new ArrayList<String>();
377 selectedRepos.add( TEST_REPO_1 );
378 selectedRepos.add( TEST_REPO_2 );
380 config.addManagedRepository( createRepositoryConfig( TEST_REPO_2 ) );
382 SearchFields searchFields = new SearchFields();
383 searchFields.setGroupId( "org.apache.archiva" );
384 searchFields.setVersion( "1.0" );
385 searchFields.setRepositories( selectedRepos );
389 SearchResultLimits limits = new SearchResultLimits( 0 );
390 limits.setPageSize( 1 );
392 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 2 );
394 archivaConfigControl.replay();
396 SearchResults results = search.search( "user", searchFields, limits );
398 archivaConfigControl.verify();
400 assertNotNull( results );
401 assertEquals( 2, results.getTotalHits() );
402 assertEquals( 1, results.getHits().size() );
405 archivaConfigControl.reset();
407 limits = new SearchResultLimits( 1 );
408 limits.setPageSize( 1 );
410 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 2 );
412 archivaConfigControl.replay();
414 results = search.search( "user", searchFields, limits );
416 archivaConfigControl.verify();
418 assertNotNull( results );
419 assertEquals( 2, results.getTotalHits() );
420 assertEquals( 1, results.getHits().size() );
422 FileUtils.deleteDirectory( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ) );
423 assertFalse( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ).exists() );