01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */package org.apache.lucene.search;
17:
18: import java.io.IOException;
19:
20: import org.apache.lucene.analysis.standard.StandardAnalyzer;
21: import org.apache.lucene.document.Document;
22: import org.apache.lucene.document.Field;
23: import org.apache.lucene.index.IndexWriter;
24: import org.apache.lucene.index.Term;
25: import org.apache.lucene.store.RAMDirectory;
26:
27: import org.apache.lucene.util.LuceneTestCase;
28:
29: /**
30: * Tests MatchAllDocsQuery.
31: *
32: * @author Daniel Naber
33: */
34: public class TestMatchAllDocsQuery extends LuceneTestCase {
35:
36: public void testQuery() throws IOException {
37: RAMDirectory dir = new RAMDirectory();
38: IndexWriter iw = new IndexWriter(dir, new StandardAnalyzer(),
39: true);
40: addDoc("one", iw);
41: addDoc("two", iw);
42: addDoc("three four", iw);
43: iw.close();
44:
45: IndexSearcher is = new IndexSearcher(dir);
46: Hits hits = is.search(new MatchAllDocsQuery());
47: assertEquals(3, hits.length());
48:
49: // some artificial queries to trigger the use of skipTo():
50:
51: BooleanQuery bq = new BooleanQuery();
52: bq.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
53: bq.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
54: hits = is.search(bq);
55: assertEquals(3, hits.length());
56:
57: bq = new BooleanQuery();
58: bq.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
59: bq.add(new TermQuery(new Term("key", "three")),
60: BooleanClause.Occur.MUST);
61: hits = is.search(bq);
62: assertEquals(1, hits.length());
63:
64: // delete a document:
65: is.getIndexReader().deleteDocument(0);
66: hits = is.search(new MatchAllDocsQuery());
67: assertEquals(2, hits.length());
68:
69: is.close();
70: }
71:
72: public void testEquals() {
73: Query q1 = new MatchAllDocsQuery();
74: Query q2 = new MatchAllDocsQuery();
75: assertTrue(q1.equals(q2));
76: q1.setBoost(1.5f);
77: assertFalse(q1.equals(q2));
78: }
79:
80: private void addDoc(String text, IndexWriter iw) throws IOException {
81: Document doc = new Document();
82: doc.add(new Field("key", text, Field.Store.YES,
83: Field.Index.TOKENIZED));
84: iw.addDocument(doc);
85: }
86:
87: }
|