lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cpoersc...@apache.org
Subject [1/2] lucene-solr:branch_5x: LUCENE-7210: Make TestCore*Parser's analyzer choice override-able. (Christine Poerschke, Daniel Collins)
Date Fri, 15 Apr 2016 10:32:00 GMT
Repository: lucene-solr
Updated Branches:
  refs/heads/branch_5x 34b99fdb1 -> 2cf9e1d00


LUCENE-7210: Make TestCore*Parser's analyzer choice override-able. (Christine Poerschke, Daniel
Collins)

(Merge conflicts resolved.)


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/b61b8cd5
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/b61b8cd5
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/b61b8cd5

Branch: refs/heads/branch_5x
Commit: b61b8cd5c881c76bfcc834ef3eb1c3f1a6c73287
Parents: 34b99fd
Author: Christine Poerschke <cpoerschke@apache.org>
Authored: Thu Apr 14 15:37:41 2016 +0100
Committer: Christine Poerschke <cpoerschke@apache.org>
Committed: Fri Apr 15 11:03:41 2016 +0100

----------------------------------------------------------------------
 .../xml/CoreParserTestIndexData.java            | 72 ++++++++++++++++
 .../lucene/queryparser/xml/TestCoreParser.java  | 87 +++++++++-----------
 .../xml/TestCorePlusExtensionsParser.java       | 19 ++---
 .../xml/TestCorePlusQueriesParser.java          | 17 +---
 4 files changed, 121 insertions(+), 74 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/b61b8cd5/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/CoreParserTestIndexData.java
----------------------------------------------------------------------
diff --git a/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/CoreParserTestIndexData.java
b/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/CoreParserTestIndexData.java
new file mode 100644
index 0000000..3a7610f
--- /dev/null
+++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/CoreParserTestIndexData.java
@@ -0,0 +1,72 @@
+/*
+ * 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.IntField;
+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 IntField("date2", Integer.valueOf(date), Field.Store.NO));
+      writer.addDocument(doc);
+      line = d.readLine();
+    }
+    d.close();
+    writer.close();
+    reader = DirectoryReader.open(dir);
+    searcher = LuceneTestCase.newSearcher(reader);
+  }
+
+  @Override
+  public void close() throws IOException {
+    reader.close();
+    dir.close();
+  }
+
+}
+

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/b61b8cd5/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/TestCoreParser.java
----------------------------------------------------------------------
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.java
index 8e369ec..e60a837 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,76 +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.IntField;
-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;
-  protected 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 IntField("date2", Integer.valueOf(date), Field.Store.NO));
-      writer.addDocument(doc);
-      line = d.readLine();
-    }
-    d.close();
-    writer.close();
-    reader = DirectoryReader.open(dir);
-    searcher = newSearcher(reader);
+    return new MockAnalyzer(random(), MockTokenizer.WHITESPACE, true, MockTokenFilter.ENGLISH_STOPSET);
+  }
 
+  protected CoreParser newCoreParser(String defaultField, Analyzer analyzer) {
+    return new CoreParser(defaultField, analyzer);
   }
 
   @AfterClass
   public 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 {
@@ -136,7 +101,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);
   }
 
@@ -187,13 +152,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);
@@ -203,13 +193,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) {

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/b61b8cd5/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/TestCorePlusExtensionsParser.java
----------------------------------------------------------------------
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.java
index 446895b..affbb2b 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,7 +16,9 @@
  */
 package org.apache.lucene.queryparser.xml;
 
+import org.apache.lucene.analysis.Analyzer;
 import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.search.IndexSearcher;
 import org.apache.lucene.search.Query;
 import org.junit.Assume;
 
@@ -25,7 +27,9 @@ import java.util.List;
 
 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");
@@ -37,6 +41,7 @@ public class TestCorePlusExtensionsParser extends TestCorePlusQueriesParser
{
   }
 
   public void testDuplicateFilterQueryXML() throws ParserException, IOException {
+    final IndexSearcher searcher = searcher();
     List<LeafReaderContext> leaves = searcher.getTopReaderContext().leaves();
     Assume.assumeTrue(leaves.size() == 1);
     Query q = parse("DuplicateFilterQuery.xml");
@@ -44,16 +49,4 @@ public class TestCorePlusExtensionsParser extends TestCorePlusQueriesParser
{
     assertEquals("DuplicateFilterQuery should produce 1 result ", 1, h);
   }
 
-  //================= Helper methods ===================================
-
-  @Override
-  protected CoreParser coreParser() {
-    if (corePlusExtensionsParser == null) {
-      corePlusExtensionsParser = new CorePlusExtensionsParser(
-          super.defaultField(),
-          super.analyzer());
-    }
-    return corePlusExtensionsParser;
-  }
-
 }

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/b61b8cd5/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/TestCorePlusQueriesParser.java
----------------------------------------------------------------------
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.java
index 9e88a69..0675b4b 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,13 +16,16 @@
  */
 package org.apache.lucene.queryparser.xml;
 
+import org.apache.lucene.analysis.Analyzer;
 import org.apache.lucene.search.Query;
 
 import java.io.IOException;
 
 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");
@@ -44,16 +47,4 @@ public class TestCorePlusQueriesParser extends TestCoreParser {
     dumpResults("Boolean filter", q, 5);
   }
 
-  //================= Helper methods ===================================
-
-  @Override
-  protected CoreParser coreParser() {
-    if (corePlusQueriesParser == null) {
-      corePlusQueriesParser = new CorePlusQueriesParser(
-          super.defaultField(),
-          super.analyzer());
-    }
-    return corePlusQueriesParser;
-  }
-
 }


Mime
View raw message