View Javadoc
1   package org.apache.maven.index;
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 java.io.File;
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Set;
29  
30  import org.apache.lucene.document.Document;
31  import org.apache.lucene.index.IndexReader;
32  import org.apache.lucene.index.MultiBits;
33  import org.apache.lucene.search.Query;
34  import org.apache.lucene.util.Bits;
35  import org.apache.maven.index.search.grouping.GAGrouping;
36  import org.junit.Test;
37  
38  import static org.junit.Assert.assertEquals;
39  import static org.junit.Assert.assertFalse;
40  import static org.junit.Assert.assertNotNull;
41  import static org.junit.Assert.fail;
42  
43  public abstract class AbstractRepoNexusIndexerTest
44      extends AbstractNexusIndexerTest
45  {
46  
47      protected File repo = new File( getBasedir(), "src/test/repo" );
48  
49      @Test
50      public void testRootGroups()
51          throws Exception
52      {
53          Set<String> rootGroups = context.getRootGroups();
54          assertEquals( rootGroups.toString(), 12, rootGroups.size() );
55  
56          assertGroup( 1, "com.adobe", context );
57          assertGroup( 1, "com.adobe.flexunit", context );
58  
59          assertGroup( 2, "qdox", context );
60  
61          assertGroup( 1, "proptest", context );
62  
63          assertGroup( 3, "junit", context );
64  
65          assertGroup( 13, "commons-logging", context );
66  
67          assertGroup( 1, "regexp", context );
68  
69          assertGroup( 2, "commons-cli", context );
70  
71          assertGroup( 22, "org", context );
72  
73          assertGroup( 10, "org.slf4j", context );
74  
75          assertGroup( 4, "org.testng", context );
76  
77          assertGroup( 5, "org.apache", context );
78  
79          assertGroup( 1, "org.apache.directory", context );
80          assertGroup( 1, "org.apache.directory.server", context );
81  
82          assertGroup( 3, "org.apache.maven", context );
83          assertGroup( 3, "org.apache.maven.plugins", context );
84          assertGroup( 0, "org.apache.maven.plugins.maven-core-it-plugin", context );
85      }
86  
87      @Test
88      public void testSearchFlatPaged()
89          throws Exception
90      {
91          FlatSearchRequest request =
92              new FlatSearchRequest( nexusIndexer.constructQuery( MAVEN.GROUP_ID, "org", SearchType.SCORED ) );
93  
94          // See MINDEXER-22
95          // Flat search is not pageable
96          // request.setStart( 0 );
97  
98          request.setCount( 50 );
99  
100         FlatSearchResponse response = nexusIndexer.searchFlat( request );
101 
102         assertEquals( response.getResults().toString(), 22, response.getTotalHits() );
103     }
104 
105     @Test
106     public void testSearchFlat()
107         throws Exception
108     {
109         Query q = nexusIndexer.constructQuery( MAVEN.GROUP_ID, "qdox", SearchType.SCORED );
110 
111         FlatSearchResponse response = nexusIndexer.searchFlat( new FlatSearchRequest( q ) );
112 
113         Collection<ArtifactInfo> r = response.getResults();
114 
115         assertEquals( 2, r.size() );
116 
117         List<ArtifactInfo> list = new ArrayList<>( r );
118 
119         assertEquals( 2, list.size() );
120 
121         {
122             ArtifactInfo ai = list.get( 0 );
123             assertEquals( "1.6.1", ai.getVersion() );
124         }
125         {
126             ArtifactInfo ai = list.get( 1 );
127             assertEquals( "1.5", ai.getVersion() );
128             assertEquals( "test", ai.getRepository() );
129         }
130     }
131 
132     @Test
133     public void testSearchGrouped()
134         throws Exception
135     {
136         // ----------------------------------------------------------------------------
137         //
138         // ----------------------------------------------------------------------------
139         Query q = nexusIndexer.constructQuery( MAVEN.GROUP_ID, "qdox", SearchType.SCORED );
140 
141         GroupedSearchResponse response = nexusIndexer.searchGrouped( new GroupedSearchRequest( q, new GAGrouping() ) );
142 
143         Map<String, ArtifactInfoGroup> r = response.getResults();
144 
145         assertEquals( 1, r.size() );
146 
147         ArtifactInfoGroup ig = r.values().iterator().next();
148 
149         assertEquals( "qdox : qdox", ig.getGroupKey() );
150 
151         assertEquals( 2, ig.getArtifactInfos().size() );
152 
153         List<ArtifactInfo> list = new ArrayList<>( ig.getArtifactInfos() );
154 
155         assertEquals( 2, list.size() );
156 
157         ArtifactInfo ai = list.get( 0 );
158 
159         assertEquals( "1.6.1", ai.getVersion() );
160 
161         ai = list.get( 1 );
162 
163         assertEquals( "1.5", ai.getVersion() );
164 
165         assertEquals( "test", ai.getRepository() );
166     }
167 
168     @Test
169     public void testSearchGroupedProblematicNames()
170         throws Exception
171     {
172         {
173             // "-" in the name
174             Query q = nexusIndexer.constructQuery( MAVEN.ARTIFACT_ID, "commons-logg*", SearchType.SCORED );
175 
176             GroupedSearchRequest request = new GroupedSearchRequest( q, new GAGrouping() );
177 
178             GroupedSearchResponse response = nexusIndexer.searchGrouped( request );
179 
180             Map<String, ArtifactInfoGroup> r = response.getResults();
181 
182             assertEquals( r.toString(), 1, r.size() );
183 
184             ArtifactInfoGroup ig = r.values().iterator().next();
185 
186             assertEquals( "commons-logging : commons-logging", ig.getGroupKey() );
187 
188             assertEquals( ig.getArtifactInfos().toString(), 13, ig.getArtifactInfos().size() );
189         }
190 
191         {
192             // "-" in the name
193             // New in 4.0! constructquery do throw error on wrong input! I left in old call and checking it fails,
194             // and then added "new" call with proper query syntax!
195             Query q;
196             try
197             {
198                 q = nexusIndexer.constructQuery( MAVEN.ARTIFACT_ID, "*logging", SearchType.SCORED );
199 
200                 fail( "Input is invalid, query cannot start with *!" );
201             }
202             catch ( IllegalArgumentException e )
203             {
204                 // good, now let's do it again with good input:
205                 q = nexusIndexer.constructQuery( MAVEN.ARTIFACT_ID, "logging", SearchType.SCORED );
206             }
207 
208             GroupedSearchRequest request = new GroupedSearchRequest( q, new GAGrouping() );
209 
210             GroupedSearchResponse response = nexusIndexer.searchGrouped( request );
211 
212             Map<String, ArtifactInfoGroup> r = response.getResults();
213 
214             assertEquals( r.toString(), 1, r.size() );
215 
216             ArtifactInfoGroup ig = r.values().iterator().next();
217 
218             assertEquals( "commons-logging : commons-logging", ig.getGroupKey() );
219 
220             assertEquals( ig.getArtifactInfos().toString(), 13, ig.getArtifactInfos().size() );
221         }
222 
223         {
224             // "-" in the name
225             // New in 4.0! constructquery do throw error on wrong input! I left in old call and checking it fails,
226             // and then added "new" call with proper query syntax!
227 
228             // Since Lucene 5.x can cope with wildcard prefixes, this case is not valid anymore
229             // see https://issues.apache.org/jira/browse/MINDEXER-108
230             Query q = nexusIndexer.constructQuery( MAVEN.ARTIFACT_ID, "*-logging", SearchType.SCORED );
231 
232             GroupedSearchRequest request = new GroupedSearchRequest( q, new GAGrouping() );
233 
234             GroupedSearchResponse response = nexusIndexer.searchGrouped( request );
235 
236             Map<String, ArtifactInfoGroup> r = response.getResults();
237 
238             assertEquals( r.toString(), 1, r.size() );
239 
240             ArtifactInfoGroup ig = r.values().iterator().next();
241 
242             assertEquals( "commons-logging : commons-logging", ig.getGroupKey() );
243 
244             assertEquals( ig.getArtifactInfos().toString(), 13, ig.getArtifactInfos().size() );
245         }
246 
247         {
248             // "-" in the name
249             Query q = nexusIndexer.constructQuery( MAVEN.ARTIFACT_ID, "comm*-logg*", SearchType.SCORED );
250 
251             GroupedSearchRequest request = new GroupedSearchRequest( q, new GAGrouping() );
252 
253             GroupedSearchResponse response = nexusIndexer.searchGrouped( request );
254 
255             Map<String, ArtifactInfoGroup> r = response.getResults();
256 
257             assertEquals( r.toString(), 1, r.size() );
258 
259             ArtifactInfoGroup ig = r.values().iterator().next();
260 
261             assertEquals( "commons-logging : commons-logging", ig.getGroupKey() );
262 
263             assertEquals( ig.getArtifactInfos().toString(), 13, ig.getArtifactInfos().size() );
264         }
265 
266         {
267             // "-" in the name
268             Query q;
269             try
270             {
271                 q = nexusIndexer.constructQuery( MAVEN.ARTIFACT_ID, "*mmons-log*", SearchType.SCORED );
272 
273                 fail( "Input is invalid, query cannot start with *!" );
274             }
275             catch ( IllegalArgumentException e )
276             {
277                 // good, now let's do it again with good input:
278                 // NOTE: without crappy prefix search (that caused zillion other problems, original input does not work)
279                 q = nexusIndexer.constructQuery( MAVEN.ARTIFACT_ID, "commons-log*", SearchType.SCORED );
280             }
281 
282             GroupedSearchRequest request = new GroupedSearchRequest( q, new GAGrouping() );
283 
284             GroupedSearchResponse response = nexusIndexer.searchGrouped( request );
285 
286             Map<String, ArtifactInfoGroup> r = response.getResults();
287 
288             assertEquals( r.toString(), 1, r.size() );
289 
290             ArtifactInfoGroup ig = r.values().iterator().next();
291 
292             assertEquals( "commons-logging : commons-logging", ig.getGroupKey() );
293 
294             assertEquals( ig.getArtifactInfos().toString(), 13, ig.getArtifactInfos().size() );
295         }
296 
297         {
298             // "-" in the name
299             Query q = nexusIndexer.constructQuery( MAVEN.ARTIFACT_ID, "commons-", SearchType.SCORED );
300 
301             GroupedSearchRequest request = new GroupedSearchRequest( q, new GAGrouping() );
302 
303             GroupedSearchResponse response = nexusIndexer.searchGrouped( request );
304 
305             Map<String, ArtifactInfoGroup> r = response.getResults();
306 
307             assertEquals( r.toString(), 2, r.size() );
308 
309             Iterator<ArtifactInfoGroup> it = r.values().iterator();
310 
311             ArtifactInfoGroup ig1 = it.next();
312             assertEquals( "commons-cli : commons-cli", ig1.getGroupKey() );
313             assertEquals( ig1.getArtifactInfos().toString(), 2, ig1.getArtifactInfos().size() );
314 
315             ArtifactInfoGroup ig2 = it.next();
316             assertEquals( "commons-logging : commons-logging", ig2.getGroupKey() );
317             assertEquals( ig2.getArtifactInfos().toString(), 13, ig2.getArtifactInfos().size() );
318         }
319 
320         {
321             Query q = nexusIndexer.constructQuery( MAVEN.ARTIFACT_ID, "logging-commons", SearchType.SCORED );
322 
323             GroupedSearchRequest request = new GroupedSearchRequest( q, new GAGrouping() );
324 
325             GroupedSearchResponse response = nexusIndexer.searchGrouped( request );
326 
327             Map<String, ArtifactInfoGroup> r = response.getResults();
328 
329             // since 4.0 we do handle this
330             // assertEquals( r.toString(), 0, r.size() );
331             assertEquals( r.toString(), 1, r.size() );
332         }
333 
334         {
335             // numbers and "-" in the name
336             Query q;
337             try
338             {
339                 q = nexusIndexer.constructQuery( MAVEN.ARTIFACT_ID, "*slf4*", SearchType.SCORED );
340 
341                 fail( "Input is invalid, query cannot start with *!" );
342             }
343             catch ( IllegalArgumentException e )
344             {
345                 // good, now let's do it again with good input:
346                 q = nexusIndexer.constructQuery( MAVEN.ARTIFACT_ID, "slf4*", SearchType.SCORED );
347             }
348 
349             GroupedSearchRequest request = new GroupedSearchRequest( q, new GAGrouping() );
350 
351             GroupedSearchResponse response = nexusIndexer.searchGrouped( request );
352             Map<String, ArtifactInfoGroup> r = response.getResults();
353 
354             assertEquals( r.toString(), 3, r.size() );
355 
356             Iterator<ArtifactInfoGroup> it = r.values().iterator();
357 
358             ArtifactInfoGroup ig1 = it.next();
359             assertEquals( ig1.getArtifactInfos().toString(), 2, ig1.getArtifactInfos().size() );
360             assertEquals( "org.slf4j : jcl104-over-slf4j", ig1.getGroupKey() );
361 
362             ArtifactInfoGroup ig2 = it.next();
363             assertEquals( ig2.getArtifactInfos().toString(), 4, ig2.getArtifactInfos().size() );
364             assertEquals( "org.slf4j : slf4j-api", ig2.getGroupKey() );
365 
366             ArtifactInfoGroup ig3 = it.next();
367             assertEquals( ig3.getArtifactInfos().toString(), 4, ig3.getArtifactInfos().size() );
368             assertEquals( "org.slf4j : slf4j-log4j12", ig3.getGroupKey() );
369         }
370         {
371             // numbers and "-" in the name
372             Query q = nexusIndexer.constructQuery( MAVEN.ARTIFACT_ID, "jcl104-over-slf4*", SearchType.SCORED );
373 
374             GroupedSearchRequest request = new GroupedSearchRequest( q, new GAGrouping() );
375 
376             GroupedSearchResponse response = nexusIndexer.searchGrouped( request );
377             Map<String, ArtifactInfoGroup> r = response.getResults();
378 
379             assertEquals( r.toString(), 1, r.size() );
380 
381             ArtifactInfoGroup ig = r.values().iterator().next();
382 
383             assertEquals( ig.getArtifactInfos().toString(), 2, ig.getArtifactInfos().size() );
384 
385             assertEquals( "org.slf4j : jcl104-over-slf4j", ig.getGroupKey() );
386         }
387     }
388 
389     // public void testConstructQuery()
390     // {
391     // Query q = nexusIndexer.constructQuery( MAVEN.ARTIFACT_ID, "jcl104-over-slf4*" );
392     //
393     // assertEquals( "+a:jcl104 +a:over +a:slf4*", q.toString() );
394     //
395     // }
396 
397     @Test
398     public void testIdentify()
399         throws Exception
400     {
401         Collection<ArtifactInfo> ais = nexusIndexer.identify( MAVEN.SHA1, "4d2db265eddf1576cb9d896abc90c7ba46b48d87" );
402         
403         assertEquals( 1, ais.size() );
404 
405         ArtifactInfo ai = ais.iterator().next();
406         
407         assertNotNull( ai );
408 
409         assertEquals( "qdox", ai.getGroupId() );
410 
411         assertEquals( "qdox", ai.getArtifactId() );
412 
413         assertEquals( "1.5", ai.getVersion() );
414 
415         // Using a file
416 
417         File artifact = new File( repo, "qdox/qdox/1.5/qdox-1.5.jar" );
418 
419         ais = nexusIndexer.identify( artifact );
420         
421         assertEquals( 1, ais.size() );
422         
423         ai = ais.iterator().next();
424 
425         assertNotNull( "Can't identify qdox-1.5.jar", ai );
426 
427         assertEquals( "qdox", ai.getGroupId() );
428 
429         assertEquals( "qdox", ai.getArtifactId() );
430 
431         assertEquals( "1.5", ai.getVersion() );
432     }
433 
434     // Paging is currently disabled
435     // See MINDEXER-22
436     // Flat search is not pageable
437 //    public void donttestPaging()
438 //        throws Exception
439 //    {
440 //        // we have 22 artifact for this search
441 //        int total = 22;
442 //
443 //        int pageSize = 4;
444 //
445 //        Query q = nexusIndexer.constructQuery( MAVEN.GROUP_ID, "org", SearchType.SCORED );
446 //
447 //        FlatSearchRequest req = new FlatSearchRequest( q );
448 //
449 //        // have page size of 4, that will make us 4 pages
450 //        req.setCount( pageSize );
451 //
452 //        List<ArtifactInfo> constructedPageList = new ArrayList<ArtifactInfo>();
453 //
454 //        int offset = 0;
455 //
456 //        while ( true )
457 //        {
458 //            req.setStart( offset );
459 //
460 //            FlatSearchResponse resp = nexusIndexer.searchFlat( req );
461 //
462 //            Collection<ArtifactInfo> p = resp.getResults();
463 //
464 //            assertEquals( p.toString(), total, resp.getTotalHits() );
465 //
466 //            assertEquals( Math.min( pageSize, total - offset ), p.size() );
467 //
468 //            constructedPageList.addAll( p );
469 //
470 //            offset += pageSize;
471 //
472 //            if ( offset > total )
473 //            {
474 //                break;
475 //            }
476 //        }
477 //
478 //        //
479 //        FlatSearchResponse response = nexusIndexer.searchFlat( new FlatSearchRequest( q ) );
480 //        Collection<ArtifactInfo> onePage = response.getResults();
481 //
482 //        List<ArtifactInfo> onePageList = new ArrayList<ArtifactInfo>( onePage );
483 //
484 //        // onePage and constructedPage should hold equal elems in the same order
485 //        assertTrue( resultsAreEqual( onePageList, constructedPageList ) );
486 //    }
487 
488     @Test
489     public void testPurge()
490         throws Exception
491     {
492         // we have 14 artifact for this search
493         Query q = nexusIndexer.constructQuery( MAVEN.GROUP_ID, "org", SearchType.SCORED );
494         FlatSearchRequest request = new FlatSearchRequest( q );
495 
496         FlatSearchResponse response1 = nexusIndexer.searchFlat( request );
497         Collection<ArtifactInfo> p1 = response1.getResults();
498 
499         assertEquals( 22, p1.size() );
500 
501         context.purge();
502 
503         FlatSearchResponse response2 = nexusIndexer.searchFlat( request );
504         Collection<ArtifactInfo> p2 = response2.getResults();
505 
506         assertEquals( 0, p2.size() );
507     }
508 
509     protected boolean resultsAreEqual( List<ArtifactInfo> left, List<ArtifactInfo> right )
510     {
511         assertEquals( left.size(), right.size() );
512 
513         for ( int i = 0; i < left.size(); i++ )
514         {
515             if ( ArtifactInfo.VERSION_COMPARATOR.compare( left.get( i ), right.get( i ) ) != 0 )
516             {
517                 // TODO: we are FAKING here!
518                 // return false;
519             }
520         }
521 
522         return true;
523     }
524 
525     @Test
526     public void testPackaging()
527         throws Exception
528     {
529         IndexReader reader = context.acquireIndexSearcher().getIndexReader();
530 
531         Bits liveDocs = MultiBits.getLiveDocs(reader);
532         for ( int i = 0; i < reader.maxDoc(); i++ )
533         {
534             if (liveDocs == null || liveDocs.get(i) )
535             {
536                 Document document = reader.document( i );
537 
538                 String uinfo = document.get( ArtifactInfo.UINFO );
539 
540                 if ( uinfo != null )
541                 {
542                     String info = document.get( ArtifactInfo.INFO );
543                     assertFalse( "Bad:" + info,  info.startsWith( "null" ) );
544                 }
545             }
546         }
547 
548         // {
549         // Query query = new TermQuery( new Term( MAVEN.PACKAGING, "jar" ) );
550         // FlatSearchResponse response = nexusIndexer.searchFlat(new FlatSearchRequest(query));
551         // assertEquals(response.getResults().toString(), 22, response.getTotalHits());
552         // }
553         {
554             Query query = nexusIndexer.constructQuery( MAVEN.PACKAGING, "tar.gz", SearchType.EXACT );
555             FlatSearchResponse response = nexusIndexer.searchFlat( new FlatSearchRequest( query ) );
556             assertEquals( response.getResults().toString(), 1, response.getTotalHits() );
557 
558             ArtifactInfo ai = response.getResults().iterator().next();
559             assertEquals( "tar.gz", ai.getPackaging() );
560             assertEquals( "tar.gz", ai.getFileExtension() );
561         }
562         {
563             Query query = nexusIndexer.constructQuery( MAVEN.PACKAGING, "zip", SearchType.EXACT );
564             FlatSearchResponse response = nexusIndexer.searchFlat( new FlatSearchRequest( query ) );
565             assertEquals( response.getResults().toString(), 1, response.getTotalHits() );
566 
567             ArtifactInfo ai = response.getResults().iterator().next();
568             assertEquals( "zip", ai.getPackaging() );
569             assertEquals( "zip", ai.getFileExtension() );
570         }
571     }
572 
573     @Test
574     public void testPrefixWildcard()
575         throws Exception
576     {
577         // see https://issues.apache.org/jira/browse/MINDEXER-108
578         IteratorSearchRequest request =
579             new IteratorSearchRequest( nexusIndexer.constructQuery( MAVEN.GROUP_ID, "*.forge", SearchType.EXACT ) );
580 
581         // two candidates (see src/test/repo):
582         // org.terracotta.forge:forge-parent:1.0.5
583         // org.terracotta.forge:archetype-parent:1.0.1
584 
585         try ( IteratorSearchResponse response = nexusIndexer.searchIterator( request ) )
586         {
587             assertEquals( response.getResults().toString(), 2, response.getTotalHitsCount() );
588 
589             for ( ArtifactInfo ai : response )
590             {
591                 assertEquals( ai.getGroupId(), "org.terracotta.forge" );
592             }
593         }
594     }
595 
596 }