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