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.MultiFields;
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
87
88
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
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
182
183
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
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
214
215
216
217
218
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
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
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
267
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
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
319
320 assertEquals( r.toString(), 1, r.size() );
321 }
322
323 {
324
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
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
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
379
380
381
382
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
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
423
424
425
426
427
428
429
430
431
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 public void testPurge()
477 throws Exception
478 {
479
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
505
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 = MultiFields.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
536
537
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
563 IteratorSearchRequest request =
564 new IteratorSearchRequest( nexusIndexer.constructQuery( MAVEN.GROUP_ID, "*.forge", SearchType.EXACT ) );
565
566
567
568
569
570 IteratorSearchResponse response = nexusIndexer.searchIterator( request );
571
572 try
573 {
574 assertEquals( response.getResults().toString(), 2, response.getTotalHitsCount() );
575
576 for ( ArtifactInfo ai : response )
577 {
578 assertEquals( ai.getGroupId(), "org.terracotta.forge" );
579 }
580 }
581 finally
582 {
583 response.close();
584 }
585 }
586
587 }