001: package org.apache.lucene.search;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import org.apache.lucene.analysis.WhitespaceAnalyzer;
021: import org.apache.lucene.document.Document;
022: import org.apache.lucene.document.Field;
023: import org.apache.lucene.index.IndexWriter;
024: import org.apache.lucene.index.Term;
025: import org.apache.lucene.store.RAMDirectory;
026:
027: import org.apache.lucene.util.LuceneTestCase;
028: import java.io.IOException;
029:
030: /**
031: * @author goller
032: */
033: public class TestRangeQuery extends LuceneTestCase {
034:
035: private int docCount = 0;
036: private RAMDirectory dir;
037:
038: public void setUp() throws Exception {
039: super .setUp();
040: dir = new RAMDirectory();
041: }
042:
043: public void testExclusive() throws Exception {
044: Query query = new RangeQuery(new Term("content", "A"),
045: new Term("content", "C"), false);
046: initializeIndex(new String[] { "A", "B", "C", "D" });
047: IndexSearcher searcher = new IndexSearcher(dir);
048: Hits hits = searcher.search(query);
049: assertEquals("A,B,C,D, only B in range", 1, hits.length());
050: searcher.close();
051:
052: initializeIndex(new String[] { "A", "B", "D" });
053: searcher = new IndexSearcher(dir);
054: hits = searcher.search(query);
055: assertEquals("A,B,D, only B in range", 1, hits.length());
056: searcher.close();
057:
058: addDoc("C");
059: searcher = new IndexSearcher(dir);
060: hits = searcher.search(query);
061: assertEquals("C added, still only B in range", 1, hits.length());
062: searcher.close();
063: }
064:
065: public void testInclusive() throws Exception {
066: Query query = new RangeQuery(new Term("content", "A"),
067: new Term("content", "C"), true);
068:
069: initializeIndex(new String[] { "A", "B", "C", "D" });
070: IndexSearcher searcher = new IndexSearcher(dir);
071: Hits hits = searcher.search(query);
072: assertEquals("A,B,C,D - A,B,C in range", 3, hits.length());
073: searcher.close();
074:
075: initializeIndex(new String[] { "A", "B", "D" });
076: searcher = new IndexSearcher(dir);
077: hits = searcher.search(query);
078: assertEquals("A,B,D - A and B in range", 2, hits.length());
079: searcher.close();
080:
081: addDoc("C");
082: searcher = new IndexSearcher(dir);
083: hits = searcher.search(query);
084: assertEquals("C added - A, B, C in range", 3, hits.length());
085: searcher.close();
086: }
087:
088: public void testEqualsHashcode() {
089: Query query = new RangeQuery(new Term("content", "A"),
090: new Term("content", "C"), true);
091: query.setBoost(1.0f);
092: Query other = new RangeQuery(new Term("content", "A"),
093: new Term("content", "C"), true);
094: other.setBoost(1.0f);
095:
096: assertEquals("query equals itself is true", query, query);
097: assertEquals("equivalent queries are equal", query, other);
098: assertEquals(
099: "hashcode must return same value when equals is true",
100: query.hashCode(), other.hashCode());
101:
102: other.setBoost(2.0f);
103: assertFalse("Different boost queries are not equal", query
104: .equals(other));
105:
106: other = new RangeQuery(new Term("notcontent", "A"), new Term(
107: "notcontent", "C"), true);
108: assertFalse("Different fields are not equal", query
109: .equals(other));
110:
111: other = new RangeQuery(new Term("content", "X"), new Term(
112: "content", "C"), true);
113: assertFalse("Different lower terms are not equal", query
114: .equals(other));
115:
116: other = new RangeQuery(new Term("content", "A"), new Term(
117: "content", "Z"), true);
118: assertFalse("Different upper terms are not equal", query
119: .equals(other));
120:
121: query = new RangeQuery(null, new Term("content", "C"), true);
122: other = new RangeQuery(null, new Term("content", "C"), true);
123: assertEquals(
124: "equivalent queries with null lowerterms are equal()",
125: query, other);
126: assertEquals(
127: "hashcode must return same value when equals is true",
128: query.hashCode(), other.hashCode());
129:
130: query = new RangeQuery(new Term("content", "C"), null, true);
131: other = new RangeQuery(new Term("content", "C"), null, true);
132: assertEquals(
133: "equivalent queries with null upperterms are equal()",
134: query, other);
135: assertEquals("hashcode returns same value", query.hashCode(),
136: other.hashCode());
137:
138: query = new RangeQuery(null, new Term("content", "C"), true);
139: other = new RangeQuery(new Term("content", "C"), null, true);
140: assertFalse(
141: "queries with different upper and lower terms are not equal",
142: query.equals(other));
143:
144: query = new RangeQuery(new Term("content", "A"), new Term(
145: "content", "C"), false);
146: other = new RangeQuery(new Term("content", "A"), new Term(
147: "content", "C"), true);
148: assertFalse("queries with different inclusive are not equal",
149: query.equals(other));
150: }
151:
152: private void initializeIndex(String[] values) throws IOException {
153: IndexWriter writer = new IndexWriter(dir,
154: new WhitespaceAnalyzer(), true);
155: for (int i = 0; i < values.length; i++) {
156: insertDoc(writer, values[i]);
157: }
158: writer.close();
159: }
160:
161: private void addDoc(String content) throws IOException {
162: IndexWriter writer = new IndexWriter(dir,
163: new WhitespaceAnalyzer(), false);
164: insertDoc(writer, content);
165: writer.close();
166: }
167:
168: private void insertDoc(IndexWriter writer, String content)
169: throws IOException {
170: Document doc = new Document();
171:
172: doc.add(new Field("id", "id" + docCount, Field.Store.YES,
173: Field.Index.UN_TOKENIZED));
174: doc.add(new Field("content", content, Field.Store.NO,
175: Field.Index.TOKENIZED));
176:
177: writer.addDocument(doc);
178: docCount++;
179: }
180: }
|