Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

LUCENE-7252: add TestCoreParser.testTermQueryEmptyXML test

Christine Poerschke9c69c4cf12f0
diff --git a/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/TermQueryEmpty.xml b/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/TermQueryEmpty.xml
new file mode 100644
index 00000000000..229a11221a9
--- /dev/null
+++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/TermQueryEmpty.xml
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+-->
+<TermQuery fieldName="contents"></TermQuery>
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 f252600d954..5f33545a18c 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
@@ -66,6 +66,11 @@ public class TestCoreParser extends LuceneTestCase {
dumpResults("TermQuery", q, 5);
}
+ public void testTermQueryEmptyXML() throws ParserException, IOException {
+ parseShouldFail("TermQueryEmpty.xml",
+ "TermQuery has no text");
+ }
+
public void testTermsQueryXML() throws ParserException, IOException {
Query q = parse("TermsQuery.xml");
dumpResults("TermsQuery", q, 5);
@@ -207,6 +212,20 @@ public class TestCoreParser extends LuceneTestCase {
return indexData().searcher;
}
+ protected void parseShouldFail(String xmlFileName, String expectedParserExceptionMessage) throws IOException {
+ Query q = null;
+ ParserException pe = null;
+ try {
+ q = parse(xmlFileName);
+ } catch (ParserException e) {
+ pe = e;
+ }
+ assertNull("for "+xmlFileName+" unexpectedly got "+q, q);
+ assertNotNull("expected a ParserException for "+xmlFileName, pe);
+ assertEquals("expected different ParserException for "+xmlFileName,
+ expectedParserExceptionMessage, pe.getMessage());
+ }
+
protected Query parse(String xmlFileName) throws ParserException, IOException {
try (InputStream xmlStream = TestCoreParser.class.getResourceAsStream(xmlFileName)) {
assertNotNull("Test XML file " + xmlFileName + " cannot be found", xmlStream);