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