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