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.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.springframework.test.context.ContextConfiguration;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.Collection;
34 import java.util.Collections;
35 import java.util.List;
38 @RunWith ( ArchivaSpringJUnit4ClassRunner.class )
39 @ContextConfiguration ( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
40 public class MavenRepositorySearchTest
41 extends AbstractMavenRepositorySearch
45 private void createSimpleIndex( boolean scan )
48 List<File> files = new ArrayList<File>();
49 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
50 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
51 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
52 + "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" ) );
53 files.add( new File( FileUtil.getBasedir(), "/src/test/" + TEST_REPO_1
54 + "/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ) );
56 createIndex( TEST_REPO_1, files, scan );
59 private void createIndexContainingMoreArtifacts( boolean scan )
62 List<File> files = new ArrayList<File>();
63 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
64 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
65 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
66 + "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" ) );
67 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
68 + "/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ) );
69 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
70 + "/org/apache/archiva/archiva-webapp/1.0/archiva-webapp-1.0.war" ) );
71 files.add( new File( FileUtil.getBasedir(),
72 "src/test/" + TEST_REPO_1 + "/com/artifactid-numeric/1.0/artifactid-numeric-1.0.jar" ) );
73 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
74 + "/com/artifactid-numeric123/1.0/artifactid-numeric123-1.0.jar" ) );
75 files.add( new File( FileUtil.getBasedir(),
76 "src/test/" + TEST_REPO_1 + "/com/classname-search/1.0/classname-search-1.0.jar" ) );
78 createIndex( TEST_REPO_1, files, scan );
81 private void createIndexContainingMultipleArtifactsSameVersion( boolean scan )
84 List<File> files = new ArrayList<File>();
86 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
87 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
89 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
90 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.pom" ) );
92 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
93 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0-sources.jar" ) );
95 createIndex( TEST_REPO_1, files, scan );
99 public void testQuickSearch()
102 createSimpleIndex( false );
104 List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
107 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
109 archivaConfigControl.replay();
111 SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
113 archivaConfigControl.verify();
115 assertNotNull( results );
117 SearchResultHit hit =
118 results.getSearchResultHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-search", null, "jar" ) );
119 assertNotNull( "hit null in result " + results.getHits(), hit );
120 assertEquals( "org.apache.archiva", hit.getGroupId() );
121 assertEquals( "archiva-search", hit.getArtifactId() );
122 assertEquals( "1.0", hit.getVersions().get( 0 ) );
124 archivaConfigControl.reset();
127 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
129 archivaConfigControl.replay();
131 results = search.search( "user", selectedRepos, "org.apache.archiva", null, null );
133 archivaConfigControl.verify();
135 assertNotNull( results );
136 assertEquals( "total hints not 3", 3, results.getTotalHits() );
138 //TODO: search for class & package names
142 public void testQuickSearchNotWithClassifier()
145 createSimpleIndex( true );
147 List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
150 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
152 archivaConfigControl.replay();
154 SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
156 archivaConfigControl.verify();
158 assertNotNull( results );
160 SearchResultHit hit =
161 results.getSearchResultHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-search", null, "jar" ) );
162 assertNotNull( "hit null in result " + results.getHits(), hit );
163 assertEquals( "org.apache.archiva", hit.getGroupId() );
164 assertEquals( "archiva-search", hit.getArtifactId() );
165 assertEquals( "1.0", hit.getVersions().get( 0 ) );
167 archivaConfigControl.reset();
170 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
172 archivaConfigControl.replay();
174 results = search.search( "user", selectedRepos, "archiva-search", null, null );
176 archivaConfigControl.verify();
178 assertNotNull( results );
179 assertEquals( "total hints not 3 hits " + results.getHits(), 3, results.getTotalHits() );
181 //TODO: search for class & package names
185 public void testQuickSearchMultipleArtifactsSameVersion()
188 createIndexContainingMultipleArtifactsSameVersion( false );
190 List<String> selectedRepos = new ArrayList<String>();
191 selectedRepos.add( TEST_REPO_1 );
194 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
196 archivaConfigControl.replay();
198 SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
200 archivaConfigControl.verify();
202 assertNotNull( results );
203 assertEquals( 3, results.getTotalHits() );
205 SearchResultHit hit = results.getHits().get( 0 );
206 assertEquals( "org.apache.archiva", hit.getGroupId() );
207 assertEquals( "archiva-search", hit.getArtifactId() );
208 assertEquals( "1.0", hit.getVersions().get( 0 ) );
210 //only 1 version of 1.0 is retrieved
211 assertEquals( 1, hit.getVersions().size() );
215 public void testMultipleArtifactsSameVersionWithClassifier()
218 createIndexContainingMultipleArtifactsSameVersion( true );
220 List<String> selectedRepos = new ArrayList<String>();
221 selectedRepos.add( TEST_REPO_1 );
224 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
226 archivaConfigControl.replay();
228 SearchFields searchFields = new SearchFields();
229 searchFields.setGroupId( "org.apache.archiva" );
230 searchFields.setArtifactId( "archiva-search" );
231 searchFields.setClassifier( "sources" );
232 searchFields.setRepositories( selectedRepos );
234 SearchResults results = search.search( "user", searchFields, null );
236 archivaConfigControl.verify();
238 assertNotNull( results );
239 assertEquals( 1, results.getTotalHits() );
241 SearchResultHit hit = results.getHits().get( 0 );
242 assertEquals( "org.apache.archiva", hit.getGroupId() );
243 assertEquals( "archiva-search", hit.getArtifactId() );
244 assertEquals( "1.0", hit.getVersions().get( 0 ) );
246 //only 1 version of 1.0 is retrieved
247 assertEquals( 1, hit.getVersions().size() );
250 // search for existing artifact using multiple keywords
252 public void testQuickSearchWithMultipleKeywords()
255 createSimpleIndex( false );
257 List<String> selectedRepos = new ArrayList<String>();
258 selectedRepos.add( TEST_REPO_1 );
260 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
261 archivaConfigControl.replay();
263 SearchResults results = search.search( "user", selectedRepos, "archiva search", null, null );
265 archivaConfigControl.verify();
267 assertNotNull( results );
268 assertEquals( 1, results.getTotalHits() );
272 public void testQuickSearchWithPagination()
275 createSimpleIndex( true );
277 List<String> selectedRepos = new ArrayList<String>();
278 selectedRepos.add( TEST_REPO_1 );
281 SearchResultLimits limits = new SearchResultLimits( 0 );
282 limits.setPageSize( 1 );
284 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
286 archivaConfigControl.replay();
288 SearchResults results = search.search( "user", selectedRepos, "org", limits, new ArrayList<String>() );
290 archivaConfigControl.verify();
292 assertNotNull( results );
293 assertEquals( 1, results.getHits().size() );
294 assertEquals( "total hits not 8 for page1 " + results, 8, results.getTotalHits() );
295 assertEquals( "returned hits not 1 for page1 " + results, 1, results.getReturnedHitsCount() );
296 assertEquals( limits, results.getLimits() );
298 archivaConfigControl.reset();
301 limits = new SearchResultLimits( 1 );
302 limits.setPageSize( 1 );
304 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
306 archivaConfigControl.replay();
308 results = search.search( "user", selectedRepos, "org", limits, null );
310 archivaConfigControl.verify();
312 assertNotNull( results );
314 assertEquals( "hits not 1", 1, results.getHits().size() );
315 assertEquals( "total hits not 8 for page 2 " + results, 8, results.getTotalHits() );
316 assertEquals( "returned hits not 1 for page2 " + results, 1, results.getReturnedHitsCount() );
317 assertEquals( limits, results.getLimits() );
321 public void testArtifactFoundInMultipleRepositories()
324 createSimpleIndex( true );
326 List<File> files = new ArrayList<File>();
327 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_2
328 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
329 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_2
330 + "/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar" ) );
331 createIndex( TEST_REPO_2, files, false );
333 List<String> selectedRepos = new ArrayList<String>();
334 selectedRepos.add( TEST_REPO_1 );
335 selectedRepos.add( TEST_REPO_2 );
337 config.addManagedRepository( createRepositoryConfig( TEST_REPO_2 ) );
339 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 5 );
341 archivaConfigControl.replay();
343 // wait lucene flush.....
344 Thread.sleep( 2000 );
346 SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
348 archivaConfigControl.verify();
350 assertNotNull( results );
352 SearchResultHit hit =
353 results.getSearchResultHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-search", null, "jar" ) );
354 assertEquals( "org.apache.archiva", hit.getGroupId() );
355 assertEquals( "archiva-search", hit.getArtifactId() );
356 assertEquals( "not 2 version for hit " + hit + "::" + niceDisplay( results ), 2, hit.getVersions().size() );
357 assertTrue( hit.getVersions().contains( "1.0" ) );
358 assertTrue( hit.getVersions().contains( "1.1" ) );
360 archivaConfigControl.reset();
362 // TODO: [BROWSE] in artifact info from browse, display all the repositories where the artifact is found
366 public void testNoMatchFound()
369 createSimpleIndex( false );
371 List<String> selectedRepos = new ArrayList<String>();
372 selectedRepos.add( TEST_REPO_1 );
374 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
376 archivaConfigControl.replay();
378 SearchResults results = search.search( "user", selectedRepos, "dfghdfkweriuasndsaie", null, null );
380 archivaConfigControl.verify();
382 assertNotNull( results );
383 assertEquals( 0, results.getTotalHits() );
387 public void testNoIndexFound()
390 List<String> selectedRepos = new ArrayList<String>();
391 selectedRepos.add( TEST_REPO_1 );
393 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
395 archivaConfigControl.replay();
397 SearchResults results = search.search( "user", selectedRepos, "org.apache.archiva", null, null );
398 assertNotNull( results );
399 assertEquals( 0, results.getTotalHits() );
401 archivaConfigControl.verify();
405 public void testRepositoryNotFound()
408 List<String> selectedRepos = new ArrayList<String>();
409 selectedRepos.add( "non-existing-repo" );
411 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
413 archivaConfigControl.replay();
415 SearchResults results = search.search( "user", selectedRepos, "org.apache.archiva", null, null );
416 assertNotNull( results );
417 assertEquals( 0, results.getTotalHits() );
419 archivaConfigControl.verify();
423 public void testSearchWithinSearchResults()
426 createSimpleIndex( true );
428 List<String> selectedRepos = new ArrayList<String>();
429 selectedRepos.add( TEST_REPO_1 );
431 List<String> previousSearchTerms = new ArrayList<String>();
432 previousSearchTerms.add( "archiva-test" );
434 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
436 archivaConfigControl.replay();
438 SearchResults results = search.search( "user", selectedRepos, "1.0", null, previousSearchTerms );
440 archivaConfigControl.verify();
442 assertNotNull( results );
443 assertEquals( "total hints not 1", 1, results.getTotalHits() );
445 SearchResultHit hit = results.getHits().get( 0 );
446 assertEquals( "org.apache.archiva", hit.getGroupId() );
447 assertEquals( "archiva-test", hit.getArtifactId() );
448 assertEquals( "versions not 1", 1, hit.getVersions().size() );
449 assertEquals( "1.0", hit.getVersions().get( 0 ) );
452 // tests for advanced search
454 public void testAdvancedSearch()
457 List<File> files = new ArrayList<File>();
458 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_2
459 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
460 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_2
461 + "/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar" ) );
462 createIndex( TEST_REPO_2, files, false );
464 List<String> selectedRepos = new ArrayList<String>();
465 selectedRepos.add( TEST_REPO_2 );
467 SearchFields searchFields = new SearchFields();
468 searchFields.setGroupId( "org.apache.archiva" );
469 searchFields.setVersion( "1.0" );
470 searchFields.setRepositories( selectedRepos );
472 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
474 archivaConfigControl.replay();
476 SearchResults results = search.search( "user", searchFields, null );
478 archivaConfigControl.verify();
480 assertNotNull( results );
481 assertEquals( 1, results.getTotalHits() );
483 SearchResultHit hit = results.getHits().get( 0 );
484 assertEquals( "org.apache.archiva", hit.getGroupId() );
485 assertEquals( "archiva-search", hit.getArtifactId() );
486 assertEquals( "1.0", hit.getVersions().get( 0 ) );
490 public void testAdvancedSearchWithPagination()
493 createIndexContainingMoreArtifacts( false );
495 List<String> selectedRepos = new ArrayList<String>();
496 selectedRepos.add( TEST_REPO_1 );
498 SearchFields searchFields = new SearchFields();
499 searchFields.setGroupId( "org.apache.archiva" );
500 searchFields.setRepositories( selectedRepos );
504 SearchResultLimits limits = new SearchResultLimits( 0 );
505 limits.setPageSize( 1 );
507 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
509 archivaConfigControl.replay();
511 SearchResults results = search.search( "user", searchFields, limits );
513 archivaConfigControl.verify();
515 assertNotNull( results );
516 assertEquals( 4, results.getTotalHits() );
517 assertEquals( 1, results.getHits().size() );
520 archivaConfigControl.reset();
522 limits = new SearchResultLimits( 1 );
523 limits.setPageSize( 1 );
525 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
527 archivaConfigControl.replay();
529 results = search.search( "user", searchFields, limits );
531 archivaConfigControl.verify();
533 assertNotNull( results );
534 assertEquals( 4, results.getTotalHits() );
535 assertEquals( 1, results.getHits().size() );
538 // MRM-981 - artifactIds with numeric characters aren't found in advanced search
540 public void testAdvancedSearchArtifactIdHasNumericChar()
543 List<File> files = new ArrayList<File>();
544 files.add( new File( FileUtil.getBasedir(),
545 "src/test/" + TEST_REPO_1 + "/com/artifactid-numeric/1.0/artifactid-numeric-1.0.jar" ) );
546 files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
547 + "/com/artifactid-numeric123/1.0/artifactid-numeric123-1.0.jar" ) );
548 createIndex( TEST_REPO_1, files, true );
550 List<String> selectedRepos = new ArrayList<String>();
551 selectedRepos.add( TEST_REPO_1 );
553 SearchFields searchFields = new SearchFields();
554 searchFields.setArtifactId( "artifactid-numeric" );
555 searchFields.setRepositories( selectedRepos );
557 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
559 archivaConfigControl.replay();
561 SearchResults results = search.search( "user", searchFields, null );
563 archivaConfigControl.verify();
565 assertNotNull( results );
566 assertEquals( 2, results.getTotalHits() );
570 public void testAdvancedSearchNoRepositoriesConfigured()
573 SearchFields searchFields = new SearchFields();
574 searchFields.setArtifactId( "archiva" );
575 searchFields.setRepositories( null );
579 search.search( "user", searchFields, null );
580 fail( "A RepositorySearchExcecption should have been thrown." );
582 catch ( RepositorySearchException e )
584 assertEquals( "Repositories cannot be null.", e.getMessage() );
589 public void testAdvancedSearchSearchFieldsAreNull()
592 List<String> selectedRepos = new ArrayList<String>();
593 selectedRepos.add( TEST_REPO_1 );
595 SearchFields searchFields = new SearchFields();
596 searchFields.setRepositories( selectedRepos );
600 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
602 archivaConfigControl.replay();
604 search.search( "user", searchFields, null );
606 archivaConfigControl.verify();
608 fail( "A RepositorySearchExcecption should have been thrown." );
610 catch ( RepositorySearchException e )
612 assertEquals( "No search fields set.", e.getMessage() );
617 public void testAdvancedSearchSearchFieldsAreBlank()
620 List<String> selectedRepos = new ArrayList<String>();
621 selectedRepos.add( TEST_REPO_1 );
623 SearchFields searchFields = new SearchFields();
624 searchFields.setGroupId( "" );
625 searchFields.setArtifactId( "" );
626 searchFields.setVersion( "" );
627 searchFields.setPackaging( "" );
628 searchFields.setClassName( "" );
630 searchFields.setRepositories( selectedRepos );
634 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
636 archivaConfigControl.replay();
638 search.search( "user", searchFields, null );
640 archivaConfigControl.verify();
642 fail( "A RepositorySearchExcecption should have been thrown." );
644 catch ( RepositorySearchException e )
646 assertEquals( "No search fields set.", e.getMessage() );
651 public void testAdvancedSearchAllSearchCriteriaSpecified()
654 createSimpleIndex( true );
656 List<String> selectedRepos = new ArrayList<String>();
657 selectedRepos.add( TEST_REPO_1 );
659 SearchFields searchFields = new SearchFields();
660 searchFields.setGroupId( "org.apache.archiva" );
661 searchFields.setArtifactId( "archiva-test" );
662 searchFields.setVersion( "2.0" );
663 searchFields.setPackaging( "jar" );
664 searchFields.setClassName( "org.apache.archiva.test.App" );
665 searchFields.setRepositories( selectedRepos );
667 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
669 archivaConfigControl.replay();
671 SearchResults results = search.search( "user", searchFields, null );
673 archivaConfigControl.verify();
675 assertNotNull( results );
677 assertEquals( "total hints not 1" + results, 1, results.getTotalHits() );
679 SearchResultHit hit = results.getHits().get( 0 );
680 assertEquals( "org.apache.archiva", hit.getGroupId() );
681 assertEquals( "archiva-test", hit.getArtifactId() );
682 assertEquals( "version not 2.0", "2.0", hit.getVersions().get( 0 ) );
686 public void testAdvancedSearchJarArtifacts()
689 createIndexContainingMoreArtifacts( true );
691 List<String> selectedRepos = new ArrayList<String>();
692 selectedRepos.add( TEST_REPO_1 );
694 SearchFields searchFields = new SearchFields();
695 searchFields.setPackaging( "jar" );
696 searchFields.setRepositories( selectedRepos );
698 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
700 archivaConfigControl.replay();
702 SearchResults results = search.search( "user", searchFields, null );
704 archivaConfigControl.verify();
706 assertNotNull( results );
707 assertEquals( "not 8 but " + results.getTotalHits() + ":" + niceDisplay( results ), 8, results.getTotalHits() );
711 public void testAdvancedSearchWithIncorrectPackaging()
714 createSimpleIndex( true );
716 List<String> selectedRepos = new ArrayList<String>();
717 selectedRepos.add( TEST_REPO_1 );
719 SearchFields searchFields = new SearchFields();
720 searchFields.setGroupId( "org.apache.archiva" );
721 searchFields.setArtifactId( "archiva-test" );
722 searchFields.setVersion( "2.0" );
723 searchFields.setPackaging( "war" );
724 searchFields.setRepositories( selectedRepos );
726 archivaConfigControl.expectAndDefaultReturn( archivaConfig.getConfiguration(), config );
727 archivaConfigControl.replay();
729 SearchResults results = search.search( "user", searchFields, null );
731 archivaConfigControl.verify();
733 assertNotNull( results );
734 assertEquals( 0, results.getTotalHits() );
738 public void testAdvancedSearchClassname()
741 createIndexContainingMoreArtifacts( true );
743 List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
745 SearchFields searchFields = new SearchFields();
746 searchFields.setClassName( "com.classname.search.App" );
747 searchFields.setRepositories( selectedRepos );
749 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
751 archivaConfigControl.replay();
753 SearchResults results = search.search( "user", searchFields, null );
755 archivaConfigControl.verify();
757 assertNotNull( results );
758 assertEquals( "totalHits not 1 results " + results, 1, results.getTotalHits() );
760 SearchResultHit hit = results.getHits().get( 0 );
761 assertEquals( "groupId not com", "com", hit.getGroupId() );
762 assertEquals( "arttifactId not classname-search", "classname-search", hit.getArtifactId() );
763 assertEquals( " hits.version(0) not 1.0", "1.0", hit.getVersions().get( 0 ) );
767 public void testAdvancedSearchNoIndexFound()
770 List<String> selectedRepos = new ArrayList<String>();
771 selectedRepos.add( TEST_REPO_1 );
773 SearchFields searchFields = new SearchFields();
774 searchFields.setGroupId( "org.apache.archiva" );
775 searchFields.setRepositories( selectedRepos );
777 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
779 archivaConfigControl.replay();
781 SearchResults results = search.search( "user", searchFields, null );
783 archivaConfigControl.verify();
785 assertNotNull( results );
786 assertEquals( 0, results.getTotalHits() );
790 public void testAdvancedSearchClassNameInWar()
793 createIndexContainingMoreArtifacts( true );
795 List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
797 SearchFields searchFields = new SearchFields();
798 searchFields.setClassName( "SomeClass" );
799 searchFields.setRepositories( selectedRepos );
801 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
803 archivaConfigControl.replay();
805 SearchResults results = search.search( "user", searchFields, null );
807 archivaConfigControl.verify();
809 assertNotNull( results );
810 assertEquals( 1, results.getHits().size() );
811 assertEquals( "test-webapp", results.getHits().get( 0 ).getArtifactId() );
815 public void getAllGroupIds()
818 createIndexContainingMoreArtifacts( true );
820 List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
822 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 0, 2 );
824 archivaConfigControl.replay();
826 Collection<String> groupIds = search.getAllGroupIds( "user", selectedRepos );
828 archivaConfigControl.verify();
830 log.info( "groupIds: " + groupIds );
832 assertEquals( 3, groupIds.size() );
833 assertTrue( groupIds.contains( "com" ) );
834 assertTrue( groupIds.contains( "org.apache.felix" ) );
835 assertTrue( groupIds.contains( "org.apache.archiva" ) );
839 public void testSearchWithUnknownRepo()
842 createIndexContainingMoreArtifacts( true );
844 List<String> selectedRepos = Arrays.asList( "foo" );
846 SearchFields searchFields = new SearchFields();
847 searchFields.setClassName( "SomeClass" );
848 searchFields.setRepositories( selectedRepos );
850 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 2 );
852 archivaConfigControl.replay();
854 SearchResults results = search.search( "user", searchFields, null );
856 archivaConfigControl.verify();
858 assertNotNull( results );
859 assertEquals( 0, results.getHits().size() );
863 public void nolimitedResult()
867 File repo = new File( "target/repo-release" );
868 File indexDirectory = new File( repo, ".index" );
869 FileUtils.copyDirectoryStructure( new File( "src/test/repo-release" ), repo );
871 createIndex( "repo-release", Collections.<File>emptyList(), false );
873 nexusIndexer.addIndexingContext( REPO_RELEASE, REPO_RELEASE, repo, indexDirectory,
874 repo.toURI().toURL().toExternalForm(),
875 indexDirectory.toURI().toURL().toString(), search.getAllIndexCreators() );
877 SearchResultLimits limits = new SearchResultLimits( 0 );
878 limits.setPageSize( 300 );
880 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1, 5 );
882 archivaConfigControl.replay();
884 SearchResults searchResults = search.search( null, Arrays.asList( REPO_RELEASE ), "org.example", limits,
885 Collections.<String>emptyList() );
887 log.info( "results: {}", searchResults.getHits().size() );
889 assertEquals( 255, searchResults.getHits().size() );
891 SearchFields searchFields = new SearchFields();
892 searchFields.setGroupId( "org.example" );
893 searchFields.setRepositories( Arrays.asList( REPO_RELEASE ) );
895 searchResults = search.search( null, searchFields, limits );
897 log.info( "results: {}", searchResults.getHits().size() );
899 assertEquals( 255, searchResults.getHits().size() );
901 archivaConfigControl.verify();