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.io.IOException;
23
24 import org.apache.lucene.search.Query;
25 import org.apache.maven.index.context.IndexingContext;
26 import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException;
27 import org.codehaus.plexus.util.FileUtils;
28 import org.junit.Test;
29
30 import static org.junit.Assert.assertEquals;
31 import static org.junit.Assert.assertTrue;
32
33 public class UniqueArtifactFilterTest extends AbstractIndexCreatorHelper {
34 private IndexingContext context;
35
36 @Test
37 public void testSearchIterator() throws Exception {
38 NexusIndexer indexer = prepare();
39
40 Query q = indexer.constructQuery(MAVEN.GROUP_ID, "qdox", SearchType.SCORED);
41
42 IteratorSearchRequest request = new IteratorSearchRequest(q);
43
44 IteratorSearchResponse response = indexer.searchIterator(request);
45
46 assertEquals(2, response.getTotalHits());
47
48 for (ArtifactInfo ai : response.getResults()) {
49 assertEquals("GroupId must match \"qdox\"!", "qdox", ai.getGroupId());
50 }
51 }
52
53 @Test
54 public void testSearchIteratorWithFilter() throws Exception {
55 NexusIndexer indexer = prepare();
56
57 Query q = indexer.constructQuery(MAVEN.GROUP_ID, "commons", SearchType.SCORED);
58
59 UniqueArtifactFilterPostprocessor filter = new UniqueArtifactFilterPostprocessor();
60 filter.addField(MAVEN.GROUP_ID);
61 filter.addField(MAVEN.ARTIFACT_ID);
62
63 IteratorSearchRequest request = new IteratorSearchRequest(q, filter);
64
65 IteratorSearchResponse response = indexer.searchIterator(request);
66
67 assertEquals("15 total hits (before filtering!)", 15, response.getTotalHits());
68
69 ArtifactInfo ai = response.getResults().next();
70 assertTrue("Iterator has to have next (2 should be returned)", ai != null);
71
72 ai = response.getResults().next();
73 assertTrue("Iterator has to have next (2 should be returned)", ai != null);
74
75 assertEquals(
76 "Property that is not unique has to have \"COLLAPSED\" value!",
77 UniqueArtifactFilterPostprocessor.COLLAPSED,
78 ai.getVersion());
79 assertEquals(
80 "Property that is not unique has to have \"COLLAPSED\" value!",
81 UniqueArtifactFilterPostprocessor.COLLAPSED,
82 ai.getPackaging());
83 assertEquals(
84 "Property that is not unique has to have \"COLLAPSED\" value!",
85 UniqueArtifactFilterPostprocessor.COLLAPSED,
86 ai.getClassifier());
87 }
88
89
90
91 private NexusIndexer prepare() throws Exception, IOException, UnsupportedExistingLuceneIndexException {
92 NexusIndexer indexer = lookup(NexusIndexer.class);
93
94
95 File indexDir = new File(getBasedir(), "target/index/test-" + System.currentTimeMillis());
96 FileUtils.deleteDirectory(indexDir);
97
98 File repo = new File(getBasedir(), "src/test/repo");
99
100 context = indexer.addIndexingContext("test", "test", repo, indexDir, null, null, DEFAULT_CREATORS);
101
102 indexer.scan(context);
103
104 return indexer;
105 }
106 }