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