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 javax.inject.Inject;
22
23 import java.io.File;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.List;
27
28 import org.apache.lucene.search.BooleanQuery;
29 import org.apache.lucene.search.IndexSearcher;
30 import org.apache.maven.index.context.IndexCreator;
31 import org.apache.maven.index.context.IndexingContext;
32 import org.apache.maven.index.expr.StringSearchExpression;
33 import org.apache.maven.index.packer.IndexPacker;
34 import org.apache.maven.index.packer.IndexPackingRequest;
35 import org.codehaus.plexus.util.FileUtils;
36 import org.junit.Test;
37
38 import static org.apache.lucene.search.BooleanClause.*;
39 import static org.junit.Assert.assertEquals;
40
41
42
43
44 public class SearchWithAnEmptyIndexTest extends AbstractTestSupport {
45 static final String INDEX_ID1 = "osgi-test1";
46 static final String INDEX_ID2 = "empty-repo";
47
48 @Inject
49 private NexusIndexer nexusIndexer;
50
51 @Inject
52 private IndexPacker indexPacker;
53
54 @Inject
55 protected List<IndexCreator> indexCreators;
56
57 @Override
58 public void setUp() throws Exception {
59 super.setUp();
60
61 if (!nexusIndexer.getIndexingContexts().isEmpty()) {
62 for (IndexingContext context : nexusIndexer.getIndexingContexts().values()) {
63 nexusIndexer.removeIndexingContext(context, true);
64 }
65 }
66 }
67
68 @Test
69 public void testWithTwoContextWithOneEmptyFirstInContextsListSearchFlat() throws Exception {
70
71 String repoPath = "target/test/empty-repo-for-searchtest";
72
73 File emptyRepo = new File(getBasedir(), repoPath);
74
75 if (emptyRepo.exists()) {
76 FileUtils.deleteDirectory(emptyRepo);
77 }
78
79 emptyRepo.mkdirs();
80
81
82 createIndex(repoPath, repoPath, INDEX_ID2);
83
84 createIndex("src/test/repo-with-osgi", "target/test/repo-with-osgi/", INDEX_ID1);
85
86 try {
87 BooleanQuery q = new BooleanQuery.Builder()
88 .add(
89 nexusIndexer.constructQuery(
90 OSGI.SYMBOLIC_NAME,
91 new StringSearchExpression("org.apache.karaf.features.command")),
92 Occur.MUST)
93 .build();
94
95 FlatSearchRequest request = new FlatSearchRequest(q);
96 assertEquals(2, nexusIndexer.getIndexingContexts().values().size());
97 request.setContexts(Arrays.asList(
98 nexusIndexer.getIndexingContexts().get(INDEX_ID2),
99 nexusIndexer.getIndexingContexts().get(INDEX_ID1)));
100
101 FlatSearchResponse response = nexusIndexer.searchFlat(request);
102
103 assertEquals(1, response.getResults().size());
104
105 q = new BooleanQuery.Builder()
106 .add(
107 nexusIndexer.constructQuery(
108 OSGI.SYMBOLIC_NAME, new StringSearchExpression("org.apache.karaf.features.core")),
109 Occur.MUST)
110 .build();
111
112 request = new FlatSearchRequest(q);
113 request.setContexts(
114 new ArrayList<>(nexusIndexer.getIndexingContexts().values()));
115
116 response = nexusIndexer.searchFlat(request);
117
118 assertEquals(3, response.getResults().size());
119
120 String term = "org.apache.karaf.features";
121
122 q = new BooleanQuery.Builder()
123 .add(nexusIndexer.constructQuery(MAVEN.GROUP_ID, new StringSearchExpression(term)), Occur.SHOULD)
124 .add(nexusIndexer.constructQuery(MAVEN.ARTIFACT_ID, new StringSearchExpression(term)), Occur.SHOULD)
125 .add(nexusIndexer.constructQuery(MAVEN.VERSION, new StringSearchExpression(term)), Occur.SHOULD)
126 .add(nexusIndexer.constructQuery(MAVEN.PACKAGING, new StringSearchExpression(term)), Occur.SHOULD)
127 .add(nexusIndexer.constructQuery(MAVEN.CLASSNAMES, new StringSearchExpression(term)), Occur.SHOULD)
128 .build();
129
130 request = new FlatSearchRequest(q);
131 request.setContexts(
132 new ArrayList<>(nexusIndexer.getIndexingContexts().values()));
133
134 response = nexusIndexer.searchFlat(request);
135
136 System.out.println(
137 " result size with term usage " + response.getResults().size());
138
139 assertEquals(4, response.getResults().size());
140
141 } finally {
142 closeAllIndexs();
143 }
144 }
145
146
147
148
149 @Test
150 public void testSearchNoDuplicateArtifactInfo() throws Exception {
151
152 String repoPathIndex = "target/test/repo-for-searchdupe";
153
154 File emptyRepo = new File(getBasedir(), repoPathIndex);
155
156 if (emptyRepo.exists()) {
157 FileUtils.deleteDirectory(emptyRepo);
158 }
159
160 emptyRepo.mkdirs();
161
162
163 createIndex("/src/test/repo", repoPathIndex, INDEX_ID2);
164
165 createIndex("src/test/repo-with-osgi", "target/test/repo-with-osgi/", INDEX_ID1);
166
167 try {
168 BooleanQuery q = new BooleanQuery.Builder()
169 .add(
170 nexusIndexer.constructQuery(MAVEN.GROUP_ID, new StringSearchExpression("commons-cli")),
171 Occur.MUST)
172 .add(nexusIndexer.constructQuery(MAVEN.PACKAGING, new StringSearchExpression("jar")), Occur.MUST)
173 .add(
174 nexusIndexer.constructQuery(MAVEN.CLASSIFIER, new StringSearchExpression("sources")),
175 Occur.MUST)
176 .build();
177
178 FlatSearchRequest request = new FlatSearchRequest(q);
179 assertEquals(2, nexusIndexer.getIndexingContexts().values().size());
180 request.setContexts(Arrays.asList(
181 nexusIndexer.getIndexingContexts().get(INDEX_ID2),
182 nexusIndexer.getIndexingContexts().get(INDEX_ID1)));
183
184 FlatSearchResponse response = nexusIndexer.searchFlat(request);
185
186 assertEquals(1, response.getResults().size());
187
188 } finally {
189 closeAllIndexs();
190 }
191 }
192
193 private void closeAllIndexs() throws Exception {
194 for (IndexingContext context : nexusIndexer.getIndexingContexts().values()) {
195 context.close(true);
196 }
197 }
198
199 private void createIndex(String filePath, String repoIndex, String contextId) throws Exception {
200
201 File repo = new File(getBasedir(), filePath);
202
203 File repoIndexDir = new File(getBasedir(), repoIndex + "/.index");
204
205 if (repoIndexDir.exists()) {
206 FileUtils.deleteDirectory(repoIndexDir);
207 }
208
209 repoIndexDir.mkdirs();
210
211 System.out.println("creating Index with id " + contextId + " path : " + filePath + " , indexPath " + repoIndex);
212
213 IndexingContext indexingContext = nexusIndexer.addIndexingContext(
214 contextId,
215 contextId,
216 repo,
217 repoIndexDir,
218 "http://www.apache.org",
219 "http://www.apache.org/.index",
220 indexCreators);
221 indexingContext.setSearchable(true);
222 nexusIndexer.scan(indexingContext, false);
223
224 indexingContext.optimize();
225
226 File managedRepository = new File(repoIndex);
227 final IndexSearcher indexSearcher = indexingContext.acquireIndexSearcher();
228 try {
229 final File indexLocation = new File(managedRepository, ".index");
230 IndexPackingRequest request =
231 new IndexPackingRequest(indexingContext, indexSearcher.getIndexReader(), indexLocation);
232 indexPacker.packIndex(request);
233 } finally {
234 indexingContext.releaseIndexSearcher(indexSearcher);
235 }
236 }
237 }