1 package org.apache.maven.archiva.indexing.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.search.BooleanClause;
21 import org.apache.lucene.search.BooleanQuery;
22 import org.apache.lucene.search.MatchAllDocsQuery;
23 import org.apache.lucene.search.Query;
24 import org.apache.lucene.search.TermQuery;
25 import org.apache.maven.archiva.indexing.RepositoryArtifactIndex;
26 import org.apache.maven.archiva.indexing.RepositoryArtifactIndexFactory;
27 import org.apache.maven.archiva.indexing.RepositoryIndexSearchException;
28 import org.apache.maven.archiva.indexing.record.RepositoryIndexRecordFactory;
29 import org.apache.maven.archiva.indexing.record.StandardIndexRecordFields;
30 import org.apache.maven.artifact.Artifact;
31 import org.apache.maven.artifact.factory.ArtifactFactory;
32 import org.apache.maven.artifact.repository.ArtifactRepository;
33 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
34 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
35 import org.apache.maven.artifact.versioning.VersionRange;
36 import org.codehaus.plexus.PlexusTestCase;
37 import org.codehaus.plexus.util.FileUtils;
40 import java.util.HashMap;
41 import java.util.List;
45 * Test the Lucene implementation of the artifact index search.
47 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
48 * @todo would be nice to abstract some of the query away, but for now passing in a Lucene query directly is good enough
50 public class LuceneStandardArtifactIndexSearchTest
51 extends PlexusTestCase
53 private RepositoryArtifactIndex index;
55 private ArtifactRepository repository;
57 private ArtifactFactory artifactFactory;
59 private File indexLocation;
61 private RepositoryIndexRecordFactory recordFactory;
63 private Map records = new HashMap();
65 protected void setUp()
70 recordFactory = (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "standard" );
72 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
74 ArtifactRepositoryFactory repositoryFactory =
75 (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
77 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
79 File file = getTestFile( "src/test/managed-repository" );
81 repositoryFactory.createArtifactRepository( "test", file.toURI().toURL().toString(), layout, null, null );
83 RepositoryArtifactIndexFactory factory =
84 (RepositoryArtifactIndexFactory) lookup( RepositoryArtifactIndexFactory.ROLE, "lucene" );
86 indexLocation = getTestFile( "target/test-index" );
88 FileUtils.deleteDirectory( indexLocation );
90 index = factory.createStandardIndex( indexLocation );
92 records.put( "test-jar", recordFactory.createRecord( createArtifact( "test-jar" ) ) );
93 records.put( "test-jar-jdk14",
94 recordFactory.createRecord( createArtifact( "test-jar", "1.0", "jar", "jdk14" ) ) );
95 records.put( "test-jar-and-pom",
96 recordFactory.createRecord( createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar" ) ) );
97 records.put( "test-jar-and-pom-jdk14", recordFactory.createRecord(
98 createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar", "jdk14" ) ) );
99 records.put( "test-child-pom",
100 recordFactory.createRecord( createArtifact( "test-child-pom", "1.0-20060728.121314-1", "jar" ) ) );
101 records.put( "test-archetype", recordFactory.createRecord( createArtifact( "test-archetype" ) ) );
102 records.put( "test-plugin", recordFactory.createRecord( createArtifact( "test-plugin" ) ) );
103 records.put( "test-pom", recordFactory.createRecord( createArtifact( "test-pom", "1.0", "pom" ) ) );
104 records.put( "parent-pom", recordFactory.createRecord( createArtifact( "parent-pom", "1", "pom" ) ) );
105 records.put( "test-dll", recordFactory.createRecord( createArtifact( "test-dll", "1.0.1.34", "dll" ) ) );
107 index.indexRecords( records.values() );
110 public void testExactMatchVersion()
111 throws RepositoryIndexSearchException
113 Query query = new TermQuery( new Term( StandardIndexRecordFields.VERSION_EXACT, "1.0" ) );
114 List results = index.search( new LuceneQuery( query ) );
116 assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) );
117 assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
118 assertTrue( "Check result", results.contains( records.get( "test-pom" ) ) );
119 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
120 assertTrue( "Check result", results.contains( records.get( "test-archetype" ) ) );
121 assertEquals( "Check results size", 5, results.size() );
123 query = new TermQuery( new Term( StandardIndexRecordFields.VERSION_EXACT, "1.0-SNAPSHOT" ) );
124 results = index.search( new LuceneQuery( query ) );
126 assertTrue( "Check results size", results.isEmpty() );
128 query = new TermQuery( new Term( StandardIndexRecordFields.VERSION_EXACT, "1.0-20060728.121314-1" ) );
129 results = index.search( new LuceneQuery( query ) );
131 assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
132 assertEquals( "Check results size", 1, results.size() );
134 // test non-match fails
135 query = new TermQuery( new Term( StandardIndexRecordFields.VERSION_EXACT, "foo" ) );
136 results = index.search( new LuceneQuery( query ) );
138 assertTrue( "Check results size", results.isEmpty() );
141 public void testExactMatchBaseVersion()
142 throws RepositoryIndexSearchException
144 Query query = new TermQuery( new Term( StandardIndexRecordFields.BASE_VERSION_EXACT, "1.0" ) );
145 List results = index.search( new LuceneQuery( query ) );
147 assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) );
148 assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
149 assertTrue( "Check result", results.contains( records.get( "test-pom" ) ) );
150 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
151 assertTrue( "Check result", results.contains( records.get( "test-archetype" ) ) );
152 assertEquals( "Check results size", 5, results.size() );
154 query = new TermQuery( new Term( StandardIndexRecordFields.BASE_VERSION_EXACT, "1.0-SNAPSHOT" ) );
155 results = index.search( new LuceneQuery( query ) );
157 assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
158 assertEquals( "Check results size", 1, results.size() );
160 query = new TermQuery( new Term( StandardIndexRecordFields.BASE_VERSION_EXACT, "1.0-20060728.121314-1" ) );
161 results = index.search( new LuceneQuery( query ) );
163 assertTrue( "Check results size", results.isEmpty() );
165 // test non-match fails
166 query = new TermQuery( new Term( StandardIndexRecordFields.BASE_VERSION_EXACT, "foo" ) );
167 results = index.search( new LuceneQuery( query ) );
169 assertTrue( "Check results size", results.isEmpty() );
172 public void testExactMatchGroupId()
173 throws RepositoryIndexSearchException
176 new TermQuery( new Term( StandardIndexRecordFields.GROUPID_EXACT, "org.apache.maven.archiva.record" ) );
177 List results = index.search( new LuceneQuery( query ) );
179 assertEquals( "Check results size", 10, results.size() );
181 // test partial match fails
182 query = new TermQuery( new Term( StandardIndexRecordFields.GROUPID_EXACT, "org.apache.maven" ) );
183 results = index.search( new LuceneQuery( query ) );
185 assertTrue( "Check results size", results.isEmpty() );
187 // test non-match fails
188 query = new TermQuery( new Term( StandardIndexRecordFields.GROUPID_EXACT, "foo" ) );
189 results = index.search( new LuceneQuery( query ) );
191 assertTrue( "Check results size", results.isEmpty() );
194 public void testExactMatchArtifactId()
195 throws RepositoryIndexSearchException
197 Query query = new TermQuery( new Term( StandardIndexRecordFields.ARTIFACTID_EXACT, "test-jar" ) );
198 List results = index.search( new LuceneQuery( query ) );
200 assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) );
201 assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
202 assertEquals( "Check results size", 2, results.size() );
204 // test partial match fails
205 query = new TermQuery( new Term( StandardIndexRecordFields.ARTIFACTID_EXACT, "test" ) );
206 results = index.search( new LuceneQuery( query ) );
208 assertTrue( "Check results size", results.isEmpty() );
210 // test non-match fails
211 query = new TermQuery( new Term( StandardIndexRecordFields.ARTIFACTID_EXACT, "foo" ) );
212 results = index.search( new LuceneQuery( query ) );
214 assertTrue( "Check results size", results.isEmpty() );
217 public void testExactMatchType()
218 throws RepositoryIndexSearchException
220 Query query = new TermQuery( new Term( StandardIndexRecordFields.TYPE, "maven-plugin" ) );
221 List results = index.search( new LuceneQuery( query ) );
223 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
224 assertEquals( "Check results size", 1, results.size() );
226 query = new TermQuery( new Term( StandardIndexRecordFields.TYPE, "jar" ) );
227 results = index.search( new LuceneQuery( query ) );
229 assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) );
230 assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
231 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
232 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
233 assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
234 assertEquals( "Check results size", 5, results.size() );
236 query = new TermQuery( new Term( StandardIndexRecordFields.TYPE, "dll" ) );
237 results = index.search( new LuceneQuery( query ) );
239 assertTrue( "Check result", results.contains( records.get( "test-dll" ) ) );
240 assertEquals( "Check results size", 1, results.size() );
242 query = new TermQuery( new Term( StandardIndexRecordFields.TYPE, "maven-archetype" ) );
243 results = index.search( new LuceneQuery( query ) );
245 assertTrue( "Check result", results.contains( records.get( "test-archetype" ) ) );
246 assertEquals( "Check results size", 1, results.size() );
248 // test non-match fails
249 query = new TermQuery( new Term( StandardIndexRecordFields.TYPE, "foo" ) );
250 results = index.search( new LuceneQuery( query ) );
252 assertTrue( "Check results size", results.isEmpty() );
255 public void testExactMatchPackaging()
256 throws RepositoryIndexSearchException
258 Query query = new TermQuery( new Term( StandardIndexRecordFields.PACKAGING, "maven-plugin" ) );
259 List results = index.search( new LuceneQuery( query ) );
261 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
262 assertEquals( "Check results size", 1, results.size() );
264 query = new TermQuery( new Term( StandardIndexRecordFields.PACKAGING, "jar" ) );
265 results = index.search( new LuceneQuery( query ) );
267 assertTrue( "Check result", results.contains( records.get( "test-archetype" ) ) );
268 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
269 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
270 assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
271 assertEquals( "Check results size", 4, results.size() );
273 query = new TermQuery( new Term( StandardIndexRecordFields.PACKAGING, "dll" ) );
274 results = index.search( new LuceneQuery( query ) );
276 assertTrue( "Check results size", results.isEmpty() );
278 query = new TermQuery( new Term( StandardIndexRecordFields.PACKAGING, "maven-archetype" ) );
279 results = index.search( new LuceneQuery( query ) );
281 assertTrue( "Check results size", results.isEmpty() );
283 // test non-match fails
284 query = new TermQuery( new Term( StandardIndexRecordFields.PACKAGING, "foo" ) );
285 results = index.search( new LuceneQuery( query ) );
287 assertTrue( "Check results size", results.isEmpty() );
290 public void testExactMatchPluginPrefix()
291 throws RepositoryIndexSearchException
293 Query query = new TermQuery( new Term( StandardIndexRecordFields.PLUGIN_PREFIX, "test" ) );
294 List results = index.search( new LuceneQuery( query ) );
296 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
297 assertEquals( "Check results size", 1, results.size() );
299 // test non-match fails
300 query = new TermQuery( new Term( StandardIndexRecordFields.PLUGIN_PREFIX, "foo" ) );
301 results = index.search( new LuceneQuery( query ) );
303 assertTrue( "Check results size", results.isEmpty() );
306 public void testExactMatchRepository()
307 throws RepositoryIndexSearchException
309 Query query = new TermQuery( new Term( StandardIndexRecordFields.REPOSITORY, "test" ) );
310 List results = index.search( new LuceneQuery( query ) );
312 assertEquals( "Check results size", 10, results.size() );
314 // test non-match fails
315 query = new TermQuery( new Term( StandardIndexRecordFields.REPOSITORY, "foo" ) );
316 results = index.search( new LuceneQuery( query ) );
318 assertTrue( "Check results size", results.isEmpty() );
321 public void testExactMatchMd5()
322 throws RepositoryIndexSearchException
324 Query query = new TermQuery( new Term( StandardIndexRecordFields.MD5, "3a0adc365f849366cd8b633cad155cb7" ) );
325 List results = index.search( new LuceneQuery( query ) );
327 assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) );
328 assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
329 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
330 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
331 assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
332 assertEquals( "Check results size", 5, results.size() );
334 // test non-match fails
335 query = new TermQuery( new Term( StandardIndexRecordFields.MD5, "foo" ) );
336 results = index.search( new LuceneQuery( query ) );
338 assertTrue( "Check results size", results.isEmpty() );
341 public void testExactMatchSha1()
342 throws RepositoryIndexSearchException
345 new TermQuery( new Term( StandardIndexRecordFields.SHA1, "c66f18bf192cb613fc2febb4da541a34133eedc2" ) );
346 List results = index.search( new LuceneQuery( query ) );
348 assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) );
349 assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
350 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
351 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
352 assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
353 assertEquals( "Check results size", 5, results.size() );
355 // test non-match fails
356 query = new TermQuery( new Term( StandardIndexRecordFields.SHA1, "foo" ) );
357 results = index.search( new LuceneQuery( query ) );
359 assertTrue( "Check results size", results.isEmpty() );
362 public void testExactMatchInceptionYear()
363 throws RepositoryIndexSearchException
365 Query query = new TermQuery( new Term( StandardIndexRecordFields.INCEPTION_YEAR, "2005" ) );
366 List results = index.search( new LuceneQuery( query ) );
368 assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
369 assertTrue( "Check result", results.contains( records.get( "test-pom" ) ) );
370 assertTrue( "Check result", results.contains( records.get( "parent-pom" ) ) );
371 assertEquals( "Check results size", 3, results.size() );
373 // test non-match fails
374 query = new TermQuery( new Term( StandardIndexRecordFields.INCEPTION_YEAR, "foo" ) );
375 results = index.search( new LuceneQuery( query ) );
377 assertTrue( "Check results size", results.isEmpty() );
380 public void testMatchFilename()
381 throws RepositoryIndexSearchException
383 Query query = new TermQuery( new Term( StandardIndexRecordFields.FILENAME, "maven" ) );
384 List results = index.search( new LuceneQuery( query ) );
386 assertEquals( "Check results size", 10, results.size() );
388 /* TODO: if this is a result we want, we need to change the analyzer. Currently, it is tokenizing it as plugin-1.0 and plugin/1.0 in the path
389 query = new TermQuery( new Term( StandardIndexRecordFields.FILENAME, "plugin" ) );
390 results = index.search( new LuceneQuery( query ) );
392 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
393 assertEquals( "Check results size", 1, results.size() );
395 query = new TermQuery( new Term( StandardIndexRecordFields.FILENAME, "test" ) );
396 results = index.search( new LuceneQuery( query ) );
398 assertFalse( "Check result", results.contains( records.get( "parent-pom" ) ) );
399 assertEquals( "Check results size", 9, results.size() );
401 // test non-match fails
402 query = new TermQuery( new Term( StandardIndexRecordFields.FILENAME, "foo" ) );
403 results = index.search( new LuceneQuery( query ) );
405 assertTrue( "Check results size", results.isEmpty() );
408 public void testMatchGroupId()
409 throws RepositoryIndexSearchException
411 Query query = new TermQuery( new Term( StandardIndexRecordFields.GROUPID, "org.apache.maven.archiva.record" ) );
412 List results = index.search( new LuceneQuery( query ) );
414 assertEquals( "Check results size", 10, results.size() );
416 /* TODO: if we want this result, must change the analyzer to split on '.'
417 query = new TermQuery( new Term( StandardIndexRecordFields.GROUPID, "maven" ) );
418 results = index.search( new LuceneQuery( query ) );
420 assertEquals( "Check results size", 10, results.size() );
423 // test non-match fails
424 query = new TermQuery( new Term( StandardIndexRecordFields.GROUPID, "foo" ) );
425 results = index.search( new LuceneQuery( query ) );
427 assertTrue( "Check results size", results.isEmpty() );
430 public void testMatchArtifactId()
431 throws RepositoryIndexSearchException
433 Query query = new TermQuery( new Term( StandardIndexRecordFields.ARTIFACTID, "plugin" ) );
434 List results = index.search( new LuceneQuery( query ) );
436 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
437 assertEquals( "Check results size", 1, results.size() );
439 query = new TermQuery( new Term( StandardIndexRecordFields.ARTIFACTID, "test" ) );
440 results = index.search( new LuceneQuery( query ) );
442 assertFalse( "Check result", results.contains( records.get( "parent-pom" ) ) );
443 assertEquals( "Check results size", 9, results.size() );
445 // test non-match fails
446 query = new TermQuery( new Term( StandardIndexRecordFields.ARTIFACTID, "maven" ) );
447 results = index.search( new LuceneQuery( query ) );
449 assertTrue( "Check results size", results.isEmpty() );
452 public void testMatchVersion()
453 throws RepositoryIndexSearchException
455 // If partial matches are desired, need to change the analyzer for versions to split on '.'
456 Query query = new TermQuery( new Term( StandardIndexRecordFields.VERSION, "1" ) );
457 List results = index.search( new LuceneQuery( query ) );
459 assertTrue( "Check result", results.contains( records.get( "parent-pom" ) ) );
460 assertEquals( "Check results size", 1, results.size() );
462 query = new TermQuery( new Term( StandardIndexRecordFields.VERSION, "1.0" ) );
463 results = index.search( new LuceneQuery( query ) );
465 assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) );
466 assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
467 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
468 assertTrue( "Check result", results.contains( records.get( "test-pom" ) ) );
469 assertTrue( "Check result", results.contains( records.get( "test-archetype" ) ) );
470 assertEquals( "Check results size", 5, results.size() );
472 /* TODO: need to change analyzer to split on - if we want this
473 query = new TermQuery( new Term( StandardIndexRecordFields.VERSION, "snapshot" ) );
474 results = index.search( new LuceneQuery( query ) );
476 assertTrue( "Check results size", results.isEmpty() );
478 query = new TermQuery( new Term( StandardIndexRecordFields.VERSION, "alpha" ) );
479 results = index.search( new LuceneQuery( query ) );
481 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
482 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
483 assertEquals( "Check results size", 2, results.size() );
486 // test non-match fails
487 query = new TermQuery( new Term( StandardIndexRecordFields.VERSION, "foo" ) );
488 results = index.search( new LuceneQuery( query ) );
490 assertTrue( "Check results size", results.isEmpty() );
493 public void testMatchBaseVersion()
494 throws RepositoryIndexSearchException
496 // If partial matches are desired, need to change the analyzer for versions to split on '.'
497 Query query = new TermQuery( new Term( StandardIndexRecordFields.BASE_VERSION, "1" ) );
498 List results = index.search( new LuceneQuery( query ) );
500 assertTrue( "Check result", results.contains( records.get( "parent-pom" ) ) );
501 assertEquals( "Check results size", 1, results.size() );
503 query = new TermQuery( new Term( StandardIndexRecordFields.BASE_VERSION, "1.0" ) );
504 results = index.search( new LuceneQuery( query ) );
506 assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) );
507 assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
508 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
509 assertTrue( "Check result", results.contains( records.get( "test-pom" ) ) );
510 assertTrue( "Check result", results.contains( records.get( "test-archetype" ) ) );
511 assertEquals( "Check results size", 5, results.size() );
513 /* TODO: need to change analyzer to split on - if we want this
514 query = new TermQuery( new Term( StandardIndexRecordFields.BASE_VERSION, "snapshot" ) );
515 results = index.search( new LuceneQuery( query ) );
517 assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
518 assertEquals( "Check results size", 1, results.size() );
520 query = new TermQuery( new Term( StandardIndexRecordFields.BASE_VERSION, "alpha" ) );
521 results = index.search( new LuceneQuery( query ) );
523 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
524 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
525 assertEquals( "Check results size", 2, results.size() );
528 // test non-match fails
529 query = new TermQuery( new Term( StandardIndexRecordFields.BASE_VERSION, "foo" ) );
530 results = index.search( new LuceneQuery( query ) );
532 assertTrue( "Check results size", results.isEmpty() );
535 public void testMatchClassifier()
536 throws RepositoryIndexSearchException
538 BooleanQuery bQuery = new BooleanQuery();
539 bQuery.add( new MatchAllDocsQuery(), BooleanClause.Occur.MUST );
540 bQuery.add( new TermQuery( new Term( StandardIndexRecordFields.CLASSIFIER, "jdk14" ) ),
541 BooleanClause.Occur.MUST_NOT );
542 List results = index.search( new LuceneQuery( bQuery ) );
544 assertFalse( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
545 assertFalse( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
546 assertEquals( "Check results size", 8, results.size() );
548 // TODO: can we search for "anything with no classifier" ?
550 Query query = new TermQuery( new Term( StandardIndexRecordFields.CLASSIFIER, "jdk14" ) );
551 results = index.search( new LuceneQuery( query ) );
553 assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
554 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
555 assertEquals( "Check results size", 2, results.size() );
557 // test non-match fails
558 query = new TermQuery( new Term( StandardIndexRecordFields.CLASSIFIER, "foo" ) );
559 results = index.search( new LuceneQuery( query ) );
561 assertTrue( "Check results size", results.isEmpty() );
564 public void testMatchClass()
565 throws RepositoryIndexSearchException
567 // TODO: should be preserving case!
568 Query query = new TermQuery( new Term( StandardIndexRecordFields.CLASSES, "b.c.c" ) );
569 List results = index.search( new LuceneQuery( query ) );
571 assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
572 assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) );
573 assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
574 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
575 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
576 assertEquals( "Check results size", 5, results.size() );
578 /* TODO!: need to change the analyzer if we want partial classes (split on '.')
579 query = new TermQuery( new Term( StandardIndexRecordFields.CLASSES, "C" ) );
580 results = index.search( new LuceneQuery( query ) );
582 assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) );
583 assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
584 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
585 assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
586 assertEquals( "Check results size", 4, results.size() );
588 query = new TermQuery( new Term( StandardIndexRecordFields.CLASSES, "MyMojo" ) );
589 results = index.search( new LuceneQuery( query ) );
591 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
592 assertEquals( "Check results size", 1, results.size() );
595 // test non-match fails
596 query = new TermQuery( new Term( StandardIndexRecordFields.CLASSES, "foo" ) );
597 results = index.search( new LuceneQuery( query ) );
599 assertTrue( "Check results size", results.isEmpty() );
602 public void testMatchFiles()
603 throws RepositoryIndexSearchException
605 // TODO: should be preserving case!
606 Query query = new TermQuery( new Term( StandardIndexRecordFields.FILES, "manifest.mf" ) );
607 List results = index.search( new LuceneQuery( query ) );
609 assertFalse( "Check result", results.contains( records.get( "test-pom" ) ) );
610 assertFalse( "Check result", results.contains( records.get( "parent-pom" ) ) );
611 assertFalse( "Check result", results.contains( records.get( "test-dll" ) ) );
612 assertEquals( "Check results size", 7, results.size() );
615 // TODO: should be preserving case, and '-inf'!
616 query = new TermQuery( new Term( StandardIndexRecordFields.FILES, "meta-inf" ) );
617 results = index.search( new LuceneQuery( query ) );
619 assertFalse( "Check result", results.contains( records.get( "test-pom" ) ) );
620 assertFalse( "Check result", results.contains( records.get( "parent-pom" ) ) );
621 assertFalse( "Check result", results.contains( records.get( "test-dll" ) ) );
622 assertEquals( "Check results size", 7, results.size() );
625 query = new TermQuery( new Term( StandardIndexRecordFields.FILES, "plugin.xml" ) );
626 results = index.search( new LuceneQuery( query ) );
628 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
629 assertEquals( "Check results size", 1, results.size() );
631 // test non-match fails
632 query = new TermQuery( new Term( StandardIndexRecordFields.FILES, "foo" ) );
633 results = index.search( new LuceneQuery( query ) );
635 assertTrue( "Check results size", results.isEmpty() );
638 public void testMatchProjectName()
639 throws RepositoryIndexSearchException
641 Query query = new TermQuery( new Term( StandardIndexRecordFields.PROJECT_NAME, "mojo" ) );
642 List 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 = new TermQuery( new Term( StandardIndexRecordFields.PROJECT_NAME, "maven" ) );
648 results = index.search( new LuceneQuery( query ) );
650 assertFalse( "Check result", results.contains( records.get( "parent-pom" ) ) );
651 assertFalse( "Check result", results.contains( records.get( "test-child-pom" ) ) );
652 assertEquals( "Check results size", 2, results.size() );
654 // test non-match fails
655 query = new TermQuery( new Term( StandardIndexRecordFields.PROJECT_NAME, "foo" ) );
656 results = index.search( new LuceneQuery( query ) );
658 assertTrue( "Check results size", results.isEmpty() );
661 public void testMatchProjectDescription()
662 throws RepositoryIndexSearchException
664 Query query = new TermQuery( new Term( StandardIndexRecordFields.PROJECT_DESCRIPTION, "description" ) );
665 List results = index.search( new LuceneQuery( query ) );
667 assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
668 assertTrue( "Check result", results.contains( records.get( "parent-pom" ) ) );
669 assertTrue( "Check result", results.contains( records.get( "test-pom" ) ) );
670 assertEquals( "Check results size", 3, results.size() );
672 // test non-match fails
673 query = new TermQuery( new Term( StandardIndexRecordFields.PROJECT_DESCRIPTION, "foo" ) );
674 results = index.search( new LuceneQuery( query ) );
676 assertTrue( "Check results size", results.isEmpty() );
679 private Artifact createArtifact( String artifactId )
681 return createArtifact( artifactId, "1.0", "jar", null );
684 private Artifact createArtifact( String artifactId, String version, String type )
686 return createArtifact( artifactId, version, type, null );
689 private Artifact createArtifact( String artifactId, String version, String type, String classifier )
691 Artifact artifact = artifactFactory.createDependencyArtifact( "org.apache.maven.archiva.record", artifactId,
692 VersionRange.createFromVersion( version ), type,
693 classifier, Artifact.SCOPE_RUNTIME );
694 artifact.isSnapshot();
695 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
696 artifact.setRepository( repository );