1 package org.apache.maven.index;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
95
96
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
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
193
194
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
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
225
226
227
228
229
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
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
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
278
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
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
330
331 assertEquals( r.toString(), 1, r.size() );
332 }
333
334 {
335
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
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
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
390
391
392
393
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
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488 @Test
489 public void testPurge()
490 throws Exception
491 {
492
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
518
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
550
551
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
578 IteratorSearchRequest request =
579 new IteratorSearchRequest( nexusIndexer.constructQuery( MAVEN.GROUP_ID, "*.forge", SearchType.EXACT ) );
580
581
582
583
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 }