LUCENE-2190: Add testcase for rewrite. Also add missing annotation for other test added by Mike. CustomScore uses JUnit4, so @Test is needed. We should somehow customize LuceneTestCaseJ4 to emulate old behaviour, else we get lots of tests without annotation never ran.

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@912508 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2010-02-22 08:27:05 +00:00
parent ee36dda5e7
commit a79929bc24
1 changed files with 25 additions and 0 deletions

View File

@ -29,6 +29,7 @@ import java.util.HashMap;
import java.util.Map;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
/**
* Test CustomScoreQuery search.
@ -188,6 +189,7 @@ public class TestCustomScoreQuery extends FunctionTestSetup {
}
}
@Test
public void testCustomExternalQuery() throws Exception {
QueryParser qp = new QueryParser(TEST_VERSION_CURRENT, TEXT_FIELD,anlzr);
String qtxt = "first aid text"; // from the doc texts in FunctionQuerySetup.
@ -207,6 +209,29 @@ public class TestCustomScoreQuery extends FunctionTestSetup {
s.close();
}
@Test
public void testRewrite() throws Exception {
final IndexSearcher s = new IndexSearcher(dir, true);
Query q = new TermQuery(new Term(TEXT_FIELD, "first"));
CustomScoreQuery original = new CustomScoreQuery(q);
CustomScoreQuery rewritten = (CustomScoreQuery) original.rewrite(s.getIndexReader());
assertTrue("rewritten query should be identical, as TermQuery does not rewrite", original == rewritten);
assertTrue("no hits for query", s.search(rewritten,1).totalHits > 0);
assertEquals(s.search(q,1).totalHits, s.search(rewritten,1).totalHits);
q = new TermRangeQuery(TEXT_FIELD, null, null, true, true); // everything
original = new CustomScoreQuery(q);
rewritten = (CustomScoreQuery) original.rewrite(s.getIndexReader());
assertTrue("rewritten query should not be identical, as TermRangeQuery rewrites", original != rewritten);
assertTrue("rewritten query should be a CustomScoreQuery", rewritten instanceof CustomScoreQuery);
assertTrue("no hits for query", s.search(rewritten,1).totalHits > 0);
assertEquals(s.search(q,1).totalHits, s.search(original,1).totalHits);
assertEquals(s.search(q,1).totalHits, s.search(rewritten,1).totalHits);
s.close();
}
// Test that FieldScoreQuery returns docs with expected score.
private void doTestCustomScore(String field, FieldScoreQuery.Type tp, double dboost) throws Exception, ParseException {
float boost = (float) dboost;