From a79929bc245cbb90d2b6ba9b583cfe99c49192e7 Mon Sep 17 00:00:00 2001 From: Uwe Schindler Date: Mon, 22 Feb 2010 08:27:05 +0000 Subject: [PATCH] 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 --- .../search/function/TestCustomScoreQuery.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/test/org/apache/lucene/search/function/TestCustomScoreQuery.java b/src/test/org/apache/lucene/search/function/TestCustomScoreQuery.java index 1cb5b3289cd..0965e795563 100755 --- a/src/test/org/apache/lucene/search/function/TestCustomScoreQuery.java +++ b/src/test/org/apache/lucene/search/function/TestCustomScoreQuery.java @@ -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;