lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From busc...@apache.org
Subject svn commit: r515914 - in /lucene/java/trunk: ./ src/java/org/apache/lucene/queryParser/ src/test/org/apache/lucene/queryParser/
Date Thu, 08 Mar 2007 04:18:57 GMT
Author: buschmi
Date: Wed Mar  7 20:18:56 2007
New Revision: 515914

URL: http://svn.apache.org/viewvc?view=rev&rev=515914
Log:
LUCENE-372: QueryParser.parse() now ensures that the entire input string is consumed.

Modified:
    lucene/java/trunk/CHANGES.txt
    lucene/java/trunk/src/java/org/apache/lucene/queryParser/QueryParser.java
    lucene/java/trunk/src/java/org/apache/lucene/queryParser/QueryParser.jj
    lucene/java/trunk/src/test/org/apache/lucene/queryParser/TestMultiFieldQueryParser.java
    lucene/java/trunk/src/test/org/apache/lucene/queryParser/TestQueryParser.java

Modified: lucene/java/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/java/trunk/CHANGES.txt?view=diff&rev=515914&r1=515913&r2=515914
==============================================================================
--- lucene/java/trunk/CHANGES.txt (original)
+++ lucene/java/trunk/CHANGES.txt Wed Mar  7 20:18:56 2007
@@ -53,6 +53,10 @@
     list to contain all possible characters, because every character that 
     follows a backslash should be considered as escaped. (Michael Busch)
 
+ 8. LUCENE-372: QueryParser.parse() now ensures that the entire input string 
+    is consumed. Now a ParseException is thrown if a query contains too many
+    closing parentheses. (Andreas Neumann via Michael Busch)
+
 New features
 
  1. LUCENE-759: Added two n-gram-producing TokenFilters.

Modified: lucene/java/trunk/src/java/org/apache/lucene/queryParser/QueryParser.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/queryParser/QueryParser.java?view=diff&rev=515914&r1=515913&r2=515914
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/queryParser/QueryParser.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/queryParser/QueryParser.java Wed Mar  7 20:18:56
2007
@@ -142,7 +142,8 @@
   public Query parse(String query) throws ParseException {
     ReInit(new FastCharStream(new StringReader(query)));
     try {
-      return Query(field);
+          // TopLevelQuery is a Query followed by the end-of-input (EOF)
+      return TopLevelQuery(field);
     }
     catch (ParseException tme) {
       // rethrow to include the original query:
@@ -881,6 +882,15 @@
       ;
     }
     {if (true) return ret;}
+    throw new Error("Missing return statement in function");
+  }
+
+// This makes sure that there is no garbage after the query string
+  final public Query TopLevelQuery(String field) throws ParseException {
+        Query q;
+    q = Query(field);
+    jj_consume_token(0);
+                {if (true) return q;}
     throw new Error("Missing return statement in function");
   }
 

