1 package org.apache.maven.archiva.indexer.lucene;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.commons.io.FileUtils;
23 import org.apache.lucene.index.Term;
24 import org.apache.lucene.queryParser.ParseException;
25 import org.apache.lucene.queryParser.QueryParser;
26 import org.apache.lucene.search.BooleanClause;
27 import org.apache.lucene.search.BooleanQuery;
28 import org.apache.lucene.search.MatchAllDocsQuery;
29 import org.apache.lucene.search.Query;
30 import org.apache.lucene.search.TermQuery;
31 import org.apache.maven.archiva.indexer.RepositoryArtifactIndex;
32 import org.apache.maven.archiva.indexer.RepositoryArtifactIndexFactory;
33 import org.apache.maven.archiva.indexer.RepositoryIndexSearchException;
34 import org.apache.maven.archiva.indexer.record.RepositoryIndexRecordFactory;
35 import org.apache.maven.archiva.indexer.record.StandardIndexRecordFields;
36 import org.apache.maven.artifact.Artifact;
37 import org.apache.maven.artifact.factory.ArtifactFactory;
38 import org.apache.maven.artifact.repository.ArtifactRepository;
39 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
40 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
41 import org.apache.maven.artifact.versioning.VersionRange;
42 import org.codehaus.plexus.PlexusTestCase;
45 import java.util.HashMap;
46 import java.util.List;
50 * Test the Lucene implementation of the artifact index search.
52 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
53 * @todo would be nice to abstract some of the query away, but for now passing in a Lucene query directly is good enough
55 public class LuceneStandardArtifactIndexSearchTest
56 extends PlexusTestCase
58 private RepositoryArtifactIndex index;
60 private ArtifactRepository repository;
62 private ArtifactFactory artifactFactory;
64 private File indexLocation;
66 private RepositoryIndexRecordFactory recordFactory;
68 private Map records = new HashMap();
70 protected void setUp()
75 recordFactory = (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "standard" );
77 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
79 ArtifactRepositoryFactory repositoryFactory =
80 (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
82 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
84 File file = getTestFile( "src/test/managed-repository" );
86 repositoryFactory.createArtifactRepository( "test", file.toURI().toURL().toString(), layout, null, null );
88 RepositoryArtifactIndexFactory factory =
89 (RepositoryArtifactIndexFactory) lookup( RepositoryArtifactIndexFactory.ROLE, "lucene" );
91 indexLocation = getTestFile( "target/test-index" );
93 FileUtils.deleteDirectory( indexLocation );
95 index = factory.createStandardIndex( indexLocation );
97 records.put( "test-jar", recordFactory.createRecord( createArtifact( "test-jar" ) ) );
98 records.put( "test-jar-jdk14",
99 recordFactory.createRecord( createArtifact( "test-jar", "1.0", "jar", "jdk14" ) ) );
100 records.put( "test-jar-and-pom",
101 recordFactory.createRecord( createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar" ) ) );
102 records.put( "test-jar-and-pom-jdk14", recordFactory.createRecord(
103 createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar", "jdk14" ) ) );
104 records.put( "test-child-pom",
105 recordFactory.createRecord( createArtifact( "test-child-pom", "1.0-20060728.121314-1", "jar" ) ) );
106 records.put( "test-archetype", recordFactory.createRecord( createArtifact( "test-archetype" ) ) );
107 records.put( "test-plugin", recordFactory.createRecord( createArtifact( "test-plugin" ) ) );
108 records.put( "test-pom", recordFactory.createRecord( createArtifact( "test-pom", "1.0", "pom" ) ) );
109 records.put( "parent-pom", recordFactory.createRecord( createArtifact( "parent-pom", "1", "pom" ) ) );
110 records.put( "test-dll", recordFactory.createRecord( createArtifact( "test-dll", "1.0.1.34", "dll" ) ) );
112 index.indexRecords( records.values() );
115 public void testExactMatchVersion()
116 throws RepositoryIndexSearchException
118 Query query = createExactMatchQuery( StandardIndexRecordFields.VERSION_EXACT, "1.0" );
119 List results = index.search( new LuceneQuery( query ) );
121 assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) );
122 assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
123 assertTrue( "Check result", results.contains( records.get( "test-pom" ) ) );
124 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
125 assertTrue( "Check result", results.contains( records.get( "test-archetype" ) ) );
126 assertEquals( "Check results size", 5, results.size() );
128 query = createExactMatchQuery( StandardIndexRecordFields.VERSION_EXACT, "1.0-SNAPSHOT" );
129 results = index.search( new LuceneQuery( query ) );
131 assertTrue( "Check results size", results.isEmpty() );
133 query = createExactMatchQuery( StandardIndexRecordFields.VERSION_EXACT, "1.0-snapshot" );
134 results = index.search( new LuceneQuery( query ) );
136 assertTrue( "Check results size", results.isEmpty() );
138 query = createExactMatchQuery( StandardIndexRecordFields.VERSION_EXACT, "1.0-20060728.121314-1" );
139 results = index.search( new LuceneQuery( query ) );
141 assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
142 assertEquals( "Check results size", 1, results.size() );
144 // test non-match fails
145 query = createExactMatchQuery( StandardIndexRecordFields.VERSION_EXACT, "foo" );
146 results = index.search( new LuceneQuery( query ) );
148 assertTrue( "Check results size", results.isEmpty() );
151 public void testExactMatchBaseVersion()
152 throws RepositoryIndexSearchException
154 Query query = createExactMatchQuery( StandardIndexRecordFields.BASE_VERSION_EXACT, "1.0" );
155 List results = index.search( new LuceneQuery( query ) );
157 assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) );
158 assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
159 assertTrue( "Check result", results.contains( records.get( "test-pom" ) ) );
160 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
161 assertTrue( "Check result", results.contains( records.get( "test-archetype" ) ) );
162 assertEquals( "Check results size", 5, results.size() );
164 query = createExactMatchQuery( StandardIndexRecordFields.BASE_VERSION_EXACT, "1.0-SNAPSHOT" );
165 results = index.search( new LuceneQuery( query ) );
167 assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
168 assertEquals( "Check results size", 1, results.size() );
170 query = createExactMatchQuery( StandardIndexRecordFields.BASE_VERSION_EXACT, "1.0-snapshot" );
171 results = index.search( new LuceneQuery( query ) );
173 assertTrue( "Check results size", results.isEmpty() );
175 query = createExactMatchQuery( StandardIndexRecordFields.BASE_VERSION_EXACT, "1.0-20060728.121314-1" );
176 results = index.search( new LuceneQuery( query ) );
178 assertTrue( "Check results size", results.isEmpty() );
180 // test non-match fails
181 query = createExactMatchQuery( StandardIndexRecordFields.BASE_VERSION_EXACT, "foo" );
182 results = index.search( new LuceneQuery( query ) );
184 assertTrue( "Check results size", results.isEmpty() );
187 public void testExactMatchGroupId()
188 throws RepositoryIndexSearchException
191 createExactMatchQuery( StandardIndexRecordFields.GROUPID_EXACT, "org.apache.maven.archiva.record" );
192 List results = index.search( new LuceneQuery( query ) );
194 assertEquals( "Check results size", 10, results.size() );
196 // test partial match fails
197 query = createExactMatchQuery( StandardIndexRecordFields.GROUPID_EXACT, "org.apache.maven" );
198 results = index.search( new LuceneQuery( query ) );
200 assertTrue( "Check results size", results.isEmpty() );
202 // test non-match fails
203 query = createExactMatchQuery( StandardIndexRecordFields.GROUPID_EXACT, "foo" );
204 results = index.search( new LuceneQuery( query ) );
206 assertTrue( "Check results size", results.isEmpty() );
209 public void testExactMatchArtifactId()
210 throws RepositoryIndexSearchException
212 Query query = createExactMatchQuery( StandardIndexRecordFields.ARTIFACTID_EXACT, "test-jar" );
213 List results = index.search( new LuceneQuery( query ) );
215 assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) );
216 assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
217 assertEquals( "Check results size", 2, results.size() );
219 // test partial match fails
220 query = createExactMatchQuery( StandardIndexRecordFields.ARTIFACTID_EXACT, "test" );
221 results = index.search( new LuceneQuery( query ) );
223 assertTrue( "Check results size", results.isEmpty() );
225 // test non-match fails
226 query = createExactMatchQuery( StandardIndexRecordFields.ARTIFACTID_EXACT, "foo" );
227 results = index.search( new LuceneQuery( query ) );
229 assertTrue( "Check results size", results.isEmpty() );
232 public void testExactMatchType()
233 throws RepositoryIndexSearchException
235 Query query = createExactMatchQuery( StandardIndexRecordFields.TYPE, "maven-plugin" );
236 List results = index.search( new LuceneQuery( query ) );
238 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
239 assertEquals( "Check results size", 1, results.size() );
241 query = createExactMatchQuery( StandardIndexRecordFields.TYPE, "jar" );
242 results = index.search( new LuceneQuery( query ) );
244 assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) );
245 assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
246 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
247 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
248 assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
249 assertEquals( "Check results size", 5, results.size() );
251 query = createExactMatchQuery( StandardIndexRecordFields.TYPE, "dll" );
252 results = index.search( new LuceneQuery( query ) );
254 assertTrue( "Check result", results.contains( records.get( "test-dll" ) ) );
255 assertEquals( "Check results size", 1, results.size() );
257 query = createExactMatchQuery( StandardIndexRecordFields.TYPE, "maven-archetype" );
258 results = index.search( new LuceneQuery( query ) );
260 assertTrue( "Check result", results.contains( records.get( "test-archetype" ) ) );
261 assertEquals( "Check results size", 1, results.size() );
263 // test non-match fails
264 query = createExactMatchQuery( StandardIndexRecordFields.TYPE, "foo" );
265 results = index.search( new LuceneQuery( query ) );
267 assertTrue( "Check results size", results.isEmpty() );
270 public void testExactMatchPackaging()
271 throws RepositoryIndexSearchException
273 Query query = createExactMatchQuery( StandardIndexRecordFields.PACKAGING, "maven-plugin" );
274 List results = index.search( new LuceneQuery( query ) );
276 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
277 assertEquals( "Check results size", 1, results.size() );
279 query = createExactMatchQuery( StandardIndexRecordFields.PACKAGING, "jar" );
280 results = index.search( new LuceneQuery( query ) );
282 assertTrue( "Check result", results.contains( records.get( "test-archetype" ) ) );
283 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
284 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
285 assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
286 assertEquals( "Check results size", 4, results.size() );
288 query = createExactMatchQuery( StandardIndexRecordFields.PACKAGING, "dll" );
289 results = index.search( new LuceneQuery( query ) );
291 assertTrue( "Check results size", results.isEmpty() );
293 query = createExactMatchQuery( StandardIndexRecordFields.PACKAGING, "maven-archetype" );
294 results = index.search( new LuceneQuery( query ) );
296 assertTrue( "Check results size", results.isEmpty() );
298 // test non-match fails
299 query = createExactMatchQuery( StandardIndexRecordFields.PACKAGING, "foo" );
300 results = index.search( new LuceneQuery( query ) );
302 assertTrue( "Check results size", results.isEmpty() );
305 public void testExactMatchPluginPrefix()
306 throws RepositoryIndexSearchException
308 Query query = createExactMatchQuery( StandardIndexRecordFields.PLUGIN_PREFIX, "test" );
309 List results = index.search( new LuceneQuery( query ) );
311 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
312 assertEquals( "Check results size", 1, results.size() );
314 // test non-match fails
315 query = createExactMatchQuery( StandardIndexRecordFields.PLUGIN_PREFIX, "foo" );
316 results = index.search( new LuceneQuery( query ) );
318 assertTrue( "Check results size", results.isEmpty() );
321 public void testExactMatchRepository()
322 throws RepositoryIndexSearchException
324 Query query = createExactMatchQuery( StandardIndexRecordFields.REPOSITORY, "test" );
325 List results = index.search( new LuceneQuery( query ) );
327 assertEquals( "Check results size", 10, results.size() );
329 // test non-match fails
330 query = createExactMatchQuery( StandardIndexRecordFields.REPOSITORY, "foo" );
331 results = index.search( new LuceneQuery( query ) );
333 assertTrue( "Check results size", results.isEmpty() );
336 public void testExactMatchMd5()
337 throws RepositoryIndexSearchException
339 Query query = createExactMatchQuery( StandardIndexRecordFields.MD5, "3a0adc365f849366cd8b633cad155cb7" );
340 List results = index.search( new LuceneQuery( query ) );
342 assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) );
343 assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
344 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
345 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
346 assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
347 assertEquals( "Check results size", 5, results.size() );
349 // test non-match fails
350 query = createExactMatchQuery( StandardIndexRecordFields.MD5, "foo" );
351 results = index.search( new LuceneQuery( query ) );
353 assertTrue( "Check results size", results.isEmpty() );
356 public void testExactMatchSha1()
357 throws RepositoryIndexSearchException
360 createExactMatchQuery( StandardIndexRecordFields.SHA1, "c66f18bf192cb613fc2febb4da541a34133eedc2" );
361 List results = index.search( new LuceneQuery( query ) );
363 assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) );
364 assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
365 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
366 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
367 assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
368 assertEquals( "Check results size", 5, results.size() );
370 // test non-match fails
371 query = createExactMatchQuery( StandardIndexRecordFields.SHA1, "foo" );
372 results = index.search( new LuceneQuery( query ) );
374 assertTrue( "Check results size", results.isEmpty() );
377 public void testExactMatchInceptionYear()
378 throws RepositoryIndexSearchException
380 Query query = createExactMatchQuery( StandardIndexRecordFields.INCEPTION_YEAR, "2005" );
381 List results = index.search( new LuceneQuery( query ) );
383 assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
384 assertTrue( "Check result", results.contains( records.get( "test-pom" ) ) );
385 assertTrue( "Check result", results.contains( records.get( "parent-pom" ) ) );
386 assertEquals( "Check results size", 3, results.size() );
388 // test non-match fails
389 query = createExactMatchQuery( StandardIndexRecordFields.INCEPTION_YEAR, "foo" );
390 results = index.search( new LuceneQuery( query ) );
392 assertTrue( "Check results size", results.isEmpty() );
395 public void testMatchFilename()
396 throws RepositoryIndexSearchException, ParseException
398 Query query = createMatchQuery( StandardIndexRecordFields.FILENAME, "maven" );
399 List results = index.search( new LuceneQuery( query ) );
401 assertEquals( "Check results size", 10, results.size() );
403 query = createMatchQuery( StandardIndexRecordFields.FILENAME, "plugin" );
404 results = index.search( new LuceneQuery( query ) );
406 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
407 assertEquals( "Check results size", 1, results.size() );
409 query = createMatchQuery( StandardIndexRecordFields.FILENAME, "pLuGiN" );
410 results = index.search( new LuceneQuery( query ) );
412 assertTrue( "Check results size", results.isEmpty() );
414 query = createMatchQuery( StandardIndexRecordFields.FILENAME, "test" );
415 results = index.search( new LuceneQuery( query ) );
417 assertFalse( "Check result", results.contains( records.get( "parent-pom" ) ) );
418 assertEquals( "Check results size", 9, results.size() );
420 // test non-match fails
421 query = createMatchQuery( StandardIndexRecordFields.FILENAME, "foo" );
422 results = index.search( new LuceneQuery( query ) );
424 assertTrue( "Check results size", results.isEmpty() );
427 public void testMatchGroupId()
428 throws RepositoryIndexSearchException, ParseException
430 Query query = createMatchQuery( StandardIndexRecordFields.GROUPID, "org.apache.maven.archiva.record" );
431 List results = index.search( new LuceneQuery( query ) );
433 assertEquals( "Check results size", 10, results.size() );
435 query = createMatchQuery( StandardIndexRecordFields.GROUPID, "maven" );
436 results = index.search( new LuceneQuery( query ) );
438 assertEquals( "Check results size", 10, results.size() );
440 query = createMatchQuery( StandardIndexRecordFields.GROUPID, "Maven" );
441 results = index.search( new LuceneQuery( query ) );
443 assertEquals( "Check results size", 10, results.size() );
445 // test non-match fails
446 query = createMatchQuery( StandardIndexRecordFields.GROUPID, "foo" );
447 results = index.search( new LuceneQuery( query ) );
449 assertTrue( "Check results size", results.isEmpty() );
452 public void testMatchArtifactId()
453 throws RepositoryIndexSearchException, ParseException
455 Query query = createMatchQuery( StandardIndexRecordFields.ARTIFACTID, "plugin" );
456 List results = index.search( new LuceneQuery( query ) );
458 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
459 assertEquals( "Check results size", 1, results.size() );
461 query = createMatchQuery( StandardIndexRecordFields.ARTIFACTID, "test" );
462 results = index.search( new LuceneQuery( query ) );
464 assertFalse( "Check result", results.contains( records.get( "parent-pom" ) ) );
465 assertEquals( "Check results size", 9, results.size() );
467 // test non-match fails
468 query = createMatchQuery( StandardIndexRecordFields.ARTIFACTID, "maven" );
469 results = index.search( new LuceneQuery( query ) );
471 assertTrue( "Check results size", results.isEmpty() );
474 public void testMatchVersion()
475 throws RepositoryIndexSearchException, ParseException
477 // If partial matches are desired, need to change the analyzer for versions to split on '.'
478 Query query = createMatchQuery( StandardIndexRecordFields.VERSION, "1" );
479 List results = index.search( new LuceneQuery( query ) );
481 assertTrue( "Check result", results.contains( records.get( "parent-pom" ) ) );
482 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
483 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
484 assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
485 assertEquals( "Check results size", 4, results.size() );
487 query = createMatchQuery( StandardIndexRecordFields.VERSION, "1.0" );
488 results = index.search( new LuceneQuery( query ) );
490 assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) );
491 assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
492 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
493 assertTrue( "Check result", results.contains( records.get( "test-pom" ) ) );
494 assertTrue( "Check result", results.contains( records.get( "test-archetype" ) ) );
495 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
496 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
497 assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
498 assertEquals( "Check results size", 8, results.size() );
500 query = createMatchQuery( StandardIndexRecordFields.VERSION, "snapshot" );
501 results = index.search( new LuceneQuery( query ) );
503 assertTrue( "Check results size", results.isEmpty() );
505 query = createMatchQuery( StandardIndexRecordFields.VERSION, "SNAPSHOT" );
506 results = index.search( new LuceneQuery( query ) );
508 assertTrue( "Check results size", results.isEmpty() );
510 query = createMatchQuery( StandardIndexRecordFields.VERSION, "alpha" );
511 results = index.search( new LuceneQuery( query ) );
513 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
514 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
515 assertEquals( "Check results size", 2, results.size() );
517 query = createMatchQuery( StandardIndexRecordFields.VERSION, "1.0-alpha-1" );
518 results = index.search( new LuceneQuery( query ) );
520 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
521 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
522 assertEquals( "Check results size", 2, results.size() );
524 // test non-match fails
525 query = createMatchQuery( StandardIndexRecordFields.VERSION, "foo" );
526 results = index.search( new LuceneQuery( query ) );
528 assertTrue( "Check results size", results.isEmpty() );
531 public void testMatchBaseVersion()
532 throws RepositoryIndexSearchException, ParseException
534 // If partial matches are desired, need to change the analyzer for versions to split on '.'
535 Query query = createMatchQuery( StandardIndexRecordFields.BASE_VERSION, "1" );
536 List results = index.search( new LuceneQuery( query ) );
538 assertTrue( "Check result", results.contains( records.get( "parent-pom" ) ) );
539 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
540 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
541 assertEquals( "Check results size", 3, results.size() );
543 query = createMatchQuery( StandardIndexRecordFields.BASE_VERSION, "1.0" );
544 results = index.search( new LuceneQuery( query ) );
546 assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) );
547 assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
548 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
549 assertTrue( "Check result", results.contains( records.get( "test-pom" ) ) );
550 assertTrue( "Check result", results.contains( records.get( "test-archetype" ) ) );
551 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
552 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
553 assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
554 assertEquals( "Check results size", 8, results.size() );
556 query = createMatchQuery( StandardIndexRecordFields.BASE_VERSION, "SNAPSHOT" );
557 results = index.search( new LuceneQuery( query ) );
559 assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
560 assertEquals( "Check results size", 1, results.size() );
562 query = createMatchQuery( StandardIndexRecordFields.BASE_VERSION, "SnApShOt" );
563 results = index.search( new LuceneQuery( query ) );
565 assertTrue( "Check results size", results.isEmpty() );
567 query = createMatchQuery( StandardIndexRecordFields.BASE_VERSION, "snapshot" );
568 results = index.search( new LuceneQuery( query ) );
570 assertTrue( "Check results size", results.isEmpty() );
572 query = createMatchQuery( StandardIndexRecordFields.BASE_VERSION, "alpha" );
573 results = index.search( new LuceneQuery( query ) );
575 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
576 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
577 assertEquals( "Check results size", 2, results.size() );
579 query = createMatchQuery( StandardIndexRecordFields.BASE_VERSION, "1.0-alpha-1" );
580 results = index.search( new LuceneQuery( query ) );
582 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
583 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
584 assertEquals( "Check results size", 2, results.size() );
586 // test non-match fails
587 query = createMatchQuery( StandardIndexRecordFields.BASE_VERSION, "foo" );
588 results = index.search( new LuceneQuery( query ) );
590 assertTrue( "Check results size", results.isEmpty() );
593 public void testMatchClassifier()
594 throws RepositoryIndexSearchException, ParseException
596 BooleanQuery bQuery = new BooleanQuery();
597 bQuery.add( new MatchAllDocsQuery(), BooleanClause.Occur.MUST );
598 bQuery.add( createMatchQuery( StandardIndexRecordFields.CLASSIFIER, "jdk14" ), BooleanClause.Occur.MUST_NOT );
599 List results = index.search( new LuceneQuery( bQuery ) );
601 assertFalse( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
602 assertFalse( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
603 assertEquals( "Check results size", 8, results.size() );
605 // TODO: can we search for "anything with no classifier" ?
607 Query query = createMatchQuery( StandardIndexRecordFields.CLASSIFIER, "jdk14" );
608 results = index.search( new LuceneQuery( query ) );
610 assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
611 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
612 assertEquals( "Check results size", 2, results.size() );
614 // test non-match fails
615 query = createMatchQuery( StandardIndexRecordFields.CLASSIFIER, "foo" );
616 results = index.search( new LuceneQuery( query ) );
618 assertTrue( "Check results size", results.isEmpty() );
621 public void testMatchClass()
622 throws RepositoryIndexSearchException, ParseException
624 Query query = createMatchQuery( StandardIndexRecordFields.CLASSES, "b.c.C" );
625 List results = index.search( new LuceneQuery( query ) );
627 assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
628 assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) );
629 assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
630 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
631 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
632 assertEquals( "Check results size", 5, results.size() );
634 query = createMatchQuery( StandardIndexRecordFields.CLASSES, "C" );
635 results = index.search( new LuceneQuery( query ) );
637 assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
638 assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) );
639 assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
640 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
641 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
642 assertEquals( "Check results size", 5, results.size() );
644 query = createMatchQuery( StandardIndexRecordFields.CLASSES, "MyMojo" );
645 results = index.search( new LuceneQuery( query ) );
647 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
648 assertEquals( "Check results size", 1, results.size() );
650 query = createMatchQuery( StandardIndexRecordFields.CLASSES, "MYMOJO" );
651 results = index.search( new LuceneQuery( query ) );
653 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
654 assertEquals( "Check results size", 1, results.size() );
656 query = createMatchQuery( StandardIndexRecordFields.CLASSES, "mymojo" );
657 results = index.search( new LuceneQuery( query ) );
659 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
660 assertEquals( "Check results size", 1, results.size() );
662 // test non-match fails
663 query = createMatchQuery( StandardIndexRecordFields.CLASSES, "foo" );
664 results = index.search( new LuceneQuery( query ) );
666 assertTrue( "Check results size", results.isEmpty() );
669 public void testMatchFiles()
670 throws RepositoryIndexSearchException, ParseException
672 Query query = createMatchQuery( StandardIndexRecordFields.FILES, "MANIFEST.MF" );
673 List results = index.search( new LuceneQuery( query ) );
675 assertFalse( "Check result", results.contains( records.get( "test-pom" ) ) );
676 assertFalse( "Check result", results.contains( records.get( "parent-pom" ) ) );
677 assertFalse( "Check result", results.contains( records.get( "test-dll" ) ) );
678 assertEquals( "Check results size", 7, results.size() );
680 query = createMatchQuery( StandardIndexRecordFields.FILES, "META-INF" );
681 results = index.search( new LuceneQuery( query ) );
683 assertFalse( "Check result", results.contains( records.get( "test-pom" ) ) );
684 assertFalse( "Check result", results.contains( records.get( "parent-pom" ) ) );
685 assertFalse( "Check result", results.contains( records.get( "test-dll" ) ) );
686 assertEquals( "Check results size", 7, results.size() );
688 query = createMatchQuery( StandardIndexRecordFields.FILES, "plugin.xml" );
689 results = index.search( new LuceneQuery( query ) );
691 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
692 assertEquals( "Check results size", 1, results.size() );
694 // test non-match fails
695 query = createMatchQuery( StandardIndexRecordFields.FILES, "foo" );
696 results = index.search( new LuceneQuery( query ) );
698 assertTrue( "Check results size", results.isEmpty() );
701 public void testExactMatchDependency()
702 throws RepositoryIndexSearchException
705 createExactMatchQuery( StandardIndexRecordFields.DEPENDENCIES, "org.apache.maven:maven-plugin-api:2.0" );
706 List results = index.search( new LuceneQuery( query ) );
708 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
709 assertEquals( "Check results size", 1, results.size() );
711 // test non-match fails
712 query = createExactMatchQuery( StandardIndexRecordFields.DEPENDENCIES, "foo" );
713 results = index.search( new LuceneQuery( query ) );
715 assertTrue( "Check results size", results.isEmpty() );
718 public void testMatchProjectName()
719 throws RepositoryIndexSearchException, ParseException
721 Query query = createMatchQuery( StandardIndexRecordFields.PROJECT_NAME, "mojo" );
722 List results = index.search( new LuceneQuery( query ) );
724 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
725 assertEquals( "Check results size", 1, results.size() );
727 query = createMatchQuery( StandardIndexRecordFields.PROJECT_NAME, "maven" );
728 results = index.search( new LuceneQuery( query ) );
730 assertFalse( "Check result", results.contains( records.get( "parent-pom" ) ) );
731 assertFalse( "Check result", results.contains( records.get( "test-child-pom" ) ) );
732 assertEquals( "Check results size", 2, results.size() );
734 // test non-match fails
735 query = createMatchQuery( StandardIndexRecordFields.PROJECT_NAME, "foo" );
736 results = index.search( new LuceneQuery( query ) );
738 assertTrue( "Check results size", results.isEmpty() );
741 public void testMatchProjectDescription()
742 throws RepositoryIndexSearchException, ParseException
744 Query query = createMatchQuery( StandardIndexRecordFields.PROJECT_DESCRIPTION, "description" );
745 List results = index.search( new LuceneQuery( query ) );
747 assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
748 assertTrue( "Check result", results.contains( records.get( "parent-pom" ) ) );
749 assertTrue( "Check result", results.contains( records.get( "test-pom" ) ) );
750 assertEquals( "Check results size", 3, results.size() );
752 // test non-match fails
753 query = createMatchQuery( StandardIndexRecordFields.PROJECT_DESCRIPTION, "foo" );
754 results = index.search( new LuceneQuery( query ) );
756 assertTrue( "Check results size", results.isEmpty() );
759 private static Query createExactMatchQuery( String field, String value )
761 return new TermQuery( new Term( field, value ) );
764 private static Query createMatchQuery( String field, String value )
765 throws ParseException
767 return new QueryParser( field, LuceneRepositoryArtifactIndex.getAnalyzer() ).parse( value );
770 private Artifact createArtifact( String artifactId )
772 return createArtifact( artifactId, "1.0", "jar", null );
775 private Artifact createArtifact( String artifactId, String version, String type )
777 return createArtifact( artifactId, version, type, null );
780 private Artifact createArtifact( String artifactId, String version, String type, String classifier )
782 Artifact artifact = artifactFactory.createDependencyArtifact( "org.apache.maven.archiva.record", artifactId,
783 VersionRange.createFromVersion( version ), type,
784 classifier, Artifact.SCOPE_RUNTIME );
785 artifact.isSnapshot();
786 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
787 artifact.setRepository( repository );