From 5deeaa3f100e77f296a3fdcfa88b4ae173d54807 Mon Sep 17 00:00:00 2001 From: Otis Gospodnetic Date: Wed, 3 May 2006 22:02:15 +0000 Subject: [PATCH] - Added testQueryStringEscaping() to test QueryParser.escape(String) method git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@399452 13f79535-47bb-0310-9956-ffa450edef68 --- .../lucene/queryParser/TestQueryParser.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/test/org/apache/lucene/queryParser/TestQueryParser.java b/src/test/org/apache/lucene/queryParser/TestQueryParser.java index 60d86cbc2ba..cea7895d7e2 100644 --- a/src/test/org/apache/lucene/queryParser/TestQueryParser.java +++ b/src/test/org/apache/lucene/queryParser/TestQueryParser.java @@ -134,6 +134,15 @@ public class TestQueryParser extends TestCase { } } + public void assertEscapedQueryEquals(String query, Analyzer a, String result) + throws Exception { + String escapedQuery = QueryParser.escape(query); + if (!escapedQuery.equals(result)) { + fail("Query /" + query + "/ yielded /" + escapedQuery + + "/, expecting /" + result + "/"); + } + } + public void assertWildcardQueryEquals(String query, boolean lowercase, String result) throws Exception { QueryParser qp = getParser(null); @@ -439,6 +448,41 @@ public class TestQueryParser extends TestCase { assertQueryEquals("[ a\\\\ TO a\\* ]", null, "[a\\ TO a*]"); } + public void testQueryStringEscaping() throws Exception { + Analyzer a = new WhitespaceAnalyzer(); + + assertEscapedQueryEquals("a-b:c", a, "a\\-b\\:c"); + assertEscapedQueryEquals("a+b:c", a, "a\\+b\\:c"); + assertEscapedQueryEquals("a:b:c", a, "a\\:b\\:c"); + assertEscapedQueryEquals("a\\b:c", a, "a\\\\b\\:c"); + + assertEscapedQueryEquals("a:b-c", a, "a\\:b\\-c"); + assertEscapedQueryEquals("a:b+c", a, "a\\:b\\+c"); + assertEscapedQueryEquals("a:b:c", a, "a\\:b\\:c"); + assertEscapedQueryEquals("a:b\\c", a, "a\\:b\\\\c"); + + assertEscapedQueryEquals("a:b-c*", a, "a\\:b\\-c\\*"); + assertEscapedQueryEquals("a:b+c*", a, "a\\:b\\+c\\*"); + assertEscapedQueryEquals("a:b:c*", a, "a\\:b\\:c\\*"); + + assertEscapedQueryEquals("a:b\\\\c*", a, "a\\:b\\\\\\\\c\\*"); + + assertEscapedQueryEquals("a:b-?c", a, "a\\:b\\-\\?c"); + assertEscapedQueryEquals("a:b+?c", a, "a\\:b\\+\\?c"); + assertEscapedQueryEquals("a:b:?c", a, "a\\:b\\:\\?c"); + + assertEscapedQueryEquals("a:b?c", a, "a\\:b\\?c"); + + assertEscapedQueryEquals("a:b-c~", a, "a\\:b\\-c\\~"); + assertEscapedQueryEquals("a:b+c~", a, "a\\:b\\+c\\~"); + assertEscapedQueryEquals("a:b:c~", a, "a\\:b\\:c\\~"); + assertEscapedQueryEquals("a:b\\c~", a, "a\\:b\\\\c\\~"); + + assertEscapedQueryEquals("[ a - TO a+ ]", null, "\\[ a \\- TO a\\+ \\]"); + assertEscapedQueryEquals("[ a : TO a~ ]", null, "\\[ a \\: TO a\\~ \\]"); + assertEscapedQueryEquals("[ a\\ TO a* ]", null, "\\[ a\\\\ TO a\\* \\]"); + } + public void testTabNewlineCarriageReturn() throws Exception { assertQueryEqualsDOA("+weltbank +worlbank", null,