]> source.dussan.org Git - archiva.git/blob
74df0a23c95525d4c039418af5eb5e35c1a937d3
[archiva.git] /
1 package org.apache.archiva.maven.indexer.search;
2
3 /*
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
11  *
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
18  * under the License.
19  */
20
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;
33
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;
42
43
44 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
45 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
46 public class MavenRepositorySearchTest
47     extends AbstractMavenRepositorySearch
48 {
49
50
51     private void createSimpleIndex( boolean scan )
52         throws Exception
53     {
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" ));
61
62         createIndex( TEST_REPO_1, files, scan );
63     }
64
65     private void createIndexContainingMoreArtifacts( boolean scan )
66         throws Exception
67     {
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" ) );
83
84         createIndex( TEST_REPO_1, files, scan );
85     }
86
87     private void createIndexContainingMultipleArtifactsSameVersion( boolean scan )
88         throws Exception
89     {
90         List<Path> files = new ArrayList<>();
91
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" ) );
94
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" ) );
97
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" ) );
100
101         createIndex( TEST_REPO_1, files, scan );
102     }
103
104     @Test
105     public void testQuickSearch()
106         throws Exception
107     {
108         createSimpleIndex( false );
109
110         List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
111
112         // search artifactId
113         EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
114         EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
115
116         archivaConfigControl.replay();
117
118         SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
119
120         archivaConfigControl.verify();
121
122         assertNotNull( results );
123
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 ) );
130
131         archivaConfigControl.reset();
132
133         EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
134         EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
135
136         // search groupId
137         archivaConfigControl.replay();
138
139         results = search.search( "user", selectedRepos, "org.apache.archiva", null, null );
140
141         archivaConfigControl.verify();
142
143         assertNotNull( results );
144         assertEquals( "total hints not 3", 3, results.getTotalHits() );
145
146         //TODO: search for class & package names
147     }
148
149     @Test
150     public void testQuickSearchNotWithClassifier()
151         throws Exception
152     {
153         createSimpleIndex( true );
154
155         List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
156
157         // search artifactId
158         EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
159         EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
160
161         archivaConfigControl.replay();
162
163         SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
164
165         archivaConfigControl.verify();
166
167         assertNotNull( results );
168
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 ) );
175
176         archivaConfigControl.reset();
177
178         // search groupId
179         EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
180         EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
181
182         archivaConfigControl.replay();
183
184         results = search.search( "user", selectedRepos, "archiva-search", null, null );
185
186         archivaConfigControl.verify();
187
188         assertNotNull( results );
189         assertEquals( "total hints not 3 hits " + results.getHits(), 3, results.getTotalHits() );
190
191         //TODO: search for class & package names
192     }
193
194     @Test
195     public void testQuickSearchMultipleArtifactsSameVersion()
196         throws Exception
197     {
198         createIndexContainingMultipleArtifactsSameVersion( false );
199
200         List<String> selectedRepos = new ArrayList<>();
201         selectedRepos.add( TEST_REPO_1 );
202
203         // search artifactId
204         EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
205         EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
206
207         archivaConfigControl.replay();
208
209         SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
210
211         archivaConfigControl.verify();
212
213         assertNotNull( results );
214         assertEquals( 3, results.getTotalHits() );
215
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 ) );
220
221         //only 1 version of 1.0 is retrieved
222         assertEquals( 1, hit.getVersions().size() );
223     }
224
225     @Test
226     public void testMultipleArtifactsSameVersionWithClassifier()
227         throws Exception
228     {
229         createIndexContainingMultipleArtifactsSameVersion( true );
230
231         List<String> selectedRepos = new ArrayList<>();
232         selectedRepos.add( TEST_REPO_1 );
233
234         // search artifactId
235         EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
236         EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
237
238         archivaConfigControl.replay();
239
240         SearchFields searchFields = new SearchFields();
241         searchFields.setGroupId( "org.apache.archiva" );
242         searchFields.setArtifactId( "archiva-search" );
243         searchFields.setClassifier( "sources" );
244         searchFields.setRepositories( selectedRepos );
245
246         SearchResults results = search.search( "user", searchFields, null );
247
248         archivaConfigControl.verify();
249
250         assertNotNull( results );
251         assertEquals( 1, results.getTotalHits() );
252
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 ) );
257
258         //only 1 version of 1.0 is retrieved
259         assertEquals( 1, hit.getVersions().size() );
260     }
261
262     // search for existing artifact using multiple keywords
263     @Test
264     public void testQuickSearchWithMultipleKeywords()
265         throws Exception
266     {
267         createSimpleIndex( false );
268
269         List<String> selectedRepos = new ArrayList<>();
270         selectedRepos.add( TEST_REPO_1 );
271
272         EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
273         EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
274         archivaConfigControl.replay();
275
276         SearchResults results = search.search( "user", selectedRepos, "archiva search", null, null );
277
278         archivaConfigControl.verify();
279
280         assertNotNull( results );
281         assertEquals( 1, results.getTotalHits() );
282     }
283
284     @Test
285     public void testQuickSearchWithPagination()
286         throws Exception
287     {
288         createSimpleIndex( true );
289
290         List<String> selectedRepos = new ArrayList<>();
291         selectedRepos.add( TEST_REPO_1 );
292
293         // page 1
294         SearchResultLimits limits = new SearchResultLimits( 0 );
295         limits.setPageSize( 1 );
296
297         EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
298         EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
299
300         archivaConfigControl.replay();
301
302         SearchResults results = search.search( "user", selectedRepos, "org", limits, Collections.emptyList() );
303
304         archivaConfigControl.verify();
305
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() );
311
312         archivaConfigControl.reset();
313
314         // page 2
315         limits = new SearchResultLimits( 1 );
316         limits.setPageSize( 1 );
317
318         EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
319         EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
320
321         archivaConfigControl.replay();
322
323         results = search.search( "user", selectedRepos, "org", limits, null );
324
325         archivaConfigControl.verify();
326
327         assertNotNull( results );
328
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() );
333     }
334
335     @Test
336     public void testArtifactFoundInMultipleRepositories()
337         throws Exception
338     {
339         createSimpleIndex( true );
340
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 );
347
348         List<String> selectedRepos = new ArrayList<>();
349         selectedRepos.add( TEST_REPO_1 );
350         selectedRepos.add( TEST_REPO_2 );
351
352         config.addManagedRepository( createRepositoryConfig( TEST_REPO_2 ) );
353
354         EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
355         EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
356
357         archivaConfigControl.replay();
358
359         // wait lucene flush.....
360         Thread.sleep( 2000 );
361
362         SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
363
364         archivaConfigControl.verify();
365
366         assertNotNull( results );
367
368         SearchResultHit hit =
369             results.getSearchResultHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-search", null, "jar" ) );
370         assertNotNull(hit);
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" ) );
376
377         archivaConfigControl.reset();
378
379         // TODO: [BROWSE] in artifact info from browse, display all the repositories where the artifact is found
380     }
381
382     @Test
383     public void testNoMatchFound()
384         throws Exception
385     {
386         createSimpleIndex( false );
387
388         List<String> selectedRepos = new ArrayList<>();
389         selectedRepos.add( TEST_REPO_1 );
390
391         EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
392         EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
393
394         archivaConfigControl.replay();
395
396         SearchResults results = search.search( "user", selectedRepos, "dfghdfkweriuasndsaie", null, null );
397
398         archivaConfigControl.verify();
399
400         assertNotNull( results );
401         assertEquals( 0, results.getTotalHits() );
402     }
403
404     @Test
405     public void testNoIndexFound()
406         throws Exception
407     {
408         List<String> selectedRepos = new ArrayList<>();
409         selectedRepos.add( TEST_REPO_1 );
410
411         // EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
412         // EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
413
414         // archivaConfigControl.replay();
415
416         SearchResults results = search.search( "user", selectedRepos, "org.apache.archiva", null, null );
417         assertNotNull( results );
418         assertEquals( 0, results.getTotalHits() );
419
420         archivaConfigControl.verify();
421     }
422
423     @Test
424     public void testRepositoryNotFound()
425         throws Exception
426     {
427         List<String> selectedRepos = new ArrayList<>();
428         selectedRepos.add( "non-existing-repo" );
429
430         // archivaConfigControl.replay();
431
432         SearchResults results = search.search( "user", selectedRepos, "org.apache.archiva", null, null );
433         assertNotNull( results );
434         assertEquals( 0, results.getTotalHits() );
435
436         archivaConfigControl.verify();
437     }
438
439     @Test
440     public void testSearchWithinSearchResults()
441         throws Exception
442     {
443         createSimpleIndex( true );
444
445         List<String> selectedRepos = new ArrayList<>();
446         selectedRepos.add( TEST_REPO_1 );
447
448         List<String> previousSearchTerms = new ArrayList<>();
449         previousSearchTerms.add( "archiva-test" );
450
451         EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
452         EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
453
454         archivaConfigControl.replay();
455
456         SearchResults results = search.search( "user", selectedRepos, "1.0", null, previousSearchTerms );
457
458         archivaConfigControl.verify();
459
460         assertNotNull( results );
461         assertEquals( "total hints not 1", 1, results.getTotalHits() );
462
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 ) );
468     }
469
470     // tests for advanced search
471     @Test
472     public void testAdvancedSearch()
473         throws Exception
474     {
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 );
481
482         List<String> selectedRepos = new ArrayList<>();
483         selectedRepos.add( TEST_REPO_2 );
484
485         SearchFields searchFields = new SearchFields();
486         searchFields.setGroupId( "org.apache.archiva" );
487         searchFields.setVersion( "1.0" );
488         searchFields.setRepositories( selectedRepos );
489
490         EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
491         EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
492
493         archivaConfigControl.replay();
494
495         SearchResults results = search.search( "user", searchFields, null );
496
497         archivaConfigControl.verify();
498
499         assertNotNull( results );
500         assertEquals( 1, results.getTotalHits() );
501
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 ) );
506     }
507
508     @Test
509     public void testAdvancedSearchWithPagination()
510         throws Exception
511     {
512         createIndexContainingMoreArtifacts( false );
513
514         List<String> selectedRepos = new ArrayList<>();
515         selectedRepos.add( TEST_REPO_1 );
516
517         SearchFields searchFields = new SearchFields();
518         searchFields.setGroupId( "org.apache.archiva" );
519         searchFields.setRepositories( selectedRepos );
520
521         // page 1
522
523         SearchResultLimits limits = new SearchResultLimits( 0 );
524         limits.setPageSize( 1 );
525
526         EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
527         EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
528
529         archivaConfigControl.replay();
530
531         SearchResults results = search.search( "user", searchFields, limits );
532
533         archivaConfigControl.verify();
534
535         assertNotNull( results );
536         assertEquals( 4, results.getTotalHits() );
537         assertEquals( 1, results.getHits().size() );
538
539         // page 2
540         archivaConfigControl.reset();
541
542         limits = new SearchResultLimits( 1 );
543         limits.setPageSize( 1 );
544
545         EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
546         EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
547
548         archivaConfigControl.replay();
549
550         results = search.search( "user", searchFields, limits );
551
552         archivaConfigControl.verify();
553
554         assertNotNull( results );
555         assertEquals( 4, results.getTotalHits() );
556         assertEquals( 1, results.getHits().size() );
557     }
558
559     // MRM-981 - artifactIds with numeric characters aren't found in advanced search
560     @Test
561     public void testAdvancedSearchArtifactIdHasNumericChar()
562         throws Exception
563     {
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 );
570
571         List<String> selectedRepos = new ArrayList<>();
572         selectedRepos.add( TEST_REPO_1 );
573
574         SearchFields searchFields = new SearchFields();
575         searchFields.setArtifactId( "artifactid-numeric" );
576         searchFields.setRepositories( selectedRepos );
577
578         EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
579         EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
580
581         archivaConfigControl.replay();
582
583         SearchResults results = search.search( "user", searchFields, null );
584
585         archivaConfigControl.verify();
586
587         assertNotNull( results );
588         assertEquals( 2, results.getTotalHits() );
589     }
590
591     @Test
592     public void testAdvancedSearchNoRepositoriesConfigured()
593         throws Exception
594     {
595         SearchFields searchFields = new SearchFields();
596         searchFields.setArtifactId( "archiva" );
597         searchFields.setRepositories( null );
598
599         try
600         {
601             search.search( "user", searchFields, null );
602             fail( "A RepositorySearchExcecption should have been thrown." );
603         }
604         catch ( RepositorySearchException e )
605         {
606             assertEquals( "Repositories cannot be null.", e.getMessage() );
607         }
608     }
609
610     @Test
611     public void testAdvancedSearchSearchFieldsAreNull()
612         throws Exception
613     {
614         List<String> selectedRepos = new ArrayList<>();
615         selectedRepos.add( TEST_REPO_1 );
616
617         SearchFields searchFields = new SearchFields();
618         searchFields.setRepositories( selectedRepos );
619
620         try
621         {
622             // EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
623             // EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
624
625             // archivaConfigControl.replay();
626
627             search.search( "user", searchFields, null );
628
629             archivaConfigControl.verify();
630
631             fail( "A RepositorySearchExcecption should have been thrown." );
632         }
633         catch ( RepositorySearchException e )
634         {
635             assertEquals( "No search fields set.", e.getMessage() );
636         }
637     }
638
639     @Test
640     public void testAdvancedSearchSearchFieldsAreBlank()
641         throws Exception
642     {
643         List<String> selectedRepos = new ArrayList<>();
644         selectedRepos.add( TEST_REPO_1 );
645
646         SearchFields searchFields = new SearchFields();
647         searchFields.setGroupId( "" );
648         searchFields.setArtifactId( "" );
649         searchFields.setVersion( "" );
650         searchFields.setPackaging( "" );
651         searchFields.setClassName( "" );
652
653         searchFields.setRepositories( selectedRepos );
654
655         try
656         {
657
658             // EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
659             // EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
660             // archivaConfigControl.replay();
661
662             search.search( "user", searchFields, null );
663
664             archivaConfigControl.verify();
665
666             fail( "A RepositorySearchException should have been thrown." );
667         }
668         catch ( RepositorySearchException e )
669         {
670             assertEquals( "No search fields set.", e.getMessage() );
671         }
672     }
673
674     @Test
675     public void testAdvancedSearchAllSearchCriteriaSpecified()
676         throws Exception
677     {
678         createSimpleIndex( true );
679
680         List<String> selectedRepos = new ArrayList<>();
681         selectedRepos.add( TEST_REPO_1 );
682
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 );
690
691         EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
692         EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
693
694         archivaConfigControl.replay();
695
696         SearchResults results = search.search( "user", searchFields, null );
697
698         archivaConfigControl.verify();
699
700         assertNotNull( results );
701
702         assertEquals( "total hints not 1" + results, 1, results.getTotalHits() );
703
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 ) );
708     }
709
710     @Test
711     public void testAdvancedSearchJarArtifacts()
712         throws Exception
713     {
714         createIndexContainingMoreArtifacts( true );
715
716         List<String> selectedRepos = new ArrayList<>();
717         selectedRepos.add( TEST_REPO_1 );
718
719         SearchFields searchFields = new SearchFields();
720         searchFields.setPackaging( "jar" );
721         searchFields.setRepositories( selectedRepos );
722
723         EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
724         EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
725
726         archivaConfigControl.replay();
727
728         SearchResults results = search.search( "user", searchFields, null );
729
730         archivaConfigControl.verify();
731
732         assertNotNull( results );
733         assertEquals( "not 8 but " + results.getTotalHits() + ":" + niceDisplay( results ), 8, results.getTotalHits() );
734     }
735
736     @Test
737     public void testAdvancedSearchWithIncorrectPackaging()
738         throws Exception
739     {
740         createSimpleIndex( true );
741
742         List<String> selectedRepos = new ArrayList<>();
743         selectedRepos.add( TEST_REPO_1 );
744
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 );
751
752         EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
753         EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
754
755         archivaConfigControl.replay();
756
757         SearchResults results = search.search( "user", searchFields, null );
758
759         archivaConfigControl.verify();
760
761         assertNotNull( results );
762         assertEquals( 0, results.getTotalHits() );
763     }
764
765     @Test
766     public void testAdvancedSearchClassname()
767         throws Exception
768     {
769         createIndexContainingMoreArtifacts( true );
770
771         List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
772
773         SearchFields searchFields = new SearchFields();
774         searchFields.setClassName( "com.classname.search.App" );
775         searchFields.setRepositories( selectedRepos );
776
777         EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
778         EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
779
780         archivaConfigControl.replay();
781
782         SearchResults results = search.search( "user", searchFields, null );
783
784         archivaConfigControl.verify();
785
786         assertNotNull( results );
787         assertEquals( "totalHits not 1 results " + results, 1, results.getTotalHits() );
788
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 ) );
793     }
794
795     @Test
796     public void testAdvancedSearchNoIndexFound()
797         throws Exception
798     {
799         List<String> selectedRepos = new ArrayList<>();
800         selectedRepos.add( TEST_REPO_1 );
801
802         SearchFields searchFields = new SearchFields();
803         searchFields.setGroupId( "org.apache.archiva" );
804         searchFields.setRepositories( selectedRepos );
805
806         // EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
807         // EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
808
809         // archivaConfigControl.replay();
810
811         SearchResults results = search.search( "user", searchFields, null );
812
813         archivaConfigControl.verify();
814
815         assertNotNull( results );
816         assertEquals( 0, results.getTotalHits() );
817     }
818
819     @Test
820     public void testAdvancedSearchClassNameInWar()
821         throws Exception
822     {
823         createIndexContainingMoreArtifacts( true );
824
825         List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
826
827         SearchFields searchFields = new SearchFields();
828         searchFields.setClassName( "SomeClass" );
829         searchFields.setRepositories( selectedRepos );
830
831         EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
832         EasyMock.expect( archivaConfig.getConfiguration()).andReturn(config).anyTimes();
833
834         archivaConfigControl.replay();
835
836         SearchResults results = search.search( "user", searchFields, null );
837
838         archivaConfigControl.verify();
839
840         assertNotNull( results );
841         assertEquals( 1, results.getHits().size() );
842         assertEquals( "test-webapp", results.getHits().get( 0 ).getArtifactId() );
843     }
844
845     @Test
846     public void getAllGroupIds()
847         throws Exception
848     {
849         createIndexContainingMoreArtifacts( true );
850
851         List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
852
853         EasyMock.expect( archivaConfig.getConfiguration() ).andReturn( config ).times( 0, 2 );
854
855         archivaConfigControl.replay();
856
857         Collection<String> groupIds = search.getAllGroupIds( "user", selectedRepos );
858
859         archivaConfigControl.verify();
860
861         log.info( "groupIds: {}", groupIds );
862
863         assertEquals( 3, groupIds.size() );
864         assertTrue( groupIds.contains( "com" ) );
865         assertTrue( groupIds.contains( "org.apache.felix" ) );
866         assertTrue( groupIds.contains( "org.apache.archiva" ) );
867     }
868
869     @Test
870     public void testSearchWithUnknownRepo()
871         throws Exception
872     {
873         createIndexContainingMoreArtifacts( true );
874
875         List<String> selectedRepos = Arrays.asList( "foo" );
876
877         SearchFields searchFields = new SearchFields();
878         searchFields.setClassName( "SomeClass" );
879         searchFields.setRepositories( selectedRepos );
880
881         archivaConfigControl.replay();
882
883         SearchResults results = search.search( "user", searchFields, null );
884
885         archivaConfigControl.verify();
886
887         assertNotNull( results );
888         assertEquals( 0, results.getHits().size() );
889     }
890
891     @Test
892     public void nolimitedResult()
893         throws Exception
894     {
895
896         Path repo = Paths.get("target/repo-release-index-test/repo-release");
897         try {
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);
903
904 //        indexer.addIndexingContext( REPO_RELEASE, REPO_RELEASE, repo.toFile(), indexDirectory.toFile(),
905 //                                         repo.toUri().toURL().toExternalForm(),
906 //                                         indexDirectory.toUri().toURL().toString(), indexCreators );
907
908
909             SearchResultLimits limits = new SearchResultLimits(SearchResultLimits.ALL_PAGES);
910             limits.setPageSize(300);
911
912             // EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
913             EasyMock.expect(archivaConfig.getConfiguration()).andReturn(config).anyTimes();
914
915             archivaConfigControl.replay();
916
917             SearchResults searchResults = search.search(null, Arrays.asList(REPO_RELEASE), //
918                     "org.example", limits, //
919                     Collections.emptyList());
920
921             log.info("results: {}", searchResults.getHits().size());
922
923             assertEquals(255, searchResults.getHits().size());
924
925             SearchFields searchFields = new SearchFields();
926             searchFields.setGroupId("org.example");
927             searchFields.setRepositories(Arrays.asList(REPO_RELEASE));
928
929             searchResults = search.search(null, searchFields, limits);
930
931             log.info("results: {}", searchResults.getHits().size());
932
933             assertEquals(255, searchResults.getHits().size());
934
935             archivaConfigControl.verify();
936         } finally {
937             FileUtils.deleteQuietly(repo);
938         }
939     }
940 }