1 package org.apache.archiva.indexer.maven.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.FileUtils;
23 import org.apache.archiva.indexer.search.RepositorySearchException;
24 import org.apache.archiva.indexer.search.SearchFields;
25 import org.apache.archiva.indexer.search.SearchResultHit;
26 import org.apache.archiva.indexer.search.SearchResultLimits;
27 import org.apache.archiva.indexer.search.SearchResults;
28 import org.apache.archiva.indexer.util.SearchUtil;
29 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
30 import org.apache.maven.index_shaded.lucene.index.IndexUpgrader;
31 import org.easymock.EasyMock;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.springframework.test.context.ContextConfiguration;
36 import java.nio.file.Path;
37 import java.nio.file.Paths;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.Collection;
41 import java.util.Collections;
42 import java.util.List;
43 import java.util.Locale;
46 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
47 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
48 public class MavenRepositorySearchTest
49 extends AbstractMavenRepositorySearch
53 private void createSimpleIndex( boolean scan )
56 List<Path> files = new ArrayList<>();
57 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos", TEST_REPO_1,
58 "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
59 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos", TEST_REPO_1,
60 "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" ));
61 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos", TEST_REPO_1,
62 "org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ));
64 createIndex( TEST_REPO_1, files, scan );
67 private void createIndexContainingMoreArtifacts( boolean scan )
70 List<Path> files = new ArrayList<>();
71 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_1
72 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
73 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_1
74 + "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" ) );
75 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_1
76 + "/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ) );
77 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_1
78 + "/org/apache/archiva/archiva-webapp/1.0/archiva-webapp-1.0.war" ) );
79 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(),
80 "target/repos/" + TEST_REPO_1 + "/com/artifactid-numeric/1.0/artifactid-numeric-1.0.jar" ) );
81 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_1
82 + "/com/artifactid-numeric123/1.0/artifactid-numeric123-1.0.jar" ) );
83 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(),
84 "target/repos/" + TEST_REPO_1 + "/com/classname-search/1.0/classname-search-1.0.jar" ) );
86 createIndex( TEST_REPO_1, files, scan );
89 private void createIndexContainingMultipleArtifactsSameVersion( boolean scan )
92 List<Path> files = new ArrayList<>();
94 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_1
95 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
97 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_1
98 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.pom" ) );
100 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_1
101 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0-sources.jar" ) );
103 createIndex( TEST_REPO_1, files, scan );
107 public void testQuickSearch()
110 createSimpleIndex( false );
112 List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
115 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
116 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
118 archivaConfigControl.replay();
120 SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
122 archivaConfigControl.verify();
124 assertNotNull( results );
126 SearchResultHit hit =
127 results.getSearchResultHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-search", null, "jar" ) );
128 assertNotNull( "hit null in result " + results.getHits(), hit );
129 assertEquals( "org.apache.archiva", hit.getGroupId() );
130 assertEquals( "archiva-search", hit.getArtifactId() );
131 assertEquals( "1.0", hit.getVersions().get( 0 ) );
133 archivaConfigControl.reset();
135 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
136 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
139 archivaConfigControl.replay();
141 results = search.search( "user", selectedRepos, "org.apache.archiva", null, null );
143 archivaConfigControl.verify();
145 assertNotNull( results );
146 assertEquals( "total hints not 3", 3, results.getTotalHits() );
148 //TODO: search for class & package names
152 public void testQuickSearchNotWithClassifier()
155 createSimpleIndex( true );
157 List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
160 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
161 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
163 archivaConfigControl.replay();
165 SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
167 archivaConfigControl.verify();
169 assertNotNull( results );
171 SearchResultHit hit =
172 results.getSearchResultHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-search", null, "jar" ) );
173 assertNotNull( "hit null in result " + results.getHits(), hit );
174 assertEquals( "org.apache.archiva", hit.getGroupId() );
175 assertEquals( "archiva-search", hit.getArtifactId() );
176 assertEquals( "1.0", hit.getVersions().get( 0 ) );
178 archivaConfigControl.reset();
181 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
182 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
184 archivaConfigControl.replay();
186 results = search.search( "user", selectedRepos, "archiva-search", null, null );
188 archivaConfigControl.verify();
190 assertNotNull( results );
191 assertEquals( "total hints not 3 hits " + results.getHits(), 3, results.getTotalHits() );
193 //TODO: search for class & package names
197 public void testQuickSearchMultipleArtifactsSameVersion()
200 createIndexContainingMultipleArtifactsSameVersion( false );
202 List<String> selectedRepos = new ArrayList<>();
203 selectedRepos.add( TEST_REPO_1 );
206 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
207 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
209 archivaConfigControl.replay();
211 SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
213 archivaConfigControl.verify();
215 assertNotNull( results );
216 assertEquals( 3, results.getTotalHits() );
218 SearchResultHit hit = results.getHits().get( 0 );
219 assertEquals( "org.apache.archiva", hit.getGroupId() );
220 assertEquals( "archiva-search", hit.getArtifactId() );
221 assertEquals( "1.0", hit.getVersions().get( 0 ) );
223 //only 1 version of 1.0 is retrieved
224 assertEquals( 1, hit.getVersions().size() );
228 public void testMultipleArtifactsSameVersionWithClassifier()
231 createIndexContainingMultipleArtifactsSameVersion( true );
233 List<String> selectedRepos = new ArrayList<>();
234 selectedRepos.add( TEST_REPO_1 );
237 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
238 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
240 archivaConfigControl.replay();
242 SearchFields searchFields = new SearchFields();
243 searchFields.setGroupId( "org.apache.archiva" );
244 searchFields.setArtifactId( "archiva-search" );
245 searchFields.setClassifier( "sources" );
246 searchFields.setRepositories( selectedRepos );
248 SearchResults results = search.search( "user", searchFields, null );
250 archivaConfigControl.verify();
252 assertNotNull( results );
253 assertEquals( 1, results.getTotalHits() );
255 SearchResultHit hit = results.getHits().get( 0 );
256 assertEquals( "org.apache.archiva", hit.getGroupId() );
257 assertEquals( "archiva-search", hit.getArtifactId() );
258 assertEquals( "1.0", hit.getVersions().get( 0 ) );
260 //only 1 version of 1.0 is retrieved
261 assertEquals( 1, hit.getVersions().size() );
264 // search for existing artifact using multiple keywords
266 public void testQuickSearchWithMultipleKeywords()
269 createSimpleIndex( false );
271 List<String> selectedRepos = new ArrayList<>();
272 selectedRepos.add( TEST_REPO_1 );
274 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
275 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
276 archivaConfigControl.replay();
278 SearchResults results = search.search( "user", selectedRepos, "archiva search", null, null );
280 archivaConfigControl.verify();
282 assertNotNull( results );
283 assertEquals( 1, results.getTotalHits() );
287 public void testQuickSearchWithPagination()
290 createSimpleIndex( true );
292 List<String> selectedRepos = new ArrayList<>();
293 selectedRepos.add( TEST_REPO_1 );
296 SearchResultLimits limits = new SearchResultLimits( 0 );
297 limits.setPageSize( 1 );
299 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
300 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
302 archivaConfigControl.replay();
304 SearchResults results = search.search( "user", selectedRepos, "org", limits, Collections.emptyList() );
306 archivaConfigControl.verify();
308 assertNotNull( results );
309 assertEquals( 1, results.getHits().size() );
310 assertEquals( "total hits not 9 for page1 " + results, 9, results.getTotalHits() );
311 assertEquals( "returned hits not 1 for page1 " + results, 1, results.getReturnedHitsCount() );
312 assertEquals( limits, results.getLimits() );
314 archivaConfigControl.reset();
317 limits = new SearchResultLimits( 1 );
318 limits.setPageSize( 1 );
320 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
321 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
323 archivaConfigControl.replay();
325 results = search.search( "user", selectedRepos, "org", limits, null );
327 archivaConfigControl.verify();
329 assertNotNull( results );
331 assertEquals( "hits not 1", 1, results.getHits().size() );
332 assertEquals( "total hits not 9 for page 2 " + results, 9, results.getTotalHits() );
333 assertEquals( "returned hits not 1 for page2 " + results, 1, results.getReturnedHitsCount() );
334 assertEquals( limits, results.getLimits() );
338 public void testArtifactFoundInMultipleRepositories()
341 createSimpleIndex( true );
343 List<Path> files = new ArrayList<>();
344 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_2
345 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
346 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_2
347 + "/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar" ) );
348 createIndex( TEST_REPO_2, files, false );
350 List<String> selectedRepos = new ArrayList<>();
351 selectedRepos.add( TEST_REPO_1 );
352 selectedRepos.add( TEST_REPO_2 );
354 config.addManagedRepository( createRepositoryConfig( TEST_REPO_2 ) );
356 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
357 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
359 archivaConfigControl.replay();
361 // wait lucene flush.....
362 Thread.sleep( 2000 );
364 SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
366 archivaConfigControl.verify();
368 assertNotNull( results );
370 SearchResultHit hit =
371 results.getSearchResultHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-search", null, "jar" ) );
373 assertEquals( "org.apache.archiva", hit.getGroupId() );
374 assertEquals( "archiva-search", hit.getArtifactId() );
375 assertEquals( "not 2 version for hit " + hit + "::" + niceDisplay( results ), 2, hit.getVersions().size() );
376 assertTrue( hit.getVersions().contains( "1.0" ) );
377 assertTrue( hit.getVersions().contains( "1.1" ) );
379 archivaConfigControl.reset();
381 // TODO: [BROWSE] in artifact info from browse, display all the repositories where the artifact is found
385 public void testNoMatchFound()
388 createSimpleIndex( false );
390 List<String> selectedRepos = new ArrayList<>();
391 selectedRepos.add( TEST_REPO_1 );
393 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
394 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
396 archivaConfigControl.replay();
398 SearchResults results = search.search( "user", selectedRepos, "dfghdfkweriuasndsaie", null, null );
400 archivaConfigControl.verify();
402 assertNotNull( results );
403 assertEquals( 0, results.getTotalHits() );
407 public void testNoIndexFound()
410 List<String> selectedRepos = new ArrayList<>();
411 selectedRepos.add( TEST_REPO_1 );
413 // EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
414 // EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
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 testRepositoryNotFound()
429 List<String> selectedRepos = new ArrayList<>();
430 selectedRepos.add( "non-existing-repo" );
432 // archivaConfigControl.replay();
434 SearchResults results = search.search( "user", selectedRepos, "org.apache.archiva", null, null );
435 assertNotNull( results );
436 assertEquals( 0, results.getTotalHits() );
438 archivaConfigControl.verify();
442 public void testSearchWithinSearchResults()
445 createSimpleIndex( true );
447 List<String> selectedRepos = new ArrayList<>();
448 selectedRepos.add( TEST_REPO_1 );
450 List<String> previousSearchTerms = new ArrayList<>();
451 previousSearchTerms.add( "archiva-test" );
453 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
454 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
456 archivaConfigControl.replay();
458 SearchResults results = search.search( "user", selectedRepos, "1.0", null, previousSearchTerms );
460 archivaConfigControl.verify();
462 assertNotNull( results );
463 assertEquals( "total hints not 1", 1, results.getTotalHits() );
465 SearchResultHit hit = results.getHits().get( 0 );
466 assertEquals( "org.apache.archiva", hit.getGroupId() );
467 assertEquals( "archiva-test", hit.getArtifactId() );
468 assertEquals( "versions not 1", 1, hit.getVersions().size() );
469 assertEquals( "1.0", hit.getVersions().get( 0 ) );
472 // tests for advanced search
474 public void testAdvancedSearch()
477 List<Path> files = new ArrayList<>();
478 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_2
479 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
480 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_2
481 + "/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar" ) );
482 createIndex( TEST_REPO_2, files, false );
484 List<String> selectedRepos = new ArrayList<>();
485 selectedRepos.add( TEST_REPO_2 );
487 SearchFields searchFields = new SearchFields();
488 searchFields.setGroupId( "org.apache.archiva" );
489 searchFields.setVersion( "1.0" );
490 searchFields.setRepositories( selectedRepos );
492 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
493 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
495 archivaConfigControl.replay();
497 SearchResults results = search.search( "user", searchFields, null );
499 archivaConfigControl.verify();
501 assertNotNull( results );
502 assertEquals( 1, results.getTotalHits() );
504 SearchResultHit hit = results.getHits().get( 0 );
505 assertEquals( "org.apache.archiva", hit.getGroupId() );
506 assertEquals( "archiva-search", hit.getArtifactId() );
507 assertEquals( "1.0", hit.getVersions().get( 0 ) );
511 public void testAdvancedSearchWithPagination()
514 createIndexContainingMoreArtifacts( false );
516 List<String> selectedRepos = new ArrayList<>();
517 selectedRepos.add( TEST_REPO_1 );
519 SearchFields searchFields = new SearchFields();
520 searchFields.setGroupId( "org.apache.archiva" );
521 searchFields.setRepositories( selectedRepos );
525 SearchResultLimits limits = new SearchResultLimits( 0 );
526 limits.setPageSize( 1 );
528 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
529 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
531 archivaConfigControl.replay();
533 SearchResults results = search.search( "user", searchFields, limits );
535 archivaConfigControl.verify();
537 assertNotNull( results );
538 assertEquals( 4, results.getTotalHits() );
539 assertEquals( 1, results.getHits().size() );
542 archivaConfigControl.reset();
544 limits = new SearchResultLimits( 1 );
545 limits.setPageSize( 1 );
547 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
548 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
550 archivaConfigControl.replay();
552 results = search.search( "user", searchFields, limits );
554 archivaConfigControl.verify();
556 assertNotNull( results );
557 assertEquals( 4, results.getTotalHits() );
558 assertEquals( 1, results.getHits().size() );
561 // MRM-981 - artifactIds with numeric characters aren't found in advanced search
563 public void testAdvancedSearchArtifactIdHasNumericChar()
566 List<Path> files = new ArrayList<>();
567 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(),
568 "target/repos/" + TEST_REPO_1 + "/com/artifactid-numeric/1.0/artifactid-numeric-1.0.jar" ) );
569 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_1
570 + "/com/artifactid-numeric123/1.0/artifactid-numeric123-1.0.jar" ) );
571 createIndex( TEST_REPO_1, files, true );
573 List<String> selectedRepos = new ArrayList<>();
574 selectedRepos.add( TEST_REPO_1 );
576 SearchFields searchFields = new SearchFields();
577 searchFields.setArtifactId( "artifactid-numeric" );
578 searchFields.setRepositories( selectedRepos );
580 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
581 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
583 archivaConfigControl.replay();
585 SearchResults results = search.search( "user", searchFields, null );
587 archivaConfigControl.verify();
589 assertNotNull( results );
590 assertEquals( 2, results.getTotalHits() );
594 public void testAdvancedSearchNoRepositoriesConfigured()
597 SearchFields searchFields = new SearchFields();
598 searchFields.setArtifactId( "archiva" );
599 searchFields.setRepositories( null );
603 search.search( "user", searchFields, null );
604 fail( "A RepositorySearchExcecption should have been thrown." );
606 catch ( RepositorySearchException e )
608 assertEquals( "Repositories cannot be null.", e.getMessage() );
613 public void testAdvancedSearchSearchFieldsAreNull()
616 List<String> selectedRepos = new ArrayList<>();
617 selectedRepos.add( TEST_REPO_1 );
619 SearchFields searchFields = new SearchFields();
620 searchFields.setRepositories( selectedRepos );
624 // EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
625 // EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
627 // archivaConfigControl.replay();
629 search.search( "user", searchFields, null );
631 archivaConfigControl.verify();
633 fail( "A RepositorySearchExcecption should have been thrown." );
635 catch ( RepositorySearchException e )
637 assertEquals( "No search fields set.", e.getMessage() );
642 public void testAdvancedSearchSearchFieldsAreBlank()
645 List<String> selectedRepos = new ArrayList<>();
646 selectedRepos.add( TEST_REPO_1 );
648 SearchFields searchFields = new SearchFields();
649 searchFields.setGroupId( "" );
650 searchFields.setArtifactId( "" );
651 searchFields.setVersion( "" );
652 searchFields.setPackaging( "" );
653 searchFields.setClassName( "" );
655 searchFields.setRepositories( selectedRepos );
660 // EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
661 // EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
662 // archivaConfigControl.replay();
664 search.search( "user", searchFields, null );
666 archivaConfigControl.verify();
668 fail( "A RepositorySearchException should have been thrown." );
670 catch ( RepositorySearchException e )
672 assertEquals( "No search fields set.", e.getMessage() );
677 public void testAdvancedSearchAllSearchCriteriaSpecified()
680 createSimpleIndex( true );
682 List<String> selectedRepos = new ArrayList<>();
683 selectedRepos.add( TEST_REPO_1 );
685 SearchFields searchFields = new SearchFields();
686 searchFields.setGroupId( "org.apache.archiva" );
687 searchFields.setArtifactId( "archiva-test" );
688 searchFields.setVersion( "2.0" );
689 searchFields.setPackaging( "jar" );
690 searchFields.setClassName( "org.apache.archiva.test.App" );
691 searchFields.setRepositories( selectedRepos );
693 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
694 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
696 archivaConfigControl.replay();
698 SearchResults results = search.search( "user", searchFields, null );
700 archivaConfigControl.verify();
702 assertNotNull( results );
704 assertEquals( "total hints not 1" + results, 1, results.getTotalHits() );
706 SearchResultHit hit = results.getHits().get( 0 );
707 assertEquals( "org.apache.archiva", hit.getGroupId() );
708 assertEquals( "archiva-test", hit.getArtifactId() );
709 assertEquals( "version not 2.0", "2.0", hit.getVersions().get( 0 ) );
713 public void testAdvancedSearchJarArtifacts()
716 createIndexContainingMoreArtifacts( true );
718 List<String> selectedRepos = new ArrayList<>();
719 selectedRepos.add( TEST_REPO_1 );
721 SearchFields searchFields = new SearchFields();
722 searchFields.setPackaging( "jar" );
723 searchFields.setRepositories( selectedRepos );
725 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
726 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
728 archivaConfigControl.replay();
730 SearchResults results = search.search( "user", searchFields, null );
732 archivaConfigControl.verify();
734 assertNotNull( results );
735 assertEquals( "not 8 but " + results.getTotalHits() + ":" + niceDisplay( results ), 8, results.getTotalHits() );
739 public void testAdvancedSearchWithIncorrectPackaging()
742 createSimpleIndex( true );
744 List<String> selectedRepos = new ArrayList<>();
745 selectedRepos.add( TEST_REPO_1 );
747 SearchFields searchFields = new SearchFields();
748 searchFields.setGroupId( "org.apache.archiva" );
749 searchFields.setArtifactId( "archiva-test" );
750 searchFields.setVersion( "2.0" );
751 searchFields.setPackaging( "war" );
752 searchFields.setRepositories( selectedRepos );
754 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
755 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
757 archivaConfigControl.replay();
759 SearchResults results = search.search( "user", searchFields, null );
761 archivaConfigControl.verify();
763 assertNotNull( results );
764 assertEquals( 0, results.getTotalHits() );
768 public void testAdvancedSearchClassname()
771 createIndexContainingMoreArtifacts( true );
773 List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
775 SearchFields searchFields = new SearchFields();
776 searchFields.setClassName( "com.classname.search.App" );
777 searchFields.setRepositories( selectedRepos );
779 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
780 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
782 archivaConfigControl.replay();
784 SearchResults results = search.search( "user", searchFields, null );
786 archivaConfigControl.verify();
788 assertNotNull( results );
789 assertEquals( "totalHits not 1 results " + results, 1, results.getTotalHits() );
791 SearchResultHit hit = results.getHits().get( 0 );
792 assertEquals( "groupId not com", "com", hit.getGroupId() );
793 assertEquals( "arttifactId not classname-search", "classname-search", hit.getArtifactId() );
794 assertEquals( " hits.version(0) not 1.0", "1.0", hit.getVersions().get( 0 ) );
798 public void testAdvancedSearchNoIndexFound()
801 List<String> selectedRepos = new ArrayList<>();
802 selectedRepos.add( TEST_REPO_1 );
804 SearchFields searchFields = new SearchFields();
805 searchFields.setGroupId( "org.apache.archiva" );
806 searchFields.setRepositories( selectedRepos );
808 // EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
809 // EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
811 // archivaConfigControl.replay();
813 SearchResults results = search.search( "user", searchFields, null );
815 archivaConfigControl.verify();
817 assertNotNull( results );
818 assertEquals( 0, results.getTotalHits() );
822 public void testAdvancedSearchClassNameInWar()
825 createIndexContainingMoreArtifacts( true );
827 List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
829 SearchFields searchFields = new SearchFields();
830 searchFields.setClassName( "SomeClass" );
831 searchFields.setRepositories( selectedRepos );
833 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
834 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
836 archivaConfigControl.replay();
838 SearchResults results = search.search( "user", searchFields, null );
840 archivaConfigControl.verify();
842 assertNotNull( results );
843 assertEquals( 1, results.getHits().size() );
844 assertEquals( "test-webapp", results.getHits().get( 0 ).getArtifactId() );
848 public void getAllGroupIds()
851 createIndexContainingMoreArtifacts( true );
853 List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
855 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 0, 2 );
857 archivaConfigControl.replay();
859 Collection<String> groupIds = search.getAllGroupIds( "user", selectedRepos );
861 archivaConfigControl.verify();
863 log.info( "groupIds: {}", groupIds );
865 assertEquals( 3, groupIds.size() );
866 assertTrue( groupIds.contains( "com" ) );
867 assertTrue( groupIds.contains( "org.apache.felix" ) );
868 assertTrue( groupIds.contains( "org.apache.archiva" ) );
872 public void testSearchWithUnknownRepo()
875 createIndexContainingMoreArtifacts( true );
877 List<String> selectedRepos = Arrays.asList( "foo" );
879 SearchFields searchFields = new SearchFields();
880 searchFields.setClassName( "SomeClass" );
881 searchFields.setRepositories( selectedRepos );
883 archivaConfigControl.replay();
885 SearchResults results = search.search( "user", searchFields, null );
887 archivaConfigControl.verify();
889 assertNotNull( results );
890 assertEquals( 0, results.getHits().size() );
894 public void nolimitedResult()
898 Path repo = Paths.get("target/repo-release");
900 Path indexDirectory = repo.resolve(".indexer");
901 Path zipFile = Paths.get(Thread.currentThread().getContextClassLoader().getResource("repo-release.zip").toURI());
902 FileUtils.unzip(zipFile, repo.getParent());
903 IndexUpgrader.main(new String[]{indexDirectory.toAbsolutePath().toString()});
904 createIndex(REPO_RELEASE, Collections.emptyList(), false, indexDirectory, false);
906 // indexer.addIndexingContext( REPO_RELEASE, REPO_RELEASE, repo.toFile(), indexDirectory.toFile(),
907 // repo.toUri().toURL().toExternalForm(),
908 // indexDirectory.toUri().toURL().toString(), indexCreators );
911 SearchResultLimits limits = new SearchResultLimits(SearchResultLimits.ALL_PAGES);
912 limits.setPageSize(300);
914 // EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
915 EasyMock.expect(archivaConfig.getConfiguration()).andReturn(config).anyTimes();
917 archivaConfigControl.replay();
919 SearchResults searchResults = search.search(null, Arrays.asList(REPO_RELEASE), //
920 "org.example", limits, //
921 Collections.emptyList());
923 log.info("results: {}", searchResults.getHits().size());
925 assertEquals(255, searchResults.getHits().size());
927 SearchFields searchFields = new SearchFields();
928 searchFields.setGroupId("org.example");
929 searchFields.setRepositories(Arrays.asList(REPO_RELEASE));
931 searchResults = search.search(null, searchFields, limits);
933 log.info("results: {}", searchResults.getHits().size());
935 assertEquals(255, searchResults.getHits().size());
937 archivaConfigControl.verify();
939 FileUtils.deleteQuietly(repo);