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.indexer.util.SearchUtil;
23 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
24 import org.apache.maven.index_shaded.lucene.index.IndexUpgrader;
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;
31 import java.nio.file.Path;
32 import java.nio.file.Paths;
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.Collection;
36 import java.util.Collections;
37 import java.util.List;
40 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
41 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
42 public class MavenRepositorySearchTest
43 extends AbstractMavenRepositorySearch
47 private void createSimpleIndex( boolean scan )
50 List<Path> files = new ArrayList<>();
51 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "src/test", TEST_REPO_1,
52 "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
53 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "src/test", TEST_REPO_1,
54 "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" ));
55 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "src/test", TEST_REPO_1,
56 "org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ));
58 createIndex( TEST_REPO_1, files, scan );
61 private void createIndexContainingMoreArtifacts( boolean scan )
64 List<Path> files = new ArrayList<>();
65 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "src/test/" + TEST_REPO_1
66 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
67 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "src/test/" + TEST_REPO_1
68 + "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" ) );
69 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "src/test/" + TEST_REPO_1
70 + "/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ) );
71 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "src/test/" + TEST_REPO_1
72 + "/org/apache/archiva/archiva-webapp/1.0/archiva-webapp-1.0.war" ) );
73 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(),
74 "src/test/" + TEST_REPO_1 + "/com/artifactid-numeric/1.0/artifactid-numeric-1.0.jar" ) );
75 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "src/test/" + TEST_REPO_1
76 + "/com/artifactid-numeric123/1.0/artifactid-numeric123-1.0.jar" ) );
77 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(),
78 "src/test/" + TEST_REPO_1 + "/com/classname-search/1.0/classname-search-1.0.jar" ) );
80 createIndex( TEST_REPO_1, files, scan );
83 private void createIndexContainingMultipleArtifactsSameVersion( boolean scan )
86 List<Path> files = new ArrayList<>();
88 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "src/test/" + TEST_REPO_1
89 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
91 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "src/test/" + TEST_REPO_1
92 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.pom" ) );
94 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "src/test/" + TEST_REPO_1
95 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0-sources.jar" ) );
97 createIndex( TEST_REPO_1, files, scan );
101 public void testQuickSearch()
104 createSimpleIndex( false );
106 List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
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 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
131 archivaConfigControl.replay();
133 results = search.search( "user", selectedRepos, "org.apache.archiva", null, null );
135 archivaConfigControl.verify();
137 assertNotNull( results );
138 assertEquals( "total hints not 3", 3, results.getTotalHits() );
140 //TODO: search for class & package names
144 public void testQuickSearchNotWithClassifier()
147 createSimpleIndex( true );
149 List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
152 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
154 archivaConfigControl.replay();
156 SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
158 archivaConfigControl.verify();
160 assertNotNull( results );
162 SearchResultHit hit =
163 results.getSearchResultHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-search", null, "jar" ) );
164 assertNotNull( "hit null in result " + results.getHits(), hit );
165 assertEquals( "org.apache.archiva", hit.getGroupId() );
166 assertEquals( "archiva-search", hit.getArtifactId() );
167 assertEquals( "1.0", hit.getVersions().get( 0 ) );
169 archivaConfigControl.reset();
172 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
174 archivaConfigControl.replay();
176 results = search.search( "user", selectedRepos, "archiva-search", null, null );
178 archivaConfigControl.verify();
180 assertNotNull( results );
181 assertEquals( "total hints not 3 hits " + results.getHits(), 3, results.getTotalHits() );
183 //TODO: search for class & package names
187 public void testQuickSearchMultipleArtifactsSameVersion()
190 createIndexContainingMultipleArtifactsSameVersion( false );
192 List<String> selectedRepos = new ArrayList<>();
193 selectedRepos.add( TEST_REPO_1 );
196 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
198 archivaConfigControl.replay();
200 SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
202 archivaConfigControl.verify();
204 assertNotNull( results );
205 assertEquals( 3, results.getTotalHits() );
207 SearchResultHit hit = results.getHits().get( 0 );
208 assertEquals( "org.apache.archiva", hit.getGroupId() );
209 assertEquals( "archiva-search", hit.getArtifactId() );
210 assertEquals( "1.0", hit.getVersions().get( 0 ) );
212 //only 1 version of 1.0 is retrieved
213 assertEquals( 1, hit.getVersions().size() );
217 public void testMultipleArtifactsSameVersionWithClassifier()
220 createIndexContainingMultipleArtifactsSameVersion( true );
222 List<String> selectedRepos = new ArrayList<>();
223 selectedRepos.add( TEST_REPO_1 );
226 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
228 archivaConfigControl.replay();
230 SearchFields searchFields = new SearchFields();
231 searchFields.setGroupId( "org.apache.archiva" );
232 searchFields.setArtifactId( "archiva-search" );
233 searchFields.setClassifier( "sources" );
234 searchFields.setRepositories( selectedRepos );
236 SearchResults results = search.search( "user", searchFields, null );
238 archivaConfigControl.verify();
240 assertNotNull( results );
241 assertEquals( 1, results.getTotalHits() );
243 SearchResultHit hit = results.getHits().get( 0 );
244 assertEquals( "org.apache.archiva", hit.getGroupId() );
245 assertEquals( "archiva-search", hit.getArtifactId() );
246 assertEquals( "1.0", hit.getVersions().get( 0 ) );
248 //only 1 version of 1.0 is retrieved
249 assertEquals( 1, hit.getVersions().size() );
252 // search for existing artifact using multiple keywords
254 public void testQuickSearchWithMultipleKeywords()
257 createSimpleIndex( false );
259 List<String> selectedRepos = new ArrayList<>();
260 selectedRepos.add( TEST_REPO_1 );
262 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
263 archivaConfigControl.replay();
265 SearchResults results = search.search( "user", selectedRepos, "archiva search", null, null );
267 archivaConfigControl.verify();
269 assertNotNull( results );
270 assertEquals( 1, results.getTotalHits() );
274 public void testQuickSearchWithPagination()
277 createSimpleIndex( true );
279 List<String> selectedRepos = new ArrayList<>();
280 selectedRepos.add( TEST_REPO_1 );
283 SearchResultLimits limits = new SearchResultLimits( 0 );
284 limits.setPageSize( 1 );
286 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
288 archivaConfigControl.replay();
290 SearchResults results = search.search( "user", selectedRepos, "org", limits, Collections.emptyList() );
292 archivaConfigControl.verify();
294 assertNotNull( results );
295 assertEquals( 1, results.getHits().size() );
296 assertEquals( "total hits not 9 for page1 " + results, 9, results.getTotalHits() );
297 assertEquals( "returned hits not 1 for page1 " + results, 1, results.getReturnedHitsCount() );
298 assertEquals( limits, results.getLimits() );
300 archivaConfigControl.reset();
303 limits = new SearchResultLimits( 1 );
304 limits.setPageSize( 1 );
306 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
308 archivaConfigControl.replay();
310 results = search.search( "user", selectedRepos, "org", limits, null );
312 archivaConfigControl.verify();
314 assertNotNull( results );
316 assertEquals( "hits not 1", 1, results.getHits().size() );
317 assertEquals( "total hits not 9 for page 2 " + results, 9, results.getTotalHits() );
318 assertEquals( "returned hits not 1 for page2 " + results, 1, results.getReturnedHitsCount() );
319 assertEquals( limits, results.getLimits() );
323 public void testArtifactFoundInMultipleRepositories()
326 createSimpleIndex( true );
328 List<Path> files = new ArrayList<>();
329 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "src/test/" + TEST_REPO_2
330 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
331 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "src/test/" + TEST_REPO_2
332 + "/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar" ) );
333 createIndex( TEST_REPO_2, files, false );
335 List<String> selectedRepos = new ArrayList<>();
336 selectedRepos.add( TEST_REPO_1 );
337 selectedRepos.add( TEST_REPO_2 );
339 config.addManagedRepository( createRepositoryConfig( TEST_REPO_2 ) );
341 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 5 );
343 archivaConfigControl.replay();
345 // wait lucene flush.....
346 Thread.sleep( 2000 );
348 SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
350 archivaConfigControl.verify();
352 assertNotNull( results );
354 SearchResultHit hit =
355 results.getSearchResultHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-search", null, "jar" ) );
356 assertEquals( "org.apache.archiva", hit.getGroupId() );
357 assertEquals( "archiva-search", hit.getArtifactId() );
358 assertEquals( "not 2 version for hit " + hit + "::" + niceDisplay( results ), 2, hit.getVersions().size() );
359 assertTrue( hit.getVersions().contains( "1.0" ) );
360 assertTrue( hit.getVersions().contains( "1.1" ) );
362 archivaConfigControl.reset();
364 // TODO: [BROWSE] in artifact info from browse, display all the repositories where the artifact is found
368 public void testNoMatchFound()
371 createSimpleIndex( false );
373 List<String> selectedRepos = new ArrayList<>();
374 selectedRepos.add( TEST_REPO_1 );
376 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
378 archivaConfigControl.replay();
380 SearchResults results = search.search( "user", selectedRepos, "dfghdfkweriuasndsaie", null, null );
382 archivaConfigControl.verify();
384 assertNotNull( results );
385 assertEquals( 0, results.getTotalHits() );
389 public void testNoIndexFound()
392 List<String> selectedRepos = new ArrayList<>();
393 selectedRepos.add( TEST_REPO_1 );
395 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
397 archivaConfigControl.replay();
399 SearchResults results = search.search( "user", selectedRepos, "org.apache.archiva", null, null );
400 assertNotNull( results );
401 assertEquals( 0, results.getTotalHits() );
403 archivaConfigControl.verify();
407 public void testRepositoryNotFound()
410 List<String> selectedRepos = new ArrayList<>();
411 selectedRepos.add( "non-existing-repo" );
413 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
415 archivaConfigControl.replay();
417 SearchResults results = search.search( "user", selectedRepos, "org.apache.archiva", null, null );
418 assertNotNull( results );
419 assertEquals( 0, results.getTotalHits() );
421 archivaConfigControl.verify();
425 public void testSearchWithinSearchResults()
428 createSimpleIndex( true );
430 List<String> selectedRepos = new ArrayList<>();
431 selectedRepos.add( TEST_REPO_1 );
433 List<String> previousSearchTerms = new ArrayList<>();
434 previousSearchTerms.add( "archiva-test" );
436 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
438 archivaConfigControl.replay();
440 SearchResults results = search.search( "user", selectedRepos, "1.0", null, previousSearchTerms );
442 archivaConfigControl.verify();
444 assertNotNull( results );
445 assertEquals( "total hints not 1", 1, results.getTotalHits() );
447 SearchResultHit hit = results.getHits().get( 0 );
448 assertEquals( "org.apache.archiva", hit.getGroupId() );
449 assertEquals( "archiva-test", hit.getArtifactId() );
450 assertEquals( "versions not 1", 1, hit.getVersions().size() );
451 assertEquals( "1.0", hit.getVersions().get( 0 ) );
454 // tests for advanced search
456 public void testAdvancedSearch()
459 List<Path> files = new ArrayList<>();
460 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "src/test/" + TEST_REPO_2
461 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
462 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "src/test/" + TEST_REPO_2
463 + "/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar" ) );
464 createIndex( TEST_REPO_2, files, false );
466 List<String> selectedRepos = new ArrayList<>();
467 selectedRepos.add( TEST_REPO_2 );
469 SearchFields searchFields = new SearchFields();
470 searchFields.setGroupId( "org.apache.archiva" );
471 searchFields.setVersion( "1.0" );
472 searchFields.setRepositories( selectedRepos );
474 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
476 archivaConfigControl.replay();
478 SearchResults results = search.search( "user", searchFields, null );
480 archivaConfigControl.verify();
482 assertNotNull( results );
483 assertEquals( 1, results.getTotalHits() );
485 SearchResultHit hit = results.getHits().get( 0 );
486 assertEquals( "org.apache.archiva", hit.getGroupId() );
487 assertEquals( "archiva-search", hit.getArtifactId() );
488 assertEquals( "1.0", hit.getVersions().get( 0 ) );
492 public void testAdvancedSearchWithPagination()
495 createIndexContainingMoreArtifacts( false );
497 List<String> selectedRepos = new ArrayList<>();
498 selectedRepos.add( TEST_REPO_1 );
500 SearchFields searchFields = new SearchFields();
501 searchFields.setGroupId( "org.apache.archiva" );
502 searchFields.setRepositories( selectedRepos );
506 SearchResultLimits limits = new SearchResultLimits( 0 );
507 limits.setPageSize( 1 );
509 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
511 archivaConfigControl.replay();
513 SearchResults results = search.search( "user", searchFields, limits );
515 archivaConfigControl.verify();
517 assertNotNull( results );
518 assertEquals( 4, results.getTotalHits() );
519 assertEquals( 1, results.getHits().size() );
522 archivaConfigControl.reset();
524 limits = new SearchResultLimits( 1 );
525 limits.setPageSize( 1 );
527 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
529 archivaConfigControl.replay();
531 results = search.search( "user", searchFields, limits );
533 archivaConfigControl.verify();
535 assertNotNull( results );
536 assertEquals( 4, results.getTotalHits() );
537 assertEquals( 1, results.getHits().size() );
540 // MRM-981 - artifactIds with numeric characters aren't found in advanced search
542 public void testAdvancedSearchArtifactIdHasNumericChar()
545 List<Path> files = new ArrayList<>();
546 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(),
547 "src/test/" + TEST_REPO_1 + "/com/artifactid-numeric/1.0/artifactid-numeric-1.0.jar" ) );
548 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "src/test/" + TEST_REPO_1
549 + "/com/artifactid-numeric123/1.0/artifactid-numeric123-1.0.jar" ) );
550 createIndex( TEST_REPO_1, files, true );
552 List<String> selectedRepos = new ArrayList<>();
553 selectedRepos.add( TEST_REPO_1 );
555 SearchFields searchFields = new SearchFields();
556 searchFields.setArtifactId( "artifactid-numeric" );
557 searchFields.setRepositories( selectedRepos );
559 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
561 archivaConfigControl.replay();
563 SearchResults results = search.search( "user", searchFields, null );
565 archivaConfigControl.verify();
567 assertNotNull( results );
568 assertEquals( 2, results.getTotalHits() );
572 public void testAdvancedSearchNoRepositoriesConfigured()
575 SearchFields searchFields = new SearchFields();
576 searchFields.setArtifactId( "archiva" );
577 searchFields.setRepositories( null );
581 search.search( "user", searchFields, null );
582 fail( "A RepositorySearchExcecption should have been thrown." );
584 catch ( RepositorySearchException e )
586 assertEquals( "Repositories cannot be null.", e.getMessage() );
591 public void testAdvancedSearchSearchFieldsAreNull()
594 List<String> selectedRepos = new ArrayList<>();
595 selectedRepos.add( TEST_REPO_1 );
597 SearchFields searchFields = new SearchFields();
598 searchFields.setRepositories( selectedRepos );
602 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
604 archivaConfigControl.replay();
606 search.search( "user", searchFields, null );
608 archivaConfigControl.verify();
610 fail( "A RepositorySearchExcecption should have been thrown." );
612 catch ( RepositorySearchException e )
614 assertEquals( "No search fields set.", e.getMessage() );
619 public void testAdvancedSearchSearchFieldsAreBlank()
622 List<String> selectedRepos = new ArrayList<>();
623 selectedRepos.add( TEST_REPO_1 );
625 SearchFields searchFields = new SearchFields();
626 searchFields.setGroupId( "" );
627 searchFields.setArtifactId( "" );
628 searchFields.setVersion( "" );
629 searchFields.setPackaging( "" );
630 searchFields.setClassName( "" );
632 searchFields.setRepositories( selectedRepos );
636 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
638 archivaConfigControl.replay();
640 search.search( "user", searchFields, null );
642 archivaConfigControl.verify();
644 fail( "A RepositorySearchExcecption should have been thrown." );
646 catch ( RepositorySearchException e )
648 assertEquals( "No search fields set.", e.getMessage() );
653 public void testAdvancedSearchAllSearchCriteriaSpecified()
656 createSimpleIndex( true );
658 List<String> selectedRepos = new ArrayList<>();
659 selectedRepos.add( TEST_REPO_1 );
661 SearchFields searchFields = new SearchFields();
662 searchFields.setGroupId( "org.apache.archiva" );
663 searchFields.setArtifactId( "archiva-test" );
664 searchFields.setVersion( "2.0" );
665 searchFields.setPackaging( "jar" );
666 searchFields.setClassName( "org.apache.archiva.test.App" );
667 searchFields.setRepositories( selectedRepos );
669 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
671 archivaConfigControl.replay();
673 SearchResults results = search.search( "user", searchFields, null );
675 archivaConfigControl.verify();
677 assertNotNull( results );
679 assertEquals( "total hints not 1" + results, 1, results.getTotalHits() );
681 SearchResultHit hit = results.getHits().get( 0 );
682 assertEquals( "org.apache.archiva", hit.getGroupId() );
683 assertEquals( "archiva-test", hit.getArtifactId() );
684 assertEquals( "version not 2.0", "2.0", hit.getVersions().get( 0 ) );
688 public void testAdvancedSearchJarArtifacts()
691 createIndexContainingMoreArtifacts( true );
693 List<String> selectedRepos = new ArrayList<>();
694 selectedRepos.add( TEST_REPO_1 );
696 SearchFields searchFields = new SearchFields();
697 searchFields.setPackaging( "jar" );
698 searchFields.setRepositories( selectedRepos );
700 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
702 archivaConfigControl.replay();
704 SearchResults results = search.search( "user", searchFields, null );
706 archivaConfigControl.verify();
708 assertNotNull( results );
709 assertEquals( "not 8 but " + results.getTotalHits() + ":" + niceDisplay( results ), 8, results.getTotalHits() );
713 public void testAdvancedSearchWithIncorrectPackaging()
716 createSimpleIndex( true );
718 List<String> selectedRepos = new ArrayList<>();
719 selectedRepos.add( TEST_REPO_1 );
721 SearchFields searchFields = new SearchFields();
722 searchFields.setGroupId( "org.apache.archiva" );
723 searchFields.setArtifactId( "archiva-test" );
724 searchFields.setVersion( "2.0" );
725 searchFields.setPackaging( "war" );
726 searchFields.setRepositories( selectedRepos );
728 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
729 archivaConfigControl.replay();
731 SearchResults results = search.search( "user", searchFields, null );
733 archivaConfigControl.verify();
735 assertNotNull( results );
736 assertEquals( 0, results.getTotalHits() );
740 public void testAdvancedSearchClassname()
743 createIndexContainingMoreArtifacts( true );
745 List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
747 SearchFields searchFields = new SearchFields();
748 searchFields.setClassName( "com.classname.search.App" );
749 searchFields.setRepositories( selectedRepos );
751 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
753 archivaConfigControl.replay();
755 SearchResults results = search.search( "user", searchFields, null );
757 archivaConfigControl.verify();
759 assertNotNull( results );
760 assertEquals( "totalHits not 1 results " + results, 1, results.getTotalHits() );
762 SearchResultHit hit = results.getHits().get( 0 );
763 assertEquals( "groupId not com", "com", hit.getGroupId() );
764 assertEquals( "arttifactId not classname-search", "classname-search", hit.getArtifactId() );
765 assertEquals( " hits.version(0) not 1.0", "1.0", hit.getVersions().get( 0 ) );
769 public void testAdvancedSearchNoIndexFound()
772 List<String> selectedRepos = new ArrayList<>();
773 selectedRepos.add( TEST_REPO_1 );
775 SearchFields searchFields = new SearchFields();
776 searchFields.setGroupId( "org.apache.archiva" );
777 searchFields.setRepositories( selectedRepos );
779 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
781 archivaConfigControl.replay();
783 SearchResults results = search.search( "user", searchFields, null );
785 archivaConfigControl.verify();
787 assertNotNull( results );
788 assertEquals( 0, results.getTotalHits() );
792 public void testAdvancedSearchClassNameInWar()
795 createIndexContainingMoreArtifacts( true );
797 List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
799 SearchFields searchFields = new SearchFields();
800 searchFields.setClassName( "SomeClass" );
801 searchFields.setRepositories( selectedRepos );
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( 1, results.getHits().size() );
813 assertEquals( "test-webapp", results.getHits().get( 0 ).getArtifactId() );
817 public void getAllGroupIds()
820 createIndexContainingMoreArtifacts( true );
822 List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
824 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 0, 2 );
826 archivaConfigControl.replay();
828 Collection<String> groupIds = search.getAllGroupIds( "user", selectedRepos );
830 archivaConfigControl.verify();
832 log.info( "groupIds: {}", groupIds );
834 assertEquals( 3, groupIds.size() );
835 assertTrue( groupIds.contains( "com" ) );
836 assertTrue( groupIds.contains( "org.apache.felix" ) );
837 assertTrue( groupIds.contains( "org.apache.archiva" ) );
841 public void testSearchWithUnknownRepo()
844 createIndexContainingMoreArtifacts( true );
846 List<String> selectedRepos = Arrays.asList( "foo" );
848 SearchFields searchFields = new SearchFields();
849 searchFields.setClassName( "SomeClass" );
850 searchFields.setRepositories( selectedRepos );
852 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 2 );
854 archivaConfigControl.replay();
856 SearchResults results = search.search( "user", searchFields, null );
858 archivaConfigControl.verify();
860 assertNotNull( results );
861 assertEquals( 0, results.getHits().size() );
865 public void nolimitedResult()
869 Path repo = Paths.get( "target/repo-release" );
870 Path indexDirectory = repo.resolve(".index" );
871 FileUtils.copyDirectoryStructure( Paths.get( "src/test/repo-release" ).toFile(), repo.toFile() );
873 IndexUpgrader.main( new String[]{ indexDirectory.toAbsolutePath().toString() } );
875 createIndex( "repo-release", Collections.emptyList(), false );
877 nexusIndexer.addIndexingContext( REPO_RELEASE, REPO_RELEASE, repo.toFile(), indexDirectory.toFile(),
878 repo.toUri().toURL().toExternalForm(),
879 indexDirectory.toUri().toURL().toString(), indexCreators );
881 SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
882 limits.setPageSize( 300 );
884 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 1, 5 );
886 archivaConfigControl.replay();
888 SearchResults searchResults = search.search( null, Arrays.asList( REPO_RELEASE ), //
889 "org.example", limits, //
890 Collections.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();