streamlining test case a bit

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@150053 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Erik Hatcher 2003-09-20 21:12:41 +00:00
parent 1e5eccf552
commit c31601d073
1 changed files with 42 additions and 54 deletions

View File

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