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.nio.file.Files;
33 import java.nio.file.Path;
34 import java.nio.file.Paths;
35 import java.nio.file.StandardCopyOption;
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.Collection;
39 import java.util.Collections;
40 import java.util.List;
43 @RunWith(ArchivaSpringJUnit4ClassRunner.class)
44 @ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" })
45 public class MavenRepositorySearchTest
46 extends AbstractMavenRepositorySearch
50 private void createSimpleIndex( boolean scan )
53 List<File> files = new ArrayList<>();
54 files.add( Paths.get( FileUtil.getBasedir(), "src/test", TEST_REPO_1,
55 "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ).toFile() );
56 files.add( Paths.get( FileUtil.getBasedir(), "src/test", TEST_REPO_1,
57 "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" ).toFile() );
58 files.add( Paths.get( FileUtil.getBasedir(), "src/test", TEST_REPO_1,
59 "org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ).toFile() );
61 createIndex( TEST_REPO_1, files, scan );
64 private void createIndexContainingMoreArtifacts( boolean scan )
67 List<File> files = new ArrayList<>();
68 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
69 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
70 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
71 + "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" ) );
72 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
73 + "/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ) );
74 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
75 + "/org/apache/archiva/archiva-webapp/1.0/archiva-webapp-1.0.war" ) );
76 files.add( new File( FileUtil.getBasedir(),
77 "src/test/" + TEST_REPO_1 + "/com/artifactid-numeric/1.0/artifactid-numeric-1.0.jar" ) );
78 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
79 + "/com/artifactid-numeric123/1.0/artifactid-numeric123-1.0.jar" ) );
80 files.add( new File( FileUtil.getBasedir(),
81 "src/test/" + TEST_REPO_1 + "/com/classname-search/1.0/classname-search-1.0.jar" ) );
83 createIndex( TEST_REPO_1, files, scan );
86 private void createIndexContainingMultipleArtifactsSameVersion( boolean scan )
89 List<File> files = new ArrayList<>();
91 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
92 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
94 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
95 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.pom" ) );
97 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
98 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0-sources.jar" ) );
100 createIndex( TEST_REPO_1, files, scan );
104 public void testQuickSearch()
107 createSimpleIndex( false );
109 List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
112 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
114 archivaConfigControl.replay();
116 SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
118 archivaConfigControl.verify();
120 assertNotNull( results );
122 SearchResultHit hit =
123 results.getSearchResultHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-search", null, "jar" ) );
124 assertNotNull( "hit null in result " + results.getHits(), hit );
125 assertEquals( "org.apache.archiva", hit.getGroupId() );
126 assertEquals( "archiva-search", hit.getArtifactId() );
127 assertEquals( "1.0", hit.getVersions().get( 0 ) );
129 archivaConfigControl.reset();
132 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
134 archivaConfigControl.replay();
136 results = search.search( "user", selectedRepos, "org.apache.archiva", null, null );
138 archivaConfigControl.verify();
140 assertNotNull( results );
141 assertEquals( "total hints not 3", 3, results.getTotalHits() );
143 //TODO: search for class & package names
147 public void testQuickSearchNotWithClassifier()
150 createSimpleIndex( true );
152 List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
155 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
157 archivaConfigControl.replay();
159 SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
161 archivaConfigControl.verify();
163 assertNotNull( results );
165 SearchResultHit hit =
166 results.getSearchResultHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-search", null, "jar" ) );
167 assertNotNull( "hit null in result " + results.getHits(), hit );
168 assertEquals( "org.apache.archiva", hit.getGroupId() );
169 assertEquals( "archiva-search", hit.getArtifactId() );
170 assertEquals( "1.0", hit.getVersions().get( 0 ) );
172 archivaConfigControl.reset();
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<>();
196 selectedRepos.add( TEST_REPO_1 );
199 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
201 archivaConfigControl.replay();
203 SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
205 archivaConfigControl.verify();
207 assertNotNull( results );
208 assertEquals( 3, results.getTotalHits() );
210 SearchResultHit hit = results.getHits().get( 0 );
211 assertEquals( "org.apache.archiva", hit.getGroupId() );
212 assertEquals( "archiva-search", hit.getArtifactId() );
213 assertEquals( "1.0", hit.getVersions().get( 0 ) );
215 //only 1 version of 1.0 is retrieved
216 assertEquals( 1, hit.getVersions().size() );
220 public void testMultipleArtifactsSameVersionWithClassifier()
223 createIndexContainingMultipleArtifactsSameVersion( true );
225 List<String> selectedRepos = new ArrayList<>();
226 selectedRepos.add( TEST_REPO_1 );
229 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
231 archivaConfigControl.replay();
233 SearchFields searchFields = new SearchFields();
234 searchFields.setGroupId( "org.apache.archiva" );
235 searchFields.setArtifactId( "archiva-search" );
236 searchFields.setClassifier( "sources" );
237 searchFields.setRepositories( selectedRepos );
239 SearchResults results = search.search( "user", searchFields, null );
241 archivaConfigControl.verify();
243 assertNotNull( results );
244 assertEquals( 1, results.getTotalHits() );
246 SearchResultHit hit = results.getHits().get( 0 );
247 assertEquals( "org.apache.archiva", hit.getGroupId() );
248 assertEquals( "archiva-search", hit.getArtifactId() );
249 assertEquals( "1.0", hit.getVersions().get( 0 ) );
251 //only 1 version of 1.0 is retrieved
252 assertEquals( 1, hit.getVersions().size() );
255 // search for existing artifact using multiple keywords
257 public void testQuickSearchWithMultipleKeywords()
260 createSimpleIndex( false );
262 List<String> selectedRepos = new ArrayList<>();
263 selectedRepos.add( TEST_REPO_1 );
265 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
266 archivaConfigControl.replay();
268 SearchResults results = search.search( "user", selectedRepos, "archiva search", null, null );
270 archivaConfigControl.verify();
272 assertNotNull( results );
273 assertEquals( 1, results.getTotalHits() );
277 public void testQuickSearchWithPagination()
280 createSimpleIndex( true );
282 List<String> selectedRepos = new ArrayList<>();
283 selectedRepos.add( TEST_REPO_1 );
286 SearchResultLimits limits = new SearchResultLimits( 0 );
287 limits.setPageSize( 1 );
289 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
291 archivaConfigControl.replay();
293 SearchResults results = search.search( "user", selectedRepos, "org", limits, Collections.<String>emptyList() );
295 archivaConfigControl.verify();
297 assertNotNull( results );
298 assertEquals( 1, results.getHits().size() );
299 assertEquals( "total hits not 8 for page1 " + results, 8, results.getTotalHits() );
300 assertEquals( "returned hits not 1 for page1 " + results, 1, results.getReturnedHitsCount() );
301 assertEquals( limits, results.getLimits() );
303 archivaConfigControl.reset();
306 limits = new SearchResultLimits( 1 );
307 limits.setPageSize( 1 );
309 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
311 archivaConfigControl.replay();
313 results = search.search( "user", selectedRepos, "org", limits, null );
315 archivaConfigControl.verify();
317 assertNotNull( results );
319 assertEquals( "hits not 1", 1, results.getHits().size() );
320 assertEquals( "total hits not 8 for page 2 " + results, 8, results.getTotalHits() );
321 assertEquals( "returned hits not 1 for page2 " + results, 1, results.getReturnedHitsCount() );
322 assertEquals( limits, results.getLimits() );
326 public void testArtifactFoundInMultipleRepositories()
329 createSimpleIndex( true );
331 List<File> files = new ArrayList<>();
332 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_2
333 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
334 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_2
335 + "/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar" ) );
336 createIndex( TEST_REPO_2, files, false );
338 List<String> selectedRepos = new ArrayList<>();
339 selectedRepos.add( TEST_REPO_1 );
340 selectedRepos.add( TEST_REPO_2 );
342 config.addManagedRepository( createRepositoryConfig( TEST_REPO_2 ) );
344 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 5 );
346 archivaConfigControl.replay();
348 // wait lucene flush.....
349 Thread.sleep( 2000 );
351 SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
353 archivaConfigControl.verify();
355 assertNotNull( results );
357 SearchResultHit hit =
358 results.getSearchResultHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-search", null, "jar" ) );
359 assertEquals( "org.apache.archiva", hit.getGroupId() );
360 assertEquals( "archiva-search", hit.getArtifactId() );
361 assertEquals( "not 2 version for hit " + hit + "::" + niceDisplay( results ), 2, hit.getVersions().size() );
362 assertTrue( hit.getVersions().contains( "1.0" ) );
363 assertTrue( hit.getVersions().contains( "1.1" ) );
365 archivaConfigControl.reset();
367 // TODO: [BROWSE] in artifact info from browse, display all the repositories where the artifact is found
371 public void testNoMatchFound()
374 createSimpleIndex( false );
376 List<String> selectedRepos = new ArrayList<>();
377 selectedRepos.add( TEST_REPO_1 );
379 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
381 archivaConfigControl.replay();
383 SearchResults results = search.search( "user", selectedRepos, "dfghdfkweriuasndsaie", null, null );
385 archivaConfigControl.verify();
387 assertNotNull( results );
388 assertEquals( 0, results.getTotalHits() );
392 public void testNoIndexFound()
395 List<String> selectedRepos = new ArrayList<>();
396 selectedRepos.add( TEST_REPO_1 );
398 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
400 archivaConfigControl.replay();
402 SearchResults results = search.search( "user", selectedRepos, "org.apache.archiva", null, null );
403 assertNotNull( results );
404 assertEquals( 0, results.getTotalHits() );
406 archivaConfigControl.verify();
410 public void testRepositoryNotFound()
413 List<String> selectedRepos = new ArrayList<>();
414 selectedRepos.add( "non-existing-repo" );
416 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
418 archivaConfigControl.replay();
420 SearchResults results = search.search( "user", selectedRepos, "org.apache.archiva", null, null );
421 assertNotNull( results );
422 assertEquals( 0, results.getTotalHits() );
424 archivaConfigControl.verify();
428 public void testSearchWithinSearchResults()
431 createSimpleIndex( true );
433 List<String> selectedRepos = new ArrayList<>();
434 selectedRepos.add( TEST_REPO_1 );
436 List<String> previousSearchTerms = new ArrayList<>();
437 previousSearchTerms.add( "archiva-test" );
439 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
441 archivaConfigControl.replay();
443 SearchResults results = search.search( "user", selectedRepos, "1.0", null, previousSearchTerms );
445 archivaConfigControl.verify();
447 assertNotNull( results );
448 assertEquals( "total hints not 1", 1, results.getTotalHits() );
450 SearchResultHit hit = results.getHits().get( 0 );
451 assertEquals( "org.apache.archiva", hit.getGroupId() );
452 assertEquals( "archiva-test", hit.getArtifactId() );
453 assertEquals( "versions not 1", 1, hit.getVersions().size() );
454 assertEquals( "1.0", hit.getVersions().get( 0 ) );
457 // tests for advanced search
459 public void testAdvancedSearch()
462 List<File> files = new ArrayList<>();
463 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_2
464 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
465 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_2
466 + "/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar" ) );
467 createIndex( TEST_REPO_2, files, false );
469 List<String> selectedRepos = new ArrayList<>();
470 selectedRepos.add( TEST_REPO_2 );
472 SearchFields searchFields = new SearchFields();
473 searchFields.setGroupId( "org.apache.archiva" );
474 searchFields.setVersion( "1.0" );
475 searchFields.setRepositories( selectedRepos );
477 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
479 archivaConfigControl.replay();
481 SearchResults results = search.search( "user", searchFields, null );
483 archivaConfigControl.verify();
485 assertNotNull( results );
486 assertEquals( 1, results.getTotalHits() );
488 SearchResultHit hit = results.getHits().get( 0 );
489 assertEquals( "org.apache.archiva", hit.getGroupId() );
490 assertEquals( "archiva-search", hit.getArtifactId() );
491 assertEquals( "1.0", hit.getVersions().get( 0 ) );
495 public void testAdvancedSearchWithPagination()
498 createIndexContainingMoreArtifacts( false );
500 List<String> selectedRepos = new ArrayList<>();
501 selectedRepos.add( TEST_REPO_1 );
503 SearchFields searchFields = new SearchFields();
504 searchFields.setGroupId( "org.apache.archiva" );
505 searchFields.setRepositories( selectedRepos );
509 SearchResultLimits limits = new SearchResultLimits( 0 );
510 limits.setPageSize( 1 );
512 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
514 archivaConfigControl.replay();
516 SearchResults results = search.search( "user", searchFields, limits );
518 archivaConfigControl.verify();
520 assertNotNull( results );
521 assertEquals( 4, results.getTotalHits() );
522 assertEquals( 1, results.getHits().size() );
525 archivaConfigControl.reset();
527 limits = new SearchResultLimits( 1 );
528 limits.setPageSize( 1 );
530 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
532 archivaConfigControl.replay();
534 results = search.search( "user", searchFields, limits );
536 archivaConfigControl.verify();
538 assertNotNull( results );
539 assertEquals( 4, results.getTotalHits() );
540 assertEquals( 1, results.getHits().size() );
543 // MRM-981 - artifactIds with numeric characters aren't found in advanced search
545 public void testAdvancedSearchArtifactIdHasNumericChar()
548 List<File> files = new ArrayList<>();
549 files.add( new File( FileUtil.getBasedir(),
550 "src/test/" + TEST_REPO_1 + "/com/artifactid-numeric/1.0/artifactid-numeric-1.0.jar" ) );
551 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
552 + "/com/artifactid-numeric123/1.0/artifactid-numeric123-1.0.jar" ) );
553 createIndex( TEST_REPO_1, files, true );
555 List<String> selectedRepos = new ArrayList<>();
556 selectedRepos.add( TEST_REPO_1 );
558 SearchFields searchFields = new SearchFields();
559 searchFields.setArtifactId( "artifactid-numeric" );
560 searchFields.setRepositories( selectedRepos );
562 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
564 archivaConfigControl.replay();
566 SearchResults results = search.search( "user", searchFields, null );
568 archivaConfigControl.verify();
570 assertNotNull( results );
571 assertEquals( 2, results.getTotalHits() );
575 public void testAdvancedSearchNoRepositoriesConfigured()
578 SearchFields searchFields = new SearchFields();
579 searchFields.setArtifactId( "archiva" );
580 searchFields.setRepositories( null );
584 search.search( "user", searchFields, null );
585 fail( "A RepositorySearchExcecption should have been thrown." );
587 catch ( RepositorySearchException e )
589 assertEquals( "Repositories cannot be null.", e.getMessage() );
594 public void testAdvancedSearchSearchFieldsAreNull()
597 List<String> selectedRepos = new ArrayList<>();
598 selectedRepos.add( TEST_REPO_1 );
600 SearchFields searchFields = new SearchFields();
601 searchFields.setRepositories( selectedRepos );
605 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
607 archivaConfigControl.replay();
609 search.search( "user", searchFields, null );
611 archivaConfigControl.verify();
613 fail( "A RepositorySearchExcecption should have been thrown." );
615 catch ( RepositorySearchException e )
617 assertEquals( "No search fields set.", e.getMessage() );
622 public void testAdvancedSearchSearchFieldsAreBlank()
625 List<String> selectedRepos = new ArrayList<>();
626 selectedRepos.add( TEST_REPO_1 );
628 SearchFields searchFields = new SearchFields();
629 searchFields.setGroupId( "" );
630 searchFields.setArtifactId( "" );
631 searchFields.setVersion( "" );
632 searchFields.setPackaging( "" );
633 searchFields.setClassName( "" );
635 searchFields.setRepositories( selectedRepos );
639 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
641 archivaConfigControl.replay();
643 search.search( "user", searchFields, null );
645 archivaConfigControl.verify();
647 fail( "A RepositorySearchExcecption should have been thrown." );
649 catch ( RepositorySearchException e )
651 assertEquals( "No search fields set.", e.getMessage() );
656 public void testAdvancedSearchAllSearchCriteriaSpecified()
659 createSimpleIndex( true );
661 List<String> selectedRepos = new ArrayList<>();
662 selectedRepos.add( TEST_REPO_1 );
664 SearchFields searchFields = new SearchFields();
665 searchFields.setGroupId( "org.apache.archiva" );
666 searchFields.setArtifactId( "archiva-test" );
667 searchFields.setVersion( "2.0" );
668 searchFields.setPackaging( "jar" );
669 searchFields.setClassName( "org.apache.archiva.test.App" );
670 searchFields.setRepositories( selectedRepos );
672 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
674 archivaConfigControl.replay();
676 SearchResults results = search.search( "user", searchFields, null );
678 archivaConfigControl.verify();
680 assertNotNull( results );
682 assertEquals( "total hints not 1" + results, 1, results.getTotalHits() );
684 SearchResultHit hit = results.getHits().get( 0 );
685 assertEquals( "org.apache.archiva", hit.getGroupId() );
686 assertEquals( "archiva-test", hit.getArtifactId() );
687 assertEquals( "version not 2.0", "2.0", hit.getVersions().get( 0 ) );
691 public void testAdvancedSearchJarArtifacts()
694 createIndexContainingMoreArtifacts( true );
696 List<String> selectedRepos = new ArrayList<>();
697 selectedRepos.add( TEST_REPO_1 );
699 SearchFields searchFields = new SearchFields();
700 searchFields.setPackaging( "jar" );
701 searchFields.setRepositories( selectedRepos );
703 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
705 archivaConfigControl.replay();
707 SearchResults results = search.search( "user", searchFields, null );
709 archivaConfigControl.verify();
711 assertNotNull( results );
712 assertEquals( "not 8 but " + results.getTotalHits() + ":" + niceDisplay( results ), 8, results.getTotalHits() );
716 public void testAdvancedSearchWithIncorrectPackaging()
719 createSimpleIndex( true );
721 List<String> selectedRepos = new ArrayList<>();
722 selectedRepos.add( TEST_REPO_1 );
724 SearchFields searchFields = new SearchFields();
725 searchFields.setGroupId( "org.apache.archiva" );
726 searchFields.setArtifactId( "archiva-test" );
727 searchFields.setVersion( "2.0" );
728 searchFields.setPackaging( "war" );
729 searchFields.setRepositories( selectedRepos );
731 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
732 archivaConfigControl.replay();
734 SearchResults results = search.search( "user", searchFields, null );
736 archivaConfigControl.verify();
738 assertNotNull( results );
739 assertEquals( 0, results.getTotalHits() );
743 public void testAdvancedSearchClassname()
746 createIndexContainingMoreArtifacts( true );
748 List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
750 SearchFields searchFields = new SearchFields();
751 searchFields.setClassName( "com.classname.search.App" );
752 searchFields.setRepositories( selectedRepos );
754 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
756 archivaConfigControl.replay();
758 SearchResults results = search.search( "user", searchFields, null );
760 archivaConfigControl.verify();
762 assertNotNull( results );
763 assertEquals( "totalHits not 1 results " + results, 1, results.getTotalHits() );
765 SearchResultHit hit = results.getHits().get( 0 );
766 assertEquals( "groupId not com", "com", hit.getGroupId() );
767 assertEquals( "arttifactId not classname-search", "classname-search", hit.getArtifactId() );
768 assertEquals( " hits.version(0) not 1.0", "1.0", hit.getVersions().get( 0 ) );
772 public void testAdvancedSearchNoIndexFound()
775 List<String> selectedRepos = new ArrayList<>();
776 selectedRepos.add( TEST_REPO_1 );
778 SearchFields searchFields = new SearchFields();
779 searchFields.setGroupId( "org.apache.archiva" );
780 searchFields.setRepositories( selectedRepos );
782 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
784 archivaConfigControl.replay();
786 SearchResults results = search.search( "user", searchFields, null );
788 archivaConfigControl.verify();
790 assertNotNull( results );
791 assertEquals( 0, results.getTotalHits() );
795 public void testAdvancedSearchClassNameInWar()
798 createIndexContainingMoreArtifacts( true );
800 List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
802 SearchFields searchFields = new SearchFields();
803 searchFields.setClassName( "SomeClass" );
804 searchFields.setRepositories( selectedRepos );
806 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
808 archivaConfigControl.replay();
810 SearchResults results = search.search( "user", searchFields, null );
812 archivaConfigControl.verify();
814 assertNotNull( results );
815 assertEquals( 1, results.getHits().size() );
816 assertEquals( "test-webapp", results.getHits().get( 0 ).getArtifactId() );
820 public void getAllGroupIds()
823 createIndexContainingMoreArtifacts( true );
825 List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
827 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 0, 2 );
829 archivaConfigControl.replay();
831 Collection<String> groupIds = search.getAllGroupIds( "user", selectedRepos );
833 archivaConfigControl.verify();
835 log.info( "groupIds: {}", groupIds );
837 assertEquals( 3, groupIds.size() );
838 assertTrue( groupIds.contains( "com" ) );
839 assertTrue( groupIds.contains( "org.apache.felix" ) );
840 assertTrue( groupIds.contains( "org.apache.archiva" ) );
844 public void testSearchWithUnknownRepo()
847 createIndexContainingMoreArtifacts( true );
849 List<String> selectedRepos = Arrays.asList( "foo" );
851 SearchFields searchFields = new SearchFields();
852 searchFields.setClassName( "SomeClass" );
853 searchFields.setRepositories( selectedRepos );
855 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
857 archivaConfigControl.replay();
859 SearchResults results = search.search( "user", searchFields, null );
861 archivaConfigControl.verify();
863 assertNotNull( results );
864 assertEquals( 0, results.getHits().size() );
868 public void nolimitedResult()
872 File repo = new File( "target/repo-release" );
873 File indexDirectory = new File( repo, ".index" );
874 FileUtils.copyDirectoryStructure( new File( "src/test/repo-release" ), repo );
876 createIndex( "repo-release", Collections.<File>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( 0 );
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 ), "org.example", limits,
890 Collections.<String>emptyList() );
892 log.info( "results: {}", searchResults.getHits().size() );
894 assertEquals( 255, searchResults.getHits().size() );
896 SearchFields searchFields = new SearchFields();
897 searchFields.setGroupId( "org.example" );
898 searchFields.setRepositories( Arrays.asList( REPO_RELEASE ) );
900 searchResults = search.search( null, searchFields, limits );
902 log.info( "results: {}", searchResults.getHits().size() );
904 assertEquals( 255, searchResults.getHits().size() );
906 archivaConfigControl.verify();