LUCENE-6945: factor out TestCorePlus(Queries|Extensions)Parser from TestParser

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1721381 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Christine Poerschke 2015-12-22 12:15:58 +00:00
parent fabc3b903d
commit 3064db4d73
4 changed files with 110 additions and 22 deletions

View File

@ -190,6 +190,9 @@ Other
* LUCENE-6925: add ForceMergePolicy class in test-framework
(Christine Poerschke)
* LUCENE-6945: factor out TestCorePlus(Queries|Extensions)Parser from
TestParser (Christine Poerschke)
======================= Lucene 5.4.0 =======================
New Features

View File

@ -0,0 +1,47 @@
package org.apache.lucene.queryparser.xml;
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.apache.lucene.search.Query;
public class TestCorePlusExtensionsParser extends TestCorePlusQueriesParser {
private CoreParser corePlusExtensionsParser;
public void testFuzzyLikeThisQueryXML() throws Exception {
Query q = parse("FuzzyLikeThisQuery.xml");
//show rewritten fuzzyLikeThisQuery - see what is being matched on
if (VERBOSE) {
System.out.println(rewrite(q));
}
dumpResults("FuzzyLikeThis", q, 5);
}
//================= Helper methods ===================================
@Override
protected CoreParser coreParser() {
if (corePlusExtensionsParser == null) {
corePlusExtensionsParser = new CorePlusExtensionsParser(
super.defaultField(),
super.analyzer());
}
return corePlusExtensionsParser;
}
}

View File

@ -0,0 +1,48 @@
package org.apache.lucene.queryparser.xml;
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.apache.lucene.search.Query;
public class TestCorePlusQueriesParser extends TestParser {
private CoreParser corePlusQueriesParser;
public void testLikeThisQueryXML() throws Exception {
Query q = parse("LikeThisQuery.xml");
dumpResults("like this", q, 5);
}
public void testBoostingQueryXML() throws Exception {
Query q = parse("BoostingQuery.xml");
dumpResults("boosting ", q, 5);
}
//================= Helper methods ===================================
@Override
protected CoreParser coreParser() {
if (corePlusQueriesParser == null) {
corePlusQueriesParser = new CorePlusQueriesParser(
super.defaultField(),
super.analyzer());
}
return corePlusQueriesParser;
}
}

View File

@ -47,6 +47,7 @@ import java.nio.charset.StandardCharsets;
public class TestParser extends LuceneTestCase {
final private static String defaultField = "contents";
private static Analyzer analyzer;
private static CoreParser coreParser;
private static Directory dir;
@ -58,7 +59,7 @@ public class TestParser extends LuceneTestCase {
// TODO: rewrite test (this needs to set QueryParser.enablePositionIncrements, too, for work with CURRENT):
analyzer = new MockAnalyzer(random(), MockTokenizer.WHITESPACE, true, MockTokenFilter.ENGLISH_STOPSET);
//initialize the parser
coreParser = new CorePlusExtensionsParser("contents", analyzer);
coreParser = new CoreParser(defaultField, analyzer);
BufferedReader d = new BufferedReader(new InputStreamReader(
TestParser.class.getResourceAsStream("reuters21578.txt"), StandardCharsets.US_ASCII));
@ -136,25 +137,6 @@ public class TestParser extends LuceneTestCase {
assertEquals("UserInputQueryCustomField should produce 0 result ", 0, h);
}
public void testLikeThisQueryXML() throws Exception {
Query q = parse("LikeThisQuery.xml");
dumpResults("like this", q, 5);
}
public void testBoostingQueryXML() throws Exception {
Query q = parse("BoostingQuery.xml");
dumpResults("boosting ", q, 5);
}
public void testFuzzyLikeThisQueryXML() throws Exception {
Query q = parse("FuzzyLikeThisQuery.xml");
//show rewritten fuzzyLikeThisQuery - see what is being matched on
if (VERBOSE) {
System.out.println(q.rewrite(reader));
}
dumpResults("FuzzyLikeThis", q, 5);
}
public void testBoostingTermQueryXML() throws Exception {
Query q = parse("BoostingTermQuery.xml");
dumpResults("BoostingTermQuery", q, 5);
@ -187,6 +169,10 @@ public class TestParser extends LuceneTestCase {
//================= Helper methods ===================================
protected String defaultField() {
return defaultField;
}
protected Analyzer analyzer() {
return analyzer;
}
@ -195,14 +181,18 @@ public class TestParser extends LuceneTestCase {
return coreParser;
}
private Query parse(String xmlFileName) throws ParserException, IOException {
protected Query parse(String xmlFileName) throws ParserException, IOException {
InputStream xmlStream = TestParser.class.getResourceAsStream(xmlFileName);
Query result = coreParser().parse(xmlStream);
xmlStream.close();
return result;
}
private void dumpResults(String qType, Query q, int numDocs) throws IOException {
protected Query rewrite(Query q) throws IOException {
return q.rewrite(reader);
}
protected void dumpResults(String qType, Query q, int numDocs) throws IOException {
if (VERBOSE) {
System.out.println("TEST: query=" + q);
}