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.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
85
86
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
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
180
181
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
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
212
213
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
224
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
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
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
274
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
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
326
327 assertEquals( r.toString(), 1, r.size() );
328 }
329
330 {
331
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
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
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
386
387
388
389
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
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
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
477
478
479
480
481
482
483 public void testPurge()
484 throws Exception
485 {
486
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
512
513 }
514 }
515
516 return true;
517 }
518
519 public void testPackaging()
520 throws Exception
521 {
522 IndexReader reader = context.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
542
543
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 }