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.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));