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