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.apache.maven.index.shaded.lucene.index.IndexUpgrader;
26 import org.codehaus.plexus.util.FileUtils;
27 import org.easymock.EasyMock;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.springframework.test.context.ContextConfiguration;
33 import java.nio.file.Paths;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.Collection;
37 import java.util.Collections;
38 import java.util.List;
41 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
42 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
43 public class MavenRepositorySearchTest
44 extends AbstractMavenRepositorySearch
48 private void createSimpleIndex( boolean scan )
51 List<File> files = new ArrayList<>();
52 files.add( Paths.get( FileUtil.getBasedir(), "src/test", TEST_REPO_1,
53 "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ).toFile() );
54 files.add( Paths.get( FileUtil.getBasedir(), "src/test", TEST_REPO_1,
55 "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" ).toFile() );
56 files.add( Paths.get( FileUtil.getBasedir(), "src/test", TEST_REPO_1,
57 "org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ).toFile() );
59 createIndex( TEST_REPO_1, files, scan );
62 private void createIndexContainingMoreArtifacts( boolean scan )
65 List<File> files = new ArrayList<>();
66 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
67 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
68 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
69 + "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" ) );
70 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
71 + "/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ) );
72 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
73 + "/org/apache/archiva/archiva-webapp/1.0/archiva-webapp-1.0.war" ) );
74 files.add( new File( FileUtil.getBasedir(),
75 "src/test/" + TEST_REPO_1 + "/com/artifactid-numeric/1.0/artifactid-numeric-1.0.jar" ) );
76 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
77 + "/com/artifactid-numeric123/1.0/artifactid-numeric123-1.0.jar" ) );
78 files.add( new File( FileUtil.getBasedir(),
79 "src/test/" + TEST_REPO_1 + "/com/classname-search/1.0/classname-search-1.0.jar" ) );
81 createIndex( TEST_REPO_1, files, scan );
84 private void createIndexContainingMultipleArtifactsSameVersion( boolean scan )
87 List<File> files = new ArrayList<>();
89 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
90 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
92 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
93 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.pom" ) );
95 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
96 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0-sources.jar" ) );
98 createIndex( TEST_REPO_1, files, scan );
102 public void testQuickSearch()
105 createSimpleIndex( false );
107 List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
110 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
112 archivaConfigControl.replay();
114 SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
116 archivaConfigControl.verify();
118 assertNotNull( results );
120 SearchResultHit hit =
121 results.getSearchResultHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-search", null, "jar" ) );
122 assertNotNull( "hit null in result " + results.getHits(), hit );
123 assertEquals( "org.apache.archiva", hit.getGroupId() );
124 assertEquals( "archiva-search", hit.getArtifactId() );
125 assertEquals( "1.0", hit.getVersions().get( 0 ) );
127 archivaConfigControl.reset();
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 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
155 archivaConfigControl.replay();
157 SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
159 archivaConfigControl.verify();
161 assertNotNull( results );
163 SearchResultHit hit =
164 results.getSearchResultHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-search", null, "jar" ) );
165 assertNotNull( "hit null in result " + results.getHits(), hit );
166 assertEquals( "org.apache.archiva", hit.getGroupId() );
167 assertEquals( "archiva-search", hit.getArtifactId() );
168 assertEquals( "1.0", hit.getVersions().get( 0 ) );
170 archivaConfigControl.reset();
173 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
175 archivaConfigControl.replay();
177 results = search.search( "user", selectedRepos, "archiva-search", null, null );
179 archivaConfigControl.verify();
181 assertNotNull( results );
182 assertEquals( "total hints not 3 hits " + results.getHits(), 3, results.getTotalHits() );
184 //TODO: search for class & package names
188 public void testQuickSearchMultipleArtifactsSameVersion()
191 createIndexContainingMultipleArtifactsSameVersion( false );
193 List<String> selectedRepos = new ArrayList<>();
194 selectedRepos.add( TEST_REPO_1 );
197 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
199 archivaConfigControl.replay();
201 SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
203 archivaConfigControl.verify();
205 assertNotNull( results );
206 assertEquals( 3, results.getTotalHits() );
208 SearchResultHit hit = results.getHits().get( 0 );
209 assertEquals( "org.apache.archiva", hit.getGroupId() );
210 assertEquals( "archiva-search", hit.getArtifactId() );
211 assertEquals( "1.0", hit.getVersions().get( 0 ) );
213 //only 1 version of 1.0 is retrieved
214 assertEquals( 1, hit.getVersions().size() );
218 public void testMultipleArtifactsSameVersionWithClassifier()
221 createIndexContainingMultipleArtifactsSameVersion( true );
223 List<String> selectedRepos = new ArrayList<>();
224 selectedRepos.add( TEST_REPO_1 );
227 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
229 archivaConfigControl.replay();
231 SearchFields searchFields = new SearchFields();
232 searchFields.setGroupId( "org.apache.archiva" );
233 searchFields.setArtifactId( "archiva-search" );
234 searchFields.setClassifier( "sources" );
235 searchFields.setRepositories( selectedRepos );
237 SearchResults results = search.search( "user", searchFields, null );
239 archivaConfigControl.verify();
241 assertNotNull( results );
242 assertEquals( 1, results.getTotalHits() );
244 SearchResultHit hit = results.getHits().get( 0 );
245 assertEquals( "org.apache.archiva", hit.getGroupId() );
246 assertEquals( "archiva-search", hit.getArtifactId() );
247 assertEquals( "1.0", hit.getVersions().get( 0 ) );
249 //only 1 version of 1.0 is retrieved
250 assertEquals( 1, hit.getVersions().size() );
253 // search for existing artifact using multiple keywords
255 public void testQuickSearchWithMultipleKeywords()
258 createSimpleIndex( false );
260 List<String> selectedRepos = new ArrayList<>();
261 selectedRepos.add( TEST_REPO_1 );
263 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
264 archivaConfigControl.replay();
266 SearchResults results = search.search( "user", selectedRepos, "archiva search", null, null );
268 archivaConfigControl.verify();
270 assertNotNull( results );
271 assertEquals( 1, results.getTotalHits() );
275 public void testQuickSearchWithPagination()
278 createSimpleIndex( true );
280 List<String> selectedRepos = new ArrayList<>();
281 selectedRepos.add( TEST_REPO_1 );
284 SearchResultLimits limits = new SearchResultLimits( 0 );
285 limits.setPageSize( 1 );
287 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
289 archivaConfigControl.replay();
291 SearchResults results = search.search( "user", selectedRepos, "org", limits, Collections.emptyList() );
293 archivaConfigControl.verify();
295 assertNotNull( results );
296 assertEquals( 1, results.getHits().size() );
297 assertEquals( "total hits not 9 for page1 " + results, 9, results.getTotalHits() );
298 assertEquals( "returned hits not 1 for page1 " + results, 1, results.getReturnedHitsCount() );
299 assertEquals( limits, results.getLimits() );
301 archivaConfigControl.reset();
304 limits = new SearchResultLimits( 1 );
305 limits.setPageSize( 1 );
307 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
309 archivaConfigControl.replay();
311 results = search.search( "user", selectedRepos, "org", limits, null );
313 archivaConfigControl.verify();
315 assertNotNull( results );
317 assertEquals( "hits not 1", 1, results.getHits().size() );
318 assertEquals( "total hits not 9 for page 2 " + results, 9, results.getTotalHits() );
319 assertEquals( "returned hits not 1 for page2 " + results, 1, results.getReturnedHitsCount() );
320 assertEquals( limits, results.getLimits() );
324 public void testArtifactFoundInMultipleRepositories()
327 createSimpleIndex( true );
329 List<File> files = new ArrayList<>();
330 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_2
331 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
332 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_2
333 + "/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar" ) );
334 createIndex( TEST_REPO_2, files, false );
336 List<String> selectedRepos = new ArrayList<>();
337 selectedRepos.add( TEST_REPO_1 );
338 selectedRepos.add( TEST_REPO_2 );
340 config.addManagedRepository( createRepositoryConfig( TEST_REPO_2 ) );
342 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 5 );
344 archivaConfigControl.replay();
346 // wait lucene flush.....
347 Thread.sleep( 2000 );
349 SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
351 archivaConfigControl.verify();
353 assertNotNull( results );
355 SearchResultHit hit =
356 results.getSearchResultHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-search", null, "jar" ) );
357 assertEquals( "org.apache.archiva", hit.getGroupId() );
358 assertEquals( "archiva-search", hit.getArtifactId() );
359 assertEquals( "not 2 version for hit " + hit + "::" + niceDisplay( results ), 2, hit.getVersions().size() );
360 assertTrue( hit.getVersions().contains( "1.0" ) );
361 assertTrue( hit.getVersions().contains( "1.1" ) );
363 archivaConfigControl.reset();
365 // TODO: [BROWSE] in artifact info from browse, display all the repositories where the artifact is found
369 public void testNoMatchFound()
372 createSimpleIndex( false );
374 List<String> selectedRepos = new ArrayList<>();
375 selectedRepos.add( TEST_REPO_1 );
377 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
379 archivaConfigControl.replay();
381 SearchResults results = search.search( "user", selectedRepos, "dfghdfkweriuasndsaie", null, null );
383 archivaConfigControl.verify();
385 assertNotNull( results );
386 assertEquals( 0, results.getTotalHits() );
390 public void testNoIndexFound()
393 List<String> selectedRepos = new ArrayList<>();
394 selectedRepos.add( TEST_REPO_1 );
396 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
398 archivaConfigControl.replay();
400 SearchResults results = search.search( "user", selectedRepos, "org.apache.archiva", null, null );
401 assertNotNull( results );
402 assertEquals( 0, results.getTotalHits() );
404 archivaConfigControl.verify();
408 public void testRepositoryNotFound()
411 List<String> selectedRepos = new ArrayList<>();
412 selectedRepos.add( "non-existing-repo" );
414 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
416 archivaConfigControl.replay();
418 SearchResults results = search.search( "user", selectedRepos, "org.apache.archiva", null, null );
419 assertNotNull( results );
420 assertEquals( 0, results.getTotalHits() );
422 archivaConfigControl.verify();
426 public void testSearchWithinSearchResults()
429 createSimpleIndex( true );
431 List<String> selectedRepos = new ArrayList<>();
432 selectedRepos.add( TEST_REPO_1 );
434 List<String> previousSearchTerms = new ArrayList<>();
435 previousSearchTerms.add( "archiva-test" );
437 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
439 archivaConfigControl.replay();
441 SearchResults results = search.search( "user", selectedRepos, "1.0", null, previousSearchTerms );
443 archivaConfigControl.verify();
445 assertNotNull( results );
446 assertEquals( "total hints not 1", 1, results.getTotalHits() );
448 SearchResultHit hit = results.getHits().get( 0 );
449 assertEquals( "org.apache.archiva", hit.getGroupId() );
450 assertEquals( "archiva-test", hit.getArtifactId() );
451 assertEquals( "versions not 1", 1, hit.getVersions().size() );
452 assertEquals( "1.0", hit.getVersions().get( 0 ) );
455 // tests for advanced search
457 public void testAdvancedSearch()
460 List<File> files = new ArrayList<>();
461 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_2
462 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
463 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_2
464 + "/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar" ) );
465 createIndex( TEST_REPO_2, files, false );
467 List<String> selectedRepos = new ArrayList<>();
468 selectedRepos.add( TEST_REPO_2 );
470 SearchFields searchFields = new SearchFields();
471 searchFields.setGroupId( "org.apache.archiva" );
472 searchFields.setVersion( "1.0" );
473 searchFields.setRepositories( selectedRepos );
475 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
477 archivaConfigControl.replay();
479 SearchResults results = search.search( "user", searchFields, null );
481 archivaConfigControl.verify();
483 assertNotNull( results );
484 assertEquals( 1, results.getTotalHits() );
486 SearchResultHit hit = results.getHits().get( 0 );
487 assertEquals( "org.apache.archiva", hit.getGroupId() );
488 assertEquals( "archiva-search", hit.getArtifactId() );
489 assertEquals( "1.0", hit.getVersions().get( 0 ) );
493 public void testAdvancedSearchWithPagination()
496 createIndexContainingMoreArtifacts( false );
498 List<String> selectedRepos = new ArrayList<>();
499 selectedRepos.add( TEST_REPO_1 );
501 SearchFields searchFields = new SearchFields();
502 searchFields.setGroupId( "org.apache.archiva" );
503 searchFields.setRepositories( selectedRepos );
507 SearchResultLimits limits = new SearchResultLimits( 0 );
508 limits.setPageSize( 1 );
510 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
512 archivaConfigControl.replay();
514 SearchResults results = search.search( "user", searchFields, limits );
516 archivaConfigControl.verify();
518 assertNotNull( results );
519 assertEquals( 4, results.getTotalHits() );
520 assertEquals( 1, results.getHits().size() );
523 archivaConfigControl.reset();
525 limits = new SearchResultLimits( 1 );
526 limits.setPageSize( 1 );
528 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
530 archivaConfigControl.replay();
532 results = search.search( "user", searchFields, limits );
534 archivaConfigControl.verify();
536 assertNotNull( results );
537 assertEquals( 4, results.getTotalHits() );
538 assertEquals( 1, results.getHits().size() );
541 // MRM-981 - artifactIds with numeric characters aren't found in advanced search
543 public void testAdvancedSearchArtifactIdHasNumericChar()
546 List<File> files = new ArrayList<>();
547 files.add( new File( FileUtil.getBasedir(),
548 "src/test/" + TEST_REPO_1 + "/com/artifactid-numeric/1.0/artifactid-numeric-1.0.jar" ) );
549 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
550 + "/com/artifactid-numeric123/1.0/artifactid-numeric123-1.0.jar" ) );
551 createIndex( TEST_REPO_1, files, true );
553 List<String> selectedRepos = new ArrayList<>();
554 selectedRepos.add( TEST_REPO_1 );
556 SearchFields searchFields = new SearchFields();
557 searchFields.setArtifactId( "artifactid-numeric" );
558 searchFields.setRepositories( selectedRepos );
560 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
562 archivaConfigControl.replay();
564 SearchResults results = search.search( "user", searchFields, null );
566 archivaConfigControl.verify();
568 assertNotNull( results );
569 assertEquals( 2, results.getTotalHits() );
573 public void testAdvancedSearchNoRepositoriesConfigured()
576 SearchFields searchFields = new SearchFields();
577 searchFields.setArtifactId( "archiva" );
578 searchFields.setRepositories( null );
582 search.search( "user", searchFields, null );
583 fail( "A RepositorySearchExcecption should have been thrown." );
585 catch ( RepositorySearchException e )
587 assertEquals( "Repositories cannot be null.", e.getMessage() );
592 public void testAdvancedSearchSearchFieldsAreNull()
595 List<String> selectedRepos = new ArrayList<>();
596 selectedRepos.add( TEST_REPO_1 );
598 SearchFields searchFields = new SearchFields();
599 searchFields.setRepositories( selectedRepos );
603 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
605 archivaConfigControl.replay();
607 search.search( "user", searchFields, null );
609 archivaConfigControl.verify();
611 fail( "A RepositorySearchExcecption should have been thrown." );
613 catch ( RepositorySearchException e )
615 assertEquals( "No search fields set.", e.getMessage() );
620 public void testAdvancedSearchSearchFieldsAreBlank()
623 List<String> selectedRepos = new ArrayList<>();
624 selectedRepos.add( TEST_REPO_1 );
626 SearchFields searchFields = new SearchFields();
627 searchFields.setGroupId( "" );
628 searchFields.setArtifactId( "" );
629 searchFields.setVersion( "" );
630 searchFields.setPackaging( "" );
631 searchFields.setClassName( "" );
633 searchFields.setRepositories( selectedRepos );
637 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
639 archivaConfigControl.replay();
641 search.search( "user", searchFields, null );
643 archivaConfigControl.verify();
645 fail( "A RepositorySearchExcecption should have been thrown." );
647 catch ( RepositorySearchException e )
649 assertEquals( "No search fields set.", e.getMessage() );
654 public void testAdvancedSearchAllSearchCriteriaSpecified()
657 createSimpleIndex( true );
659 List<String> selectedRepos = new ArrayList<>();
660 selectedRepos.add( TEST_REPO_1 );
662 SearchFields searchFields = new SearchFields();
663 searchFields.setGroupId( "org.apache.archiva" );
664 searchFields.setArtifactId( "archiva-test" );
665 searchFields.setVersion( "2.0" );
666 searchFields.setPackaging( "jar" );
667 searchFields.setClassName( "org.apache.archiva.test.App" );
668 searchFields.setRepositories( selectedRepos );
670 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
672 archivaConfigControl.replay();
674 SearchResults results = search.search( "user", searchFields, null );
676 archivaConfigControl.verify();
678 assertNotNull( results );
680 assertEquals( "total hints not 1" + results, 1, results.getTotalHits() );
682 SearchResultHit hit = results.getHits().get( 0 );
683 assertEquals( "org.apache.archiva", hit.getGroupId() );
684 assertEquals( "archiva-test", hit.getArtifactId() );
685 assertEquals( "version not 2.0", "2.0", hit.getVersions().get( 0 ) );
689 public void testAdvancedSearchJarArtifacts()
692 createIndexContainingMoreArtifacts( true );
694 List<String> selectedRepos = new ArrayList<>();
695 selectedRepos.add( TEST_REPO_1 );
697 SearchFields searchFields = new SearchFields();
698 searchFields.setPackaging( "jar" );
699 searchFields.setRepositories( selectedRepos );
701 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
703 archivaConfigControl.replay();
705 SearchResults results = search.search( "user", searchFields, null );
707 archivaConfigControl.verify();
709 assertNotNull( results );
710 assertEquals( "not 8 but " + results.getTotalHits() + ":" + niceDisplay( results ), 8, results.getTotalHits() );
714 public void testAdvancedSearchWithIncorrectPackaging()
717 createSimpleIndex( true );
719 List<String> selectedRepos = new ArrayList<>();
720 selectedRepos.add( TEST_REPO_1 );
722 SearchFields searchFields = new SearchFields();
723 searchFields.setGroupId( "org.apache.archiva" );
724 searchFields.setArtifactId( "archiva-test" );
725 searchFields.setVersion( "2.0" );
726 searchFields.setPackaging( "war" );
727 searchFields.setRepositories( selectedRepos );
729 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
730 archivaConfigControl.replay();
732 SearchResults results = search.search( "user", searchFields, null );
734 archivaConfigControl.verify();
736 assertNotNull( results );
737 assertEquals( 0, results.getTotalHits() );
741 public void testAdvancedSearchClassname()
744 createIndexContainingMoreArtifacts( true );
746 List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
748 SearchFields searchFields = new SearchFields();
749 searchFields.setClassName( "com.classname.search.App" );
750 searchFields.setRepositories( selectedRepos );
752 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
754 archivaConfigControl.replay();
756 SearchResults results = search.search( "user", searchFields, null );
758 archivaConfigControl.verify();
760 assertNotNull( results );
761 assertEquals( "totalHits not 1 results " + results, 1, results.getTotalHits() );
763 SearchResultHit hit = results.getHits().get( 0 );
764 assertEquals( "groupId not com", "com", hit.getGroupId() );
765 assertEquals( "arttifactId not classname-search", "classname-search", hit.getArtifactId() );
766 assertEquals( " hits.version(0) not 1.0", "1.0", hit.getVersions().get( 0 ) );
770 public void testAdvancedSearchNoIndexFound()
773 List<String> selectedRepos = new ArrayList<>();
774 selectedRepos.add( TEST_REPO_1 );
776 SearchFields searchFields = new SearchFields();
777 searchFields.setGroupId( "org.apache.archiva" );
778 searchFields.setRepositories( selectedRepos );
780 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
782 archivaConfigControl.replay();
784 SearchResults results = search.search( "user", searchFields, null );
786 archivaConfigControl.verify();
788 assertNotNull( results );
789 assertEquals( 0, results.getTotalHits() );
793 public void testAdvancedSearchClassNameInWar()
796 createIndexContainingMoreArtifacts( true );
798 List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
800 SearchFields searchFields = new SearchFields();
801 searchFields.setClassName( "SomeClass" );
802 searchFields.setRepositories( selectedRepos );
804 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
806 archivaConfigControl.replay();
808 SearchResults results = search.search( "user", searchFields, null );
810 archivaConfigControl.verify();
812 assertNotNull( results );
813 assertEquals( 1, results.getHits().size() );
814 assertEquals( "test-webapp", results.getHits().get( 0 ).getArtifactId() );
818 public void getAllGroupIds()
821 createIndexContainingMoreArtifacts( true );
823 List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
825 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 0, 2 );
827 archivaConfigControl.replay();
829 Collection<String> groupIds = search.getAllGroupIds( "user", selectedRepos );
831 archivaConfigControl.verify();
833 log.info( "groupIds: {}", groupIds );
835 assertEquals( 3, groupIds.size() );
836 assertTrue( groupIds.contains( "com" ) );
837 assertTrue( groupIds.contains( "org.apache.felix" ) );
838 assertTrue( groupIds.contains( "org.apache.archiva" ) );
842 public void testSearchWithUnknownRepo()
845 createIndexContainingMoreArtifacts( true );
847 List<String> selectedRepos = Arrays.asList( "foo" );
849 SearchFields searchFields = new SearchFields();
850 searchFields.setClassName( "SomeClass" );
851 searchFields.setRepositories( selectedRepos );
853 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
855 archivaConfigControl.replay();
857 SearchResults results = search.search( "user", searchFields, null );
859 archivaConfigControl.verify();
861 assertNotNull( results );
862 assertEquals( 0, results.getHits().size() );
866 public void nolimitedResult()
870 File repo = new File( "target/repo-release" );
871 File indexDirectory = new File( repo, ".index" );
872 FileUtils.copyDirectoryStructure( new File( "src/test/repo-release" ), repo );
874 IndexUpgrader.main( new String[]{indexDirectory.getAbsolutePath()} );
876 createIndex( "repo-release", Collections.emptyList(), false );
878 nexusIndexer.addIndexingContext( REPO_RELEASE, REPO_RELEASE, repo, indexDirectory,
879 repo.toURI().toURL().toExternalForm(),
880 indexDirectory.toURI().toURL().toString(), search.getAllIndexCreators() );
882 SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
883 limits.setPageSize( 300 );
885 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 5 );
887 archivaConfigControl.replay();
889 SearchResults searchResults = search.search( null, Arrays.asList( REPO_RELEASE ), //
890 "org.example", limits, //
891 Collections.emptyList() );
893 log.info( "results: {}", searchResults.getHits().size() );
895 assertEquals( 255, searchResults.getHits().size() );
897 SearchFields searchFields = new SearchFields();
898 searchFields.setGroupId( "org.example" );
899 searchFields.setRepositories( Arrays.asList( REPO_RELEASE ) );
901 searchResults = search.search( null, searchFields, limits );
903 log.info( "results: {}", searchResults.getHits().size() );
905 assertEquals( 255, searchResults.getHits().size() );
907 archivaConfigControl.verify();