1 package org.apache.archiva.maven.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
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import org.apache.archiva.common.utils.FileUtils;
22 import org.apache.archiva.indexer.search.RepositorySearchException;
23 import org.apache.archiva.indexer.search.SearchFields;
24 import org.apache.archiva.indexer.search.SearchResultHit;
25 import org.apache.archiva.indexer.search.SearchResultLimits;
26 import org.apache.archiva.indexer.search.SearchResults;
27 import org.apache.archiva.maven.indexer.util.SearchUtil;
28 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
29 import org.easymock.EasyMock;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.springframework.test.context.ContextConfiguration;
34 import java.nio.file.Path;
35 import java.nio.file.Paths;
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;
41 import java.util.Locale;
44 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
45 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
46 public class MavenRepositorySearchTest
47 extends AbstractMavenRepositorySearch
51 private void createSimpleIndex( boolean scan )
54 List<Path> files = new ArrayList<>();
55 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos", TEST_REPO_1,
56 "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
57 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos", TEST_REPO_1,
58 "/org/apache/archiva/archiva-test/1.0/archiva-test-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/2.0/archiva-test-2.0.jar" ));
62 createIndex( TEST_REPO_1, files, scan );
65 private void createIndexContainingMoreArtifacts( boolean scan )
68 List<Path> files = new ArrayList<>();
69 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_1
70 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
71 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_1
72 + "/org/apache/archiva/archiva-test/1.0/archiva-test-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/2.0/archiva-test-2.0.jar" ) );
75 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_1
76 + "/org/apache/archiva/archiva-webapp/1.0/archiva-webapp-1.0.war" ) );
77 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(),
78 "target/repos/" + TEST_REPO_1 + "/com/artifactid-numeric/1.0/artifactid-numeric-1.0.jar" ) );
79 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_1
80 + "/com/artifactid-numeric123/1.0/artifactid-numeric123-1.0.jar" ) );
81 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(),
82 "target/repos/" + TEST_REPO_1 + "/com/classname-search/1.0/classname-search-1.0.jar" ) );
84 createIndex( TEST_REPO_1, files, scan );
87 private void createIndexContainingMultipleArtifactsSameVersion( boolean scan )
90 List<Path> files = new ArrayList<>();
92 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_1
93 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
95 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_1
96 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.pom" ) );
98 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_1
99 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0-sources.jar" ) );
101 createIndex( TEST_REPO_1, files, scan );
105 public void testQuickSearch()
108 createSimpleIndex( false );
110 List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
113 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
114 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
116 archivaConfigControl.replay();
118 SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
120 archivaConfigControl.verify();
122 assertNotNull( results );
124 SearchResultHit hit =
125 results.getSearchResultHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-search", null, "jar" ) );
126 assertNotNull( "hit null in result " + results.getHits(), hit );
127 assertEquals( "org.apache.archiva", hit.getGroupId() );
128 assertEquals( "archiva-search", hit.getArtifactId() );
129 assertEquals( "1.0", hit.getVersions().get( 0 ) );
131 archivaConfigControl.reset();
133 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
134 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
137 archivaConfigControl.replay();
139 results = search.search( "user", selectedRepos, "org.apache.archiva", null, null );
141 archivaConfigControl.verify();
143 assertNotNull( results );
144 assertEquals( "total hints not 3", 3, results.getTotalHits() );
146 //TODO: search for class & package names
150 public void testQuickSearchNotWithClassifier()
153 createSimpleIndex( true );
155 List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
158 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
159 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
161 archivaConfigControl.replay();
163 SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
165 archivaConfigControl.verify();
167 assertNotNull( results );
169 SearchResultHit hit =
170 results.getSearchResultHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-search", null, "jar" ) );
171 assertNotNull( "hit null in result " + results.getHits(), hit );
172 assertEquals( "org.apache.archiva", hit.getGroupId() );
173 assertEquals( "archiva-search", hit.getArtifactId() );
174 assertEquals( "1.0", hit.getVersions().get( 0 ) );
176 archivaConfigControl.reset();
179 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
180 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
182 archivaConfigControl.replay();
184 results = search.search( "user", selectedRepos, "archiva-search", null, null );
186 archivaConfigControl.verify();
188 assertNotNull( results );
189 assertEquals( "total hints not 3 hits " + results.getHits(), 3, results.getTotalHits() );
191 //TODO: search for class & package names
195 public void testQuickSearchMultipleArtifactsSameVersion()
198 createIndexContainingMultipleArtifactsSameVersion( false );
200 List<String> selectedRepos = new ArrayList<>();
201 selectedRepos.add( TEST_REPO_1 );
204 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
205 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
207 archivaConfigControl.replay();
209 SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
211 archivaConfigControl.verify();
213 assertNotNull( results );
214 assertEquals( 3, results.getTotalHits() );
216 SearchResultHit hit = results.getHits().get( 0 );
217 assertEquals( "org.apache.archiva", hit.getGroupId() );
218 assertEquals( "archiva-search", hit.getArtifactId() );
219 assertEquals( "1.0", hit.getVersions().get( 0 ) );
221 //only 1 version of 1.0 is retrieved
222 assertEquals( 1, hit.getVersions().size() );
226 public void testMultipleArtifactsSameVersionWithClassifier()
229 createIndexContainingMultipleArtifactsSameVersion( true );
231 List<String> selectedRepos = new ArrayList<>();
232 selectedRepos.add( TEST_REPO_1 );
235 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
236 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
238 archivaConfigControl.replay();
240 SearchFields searchFields = new SearchFields();
241 searchFields.setGroupId( "org.apache.archiva" );
242 searchFields.setArtifactId( "archiva-search" );
243 searchFields.setClassifier( "sources" );
244 searchFields.setRepositories( selectedRepos );
246 SearchResults results = search.search( "user", searchFields, null );
248 archivaConfigControl.verify();
250 assertNotNull( results );
251 assertEquals( 1, results.getTotalHits() );
253 SearchResultHit hit = results.getHits().get( 0 );
254 assertEquals( "org.apache.archiva", hit.getGroupId() );
255 assertEquals( "archiva-search", hit.getArtifactId() );
256 assertEquals( "1.0", hit.getVersions().get( 0 ) );
258 //only 1 version of 1.0 is retrieved
259 assertEquals( 1, hit.getVersions().size() );
262 // search for existing artifact using multiple keywords
264 public void testQuickSearchWithMultipleKeywords()
267 createSimpleIndex( false );
269 List<String> selectedRepos = new ArrayList<>();
270 selectedRepos.add( TEST_REPO_1 );
272 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
273 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
274 archivaConfigControl.replay();
276 SearchResults results = search.search( "user", selectedRepos, "archiva search", null, null );
278 archivaConfigControl.verify();
280 assertNotNull( results );
281 assertEquals( 1, results.getTotalHits() );
285 public void testQuickSearchWithPagination()
288 createSimpleIndex( true );
290 List<String> selectedRepos = new ArrayList<>();
291 selectedRepos.add( TEST_REPO_1 );
294 SearchResultLimits limits = new SearchResultLimits( 0 );
295 limits.setPageSize( 1 );
297 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
298 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
300 archivaConfigControl.replay();
302 SearchResults results = search.search( "user", selectedRepos, "org", limits, Collections.emptyList() );
304 archivaConfigControl.verify();
306 assertNotNull( results );
307 assertEquals( 1, results.getHits().size() );
308 assertEquals( "total hits not 9 for page1 " + results, 9, results.getTotalHits() );
309 assertEquals( "returned hits not 1 for page1 " + results, 1, results.getReturnedHitsCount() );
310 assertEquals( limits, results.getLimits() );
312 archivaConfigControl.reset();
315 limits = new SearchResultLimits( 1 );
316 limits.setPageSize( 1 );
318 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
319 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
321 archivaConfigControl.replay();
323 results = search.search( "user", selectedRepos, "org", limits, null );
325 archivaConfigControl.verify();
327 assertNotNull( results );
329 assertEquals( "hits not 1", 1, results.getHits().size() );
330 assertEquals( "total hits not 9 for page 2 " + results, 9, results.getTotalHits() );
331 assertEquals( "returned hits not 1 for page2 " + results, 1, results.getReturnedHitsCount() );
332 assertEquals( limits, results.getLimits() );
336 public void testArtifactFoundInMultipleRepositories()
339 createSimpleIndex( true );
341 List<Path> files = new ArrayList<>();
342 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_2
343 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
344 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_2
345 + "/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar" ) );
346 createIndex( TEST_REPO_2, files, false );
348 List<String> selectedRepos = new ArrayList<>();
349 selectedRepos.add( TEST_REPO_1 );
350 selectedRepos.add( TEST_REPO_2 );
352 config.addManagedRepository( createRepositoryConfig( TEST_REPO_2 ) );
354 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
355 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
357 archivaConfigControl.replay();
359 // wait lucene flush.....
360 Thread.sleep( 2000 );
362 SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
364 archivaConfigControl.verify();
366 assertNotNull( results );
368 SearchResultHit hit =
369 results.getSearchResultHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-search", null, "jar" ) );
371 assertEquals( "org.apache.archiva", hit.getGroupId() );
372 assertEquals( "archiva-search", hit.getArtifactId() );
373 assertEquals( "not 2 version for hit " + hit + "::" + niceDisplay( results ), 2, hit.getVersions().size() );
374 assertTrue( hit.getVersions().contains( "1.0" ) );
375 assertTrue( hit.getVersions().contains( "1.1" ) );
377 archivaConfigControl.reset();
379 // TODO: [BROWSE] in artifact info from browse, display all the repositories where the artifact is found
383 public void testNoMatchFound()
386 createSimpleIndex( false );
388 List<String> selectedRepos = new ArrayList<>();
389 selectedRepos.add( TEST_REPO_1 );
391 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
392 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
394 archivaConfigControl.replay();
396 SearchResults results = search.search( "user", selectedRepos, "dfghdfkweriuasndsaie", null, null );
398 archivaConfigControl.verify();
400 assertNotNull( results );
401 assertEquals( 0, results.getTotalHits() );
405 public void testNoIndexFound()
408 List<String> selectedRepos = new ArrayList<>();
409 selectedRepos.add( TEST_REPO_1 );
411 // EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
412 // EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
414 // archivaConfigControl.replay();
416 SearchResults results = search.search( "user", selectedRepos, "org.apache.archiva", null, null );
417 assertNotNull( results );
418 assertEquals( 0, results.getTotalHits() );
420 archivaConfigControl.verify();
424 public void testRepositoryNotFound()
427 List<String> selectedRepos = new ArrayList<>();
428 selectedRepos.add( "non-existing-repo" );
430 // archivaConfigControl.replay();
432 SearchResults results = search.search( "user", selectedRepos, "org.apache.archiva", null, null );
433 assertNotNull( results );
434 assertEquals( 0, results.getTotalHits() );
436 archivaConfigControl.verify();
440 public void testSearchWithinSearchResults()
443 createSimpleIndex( true );
445 List<String> selectedRepos = new ArrayList<>();
446 selectedRepos.add( TEST_REPO_1 );
448 List<String> previousSearchTerms = new ArrayList<>();
449 previousSearchTerms.add( "archiva-test" );
451 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
452 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
454 archivaConfigControl.replay();
456 SearchResults results = search.search( "user", selectedRepos, "1.0", null, previousSearchTerms );
458 archivaConfigControl.verify();
460 assertNotNull( results );
461 assertEquals( "total hints not 1", 1, results.getTotalHits() );
463 SearchResultHit hit = results.getHits().get( 0 );
464 assertEquals( "org.apache.archiva", hit.getGroupId() );
465 assertEquals( "archiva-test", hit.getArtifactId() );
466 assertEquals( "versions not 1", 1, hit.getVersions().size() );
467 assertEquals( "1.0", hit.getVersions().get( 0 ) );
470 // tests for advanced search
472 public void testAdvancedSearch()
475 List<Path> files = new ArrayList<>();
476 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_2
477 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
478 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_2
479 + "/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar" ) );
480 createIndex( TEST_REPO_2, files, false );
482 List<String> selectedRepos = new ArrayList<>();
483 selectedRepos.add( TEST_REPO_2 );
485 SearchFields searchFields = new SearchFields();
486 searchFields.setGroupId( "org.apache.archiva" );
487 searchFields.setVersion( "1.0" );
488 searchFields.setRepositories( selectedRepos );
490 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
491 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
493 archivaConfigControl.replay();
495 SearchResults results = search.search( "user", searchFields, null );
497 archivaConfigControl.verify();
499 assertNotNull( results );
500 assertEquals( 1, results.getTotalHits() );
502 SearchResultHit hit = results.getHits().get( 0 );
503 assertEquals( "org.apache.archiva", hit.getGroupId() );
504 assertEquals( "archiva-search", hit.getArtifactId() );
505 assertEquals( "1.0", hit.getVersions().get( 0 ) );
509 public void testAdvancedSearchWithPagination()
512 createIndexContainingMoreArtifacts( false );
514 List<String> selectedRepos = new ArrayList<>();
515 selectedRepos.add( TEST_REPO_1 );
517 SearchFields searchFields = new SearchFields();
518 searchFields.setGroupId( "org.apache.archiva" );
519 searchFields.setRepositories( selectedRepos );
523 SearchResultLimits limits = new SearchResultLimits( 0 );
524 limits.setPageSize( 1 );
526 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
527 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
529 archivaConfigControl.replay();
531 SearchResults results = search.search( "user", searchFields, limits );
533 archivaConfigControl.verify();
535 assertNotNull( results );
536 assertEquals( 4, results.getTotalHits() );
537 assertEquals( 1, results.getHits().size() );
540 archivaConfigControl.reset();
542 limits = new SearchResultLimits( 1 );
543 limits.setPageSize( 1 );
545 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
546 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
548 archivaConfigControl.replay();
550 results = search.search( "user", searchFields, limits );
552 archivaConfigControl.verify();
554 assertNotNull( results );
555 assertEquals( 4, results.getTotalHits() );
556 assertEquals( 1, results.getHits().size() );
559 // MRM-981 - artifactIds with numeric characters aren't found in advanced search
561 public void testAdvancedSearchArtifactIdHasNumericChar()
564 List<Path> files = new ArrayList<>();
565 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(),
566 "target/repos/" + TEST_REPO_1 + "/com/artifactid-numeric/1.0/artifactid-numeric-1.0.jar" ) );
567 files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_1
568 + "/com/artifactid-numeric123/1.0/artifactid-numeric123-1.0.jar" ) );
569 createIndex( TEST_REPO_1, files, true );
571 List<String> selectedRepos = new ArrayList<>();
572 selectedRepos.add( TEST_REPO_1 );
574 SearchFields searchFields = new SearchFields();
575 searchFields.setArtifactId( "artifactid-numeric" );
576 searchFields.setRepositories( selectedRepos );
578 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
579 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
581 archivaConfigControl.replay();
583 SearchResults results = search.search( "user", searchFields, null );
585 archivaConfigControl.verify();
587 assertNotNull( results );
588 assertEquals( 2, results.getTotalHits() );
592 public void testAdvancedSearchNoRepositoriesConfigured()
595 SearchFields searchFields = new SearchFields();
596 searchFields.setArtifactId( "archiva" );
597 searchFields.setRepositories( null );
601 search.search( "user", searchFields, null );
602 fail( "A RepositorySearchExcecption should have been thrown." );
604 catch ( RepositorySearchException e )
606 assertEquals( "Repositories cannot be null.", e.getMessage() );
611 public void testAdvancedSearchSearchFieldsAreNull()
614 List<String> selectedRepos = new ArrayList<>();
615 selectedRepos.add( TEST_REPO_1 );
617 SearchFields searchFields = new SearchFields();
618 searchFields.setRepositories( selectedRepos );
622 // EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
623 // EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
625 // archivaConfigControl.replay();
627 search.search( "user", searchFields, null );
629 archivaConfigControl.verify();
631 fail( "A RepositorySearchExcecption should have been thrown." );
633 catch ( RepositorySearchException e )
635 assertEquals( "No search fields set.", e.getMessage() );
640 public void testAdvancedSearchSearchFieldsAreBlank()
643 List<String> selectedRepos = new ArrayList<>();
644 selectedRepos.add( TEST_REPO_1 );
646 SearchFields searchFields = new SearchFields();
647 searchFields.setGroupId( "" );
648 searchFields.setArtifactId( "" );
649 searchFields.setVersion( "" );
650 searchFields.setPackaging( "" );
651 searchFields.setClassName( "" );
653 searchFields.setRepositories( selectedRepos );
658 // EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
659 // EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
660 // archivaConfigControl.replay();
662 search.search( "user", searchFields, null );
664 archivaConfigControl.verify();
666 fail( "A RepositorySearchException should have been thrown." );
668 catch ( RepositorySearchException e )
670 assertEquals( "No search fields set.", e.getMessage() );
675 public void testAdvancedSearchAllSearchCriteriaSpecified()
678 createSimpleIndex( true );
680 List<String> selectedRepos = new ArrayList<>();
681 selectedRepos.add( TEST_REPO_1 );
683 SearchFields searchFields = new SearchFields();
684 searchFields.setGroupId( "org.apache.archiva" );
685 searchFields.setArtifactId( "archiva-test" );
686 searchFields.setVersion( "2.0" );
687 searchFields.setPackaging( "jar" );
688 searchFields.setClassName( "org.apache.archiva.test.App" );
689 searchFields.setRepositories( selectedRepos );
691 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
692 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
694 archivaConfigControl.replay();
696 SearchResults results = search.search( "user", searchFields, null );
698 archivaConfigControl.verify();
700 assertNotNull( results );
702 assertEquals( "total hints not 1" + results, 1, results.getTotalHits() );
704 SearchResultHit hit = results.getHits().get( 0 );
705 assertEquals( "org.apache.archiva", hit.getGroupId() );
706 assertEquals( "archiva-test", hit.getArtifactId() );
707 assertEquals( "version not 2.0", "2.0", hit.getVersions().get( 0 ) );
711 public void testAdvancedSearchJarArtifacts()
714 createIndexContainingMoreArtifacts( true );
716 List<String> selectedRepos = new ArrayList<>();
717 selectedRepos.add( TEST_REPO_1 );
719 SearchFields searchFields = new SearchFields();
720 searchFields.setPackaging( "jar" );
721 searchFields.setRepositories( selectedRepos );
723 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
724 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
726 archivaConfigControl.replay();
728 SearchResults results = search.search( "user", searchFields, null );
730 archivaConfigControl.verify();
732 assertNotNull( results );
733 assertEquals( "not 8 but " + results.getTotalHits() + ":" + niceDisplay( results ), 8, results.getTotalHits() );
737 public void testAdvancedSearchWithIncorrectPackaging()
740 createSimpleIndex( true );
742 List<String> selectedRepos = new ArrayList<>();
743 selectedRepos.add( TEST_REPO_1 );
745 SearchFields searchFields = new SearchFields();
746 searchFields.setGroupId( "org.apache.archiva" );
747 searchFields.setArtifactId( "archiva-test" );
748 searchFields.setVersion( "2.0" );
749 searchFields.setPackaging( "war" );
750 searchFields.setRepositories( selectedRepos );
752 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
753 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
755 archivaConfigControl.replay();
757 SearchResults results = search.search( "user", searchFields, null );
759 archivaConfigControl.verify();
761 assertNotNull( results );
762 assertEquals( 0, results.getTotalHits() );
766 public void testAdvancedSearchClassname()
769 createIndexContainingMoreArtifacts( true );
771 List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
773 SearchFields searchFields = new SearchFields();
774 searchFields.setClassName( "com.classname.search.App" );
775 searchFields.setRepositories( selectedRepos );
777 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
778 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
780 archivaConfigControl.replay();
782 SearchResults results = search.search( "user", searchFields, null );
784 archivaConfigControl.verify();
786 assertNotNull( results );
787 assertEquals( "totalHits not 1 results " + results, 1, results.getTotalHits() );
789 SearchResultHit hit = results.getHits().get( 0 );
790 assertEquals( "groupId not com", "com", hit.getGroupId() );
791 assertEquals( "arttifactId not classname-search", "classname-search", hit.getArtifactId() );
792 assertEquals( " hits.version(0) not 1.0", "1.0", hit.getVersions().get( 0 ) );
796 public void testAdvancedSearchNoIndexFound()
799 List<String> selectedRepos = new ArrayList<>();
800 selectedRepos.add( TEST_REPO_1 );
802 SearchFields searchFields = new SearchFields();
803 searchFields.setGroupId( "org.apache.archiva" );
804 searchFields.setRepositories( selectedRepos );
806 // EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
807 // EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
809 // archivaConfigControl.replay();
811 SearchResults results = search.search( "user", searchFields, null );
813 archivaConfigControl.verify();
815 assertNotNull( results );
816 assertEquals( 0, results.getTotalHits() );
820 public void testAdvancedSearchClassNameInWar()
823 createIndexContainingMoreArtifacts( true );
825 List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
827 SearchFields searchFields = new SearchFields();
828 searchFields.setClassName( "SomeClass" );
829 searchFields.setRepositories( selectedRepos );
831 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
832 EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
834 archivaConfigControl.replay();
836 SearchResults results = search.search( "user", searchFields, null );
838 archivaConfigControl.verify();
840 assertNotNull( results );
841 assertEquals( 1, results.getHits().size() );
842 assertEquals( "test-webapp", results.getHits().get( 0 ).getArtifactId() );
846 public void getAllGroupIds()
849 createIndexContainingMoreArtifacts( true );
851 List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
853 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 0, 2 );
855 archivaConfigControl.replay();
857 Collection<String> groupIds = search.getAllGroupIds( "user", selectedRepos );
859 archivaConfigControl.verify();
861 log.info( "groupIds: {}", groupIds );
863 assertEquals( 3, groupIds.size() );
864 assertTrue( groupIds.contains( "com" ) );
865 assertTrue( groupIds.contains( "org.apache.felix" ) );
866 assertTrue( groupIds.contains( "org.apache.archiva" ) );
870 public void testSearchWithUnknownRepo()
873 createIndexContainingMoreArtifacts( true );
875 List<String> selectedRepos = Arrays.asList( "foo" );
877 SearchFields searchFields = new SearchFields();
878 searchFields.setClassName( "SomeClass" );
879 searchFields.setRepositories( selectedRepos );
881 archivaConfigControl.replay();
883 SearchResults results = search.search( "user", searchFields, null );
885 archivaConfigControl.verify();
887 assertNotNull( results );
888 assertEquals( 0, results.getHits().size() );
892 public void nolimitedResult()
896 Path repo = Paths.get("target/repo-release-index-test/repo-release");
898 Path indexDirectory = repo.resolve(".indexer");
899 Path zipFile = Paths.get(Thread.currentThread().getContextClassLoader().getResource("repo-release.zip").toURI());
900 FileUtils.unzip(zipFile, repo.getParent());
901 // IndexUpgrader.main(new String[]{indexDirectory.toAbsolutePath().toString(), "-delete-prior-commits"});
902 createIndex(REPO_RELEASE, Collections.emptyList(), false, indexDirectory, false);
904 // indexer.addIndexingContext( REPO_RELEASE, REPO_RELEASE, repo.toFile(), indexDirectory.toFile(),
905 // repo.toUri().toURL().toExternalForm(),
906 // indexDirectory.toUri().toURL().toString(), indexCreators );
909 SearchResultLimits limits = new SearchResultLimits(SearchResultLimits.ALL_PAGES);
910 limits.setPageSize(300);
912 // EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
913 EasyMock.expect(archivaConfig.getConfiguration()).andReturn(config).anyTimes();
915 archivaConfigControl.replay();
917 SearchResults searchResults = search.search(null, Arrays.asList(REPO_RELEASE), //
918 "org.example", limits, //
919 Collections.emptyList());
921 log.info("results: {}", searchResults.getHits().size());
923 assertEquals(255, searchResults.getHits().size());
925 SearchFields searchFields = new SearchFields();
926 searchFields.setGroupId("org.example");
927 searchFields.setRepositories(Arrays.asList(REPO_RELEASE));
929 searchResults = search.search(null, searchFields, limits);
931 log.info("results: {}", searchResults.getHits().size());
933 assertEquals(255, searchResults.getHits().size());
935 archivaConfigControl.verify();
937 FileUtils.deleteQuietly(repo);