diff --git a/src/test/org/apache/lucene/search/TestRangeQuery.java b/src/test/org/apache/lucene/search/TestRangeQuery.java index e9346b1c48b..c685ab6c968 100644 --- a/src/test/org/apache/lucene/search/TestRangeQuery.java +++ b/src/test/org/apache/lucene/search/TestRangeQuery.java @@ -5,10 +5,10 @@ import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.Term; -import org.apache.lucene.store.Directory; import org.apache.lucene.store.RAMDirectory; import junit.framework.TestCase; +import java.io.IOException; /** * @author goller @@ -16,86 +16,74 @@ import junit.framework.TestCase; public class TestRangeQuery extends TestCase { private int docCount = 0; + private RAMDirectory dir; + + public void setUp() { + dir = new RAMDirectory(); + } public void testExclusive() throws Exception { - Directory dir = new RAMDirectory(); - Query query = new RangeQuery(new Term("content", "A"), new Term("content", "C"), false); - Hits hits = null; - - IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true); - addDoc(writer, "A"); - addDoc(writer, "B"); - addDoc(writer, "C"); - addDoc(writer, "D"); - writer.close(); - + Query query = new RangeQuery(new Term("content", "A"), + new Term("content", "C"), + false); + initializeIndex(new String[] {"A", "B", "C", "D"}); IndexSearcher searcher = new IndexSearcher(dir); - hits = searcher.search(query); - assertEquals(1, hits.length()); + Hits hits = searcher.search(query); + assertEquals("A,B,C,D, only B in range", 1, hits.length()); searcher.close(); - writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true); - addDoc(writer, "A"); - addDoc(writer, "B"); - addDoc(writer, "D"); - writer.close(); - + initializeIndex(new String[] {"A", "B", "D"}); searcher = new IndexSearcher(dir); hits = searcher.search(query); - assertEquals(1, hits.length()); + assertEquals("A,B,D, only B in range", 1, hits.length()); searcher.close(); - writer = new IndexWriter(dir, new WhitespaceAnalyzer(), false); - addDoc(writer, "C"); - writer.close(); - + addDoc("C"); searcher = new IndexSearcher(dir); hits = searcher.search(query); - assertEquals(1, hits.length()); + assertEquals("C added, still only B in range", 1, hits.length()); searcher.close(); } public void testInclusive() throws Exception { - Directory dir = new RAMDirectory(); - IndexWriter writer = null; - Searcher searcher = null; - Query query = new RangeQuery(new Term("content", "A"), new Term("content", "C"), true); - Hits hits = null; + Query query = new RangeQuery(new Term("content", "A"), + new Term("content", "C"), + true); - writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true); - addDoc(writer, "A"); - addDoc(writer, "B"); - addDoc(writer, "C"); - addDoc(writer, "D"); - writer.close(); - - searcher = new IndexSearcher(dir); - hits = searcher.search(query); - assertEquals(3, hits.length()); + initializeIndex(new String[]{"A", "B", "C", "D"}); + IndexSearcher searcher = new IndexSearcher(dir); + Hits hits = searcher.search(query); + assertEquals("A,B,C,D - A,B,C in range", 3, hits.length()); searcher.close(); - writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true); - addDoc(writer, "A"); - addDoc(writer, "B"); - addDoc(writer, "D"); - writer.close(); - + initializeIndex(new String[]{"A", "B", "D"}); searcher = new IndexSearcher(dir); hits = searcher.search(query); - assertEquals(2, hits.length()); + assertEquals("A,B,D - A and B in range", 2, hits.length()); searcher.close(); - writer = new IndexWriter(dir, new WhitespaceAnalyzer(), false); - addDoc(writer, "C"); - writer.close(); - + addDoc("C"); searcher = new IndexSearcher(dir); hits = searcher.search(query); - assertEquals(3, hits.length()); + assertEquals("C added - A, B, C in range", 3, hits.length()); searcher.close(); } - private void addDoc(IndexWriter writer, String content) throws Exception { + private void initializeIndex(String[] values) throws IOException { + IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true); + for (int i = 0; i < values.length; i++) { + insertDoc(writer, values[i]); + } + writer.close(); + } + + private void addDoc(String content) throws IOException { + IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), false); + insertDoc(writer, content); + writer.close(); + } + + private void insertDoc(IndexWriter writer, String content) throws IOException { Document doc = new Document(); doc.add(Field.Keyword("id", "id" + docCount));