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
22 import org.apache.archiva.common.utils.FileUtil;
23 import org.apache.archiva.indexer.util.SearchUtil;
24 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
25 import org.codehaus.plexus.util.FileUtils;
26 import org.easymock.EasyMock;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.springframework.test.context.ContextConfiguration;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.Collection;
35 import java.util.Collections;
36 import java.util.List;
39 @RunWith ( ArchivaSpringJUnit4ClassRunner.class )
40 @ContextConfiguration ( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
41 public class MavenRepositorySearchTest
42 extends AbstractMavenRepositorySearch
46 private void createSimpleIndex( boolean scan )
49 List<File> files = new ArrayList<File>();
50 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
51 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
52 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
53 + "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" ) );
54 files.add( new File( FileUtil.getBasedir(), "/src/test/" + TEST_REPO_1
55 + "/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ) );
57 createIndex( TEST_REPO_1, files, scan );
60 private void createIndexContainingMoreArtifacts( boolean scan )
63 List<File> files = new ArrayList<File>();
64 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
65 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
66 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
67 + "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" ) );
68 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
69 + "/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ) );
70 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
71 + "/org/apache/archiva/archiva-webapp/1.0/archiva-webapp-1.0.war" ) );
72 files.add( new File( FileUtil.getBasedir(),
73 "src/test/" + TEST_REPO_1 + "/com/artifactid-numeric/1.0/artifactid-numeric-1.0.jar" ) );
74 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
75 + "/com/artifactid-numeric123/1.0/artifactid-numeric123-1.0.jar" ) );
76 files.add( new File( FileUtil.getBasedir(),
77 "src/test/" + TEST_REPO_1 + "/com/classname-search/1.0/classname-search-1.0.jar" ) );
79 createIndex( TEST_REPO_1, files, scan );
82 private void createIndexContainingMultipleArtifactsSameVersion( boolean scan )
85 List<File> files = new ArrayList<File>();
87 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
88 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
90 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
91 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.pom" ) );
93 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
94 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0-sources.jar" ) );
96 createIndex( TEST_REPO_1, files, scan );
100 public void testQuickSearch()
103 createSimpleIndex( false );
105 List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
108 //archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
109 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
111 archivaConfigControl.replay();
113 SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
115 archivaConfigControl.verify();
117 assertNotNull( results );
119 SearchResultHit hit =
120 results.getSearchResultHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-search", null, "jar" ) );
121 assertNotNull( "hit null in result " + results.getHits(), hit );
122 assertEquals( "org.apache.archiva", hit.getGroupId() );
123 assertEquals( "archiva-search", hit.getArtifactId() );
124 assertEquals( "1.0", hit.getVersions().get( 0 ) );
126 archivaConfigControl.reset();
129 //archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
130 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
132 archivaConfigControl.replay();
134 results = search.search( "user", selectedRepos, "org.apache.archiva", null, null );
136 archivaConfigControl.verify();
138 assertNotNull( results );
139 assertEquals( "total hints not 3", 3, results.getTotalHits() );
141 //TODO: search for class & package names
145 public void testQuickSearchNotWithClassifier()
148 createSimpleIndex( true );
150 List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
153 //archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
154 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
156 archivaConfigControl.replay();
158 SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
160 archivaConfigControl.verify();
162 assertNotNull( results );
164 SearchResultHit hit =
165 results.getSearchResultHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-search", null, "jar" ) );
166 assertNotNull( "hit null in result " + results.getHits(), hit );
167 assertEquals( "org.apache.archiva", hit.getGroupId() );
168 assertEquals( "archiva-search", hit.getArtifactId() );
169 assertEquals( "1.0", hit.getVersions().get( 0 ) );
171 archivaConfigControl.reset();
174 //archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
175 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
177 archivaConfigControl.replay();
179 results = search.search( "user", selectedRepos, "archiva-search", null, null );
181 archivaConfigControl.verify();
183 assertNotNull( results );
184 assertEquals( "total hints not 3 hits " + results.getHits(), 3, results.getTotalHits() );
186 //TODO: search for class & package names
190 public void testQuickSearchMultipleArtifactsSameVersion()
193 createIndexContainingMultipleArtifactsSameVersion( false );
195 List<String> selectedRepos = new ArrayList<String>();
196 selectedRepos.add( TEST_REPO_1 );
199 //archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
200 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
202 archivaConfigControl.replay();
204 SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
206 archivaConfigControl.verify();
208 assertNotNull( results );
209 assertEquals( 3, results.getTotalHits() );
211 SearchResultHit hit = results.getHits().get( 0 );
212 assertEquals( "org.apache.archiva", hit.getGroupId() );
213 assertEquals( "archiva-search", hit.getArtifactId() );
214 assertEquals( "1.0", hit.getVersions().get( 0 ) );
216 //only 1 version of 1.0 is retrieved
217 assertEquals( 1, hit.getVersions().size() );
221 public void testMultipleArtifactsSameVersionWithClassifier()
224 createIndexContainingMultipleArtifactsSameVersion( true );
226 List<String> selectedRepos = new ArrayList<String>();
227 selectedRepos.add( TEST_REPO_1 );
230 //archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
231 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
233 archivaConfigControl.replay();
235 SearchFields searchFields = new SearchFields();
236 searchFields.setGroupId( "org.apache.archiva" );
237 searchFields.setArtifactId( "archiva-search" );
238 searchFields.setClassifier( "sources" );
239 searchFields.setRepositories( selectedRepos );
241 SearchResults results = search.search( "user", searchFields, null );
243 archivaConfigControl.verify();
245 assertNotNull( results );
246 assertEquals( 1, results.getTotalHits() );
248 SearchResultHit hit = results.getHits().get( 0 );
249 assertEquals( "org.apache.archiva", hit.getGroupId() );
250 assertEquals( "archiva-search", hit.getArtifactId() );
251 assertEquals( "1.0", hit.getVersions().get( 0 ) );
253 //only 1 version of 1.0 is retrieved
254 assertEquals( 1, hit.getVersions().size() );
257 // search for existing artifact using multiple keywords
259 public void testQuickSearchWithMultipleKeywords()
262 createSimpleIndex( false );
264 List<String> selectedRepos = new ArrayList<String>();
265 selectedRepos.add( TEST_REPO_1 );
267 //archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
268 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
269 archivaConfigControl.replay();
271 SearchResults results = search.search( "user", selectedRepos, "archiva search", null, null );
273 archivaConfigControl.verify();
275 assertNotNull( results );
276 assertEquals( 1, results.getTotalHits() );
280 public void testQuickSearchWithPagination()
283 createSimpleIndex( true );
285 List<String> selectedRepos = new ArrayList<String>();
286 selectedRepos.add( TEST_REPO_1 );
289 SearchResultLimits limits = new SearchResultLimits( 0 );
290 limits.setPageSize( 1 );
292 //archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
293 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
295 archivaConfigControl.replay();
297 SearchResults results = search.search( "user", selectedRepos, "org", limits, new ArrayList<String>() );
299 archivaConfigControl.verify();
301 assertNotNull( results );
302 assertEquals( 1, results.getHits().size() );
303 assertEquals( "total hits not 8 for page1 " + results, 8, results.getTotalHits() );
304 assertEquals( "returned hits not 1 for page1 " + results, 1, results.getReturnedHitsCount() );
305 assertEquals( limits, results.getLimits() );
307 archivaConfigControl.reset();
310 limits = new SearchResultLimits( 1 );
311 limits.setPageSize( 1 );
313 //archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
314 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
316 archivaConfigControl.replay();
318 results = search.search( "user", selectedRepos, "org", limits, null );
320 archivaConfigControl.verify();
322 assertNotNull( results );
324 assertEquals( "hits not 1", 1, results.getHits().size() );
325 assertEquals( "total hits not 8 for page 2 " + results, 8, results.getTotalHits() );
326 assertEquals( "returned hits not 1 for page2 " + results, 1, results.getReturnedHitsCount() );
327 assertEquals( limits, results.getLimits() );
331 public void testArtifactFoundInMultipleRepositories()
334 createSimpleIndex( true );
336 List<File> files = new ArrayList<File>();
337 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_2
338 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
339 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_2
340 + "/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar" ) );
341 createIndex( TEST_REPO_2, files, false );
343 List<String> selectedRepos = new ArrayList<String>();
344 selectedRepos.add( TEST_REPO_1 );
345 selectedRepos.add( TEST_REPO_2 );
347 config.addManagedRepository( createRepositoryConfig( TEST_REPO_2 ) );
349 //archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 5 );
350 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 5 );
352 archivaConfigControl.replay();
354 // wait lucene flush.....
355 Thread.sleep( 2000 );
357 SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
359 archivaConfigControl.verify();
361 assertNotNull( results );
363 SearchResultHit hit =
364 results.getSearchResultHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-search", null, "jar" ) );
365 assertEquals( "org.apache.archiva", hit.getGroupId() );
366 assertEquals( "archiva-search", hit.getArtifactId() );
367 assertEquals( "not 2 version for hit " + hit + "::" + niceDisplay( results ), 2, hit.getVersions().size() );
368 assertTrue( hit.getVersions().contains( "1.0" ) );
369 assertTrue( hit.getVersions().contains( "1.1" ) );
371 archivaConfigControl.reset();
373 // TODO: [BROWSE] in artifact info from browse, display all the repositories where the artifact is found
377 public void testNoMatchFound()
380 createSimpleIndex( false );
382 List<String> selectedRepos = new ArrayList<String>();
383 selectedRepos.add( TEST_REPO_1 );
385 //archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
386 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
388 archivaConfigControl.replay();
390 SearchResults results = search.search( "user", selectedRepos, "dfghdfkweriuasndsaie", null, null );
392 archivaConfigControl.verify();
394 assertNotNull( results );
395 assertEquals( 0, results.getTotalHits() );
399 public void testNoIndexFound()
402 List<String> selectedRepos = new ArrayList<String>();
403 selectedRepos.add( TEST_REPO_1 );
405 //archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
406 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
408 archivaConfigControl.replay();
410 SearchResults results = search.search( "user", selectedRepos, "org.apache.archiva", null, null );
411 assertNotNull( results );
412 assertEquals( 0, results.getTotalHits() );
414 archivaConfigControl.verify();
418 public void testRepositoryNotFound()
421 List<String> selectedRepos = new ArrayList<String>();
422 selectedRepos.add( "non-existing-repo" );
424 //archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
425 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
427 archivaConfigControl.replay();
429 SearchResults results = search.search( "user", selectedRepos, "org.apache.archiva", null, null );
430 assertNotNull( results );
431 assertEquals( 0, results.getTotalHits() );
433 archivaConfigControl.verify();
437 public void testSearchWithinSearchResults()
440 createSimpleIndex( true );
442 List<String> selectedRepos = new ArrayList<String>();
443 selectedRepos.add( TEST_REPO_1 );
445 List<String> previousSearchTerms = new ArrayList<String>();
446 previousSearchTerms.add( "archiva-test" );
448 //archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
449 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
451 archivaConfigControl.replay();
453 SearchResults results = search.search( "user", selectedRepos, "1.0", null, previousSearchTerms );
455 archivaConfigControl.verify();
457 assertNotNull( results );
458 assertEquals( "total hints not 1", 1, results.getTotalHits() );
460 SearchResultHit hit = results.getHits().get( 0 );
461 assertEquals( "org.apache.archiva", hit.getGroupId() );
462 assertEquals( "archiva-test", hit.getArtifactId() );
463 assertEquals( "versions not 1", 1, hit.getVersions().size() );
464 assertEquals( "1.0", hit.getVersions().get( 0 ) );
467 // tests for advanced search
469 public void testAdvancedSearch()
472 List<File> files = new ArrayList<File>();
473 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_2
474 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
475 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_2
476 + "/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar" ) );
477 createIndex( TEST_REPO_2, files, false );
479 List<String> selectedRepos = new ArrayList<String>();
480 selectedRepos.add( TEST_REPO_2 );
482 SearchFields searchFields = new SearchFields();
483 searchFields.setGroupId( "org.apache.archiva" );
484 searchFields.setVersion( "1.0" );
485 searchFields.setRepositories( selectedRepos );
487 //archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
488 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
490 archivaConfigControl.replay();
492 SearchResults results = search.search( "user", searchFields, null );
494 archivaConfigControl.verify();
496 assertNotNull( results );
497 assertEquals( 1, results.getTotalHits() );
499 SearchResultHit hit = results.getHits().get( 0 );
500 assertEquals( "org.apache.archiva", hit.getGroupId() );
501 assertEquals( "archiva-search", hit.getArtifactId() );
502 assertEquals( "1.0", hit.getVersions().get( 0 ) );
506 public void testAdvancedSearchWithPagination()
509 createIndexContainingMoreArtifacts( false );
511 List<String> selectedRepos = new ArrayList<String>();
512 selectedRepos.add( TEST_REPO_1 );
514 SearchFields searchFields = new SearchFields();
515 searchFields.setGroupId( "org.apache.archiva" );
516 searchFields.setRepositories( selectedRepos );
520 SearchResultLimits limits = new SearchResultLimits( 0 );
521 limits.setPageSize( 1 );
523 //archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
524 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
526 archivaConfigControl.replay();
528 SearchResults results = search.search( "user", searchFields, limits );
530 archivaConfigControl.verify();
532 assertNotNull( results );
533 assertEquals( 4, results.getTotalHits() );
534 assertEquals( 1, results.getHits().size() );
537 archivaConfigControl.reset();
539 limits = new SearchResultLimits( 1 );
540 limits.setPageSize( 1 );
542 //archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
543 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
545 archivaConfigControl.replay();
547 results = search.search( "user", searchFields, limits );
549 archivaConfigControl.verify();
551 assertNotNull( results );
552 assertEquals( 4, results.getTotalHits() );
553 assertEquals( 1, results.getHits().size() );
556 // MRM-981 - artifactIds with numeric characters aren't found in advanced search
558 public void testAdvancedSearchArtifactIdHasNumericChar()
561 List<File> files = new ArrayList<File>();
562 files.add( new File( FileUtil.getBasedir(),
563 "src/test/" + TEST_REPO_1 + "/com/artifactid-numeric/1.0/artifactid-numeric-1.0.jar" ) );
564 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
565 + "/com/artifactid-numeric123/1.0/artifactid-numeric123-1.0.jar" ) );
566 createIndex( TEST_REPO_1, files, true );
568 List<String> selectedRepos = new ArrayList<String>();
569 selectedRepos.add( TEST_REPO_1 );
571 SearchFields searchFields = new SearchFields();
572 searchFields.setArtifactId( "artifactid-numeric" );
573 searchFields.setRepositories( selectedRepos );
575 //archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
576 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
578 archivaConfigControl.replay();
580 SearchResults results = search.search( "user", searchFields, null );
582 archivaConfigControl.verify();
584 assertNotNull( results );
585 assertEquals( 2, results.getTotalHits() );
589 public void testAdvancedSearchNoRepositoriesConfigured()
592 SearchFields searchFields = new SearchFields();
593 searchFields.setArtifactId( "archiva" );
594 searchFields.setRepositories( null );
598 search.search( "user", searchFields, null );
599 fail( "A RepositorySearchExcecption should have been thrown." );
601 catch ( RepositorySearchException e )
603 assertEquals( "Repositories cannot be null.", e.getMessage() );
608 public void testAdvancedSearchSearchFieldsAreNull()
611 List<String> selectedRepos = new ArrayList<String>();
612 selectedRepos.add( TEST_REPO_1 );
614 SearchFields searchFields = new SearchFields();
615 searchFields.setRepositories( selectedRepos );
619 //archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
620 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
622 archivaConfigControl.replay();
624 search.search( "user", searchFields, null );
626 archivaConfigControl.verify();
628 fail( "A RepositorySearchExcecption should have been thrown." );
630 catch ( RepositorySearchException e )
632 assertEquals( "No search fields set.", e.getMessage() );
637 public void testAdvancedSearchSearchFieldsAreBlank()
640 List<String> selectedRepos = new ArrayList<String>();
641 selectedRepos.add( TEST_REPO_1 );
643 SearchFields searchFields = new SearchFields();
644 searchFields.setGroupId( "" );
645 searchFields.setArtifactId( "" );
646 searchFields.setVersion( "" );
647 searchFields.setPackaging( "" );
648 searchFields.setClassName( "" );
650 searchFields.setRepositories( selectedRepos );
654 //archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
655 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
657 archivaConfigControl.replay();
659 search.search( "user", searchFields, null );
661 archivaConfigControl.verify();
663 fail( "A RepositorySearchExcecption should have been thrown." );
665 catch ( RepositorySearchException e )
667 assertEquals( "No search fields set.", e.getMessage() );
672 public void testAdvancedSearchAllSearchCriteriaSpecified()
675 createSimpleIndex( true );
677 List<String> selectedRepos = new ArrayList<String>();
678 selectedRepos.add( TEST_REPO_1 );
680 SearchFields searchFields = new SearchFields();
681 searchFields.setGroupId( "org.apache.archiva" );
682 searchFields.setArtifactId( "archiva-test" );
683 searchFields.setVersion( "2.0" );
684 searchFields.setPackaging( "jar" );
685 searchFields.setClassName( "org.apache.archiva.test.App" );
686 searchFields.setRepositories( selectedRepos );
688 //archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
689 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
691 archivaConfigControl.replay();
693 SearchResults results = search.search( "user", searchFields, null );
695 archivaConfigControl.verify();
697 assertNotNull( results );
699 assertEquals( "total hints not 1" + results, 1, results.getTotalHits() );
701 SearchResultHit hit = results.getHits().get( 0 );
702 assertEquals( "org.apache.archiva", hit.getGroupId() );
703 assertEquals( "archiva-test", hit.getArtifactId() );
704 assertEquals( "version not 2.0", "2.0", hit.getVersions().get( 0 ) );
708 public void testAdvancedSearchJarArtifacts()
711 createIndexContainingMoreArtifacts( true );
713 List<String> selectedRepos = new ArrayList<String>();
714 selectedRepos.add( TEST_REPO_1 );
716 SearchFields searchFields = new SearchFields();
717 searchFields.setPackaging( "jar" );
718 searchFields.setRepositories( selectedRepos );
720 //archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
721 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
723 archivaConfigControl.replay();
725 SearchResults results = search.search( "user", searchFields, null );
727 archivaConfigControl.verify();
729 assertNotNull( results );
730 assertEquals( "not 8 but " + results.getTotalHits() + ":" + niceDisplay( results ), 8, results.getTotalHits() );
734 public void testAdvancedSearchWithIncorrectPackaging()
737 createSimpleIndex( true );
739 List<String> selectedRepos = new ArrayList<String>();
740 selectedRepos.add( TEST_REPO_1 );
742 SearchFields searchFields = new SearchFields();
743 searchFields.setGroupId( "org.apache.archiva" );
744 searchFields.setArtifactId( "archiva-test" );
745 searchFields.setVersion( "2.0" );
746 searchFields.setPackaging( "war" );
747 searchFields.setRepositories( selectedRepos );
749 //archivaConfigControl.expectAndDefaultReturn( archivaConfig.getConfiguration(), config );
750 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
751 archivaConfigControl.replay();
753 SearchResults results = search.search( "user", searchFields, null );
755 archivaConfigControl.verify();
757 assertNotNull( results );
758 assertEquals( 0, results.getTotalHits() );
762 public void testAdvancedSearchClassname()
765 createIndexContainingMoreArtifacts( true );
767 List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
769 SearchFields searchFields = new SearchFields();
770 searchFields.setClassName( "com.classname.search.App" );
771 searchFields.setRepositories( selectedRepos );
773 //archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
774 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
776 archivaConfigControl.replay();
778 SearchResults results = search.search( "user", searchFields, null );
780 archivaConfigControl.verify();
782 assertNotNull( results );
783 assertEquals( "totalHits not 1 results " + results, 1, results.getTotalHits() );
785 SearchResultHit hit = results.getHits().get( 0 );
786 assertEquals( "groupId not com", "com", hit.getGroupId() );
787 assertEquals( "arttifactId not classname-search", "classname-search", hit.getArtifactId() );
788 assertEquals( " hits.version(0) not 1.0", "1.0", hit.getVersions().get( 0 ) );
792 public void testAdvancedSearchNoIndexFound()
795 List<String> selectedRepos = new ArrayList<String>();
796 selectedRepos.add( TEST_REPO_1 );
798 SearchFields searchFields = new SearchFields();
799 searchFields.setGroupId( "org.apache.archiva" );
800 searchFields.setRepositories( selectedRepos );
802 //archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
803 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
805 archivaConfigControl.replay();
807 SearchResults results = search.search( "user", searchFields, null );
809 archivaConfigControl.verify();
811 assertNotNull( results );
812 assertEquals( 0, results.getTotalHits() );
816 public void testAdvancedSearchClassNameInWar()
819 createIndexContainingMoreArtifacts( true );
821 List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
823 SearchFields searchFields = new SearchFields();
824 searchFields.setClassName( "SomeClass" );
825 searchFields.setRepositories( selectedRepos );
827 //archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
828 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
830 archivaConfigControl.replay();
832 SearchResults results = search.search( "user", searchFields, null );
834 archivaConfigControl.verify();
836 assertNotNull( results );
837 assertEquals( 1, results.getHits().size() );
838 assertEquals( "test-webapp", results.getHits().get( 0 ).getArtifactId() );
842 public void getAllGroupIds()
845 createIndexContainingMoreArtifacts( true );
847 List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
849 //archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 0, 2 );
850 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 0, 2 );
852 archivaConfigControl.replay();
854 Collection<String> groupIds = search.getAllGroupIds( "user", selectedRepos );
856 archivaConfigControl.verify();
858 log.info( "groupIds: " + groupIds );
860 assertEquals( 3, groupIds.size() );
861 assertTrue( groupIds.contains( "com" ) );
862 assertTrue( groupIds.contains( "org.apache.felix" ) );
863 assertTrue( groupIds.contains( "org.apache.archiva" ) );
867 public void testSearchWithUnknownRepo()
870 createIndexContainingMoreArtifacts( true );
872 List<String> selectedRepos = Arrays.asList( "foo" );
874 SearchFields searchFields = new SearchFields();
875 searchFields.setClassName( "SomeClass" );
876 searchFields.setRepositories( selectedRepos );
878 //archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
879 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
881 archivaConfigControl.replay();
883 SearchResults results = search.search( "user", searchFields, null );
885 archivaConfigControl.verify();
887 assertNotNull( results );
888 assertEquals( 0, results.getHits().size() );
892 public void nolimitedResult()
896 File repo = new File( "target/repo-release" );
897 File indexDirectory = new File( repo, ".index" );
898 FileUtils.copyDirectoryStructure( new File( "src/test/repo-release" ), repo );
900 createIndex( "repo-release", Collections.<File>emptyList(), false );
902 nexusIndexer.addIndexingContext( REPO_RELEASE, REPO_RELEASE, repo, indexDirectory,
903 repo.toURI().toURL().toExternalForm(),
904 indexDirectory.toURI().toURL().toString(), search.getAllIndexCreators() );
906 SearchResultLimits limits = new SearchResultLimits( 0 );
907 limits.setPageSize( 300 );
909 //archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 5 );
910 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 5 );
912 archivaConfigControl.replay();
914 SearchResults searchResults = search.search( null, Arrays.asList( REPO_RELEASE ), "org.example", limits,
915 Collections.<String>emptyList() );
917 log.info( "results: {}", searchResults.getHits().size() );
919 assertEquals( 255, searchResults.getHits().size() );
921 SearchFields searchFields = new SearchFields();
922 searchFields.setGroupId( "org.example" );
923 searchFields.setRepositories( Arrays.asList( REPO_RELEASE ) );
925 searchResults = search.search( null, searchFields, limits );
927 log.info( "results: {}", searchResults.getHits().size() );
929 assertEquals( 255, searchResults.getHits().size() );
931 archivaConfigControl.verify();