Modified: lucene/java/trunk/src/java/org/apache/lucene/queryParser/QueryParser.jj
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/queryParser/QueryParser.jj?view=diff&rev=515914&r1=515913&r2=515914
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/queryParser/QueryParser.jj (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/queryParser/QueryParser.jj Wed Mar  7 20:18:56
2007
@@ -166,7 +166,8 @@
   public Query parse(String query) throws ParseException {
     ReInit(new FastCharStream(new StringReader(query)));
     try {
-      return Query(field);
+	  // TopLevelQuery is a Query followed by the end-of-input (EOF)
+      return TopLevelQuery(field);
     }
     catch (ParseException tme) {
       // rethrow to include the original query:
@@ -929,6 +930,18 @@
      | <NOT> { ret = MOD_NOT; }
   ]
   { return ret; }
+}
+
+// This makes sure that there is no garbage after the query string
+Query TopLevelQuery(String field) : 
+{
+	Query q;
+}
+{
+	q=Query(field) <EOF>
+	{
+		return q;
+	}
 }
 
 Query Query(String field) :

Modified: lucene/java/trunk/src/test/org/apache/lucene/queryParser/TestMultiFieldQueryParser.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/test/org/apache/lucene/queryParser/TestMultiFieldQueryParser.java?view=diff&rev=515914&r1=515913&r2=515914
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/queryParser/TestMultiFieldQueryParser.java
(original)
+++ lucene/java/trunk/src/test/org/apache/lucene/queryParser/TestMultiFieldQueryParser.java
Wed Mar  7 20:18:56 2007
@@ -55,7 +55,7 @@
     q = mfqp.parse("+one +two");
     assertEquals("+(b:one t:one) +(b:two t:two)", q.toString());
 
-    q = mfqp.parse("+one -two -three)");
+    q = mfqp.parse("+one -two -three");
     assertEquals("+(b:one t:one) -(b:two t:two) -(b:three t:three)", q.toString());
     
     q = mfqp.parse("one^2 two");

Modified: lucene/java/trunk/src/test/org/apache/lucene/queryParser/TestQueryParser.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/test/org/apache/lucene/queryParser/TestQueryParser.java?view=diff&rev=515914&r1=515913&r2=515914
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/queryParser/TestQueryParser.java (original)
+++ lucene/java/trunk/src/test/org/apache/lucene/queryParser/TestQueryParser.java Wed Mar
 7 20:18:56 2007
@@ -298,12 +298,9 @@
     fq = (FuzzyQuery)getQuery("term~", null);
     assertEquals(0.5f, fq.getMinSimilarity(), 0.1f);
     assertEquals(FuzzyQuery.defaultPrefixLength, fq.getPrefixLength());
-    try {
-      getQuery("term~1.1", null);   // value > 1, throws exception
-      fail();
-    } catch(ParseException pe) {
-      // expected exception
-    }
+    
+    assertParseException("term~1.1"); // value > 1, throws exception
+
     assertTrue(getQuery("term*germ", null) instanceof WildcardQuery);
 
 /* Tests to see that wild card terms are (or are not) properly
@@ -566,11 +563,7 @@
     
     assertQueryEquals("c\\:\\\\temp\\\\\\~foo.txt", a, "c:\\temp\\~foo.txt");
     
-
-    try {
-        assertQueryEquals("XY\\", a, "XYZ");
-        fail("ParseException expected, not thrown");
-    } catch (ParseException expected) {}
+    assertParseException("XY\\"); // there must be a character after the escape char
     
     // test unicode escaping
     assertQueryEquals("a\\u0062c", a, "abc");
@@ -578,24 +571,16 @@
     assertQueryEquals("XY\\u005A", a, "XYZ");
     assertQueryEquals("\"a \\\\\\u0028\\u0062\\\" c\"", a, "\"a \\(b\" c\"");
     
-    try {
-        assertQueryEquals("XY\\u005G", a, "XYZ");
-        fail("ParseException expected, not thrown");
-    } catch (ParseException expected) {}
-
-    try {
-        assertQueryEquals("XY\\u005", a, "XYZ");
-        fail("ParseException expected, not thrown");
-    } catch (ParseException expected) {}
+    assertParseException("XY\\u005G");  // test non-hex character in escaped unicode sequence
+    assertParseException("XY\\u005");   // test incomplete escaped unicode sequence
     
     // Tests bug LUCENE-800
     assertQueryEquals("(item:\\\\ item:ABCD\\\\)", a, "item:\\ item:ABCD\\");
+    assertParseException("(item:\\\\ item:ABCD\\\\))"); // unmatched closing paranthesis

     assertQueryEquals("\\*", a, "*");
     assertQueryEquals("\\\\", a, "\\");  // escaped backslash
-    try {
-      assertQueryEquals("\\", a, "\\");
-      fail("ParseException expected not thrown (backslash must be escaped)");
-    } catch (ParseException expected) {}
+    
+    assertParseException("\\"); // a backslash must always be escaped
   }
 
   public void testQueryStringEscaping() throws Exception {
@@ -701,13 +686,24 @@
     assertEquals(1.0f, q.getBoost(), 0.01f);
   }
 
-  public void testException() throws Exception {
+  public void assertParseException(String queryString) throws Exception {
     try {
-      assertQueryEquals("\"some phrase", null, "abc");
-      fail("ParseException expected, not thrown");
+      Query q = getQuery(queryString, null);
     } catch (ParseException expected) {
+      return;
     }
+    fail("ParseException expected, not thrown");
+  }
+       
+  public void testException() throws Exception {
+    assertParseException("\"some phrase");
+    assertParseException("(foo bar");
+    assertParseException("foo bar))");
+    assertParseException("field:term:with:colon some more terms");
+    assertParseException("(sub query)^5.0^2.0 plus more");
+    assertParseException("secret AND illegal) AND access:confidential");
   }
+  
 
   public void testCustomQueryParserWildcard() {
     try {



Mime
View raw message