1 package org.apache.maven.archiva.indexer.lucene;
4 * Copyright 2005-2006 The Apache Software Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import org.apache.lucene.index.Term;
20 import org.apache.lucene.queryParser.ParseException;
21 import org.apache.lucene.queryParser.QueryParser;
22 import org.apache.lucene.search.BooleanClause;
23 import org.apache.lucene.search.BooleanQuery;
24 import org.apache.lucene.search.MatchAllDocsQuery;
25 import org.apache.lucene.search.Query;
26 import org.apache.lucene.search.TermQuery;
27 import org.apache.maven.archiva.indexer.RepositoryArtifactIndex;
28 import org.apache.maven.archiva.indexer.RepositoryArtifactIndexFactory;
29 import org.apache.maven.archiva.indexer.RepositoryIndexSearchException;
30 import org.apache.maven.archiva.indexer.record.RepositoryIndexRecordFactory;
31 import org.apache.maven.archiva.indexer.record.StandardIndexRecordFields;
32 import org.apache.maven.artifact.Artifact;
33 import org.apache.maven.artifact.factory.ArtifactFactory;
34 import org.apache.maven.artifact.repository.ArtifactRepository;
35 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
36 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
37 import org.apache.maven.artifact.versioning.VersionRange;
38 import org.codehaus.plexus.PlexusTestCase;
39 import org.apache.commons.io.FileUtils;
42 import java.util.HashMap;
43 import java.util.List;
47 * Test the Lucene implementation of the artifact index search.
49 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
50 * @todo would be nice to abstract some of the query away, but for now passing in a Lucene query directly is good enough
52 public class LuceneStandardArtifactIndexSearchTest
53 extends PlexusTestCase
55 private RepositoryArtifactIndex index;
57 private ArtifactRepository repository;
59 private ArtifactFactory artifactFactory;
61 private File indexLocation;
63 private RepositoryIndexRecordFactory recordFactory;
65 private Map records = new HashMap();
67 protected void setUp()
72 recordFactory = (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "standard" );
74 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
76 ArtifactRepositoryFactory repositoryFactory =
77 (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
79 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
81 File file = getTestFile( "src/test/managed-repository" );
83 repositoryFactory.createArtifactRepository( "test", file.toURI().toURL().toString(), layout, null, null );
85 RepositoryArtifactIndexFactory factory =
86 (RepositoryArtifactIndexFactory) lookup( RepositoryArtifactIndexFactory.ROLE, "lucene" );
88 indexLocation = getTestFile( "target/test-index" );
90 FileUtils.deleteDirectory( indexLocation );
92 index = factory.createStandardIndex( indexLocation );
94 records.put( "test-jar", recordFactory.createRecord( createArtifact( "test-jar" ) ) );
95 records.put( "test-jar-jdk14",
96 recordFactory.createRecord( createArtifact( "test-jar", "1.0", "jar", "jdk14" ) ) );
97 records.put( "test-jar-and-pom",
98 recordFactory.createRecord( createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar" ) ) );
99 records.put( "test-jar-and-pom-jdk14", recordFactory.createRecord(
100 createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar", "jdk14" ) ) );
101 records.put( "test-child-pom",
102 recordFactory.createRecord( createArtifact( "test-child-pom", "1.0-20060728.121314-1", "jar" ) ) );
103 records.put( "test-archetype", recordFactory.createRecord( createArtifact( "test-archetype" ) ) );
104 records.put( "test-plugin", recordFactory.createRecord( createArtifact( "test-plugin" ) ) );
105 records.put( "test-pom", recordFactory.createRecord( createArtifact( "test-pom", "1.0", "pom" ) ) );
106 records.put( "parent-pom", recordFactory.createRecord( createArtifact( "parent-pom", "1", "pom" ) ) );
107 records.put( "test-dll", recordFactory.createRecord( createArtifact( "test-dll", "1.0.1.34", "dll" ) ) );
109 index.indexRecords( records.values() );
112 public void testExactMatchVersion()
113 throws RepositoryIndexSearchException
115 Query query = createExactMatchQuery( StandardIndexRecordFields.VERSION_EXACT, "1.0" );
116 List results = index.search( new LuceneQuery( query ) );
118 assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) );
119 assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
120 assertTrue( "Check result", results.contains( records.get( "test-pom" ) ) );
121 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
122 assertTrue( "Check result", results.contains( records.get( "test-archetype" ) ) );
123 assertEquals( "Check results size", 5, results.size() );
125 query = createExactMatchQuery( StandardIndexRecordFields.VERSION_EXACT, "1.0-SNAPSHOT" );
126 results = index.search( new LuceneQuery( query ) );
128 assertTrue( "Check results size", results.isEmpty() );
130 query = createExactMatchQuery( StandardIndexRecordFields.VERSION_EXACT, "1.0-snapshot" );
131 results = index.search( new LuceneQuery( query ) );
133 assertTrue( "Check results size", results.isEmpty() );
135 query = createExactMatchQuery( StandardIndexRecordFields.VERSION_EXACT, "1.0-20060728.121314-1" );
136 results = index.search( new LuceneQuery( query ) );
138 assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
139 assertEquals( "Check results size", 1, results.size() );
141 // test non-match fails
142 query = createExactMatchQuery( StandardIndexRecordFields.VERSION_EXACT, "foo" );
143 results = index.search( new LuceneQuery( query ) );
145 assertTrue( "Check results size", results.isEmpty() );
148 public void testExactMatchBaseVersion()
149 throws RepositoryIndexSearchException
151 Query query = createExactMatchQuery( StandardIndexRecordFields.BASE_VERSION_EXACT, "1.0" );
152 List results = index.search( new LuceneQuery( query ) );
154 assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) );
155 assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
156 assertTrue( "Check result", results.contains( records.get( "test-pom" ) ) );
157 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
158 assertTrue( "Check result", results.contains( records.get( "test-archetype" ) ) );
159 assertEquals( "Check results size", 5, results.size() );
161 query = createExactMatchQuery( StandardIndexRecordFields.BASE_VERSION_EXACT, "1.0-SNAPSHOT" );
162 results = index.search( new LuceneQuery( query ) );
164 assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
165 assertEquals( "Check results size", 1, results.size() );
167 query = createExactMatchQuery( StandardIndexRecordFields.BASE_VERSION_EXACT, "1.0-snapshot" );
168 results = index.search( new LuceneQuery( query ) );
170 assertTrue( "Check results size", results.isEmpty() );
172 query = createExactMatchQuery( StandardIndexRecordFields.BASE_VERSION_EXACT, "1.0-20060728.121314-1" );
173 results = index.search( new LuceneQuery( query ) );
175 assertTrue( "Check results size", results.isEmpty() );
177 // test non-match fails
178 query = createExactMatchQuery( StandardIndexRecordFields.BASE_VERSION_EXACT, "foo" );
179 results = index.search( new LuceneQuery( query ) );
181 assertTrue( "Check results size", results.isEmpty() );
184 public void testExactMatchGroupId()
185 throws RepositoryIndexSearchException
188 createExactMatchQuery( StandardIndexRecordFields.GROUPID_EXACT, "org.apache.maven.archiva.record" );
189 List results = index.search( new LuceneQuery( query ) );
191 assertEquals( "Check results size", 10, results.size() );
193 // test partial match fails
194 query = createExactMatchQuery( StandardIndexRecordFields.GROUPID_EXACT, "org.apache.maven" );
195 results = index.search( new LuceneQuery( query ) );
197 assertTrue( "Check results size", results.isEmpty() );
199 // test non-match fails
200 query = createExactMatchQuery( StandardIndexRecordFields.GROUPID_EXACT, "foo" );
201 results = index.search( new LuceneQuery( query ) );
203 assertTrue( "Check results size", results.isEmpty() );
206 public void testExactMatchArtifactId()
207 throws RepositoryIndexSearchException
209 Query query = createExactMatchQuery( StandardIndexRecordFields.ARTIFACTID_EXACT, "test-jar" );
210 List results = index.search( new LuceneQuery( query ) );
212 assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) );
213 assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
214 assertEquals( "Check results size", 2, results.size() );
216 // test partial match fails
217 query = createExactMatchQuery( StandardIndexRecordFields.ARTIFACTID_EXACT, "test" );
218 results = index.search( new LuceneQuery( query ) );
220 assertTrue( "Check results size", results.isEmpty() );
222 // test non-match fails
223 query = createExactMatchQuery( StandardIndexRecordFields.ARTIFACTID_EXACT, "foo" );
224 results = index.search( new LuceneQuery( query ) );
226 assertTrue( "Check results size", results.isEmpty() );
229 public void testExactMatchType()
230 throws RepositoryIndexSearchException
232 Query query = createExactMatchQuery( StandardIndexRecordFields.TYPE, "maven-plugin" );
233 List results = index.search( new LuceneQuery( query ) );
235 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
236 assertEquals( "Check results size", 1, results.size() );
238 query = createExactMatchQuery( StandardIndexRecordFields.TYPE, "jar" );
239 results = index.search( new LuceneQuery( query ) );
241 assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) );
242 assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
243 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
244 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
245 assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
246 assertEquals( "Check results size", 5, results.size() );
248 query = createExactMatchQuery( StandardIndexRecordFields.TYPE, "dll" );
249 results = index.search( new LuceneQuery( query ) );
251 assertTrue( "Check result", results.contains( records.get( "test-dll" ) ) );
252 assertEquals( "Check results size", 1, results.size() );
254 query = createExactMatchQuery( StandardIndexRecordFields.TYPE, "maven-archetype" );
255 results = index.search( new LuceneQuery( query ) );
257 assertTrue( "Check result", results.contains( records.get( "test-archetype" ) ) );
258 assertEquals( "Check results size", 1, results.size() );
260 // test non-match fails
261 query = createExactMatchQuery( StandardIndexRecordFields.TYPE, "foo" );
262 results = index.search( new LuceneQuery( query ) );
264 assertTrue( "Check results size", results.isEmpty() );
267 public void testExactMatchPackaging()
268 throws RepositoryIndexSearchException
270 Query query = createExactMatchQuery( StandardIndexRecordFields.PACKAGING, "maven-plugin" );
271 List results = index.search( new LuceneQuery( query ) );
273 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
274 assertEquals( "Check results size", 1, results.size() );
276 query = createExactMatchQuery( StandardIndexRecordFields.PACKAGING, "jar" );
277 results = index.search( new LuceneQuery( query ) );
279 assertTrue( "Check result", results.contains( records.get( "test-archetype" ) ) );
280 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
281 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
282 assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
283 assertEquals( "Check results size", 4, results.size() );
285 query = createExactMatchQuery( StandardIndexRecordFields.PACKAGING, "dll" );
286 results = index.search( new LuceneQuery( query ) );
288 assertTrue( "Check results size", results.isEmpty() );
290 query = createExactMatchQuery( StandardIndexRecordFields.PACKAGING, "maven-archetype" );
291 results = index.search( new LuceneQuery( query ) );
293 assertTrue( "Check results size", results.isEmpty() );
295 // test non-match fails
296 query = createExactMatchQuery( StandardIndexRecordFields.PACKAGING, "foo" );
297 results = index.search( new LuceneQuery( query ) );
299 assertTrue( "Check results size", results.isEmpty() );
302 public void testExactMatchPluginPrefix()
303 throws RepositoryIndexSearchException
305 Query query = createExactMatchQuery( StandardIndexRecordFields.PLUGIN_PREFIX, "test" );
306 List results = index.search( new LuceneQuery( query ) );
308 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
309 assertEquals( "Check results size", 1, results.size() );
311 // test non-match fails
312 query = createExactMatchQuery( StandardIndexRecordFields.PLUGIN_PREFIX, "foo" );
313 results = index.search( new LuceneQuery( query ) );
315 assertTrue( "Check results size", results.isEmpty() );
318 public void testExactMatchRepository()
319 throws RepositoryIndexSearchException
321 Query query = createExactMatchQuery( StandardIndexRecordFields.REPOSITORY, "test" );
322 List results = index.search( new LuceneQuery( query ) );
324 assertEquals( "Check results size", 10, results.size() );
326 // test non-match fails
327 query = createExactMatchQuery( StandardIndexRecordFields.REPOSITORY, "foo" );
328 results = index.search( new LuceneQuery( query ) );
330 assertTrue( "Check results size", results.isEmpty() );
333 public void testExactMatchMd5()
334 throws RepositoryIndexSearchException
336 Query query = createExactMatchQuery( StandardIndexRecordFields.MD5, "3a0adc365f849366cd8b633cad155cb7" );
337 List results = index.search( new LuceneQuery( query ) );
339 assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) );
340 assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
341 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
342 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
343 assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
344 assertEquals( "Check results size", 5, results.size() );
346 // test non-match fails
347 query = createExactMatchQuery( StandardIndexRecordFields.MD5, "foo" );
348 results = index.search( new LuceneQuery( query ) );
350 assertTrue( "Check results size", results.isEmpty() );
353 public void testExactMatchSha1()
354 throws RepositoryIndexSearchException
357 createExactMatchQuery( StandardIndexRecordFields.SHA1, "c66f18bf192cb613fc2febb4da541a34133eedc2" );
358 List results = index.search( new LuceneQuery( query ) );
360 assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) );
361 assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
362 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
363 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
364 assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
365 assertEquals( "Check results size", 5, results.size() );
367 // test non-match fails
368 query = createExactMatchQuery( StandardIndexRecordFields.SHA1, "foo" );
369 results = index.search( new LuceneQuery( query ) );
371 assertTrue( "Check results size", results.isEmpty() );
374 public void testExactMatchInceptionYear()
375 throws RepositoryIndexSearchException
377 Query query = createExactMatchQuery( StandardIndexRecordFields.INCEPTION_YEAR, "2005" );
378 List results = index.search( new LuceneQuery( query ) );
380 assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
381 assertTrue( "Check result", results.contains( records.get( "test-pom" ) ) );
382 assertTrue( "Check result", results.contains( records.get( "parent-pom" ) ) );
383 assertEquals( "Check results size", 3, results.size() );
385 // test non-match fails
386 query = createExactMatchQuery( StandardIndexRecordFields.INCEPTION_YEAR, "foo" );
387 results = index.search( new LuceneQuery( query ) );
389 assertTrue( "Check results size", results.isEmpty() );
392 public void testMatchFilename()
393 throws RepositoryIndexSearchException, ParseException
395 Query query = createMatchQuery( StandardIndexRecordFields.FILENAME, "maven" );
396 List results = index.search( new LuceneQuery( query ) );
398 assertEquals( "Check results size", 10, results.size() );
400 query = createMatchQuery( StandardIndexRecordFields.FILENAME, "plugin" );
401 results = index.search( new LuceneQuery( query ) );
403 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
404 assertEquals( "Check results size", 1, results.size() );
406 query = createMatchQuery( StandardIndexRecordFields.FILENAME, "pLuGiN" );
407 results = index.search( new LuceneQuery( query ) );
409 assertTrue( "Check results size", results.isEmpty() );
411 query = createMatchQuery( StandardIndexRecordFields.FILENAME, "test" );
412 results = index.search( new LuceneQuery( query ) );
414 assertFalse( "Check result", results.contains( records.get( "parent-pom" ) ) );
415 assertEquals( "Check results size", 9, results.size() );
417 // test non-match fails
418 query = createMatchQuery( StandardIndexRecordFields.FILENAME, "foo" );
419 results = index.search( new LuceneQuery( query ) );
421 assertTrue( "Check results size", results.isEmpty() );
424 public void testMatchGroupId()
425 throws RepositoryIndexSearchException, ParseException
427 Query query = createMatchQuery( StandardIndexRecordFields.GROUPID, "org.apache.maven.archiva.record" );
428 List results = index.search( new LuceneQuery( query ) );
430 assertEquals( "Check results size", 10, results.size() );
432 query = createMatchQuery( StandardIndexRecordFields.GROUPID, "maven" );
433 results = index.search( new LuceneQuery( query ) );
435 assertEquals( "Check results size", 10, results.size() );
437 query = createMatchQuery( StandardIndexRecordFields.GROUPID, "Maven" );
438 results = index.search( new LuceneQuery( query ) );
440 assertEquals( "Check results size", 10, results.size() );
442 // test non-match fails
443 query = createMatchQuery( StandardIndexRecordFields.GROUPID, "foo" );
444 results = index.search( new LuceneQuery( query ) );
446 assertTrue( "Check results size", results.isEmpty() );
449 public void testMatchArtifactId()
450 throws RepositoryIndexSearchException, ParseException
452 Query query = createMatchQuery( StandardIndexRecordFields.ARTIFACTID, "plugin" );
453 List results = index.search( new LuceneQuery( query ) );
455 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
456 assertEquals( "Check results size", 1, results.size() );
458 query = createMatchQuery( StandardIndexRecordFields.ARTIFACTID, "test" );
459 results = index.search( new LuceneQuery( query ) );
461 assertFalse( "Check result", results.contains( records.get( "parent-pom" ) ) );
462 assertEquals( "Check results size", 9, results.size() );
464 // test non-match fails
465 query = createMatchQuery( StandardIndexRecordFields.ARTIFACTID, "maven" );
466 results = index.search( new LuceneQuery( query ) );
468 assertTrue( "Check results size", results.isEmpty() );
471 public void testMatchVersion()
472 throws RepositoryIndexSearchException, ParseException
474 // If partial matches are desired, need to change the analyzer for versions to split on '.'
475 Query query = createMatchQuery( StandardIndexRecordFields.VERSION, "1" );
476 List results = index.search( new LuceneQuery( query ) );
478 assertTrue( "Check result", results.contains( records.get( "parent-pom" ) ) );
479 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
480 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
481 assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
482 assertEquals( "Check results size", 4, results.size() );
484 query = createMatchQuery( StandardIndexRecordFields.VERSION, "1.0" );
485 results = index.search( new LuceneQuery( query ) );
487 assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) );
488 assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
489 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
490 assertTrue( "Check result", results.contains( records.get( "test-pom" ) ) );
491 assertTrue( "Check result", results.contains( records.get( "test-archetype" ) ) );
492 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
493 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
494 assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
495 assertEquals( "Check results size", 8, results.size() );
497 query = createMatchQuery( StandardIndexRecordFields.VERSION, "snapshot" );
498 results = index.search( new LuceneQuery( query ) );
500 assertTrue( "Check results size", results.isEmpty() );
502 query = createMatchQuery( StandardIndexRecordFields.VERSION, "SNAPSHOT" );
503 results = index.search( new LuceneQuery( query ) );
505 assertTrue( "Check results size", results.isEmpty() );
507 query = createMatchQuery( StandardIndexRecordFields.VERSION, "alpha" );
508 results = index.search( new LuceneQuery( query ) );
510 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
511 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
512 assertEquals( "Check results size", 2, results.size() );
514 query = createMatchQuery( StandardIndexRecordFields.VERSION, "1.0-alpha-1" );
515 results = index.search( new LuceneQuery( query ) );
517 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
518 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
519 assertEquals( "Check results size", 2, results.size() );
521 // test non-match fails
522 query = createMatchQuery( StandardIndexRecordFields.VERSION, "foo" );
523 results = index.search( new LuceneQuery( query ) );
525 assertTrue( "Check results size", results.isEmpty() );
528 public void testMatchBaseVersion()
529 throws RepositoryIndexSearchException, ParseException
531 // If partial matches are desired, need to change the analyzer for versions to split on '.'
532 Query query = createMatchQuery( StandardIndexRecordFields.BASE_VERSION, "1" );
533 List results = index.search( new LuceneQuery( query ) );
535 assertTrue( "Check result", results.contains( records.get( "parent-pom" ) ) );
536 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
537 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
538 assertEquals( "Check results size", 3, results.size() );
540 query = createMatchQuery( StandardIndexRecordFields.BASE_VERSION, "1.0" );
541 results = index.search( new LuceneQuery( query ) );
543 assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) );
544 assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
545 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
546 assertTrue( "Check result", results.contains( records.get( "test-pom" ) ) );
547 assertTrue( "Check result", results.contains( records.get( "test-archetype" ) ) );
548 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
549 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
550 assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
551 assertEquals( "Check results size", 8, results.size() );
553 query = createMatchQuery( StandardIndexRecordFields.BASE_VERSION, "SNAPSHOT" );
554 results = index.search( new LuceneQuery( query ) );
556 assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
557 assertEquals( "Check results size", 1, results.size() );
559 query = createMatchQuery( StandardIndexRecordFields.BASE_VERSION, "SnApShOt" );
560 results = index.search( new LuceneQuery( query ) );
562 assertTrue( "Check results size", results.isEmpty() );
564 query = createMatchQuery( StandardIndexRecordFields.BASE_VERSION, "snapshot" );
565 results = index.search( new LuceneQuery( query ) );
567 assertTrue( "Check results size", results.isEmpty() );
569 query = createMatchQuery( StandardIndexRecordFields.BASE_VERSION, "alpha" );
570 results = index.search( new LuceneQuery( query ) );
572 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
573 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
574 assertEquals( "Check results size", 2, results.size() );
576 query = createMatchQuery( StandardIndexRecordFields.BASE_VERSION, "1.0-alpha-1" );
577 results = index.search( new LuceneQuery( query ) );
579 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
580 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
581 assertEquals( "Check results size", 2, results.size() );
583 // test non-match fails
584 query = createMatchQuery( StandardIndexRecordFields.BASE_VERSION, "foo" );
585 results = index.search( new LuceneQuery( query ) );
587 assertTrue( "Check results size", results.isEmpty() );
590 public void testMatchClassifier()
591 throws RepositoryIndexSearchException, ParseException
593 BooleanQuery bQuery = new BooleanQuery();
594 bQuery.add( new MatchAllDocsQuery(), BooleanClause.Occur.MUST );
595 bQuery.add( createMatchQuery( StandardIndexRecordFields.CLASSIFIER, "jdk14" ), BooleanClause.Occur.MUST_NOT );
596 List results = index.search( new LuceneQuery( bQuery ) );
598 assertFalse( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
599 assertFalse( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
600 assertEquals( "Check results size", 8, results.size() );
602 // TODO: can we search for "anything with no classifier" ?
604 Query query = createMatchQuery( StandardIndexRecordFields.CLASSIFIER, "jdk14" );
605 results = index.search( new LuceneQuery( query ) );
607 assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
608 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
609 assertEquals( "Check results size", 2, results.size() );
611 // test non-match fails
612 query = createMatchQuery( StandardIndexRecordFields.CLASSIFIER, "foo" );
613 results = index.search( new LuceneQuery( query ) );
615 assertTrue( "Check results size", results.isEmpty() );
618 public void testMatchClass()
619 throws RepositoryIndexSearchException, ParseException
621 Query query = createMatchQuery( StandardIndexRecordFields.CLASSES, "b.c.C" );
622 List results = index.search( new LuceneQuery( query ) );
624 assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
625 assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) );
626 assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
627 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
628 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
629 assertEquals( "Check results size", 5, results.size() );
631 query = createMatchQuery( StandardIndexRecordFields.CLASSES, "C" );
632 results = index.search( new LuceneQuery( query ) );
634 assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
635 assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) );
636 assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
637 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
638 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
639 assertEquals( "Check results size", 5, results.size() );
641 query = createMatchQuery( StandardIndexRecordFields.CLASSES, "MyMojo" );
642 results = index.search( new LuceneQuery( query ) );
644 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
645 assertEquals( "Check results size", 1, results.size() );
647 query = createMatchQuery( StandardIndexRecordFields.CLASSES, "MYMOJO" );
648 results = index.search( new LuceneQuery( query ) );
650 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
651 assertEquals( "Check results size", 1, results.size() );
653 query = createMatchQuery( StandardIndexRecordFields.CLASSES, "mymojo" );
654 results = index.search( new LuceneQuery( query ) );
656 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
657 assertEquals( "Check results size", 1, results.size() );
659 // test non-match fails
660 query = createMatchQuery( StandardIndexRecordFields.CLASSES, "foo" );
661 results = index.search( new LuceneQuery( query ) );
663 assertTrue( "Check results size", results.isEmpty() );
666 public void testMatchFiles()
667 throws RepositoryIndexSearchException, ParseException
669 Query query = createMatchQuery( StandardIndexRecordFields.FILES, "MANIFEST.MF" );
670 List results = index.search( new LuceneQuery( query ) );
672 assertFalse( "Check result", results.contains( records.get( "test-pom" ) ) );
673 assertFalse( "Check result", results.contains( records.get( "parent-pom" ) ) );
674 assertFalse( "Check result", results.contains( records.get( "test-dll" ) ) );
675 assertEquals( "Check results size", 7, results.size() );
677 query = createMatchQuery( StandardIndexRecordFields.FILES, "META-INF" );
678 results = index.search( new LuceneQuery( query ) );
680 assertFalse( "Check result", results.contains( records.get( "test-pom" ) ) );
681 assertFalse( "Check result", results.contains( records.get( "parent-pom" ) ) );
682 assertFalse( "Check result", results.contains( records.get( "test-dll" ) ) );
683 assertEquals( "Check results size", 7, results.size() );
685 query = createMatchQuery( StandardIndexRecordFields.FILES, "plugin.xml" );
686 results = index.search( new LuceneQuery( query ) );
688 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
689 assertEquals( "Check results size", 1, results.size() );
691 // test non-match fails
692 query = createMatchQuery( StandardIndexRecordFields.FILES, "foo" );
693 results = index.search( new LuceneQuery( query ) );
695 assertTrue( "Check results size", results.isEmpty() );
698 public void testExactMatchDependency()
699 throws RepositoryIndexSearchException
702 createExactMatchQuery( StandardIndexRecordFields.DEPENDENCIES, "org.apache.maven:maven-plugin-api:2.0" );
703 List results = index.search( new LuceneQuery( query ) );
705 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
706 assertEquals( "Check results size", 1, results.size() );
708 // test non-match fails
709 query = createExactMatchQuery( StandardIndexRecordFields.DEPENDENCIES, "foo" );
710 results = index.search( new LuceneQuery( query ) );
712 assertTrue( "Check results size", results.isEmpty() );
715 public void testMatchProjectName()
716 throws RepositoryIndexSearchException, ParseException
718 Query query = createMatchQuery( StandardIndexRecordFields.PROJECT_NAME, "mojo" );
719 List results = index.search( new LuceneQuery( query ) );
721 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
722 assertEquals( "Check results size", 1, results.size() );
724 query = createMatchQuery( StandardIndexRecordFields.PROJECT_NAME, "maven" );
725 results = index.search( new LuceneQuery( query ) );
727 assertFalse( "Check result", results.contains( records.get( "parent-pom" ) ) );
728 assertFalse( "Check result", results.contains( records.get( "test-child-pom" ) ) );
729 assertEquals( "Check results size", 2, results.size() );
731 // test non-match fails
732 query = createMatchQuery( StandardIndexRecordFields.PROJECT_NAME, "foo" );
733 results = index.search( new LuceneQuery( query ) );
735 assertTrue( "Check results size", results.isEmpty() );
738 public void testMatchProjectDescription()
739 throws RepositoryIndexSearchException, ParseException
741 Query query = createMatchQuery( StandardIndexRecordFields.PROJECT_DESCRIPTION, "description" );
742 List results = index.search( new LuceneQuery( query ) );
744 assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
745 assertTrue( "Check result", results.contains( records.get( "parent-pom" ) ) );
746 assertTrue( "Check result", results.contains( records.get( "test-pom" ) ) );
747 assertEquals( "Check results size", 3, results.size() );
749 // test non-match fails
750 query = createMatchQuery( StandardIndexRecordFields.PROJECT_DESCRIPTION, "foo" );
751 results = index.search( new LuceneQuery( query ) );
753 assertTrue( "Check results size", results.isEmpty() );
756 private static Query createExactMatchQuery( String field, String value )
758 return new TermQuery( new Term( field, value ) );
761 private static Query createMatchQuery( String field, String value )
762 throws ParseException
764 return new QueryParser( field, LuceneRepositoryArtifactIndex.getAnalyzer() ).parse( value );
767 private Artifact createArtifact( String artifactId )
769 return createArtifact( artifactId, "1.0", "jar", null );
772 private Artifact createArtifact( String artifactId, String version, String type )
774 return createArtifact( artifactId, version, type, null );
777 private Artifact createArtifact( String artifactId, String version, String type, String classifier )
779 Artifact artifact = artifactFactory.createDependencyArtifact( "org.apache.maven.archiva.record", artifactId,
780 VersionRange.createFromVersion( version ), type,
781 classifier, Artifact.SCOPE_RUNTIME );
782 artifact.isSnapshot();
783 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
784 artifact.setRepository( repository );