Apache Lucene open-source search software
LUCENE-7210: Make TestCore*Parser's analyzer choice override-able. (Christine Poerschke, Daniel Collins)
Christine Poerschke8d5d20134316
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex b5cdc7c9080..decb5db6101 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -91,6 +91,9 @@ Other* LUCENE-7205: Remove repeated nl.getLength() calls in(Boolean|DisjunctionMax|FuzzyLikeThis)QueryBuilder. (Christine Poerschke)+* LUCENE-7210: Make TestCore*Parser's analyzer choice override-able+ (Christine Poerschke, Daniel Collins)+======================= Lucene 6.0.0 =======================System Requirementsdiff --git a/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/CoreParserTestIndexData.java b/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/CoreParserTestIndexData.javanew file mode 100644index 00000000000..71b627e74cd--- /dev/null+++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/CoreParserTestIndexData.java@@ -0,0 +1,74 @@+/*+ * 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.+ */+package org.apache.lucene.queryparser.xml;++import org.apache.lucene.analysis.Analyzer;+import org.apache.lucene.document.Document;+import org.apache.lucene.document.Field;+import org.apache.lucene.document.IntPoint;+import org.apache.lucene.document.LegacyIntField;+import org.apache.lucene.index.DirectoryReader;+import org.apache.lucene.index.IndexReader;+import org.apache.lucene.index.IndexWriter;+import org.apache.lucene.search.IndexSearcher;+import org.apache.lucene.store.Directory;+import org.apache.lucene.util.LuceneTestCase;++import java.io.BufferedReader;+import java.io.Closeable;+import java.io.IOException;+import java.io.InputStreamReader;+import java.nio.charset.StandardCharsets;++class CoreParserTestIndexData implements Closeable {++ final Directory dir;+ final IndexReader reader;+ final IndexSearcher searcher;++ CoreParserTestIndexData(Analyzer analyzer) throws Exception {+ BufferedReader d = new BufferedReader(new InputStreamReader(+ TestCoreParser.class.getResourceAsStream("reuters21578.txt"), StandardCharsets.US_ASCII));+ dir = LuceneTestCase.newDirectory();+ IndexWriter writer = new IndexWriter(dir, LuceneTestCase.newIndexWriterConfig(analyzer));+ String line = d.readLine();+ while (line != null) {+ int endOfDate = line.indexOf('\t');+ String date = line.substring(0, endOfDate).trim();+ String content = line.substring(endOfDate).trim();+ Document doc = new Document();+ doc.add(LuceneTestCase.newTextField("date", date, Field.Store.YES));+ doc.add(LuceneTestCase.newTextField("contents", content, Field.Store.YES));+ doc.add(new LegacyIntField("date2", Integer.valueOf(date), Field.Store.NO));+ doc.add(new IntPoint("date3", Integer.valueOf(date)));+ writer.addDocument(doc);+ line = d.readLine();+ }+ d.close();+ writer.close();+ reader = DirectoryReader.open(dir);+ searcher = LuceneTestCase.newSearcher(reader, false);+ }++ @Override+ public void close() throws IOException {+ reader.close();+ dir.close();+ }++}+diff --git a/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/TestCoreParser.java b/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/TestCoreParser.javaindex 04faa7d5f1c..9b18c81848e 100644--- a/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/TestCoreParser.java+++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/TestCoreParser.java@@ -21,78 +21,41 @@ import org.apache.lucene.analysis.MockAnalyzer;import org.apache.lucene.analysis.MockTokenFilter;import org.apache.lucene.analysis.MockTokenizer;import org.apache.lucene.document.Document;-import org.apache.lucene.document.Field;-import org.apache.lucene.document.IntPoint;-import org.apache.lucene.document.LegacyIntField;-import org.apache.lucene.index.DirectoryReader;import org.apache.lucene.index.IndexReader;-import org.apache.lucene.index.IndexWriter;import org.apache.lucene.search.DisjunctionMaxQuery;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.Query;import org.apache.lucene.search.ScoreDoc;import org.apache.lucene.search.TopDocs;-import org.apache.lucene.store.Directory;import org.apache.lucene.util.LuceneTestCase;import org.junit.AfterClass;-import org.junit.BeforeClass;-import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;-import java.io.InputStreamReader;-import java.nio.charset.StandardCharsets;-public class TestCoreParser extends LuceneTestCase {final private static String defaultField = "contents";+private static Analyzer analyzer;private static CoreParser coreParser;- private static Directory dir;- private static IndexReader reader;- private static IndexSearcher searcher;- @BeforeClass- public static void beforeClass() throws Exception {+ private static CoreParserTestIndexData indexData;++ protected Analyzer newAnalyzer() {// 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 CoreParser(defaultField, analyzer);-- BufferedReader d = new BufferedReader(new InputStreamReader(- TestCoreParser.class.getResourceAsStream("reuters21578.txt"), StandardCharsets.US_ASCII));- dir = newDirectory();- IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(analyzer));- String line = d.readLine();- while (line != null) {- int endOfDate = line.indexOf('\t');- String date = line.substring(0, endOfDate).trim();- String content = line.substring(endOfDate).trim();- Document doc = new Document();- doc.add(newTextField("date", date, Field.Store.YES));- doc.add(newTextField("contents", content, Field.Store.YES));- doc.add(new LegacyIntField("date2", Integer.valueOf(date), Field.Store.NO));- doc.add(new IntPoint("date3", Integer.valueOf(date)));- writer.addDocument(doc);- line = d.readLine();- }- d.close();- writer.close();- reader = DirectoryReader.open(dir);- searcher = newSearcher(reader, false);+ return new MockAnalyzer(random(), MockTokenizer.WHITESPACE, true, MockTokenFilter.ENGLISH_STOPSET);+ }+ protected CoreParser newCoreParser(String defaultField, Analyzer analyzer) {+ return new CoreParser(defaultField, analyzer);}@AfterClasspublic static void afterClass() throws Exception {- reader.close();- dir.close();- reader = null;- searcher = null;- dir = null;- coreParser = null;- analyzer = null;+ if (indexData != null) {+ indexData.close();+ }}public void testTermQueryXML() throws ParserException, IOException {@@ -133,7 +96,7 @@ public class TestCoreParser extends LuceneTestCase {public void testCustomFieldUserQueryXML() throws ParserException, IOException {Query q = parse("UserInputQueryCustomField.xml");- int h = searcher.search(q, 1000).totalHits;+ int h = searcher().search(q, 1000).totalHits;assertEquals("UserInputQueryCustomField should produce 0 result ", 0, h);}@@ -179,13 +142,38 @@ public class TestCoreParser extends LuceneTestCase {}protected Analyzer analyzer() {+ if (analyzer == null) {+ analyzer = newAnalyzer();+ }return analyzer;}protected CoreParser coreParser() {+ if (coreParser == null) {+ coreParser = newCoreParser(defaultField, analyzer());+ }return coreParser;}+ private CoreParserTestIndexData indexData() {+ if (indexData == null) {+ try {+ indexData = new CoreParserTestIndexData(analyzer());+ } catch (Exception e) {+ fail("caught Exception "+e);+ }+ }+ return indexData;+ }++ protected IndexReader reader() {+ return indexData().reader;+ }++ protected IndexSearcher searcher() {+ return indexData().searcher;+ }+protected Query parse(String xmlFileName) throws ParserException, IOException {try (InputStream xmlStream = TestCoreParser.class.getResourceAsStream(xmlFileName)) {assertNotNull("Test XML file " + xmlFileName + " cannot be found", xmlStream);@@ -195,13 +183,14 @@ public class TestCoreParser extends LuceneTestCase {}protected Query rewrite(Query q) throws IOException {- return q.rewrite(reader);+ return q.rewrite(reader());}protected void dumpResults(String qType, Query q, int numDocs) throws IOException {if (VERBOSE) {System.out.println("TEST: qType=" + qType + " query=" + q + " numDocs=" + numDocs);}+ final IndexSearcher searcher = searcher();TopDocs hits = searcher.search(q, numDocs);assertTrue(qType + " should produce results ", hits.totalHits > 0);if (VERBOSE) {diff --git a/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/TestCorePlusExtensionsParser.java b/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/TestCorePlusExtensionsParser.javaindex 23cfb5057a0..35f28ef1103 100644--- a/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/TestCorePlusExtensionsParser.java+++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/TestCorePlusExtensionsParser.java@@ -16,11 +16,14 @@*/package org.apache.lucene.queryparser.xml;+import org.apache.lucene.analysis.Analyzer;import org.apache.lucene.search.Query;public class TestCorePlusExtensionsParser extends TestCorePlusQueriesParser {- private CoreParser corePlusExtensionsParser;+ protected CoreParser newCoreParser(String defaultField, Analyzer analyzer) {+ return new CorePlusExtensionsParser(defaultField, analyzer);+ }public void testFuzzyLikeThisQueryXML() throws Exception {Query q = parse("FuzzyLikeThisQuery.xml");@@ -31,16 +34,4 @@ public class TestCorePlusExtensionsParser extends TestCorePlusQueriesParser {dumpResults("FuzzyLikeThis", q, 5);}- //================= Helper methods ===================================-- @Override- protected CoreParser coreParser() {- if (corePlusExtensionsParser == null) {- corePlusExtensionsParser = new CorePlusExtensionsParser(- super.defaultField(),- super.analyzer());- }- return corePlusExtensionsParser;- }-}diff --git a/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/TestCorePlusQueriesParser.java b/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/TestCorePlusQueriesParser.javaindex d87d40b7701..7e58c47c24b 100644--- a/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/TestCorePlusQueriesParser.java+++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/TestCorePlusQueriesParser.java@@ -16,11 +16,14 @@*/package org.apache.lucene.queryparser.xml;+import org.apache.lucene.analysis.Analyzer;import org.apache.lucene.search.Query;public class TestCorePlusQueriesParser extends TestCoreParser {- private CoreParser corePlusQueriesParser;+ protected CoreParser newCoreParser(String defaultField, Analyzer analyzer) {+ return new CorePlusQueriesParser(defaultField, analyzer);+ }public void testLikeThisQueryXML() throws Exception {Query q = parse("LikeThisQuery.xml");@@ -32,16 +35,4 @@ public class TestCorePlusQueriesParser extends TestCoreParser {dumpResults("boosting ", q, 5);}- //================= Helper methods ===================================-- @Override- protected CoreParser coreParser() {- if (corePlusQueriesParser == null) {- corePlusQueriesParser = new CorePlusQueriesParser(- super.defaultField(),- super.analyzer());- }- return corePlusQueriesParser;- }-}