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.nio.file.Files;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Date;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.Set;
29
30 import org.apache.lucene.index.Term;
31 import org.apache.lucene.search.BooleanClause.Occur;
32 import org.apache.lucene.search.BooleanQuery;
33 import org.apache.lucene.search.IndexSearcher;
34 import org.apache.lucene.search.PrefixQuery;
35 import org.apache.lucene.search.Query;
36 import org.apache.lucene.search.TermQuery;
37 import org.apache.lucene.store.Directory;
38 import org.apache.lucene.store.FSDirectory;
39 import org.apache.maven.index.context.IndexingContext;
40 import org.apache.maven.index.packer.IndexPacker;
41 import org.apache.maven.index.packer.IndexPackingRequest;
42 import org.apache.maven.index.updater.DefaultIndexUpdater;
43 import org.apache.maven.index.updater.IndexUpdateRequest;
44 import org.apache.maven.index.updater.IndexUpdater;
45 import org.junit.Test;
46
47 import static org.junit.Assert.assertEquals;
48 import static org.junit.Assert.assertFalse;
49 import static org.junit.Assert.assertNotNull;
50 import static org.junit.Assert.assertNull;
51
52 public class DefaultIndexNexusIndexerTest extends MinimalIndexNexusIndexerTest {
53 @Override
54 protected void prepareNexusIndexer(NexusIndexer nexusIndexer) throws Exception {
55 context = nexusIndexer.addIndexingContext("test-default", "test", repo, indexDir, null, null, DEFAULT_CREATORS);
56
57 assertNull(context.getTimestamp());
58
59 nexusIndexer.scan(context);
60
61 assertNotNull(context.getTimestamp());
62 }
63
64 @Test
65 public void testPlugin() throws Exception {
66 Query query = new BooleanQuery.Builder()
67 .add(new TermQuery(new Term(ArtifactInfo.PACKAGING, "maven-plugin")), Occur.MUST)
68 .add(new PrefixQuery(new Term(ArtifactInfo.GROUP_ID, "org.apache.maven.plugins")), Occur.FILTER)
69 .build();
70
71 FlatSearchResponse response = nexusIndexer.searchFlat(new FlatSearchRequest(query));
72
73 Collection<ArtifactInfo> r = response.getResults();
74
75 assertEquals(r.toString(), 1, r.size());
76
77 ArtifactInfo ai = r.iterator().next();
78
79 assertEquals("org.apache.maven.plugins", ai.getGroupId());
80 assertEquals("maven-core-it-plugin", ai.getArtifactId());
81 assertEquals("core-it", ai.getPrefix());
82
83 List<String> goals = ai.getGoals();
84 assertEquals(14, goals.size());
85 assertEquals("catch", goals.get(0));
86 assertEquals("fork", goals.get(1));
87 assertEquals("fork-goal", goals.get(2));
88 assertEquals("touch", goals.get(3));
89 assertEquals("setter-touch", goals.get(4));
90 assertEquals("generate-envar-properties", goals.get(5));
91 assertEquals("generate-properties", goals.get(6));
92 assertEquals("loadable", goals.get(7));
93 assertEquals("light-touch", goals.get(8));
94 assertEquals("package", goals.get(9));
95 assertEquals("reachable", goals.get(10));
96 assertEquals("runnable", goals.get(11));
97 assertEquals("throw", goals.get(12));
98 assertEquals("tricky-params", goals.get(13));
99 }
100
101 @Test
102 public void testPluginPackaging() throws Exception {
103 Query query = new TermQuery(new Term(ArtifactInfo.PACKAGING, "maven-plugin"));
104 FlatSearchResponse response = nexusIndexer.searchFlat(new FlatSearchRequest(query));
105
106 assertEquals(response.getResults().toString(), 2, response.getTotalHits());
107 }
108
109 @Test
110 public void testSearchArchetypes() throws Exception {
111 Query q = new TermQuery(new Term(ArtifactInfo.PACKAGING, "maven-archetype"));
112 FlatSearchResponse response = nexusIndexer.searchFlat(new FlatSearchRequest(q));
113 Collection<ArtifactInfo> r = response.getResults();
114
115 assertEquals(4, r.size());
116
117 Iterator<ArtifactInfo> it = r.iterator();
118 {
119 ArtifactInfo ai = it.next();
120 assertEquals("org.apache.directory.server", ai.getGroupId());
121 assertEquals("apacheds-schema-archetype", ai.getArtifactId());
122 assertEquals("1.0.2", ai.getVersion());
123 }
124 {
125 ArtifactInfo ai = it.next();
126 assertEquals("org.apache.servicemix.tooling", ai.getGroupId());
127 assertEquals("servicemix-service-engine", ai.getArtifactId());
128 assertEquals("3.1", ai.getVersion());
129 }
130 {
131 ArtifactInfo ai = it.next();
132 assertEquals("org.terracotta.maven.archetypes", ai.getGroupId());
133 assertEquals("pojo-archetype", ai.getArtifactId());
134 assertEquals("1.0.3", ai.getVersion());
135 }
136 {
137 ArtifactInfo ai = it.next();
138 assertEquals("proptest", ai.getGroupId());
139 assertEquals("proptest-archetype", ai.getArtifactId());
140 assertEquals("1.0", ai.getVersion());
141 }
142 }
143
144 @Test
145 public void testIndexTimestamp() throws Exception {
146 final File targetDir = Files.createTempDirectory("testIndexTimestamp").toFile();
147 targetDir.deleteOnExit();
148
149 final IndexPacker indexPacker = lookup(IndexPacker.class);
150 final IndexSearcher indexSearcher = context.acquireIndexSearcher();
151 try {
152 final IndexPackingRequest request =
153 new IndexPackingRequest(context, indexSearcher.getIndexReader(), targetDir);
154 indexPacker.packIndex(request);
155 } finally {
156 context.releaseIndexSearcher(indexSearcher);
157 }
158
159 Thread.sleep(1000L);
160
161 File newIndex = new File(getBasedir(), "target/test-new");
162
163 Directory newIndexDir = FSDirectory.open(newIndex.toPath());
164
165 IndexingContext newContext =
166 nexusIndexer.addIndexingContext("test-new", "test", null, newIndexDir, null, null, DEFAULT_CREATORS);
167
168 final IndexUpdater indexUpdater = lookup(IndexUpdater.class);
169 indexUpdater.fetchAndUpdateIndex(
170 new IndexUpdateRequest(newContext, new DefaultIndexUpdater.FileFetcher(targetDir)));
171
172 assertEquals(context.getTimestamp().getTime(), newContext.getTimestamp().getTime());
173
174 assertEquals(context.getTimestamp(), newContext.getTimestamp());
175
176
177
178 Query query = nexusIndexer.constructQuery(MAVEN.GROUP_ID, "qdox", SearchType.SCORED);
179
180 FlatSearchRequest request = new FlatSearchRequest(query, newContext);
181 FlatSearchResponse response = nexusIndexer.searchFlat(request);
182 Collection<ArtifactInfo> r = response.getResults();
183
184 System.out.println(r);
185
186 assertEquals(2, r.size());
187
188 List<ArtifactInfo> list = new ArrayList<>(r);
189
190 assertEquals(2, list.size());
191
192 ArtifactInfo ai = list.get(0);
193
194 assertEquals("1.6.1", ai.getVersion());
195
196 ai = list.get(1);
197
198 assertEquals("1.5", ai.getVersion());
199
200 assertEquals("test", ai.getRepository());
201
202 Date timestamp = newContext.getTimestamp();
203
204 newContext.close(false);
205
206 newIndexDir = FSDirectory.open(newIndex.toPath());
207
208 newContext =
209 nexusIndexer.addIndexingContext("test-new", "test", null, newIndexDir, null, null, DEFAULT_CREATORS);
210
211 indexUpdater.fetchAndUpdateIndex(
212 new IndexUpdateRequest(newContext, new DefaultIndexUpdater.FileFetcher(targetDir)));
213
214 assertEquals(timestamp, newContext.getTimestamp());
215
216 newContext.close(true);
217
218 assertFalse(new File(newIndex, "timestamp").exists());
219 }
220
221 @Test
222 public void testArchetype() throws Exception {
223 Query query = new BooleanQuery.Builder()
224 .add(new TermQuery(new Term(ArtifactInfo.PACKAGING, "maven-archetype")), Occur.MUST)
225 .add(new PrefixQuery(new Term(ArtifactInfo.GROUP_ID, "proptest")), Occur.FILTER)
226 .build();
227
228 FlatSearchResponse response = nexusIndexer.searchFlat(new FlatSearchRequest(query));
229
230 Collection<ArtifactInfo> r = response.getResults();
231
232 assertEquals(r.toString(), 1, r.size());
233 }
234
235 @Test
236 public void testArchetypePackaging() throws Exception {
237 Query query = new TermQuery(new Term(ArtifactInfo.PACKAGING, "maven-archetype"));
238 FlatSearchResponse response = nexusIndexer.searchFlat(new FlatSearchRequest(query));
239 assertEquals(response.getResults().toString(), 4, response.getTotalHits());
240 }
241
242 @Test
243 public void testBrokenJar() throws Exception {
244 Query q = nexusIndexer.constructQuery(MAVEN.ARTIFACT_ID, "brokenjar", SearchType.SCORED);
245
246 FlatSearchRequest searchRequest = new FlatSearchRequest(q);
247
248 FlatSearchResponse response = nexusIndexer.searchFlat(searchRequest);
249
250 Set<ArtifactInfo> r = response.getResults();
251
252 assertEquals(r.toString(), 1, r.size());
253
254 ArtifactInfo ai = r.iterator().next();
255
256 assertEquals("brokenjar", ai.getGroupId());
257 assertEquals("brokenjar", ai.getArtifactId());
258 assertEquals("1.0", ai.getVersion());
259 assertNull(ai.getClassNames());
260 }
261
262 @Test
263 public void testMissingPom() throws Exception {
264 Query q = nexusIndexer.constructQuery(MAVEN.ARTIFACT_ID, "missingpom", SearchType.SCORED);
265
266 FlatSearchRequest searchRequest = new FlatSearchRequest(q);
267
268 FlatSearchResponse response = nexusIndexer.searchFlat(searchRequest);
269
270 Set<ArtifactInfo> r = response.getResults();
271
272 assertEquals(r.toString(), 1, r.size());
273
274 ArtifactInfo ai = r.iterator().next();
275
276 assertEquals("missingpom", ai.getGroupId());
277 assertEquals("missingpom", ai.getArtifactId());
278 assertEquals("1.0", ai.getVersion());
279
280 assertNull(ai.getClassNames());
281 }
282 }