1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
88
89
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
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
181
182
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
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
211
212
213
214
215
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
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
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
263
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
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
320
321 assertEquals(r.toString(), 1, r.size());
322 }
323
324 {
325
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
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
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
381
382
383
384
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
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
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
477 @Test
478 public void testPurge() throws Exception {
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 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
502
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
529
530
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
555 IteratorSearchRequest request =
556 new IteratorSearchRequest(nexusIndexer.constructQuery(MAVEN.GROUP_ID, "*.forge", SearchType.EXACT));
557
558
559
560
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 }