001: package org.apache.lucene.queryParser.precedence;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import junit.framework.TestCase;
021: import org.apache.lucene.analysis.Analyzer;
022: import org.apache.lucene.analysis.LowerCaseTokenizer;
023: import org.apache.lucene.analysis.SimpleAnalyzer;
024: import org.apache.lucene.analysis.Token;
025: import org.apache.lucene.analysis.TokenFilter;
026: import org.apache.lucene.analysis.TokenStream;
027: import org.apache.lucene.analysis.WhitespaceAnalyzer;
028: import org.apache.lucene.analysis.standard.StandardAnalyzer;
029: import org.apache.lucene.document.DateTools;
030: import org.apache.lucene.search.BooleanQuery;
031: import org.apache.lucene.search.FuzzyQuery;
032: import org.apache.lucene.search.PhraseQuery;
033: import org.apache.lucene.search.PrefixQuery;
034: import org.apache.lucene.search.Query;
035: import org.apache.lucene.search.RangeQuery;
036: import org.apache.lucene.search.TermQuery;
037: import org.apache.lucene.search.WildcardQuery;
038:
039: import java.io.IOException;
040: import java.io.Reader;
041: import java.text.DateFormat;
042: import java.util.Calendar;
043:
044: public class TestPrecedenceQueryParser extends TestCase {
045:
046: public static Analyzer qpAnalyzer = new QPTestAnalyzer();
047:
048: public static class QPTestFilter extends TokenFilter {
049: /**
050: * Filter which discards the token 'stop' and which expands the
051: * token 'phrase' into 'phrase1 phrase2'
052: */
053: public QPTestFilter(TokenStream in) {
054: super (in);
055: }
056:
057: boolean inPhrase = false;
058: int savedStart = 0, savedEnd = 0;
059:
060: public Token next() throws IOException {
061: if (inPhrase) {
062: inPhrase = false;
063: return new Token("phrase2", savedStart, savedEnd);
064: } else
065: for (Token token = input.next(); token != null; token = input
066: .next()) {
067: if (token.termText().equals("phrase")) {
068: inPhrase = true;
069: savedStart = token.startOffset();
070: savedEnd = token.endOffset();
071: return new Token("phrase1", savedStart,
072: savedEnd);
073: } else if (!token.termText().equals("stop"))
074: return token;
075: }
076: return null;
077: }
078: }
079:
080: public static class QPTestAnalyzer extends Analyzer {
081:
082: /** Filters LowerCaseTokenizer with StopFilter. */
083: public final TokenStream tokenStream(String fieldName,
084: Reader reader) {
085: return new QPTestFilter(new LowerCaseTokenizer(reader));
086: }
087: }
088:
089: public static class QPTestParser extends PrecedenceQueryParser {
090: public QPTestParser(String f, Analyzer a) {
091: super (f, a);
092: }
093:
094: protected Query getFuzzyQuery(String field, String termStr,
095: float minSimilarity) throws ParseException {
096: throw new ParseException("Fuzzy queries not allowed");
097: }
098:
099: protected Query getWildcardQuery(String field, String termStr)
100: throws ParseException {
101: throw new ParseException("Wildcard queries not allowed");
102: }
103: }
104:
105: private int originalMaxClauses;
106:
107: public void setUp() {
108: originalMaxClauses = BooleanQuery.getMaxClauseCount();
109: }
110:
111: public PrecedenceQueryParser getParser(Analyzer a) throws Exception {
112: if (a == null)
113: a = new SimpleAnalyzer();
114: PrecedenceQueryParser qp = new PrecedenceQueryParser("field", a);
115: qp.setDefaultOperator(PrecedenceQueryParser.OR_OPERATOR);
116: return qp;
117: }
118:
119: public Query getQuery(String query, Analyzer a) throws Exception {
120: return getParser(a).parse(query);
121: }
122:
123: public void assertQueryEquals(String query, Analyzer a,
124: String result) throws Exception {
125: Query q = getQuery(query, a);
126: String s = q.toString("field");
127: if (!s.equals(result)) {
128: fail("Query /" + query + "/ yielded /" + s
129: + "/, expecting /" + result + "/");
130: }
131: }
132:
133: public void assertWildcardQueryEquals(String query,
134: boolean lowercase, String result) throws Exception {
135: PrecedenceQueryParser qp = getParser(null);
136: qp.setLowercaseExpandedTerms(lowercase);
137: Query q = qp.parse(query);
138: String s = q.toString("field");
139: if (!s.equals(result)) {
140: fail("WildcardQuery /" + query + "/ yielded /" + s
141: + "/, expecting /" + result + "/");
142: }
143: }
144:
145: public void assertWildcardQueryEquals(String query, String result)
146: throws Exception {
147: PrecedenceQueryParser qp = getParser(null);
148: Query q = qp.parse(query);
149: String s = q.toString("field");
150: if (!s.equals(result)) {
151: fail("WildcardQuery /" + query + "/ yielded /" + s
152: + "/, expecting /" + result + "/");
153: }
154: }
155:
156: public Query getQueryDOA(String query, Analyzer a) throws Exception {
157: if (a == null)
158: a = new SimpleAnalyzer();
159: PrecedenceQueryParser qp = new PrecedenceQueryParser("field", a);
160: qp.setDefaultOperator(PrecedenceQueryParser.AND_OPERATOR);
161: return qp.parse(query);
162: }
163:
164: public void assertQueryEqualsDOA(String query, Analyzer a,
165: String result) throws Exception {
166: Query q = getQueryDOA(query, a);
167: String s = q.toString("field");
168: if (!s.equals(result)) {
169: fail("Query /" + query + "/ yielded /" + s
170: + "/, expecting /" + result + "/");
171: }
172: }
173:
174: // failing tests disabled since PrecedenceQueryParser
175: // is currently unmaintained
176: public void _testSimple() throws Exception {
177: assertQueryEquals("", null, "");
178:
179: assertQueryEquals("term term term", null, "term term term");
180: assertQueryEquals("türm term term", null, "türm term term");
181: assertQueryEquals("ümlaut", null, "ümlaut");
182:
183: assertQueryEquals("+a", null, "+a");
184: assertQueryEquals("-a", null, "-a");
185: assertQueryEquals("a AND b", null, "+a +b");
186: assertQueryEquals("(a AND b)", null, "+a +b");
187: assertQueryEquals("c OR (a AND b)", null, "c (+a +b)");
188: assertQueryEquals("a AND NOT b", null, "+a -b");
189: assertQueryEquals("a AND -b", null, "+a -b");
190: assertQueryEquals("a AND !b", null, "+a -b");
191: assertQueryEquals("a && b", null, "+a +b");
192: assertQueryEquals("a && ! b", null, "+a -b");
193:
194: assertQueryEquals("a OR b", null, "a b");
195: assertQueryEquals("a || b", null, "a b");
196:
197: assertQueryEquals("+term -term term", null, "+term -term term");
198: assertQueryEquals("foo:term AND field:anotherTerm", null,
199: "+foo:term +anotherterm");
200: assertQueryEquals("term AND \"phrase phrase\"", null,
201: "+term +\"phrase phrase\"");
202: assertQueryEquals("\"hello there\"", null, "\"hello there\"");
203: assertTrue(getQuery("a AND b", null) instanceof BooleanQuery);
204: assertTrue(getQuery("hello", null) instanceof TermQuery);
205: assertTrue(getQuery("\"hello there\"", null) instanceof PhraseQuery);
206:
207: assertQueryEquals("germ term^2.0", null, "germ term^2.0");
208: assertQueryEquals("(term)^2.0", null, "term^2.0");
209: assertQueryEquals("(germ term)^2.0", null, "(germ term)^2.0");
210: assertQueryEquals("term^2.0", null, "term^2.0");
211: assertQueryEquals("term^2", null, "term^2.0");
212: assertQueryEquals("\"germ term\"^2.0", null,
213: "\"germ term\"^2.0");
214: assertQueryEquals("\"term germ\"^2", null, "\"term germ\"^2.0");
215:
216: assertQueryEquals("(foo OR bar) AND (baz OR boo)", null,
217: "+(foo bar) +(baz boo)");
218: assertQueryEquals("((a OR b) AND NOT c) OR d", null,
219: "(+(a b) -c) d");
220: assertQueryEquals("+(apple \"steve jobs\") -(foo bar baz)",
221: null, "+(apple \"steve jobs\") -(foo bar baz)");
222: assertQueryEquals("+title:(dog OR cat) -author:\"bob dole\"",
223: null, "+(title:dog title:cat) -author:\"bob dole\"");
224:
225: PrecedenceQueryParser qp = new PrecedenceQueryParser("field",
226: new StandardAnalyzer());
227: // make sure OR is the default:
228: assertEquals(PrecedenceQueryParser.OR_OPERATOR, qp
229: .getDefaultOperator());
230: qp.setDefaultOperator(PrecedenceQueryParser.AND_OPERATOR);
231: assertEquals(PrecedenceQueryParser.AND_OPERATOR, qp
232: .getDefaultOperator());
233: qp.setDefaultOperator(PrecedenceQueryParser.OR_OPERATOR);
234: assertEquals(PrecedenceQueryParser.OR_OPERATOR, qp
235: .getDefaultOperator());
236:
237: assertQueryEquals("a OR !b", null, "a (-b)");
238: assertQueryEquals("a OR ! b", null, "a (-b)");
239: assertQueryEquals("a OR -b", null, "a (-b)");
240: }
241:
242: public void testPunct() throws Exception {
243: Analyzer a = new WhitespaceAnalyzer();
244: assertQueryEquals("a&b", a, "a&b");
245: assertQueryEquals("a&&b", a, "a&&b");
246: assertQueryEquals(".NET", a, ".NET");
247: }
248:
249: public void testSlop() throws Exception {
250: assertQueryEquals("\"term germ\"~2", null, "\"term germ\"~2");
251: assertQueryEquals("\"term germ\"~2 flork", null,
252: "\"term germ\"~2 flork");
253: assertQueryEquals("\"term\"~2", null, "term");
254: assertQueryEquals("\" \"~2 germ", null, "germ");
255: assertQueryEquals("\"term germ\"~2^2", null,
256: "\"term germ\"~2^2.0");
257: }
258:
259: public void testNumber() throws Exception {
260: // The numbers go away because SimpleAnalzyer ignores them
261: assertQueryEquals("3", null, "");
262: assertQueryEquals("term 1.0 1 2", null, "term");
263: assertQueryEquals("term term1 term2", null, "term term term");
264:
265: Analyzer a = new StandardAnalyzer();
266: assertQueryEquals("3", a, "3");
267: assertQueryEquals("term 1.0 1 2", a, "term 1.0 1 2");
268: assertQueryEquals("term term1 term2", a, "term term1 term2");
269: }
270:
271: // failing tests disabled since PrecedenceQueryParser
272: // is currently unmaintained
273: public void _testWildcard() throws Exception {
274: assertQueryEquals("term*", null, "term*");
275: assertQueryEquals("term*^2", null, "term*^2.0");
276: assertQueryEquals("term~", null, "term~0.5");
277: assertQueryEquals("term~0.7", null, "term~0.7");
278: assertQueryEquals("term~^2", null, "term^2.0~0.5");
279: assertQueryEquals("term^2~", null, "term^2.0~0.5");
280: assertQueryEquals("term*germ", null, "term*germ");
281: assertQueryEquals("term*germ^3", null, "term*germ^3.0");
282:
283: assertTrue(getQuery("term*", null) instanceof PrefixQuery);
284: assertTrue(getQuery("term*^2", null) instanceof PrefixQuery);
285: assertTrue(getQuery("term~", null) instanceof FuzzyQuery);
286: assertTrue(getQuery("term~0.7", null) instanceof FuzzyQuery);
287: FuzzyQuery fq = (FuzzyQuery) getQuery("term~0.7", null);
288: assertEquals(0.7f, fq.getMinSimilarity(), 0.1f);
289: assertEquals(FuzzyQuery.defaultPrefixLength, fq
290: .getPrefixLength());
291: fq = (FuzzyQuery) getQuery("term~", null);
292: assertEquals(0.5f, fq.getMinSimilarity(), 0.1f);
293: assertEquals(FuzzyQuery.defaultPrefixLength, fq
294: .getPrefixLength());
295: try {
296: getQuery("term~1.1", null); // value > 1, throws exception
297: fail();
298: } catch (ParseException pe) {
299: // expected exception
300: }
301: assertTrue(getQuery("term*germ", null) instanceof WildcardQuery);
302:
303: /* Tests to see that wild card terms are (or are not) properly
304: * lower-cased with propery parser configuration
305: */
306: // First prefix queries:
307: // by default, convert to lowercase:
308: assertWildcardQueryEquals("Term*", true, "term*");
309: // explicitly set lowercase:
310: assertWildcardQueryEquals("term*", true, "term*");
311: assertWildcardQueryEquals("Term*", true, "term*");
312: assertWildcardQueryEquals("TERM*", true, "term*");
313: // explicitly disable lowercase conversion:
314: assertWildcardQueryEquals("term*", false, "term*");
315: assertWildcardQueryEquals("Term*", false, "Term*");
316: assertWildcardQueryEquals("TERM*", false, "TERM*");
317: // Then 'full' wildcard queries:
318: // by default, convert to lowercase:
319: assertWildcardQueryEquals("Te?m", "te?m");
320: // explicitly set lowercase:
321: assertWildcardQueryEquals("te?m", true, "te?m");
322: assertWildcardQueryEquals("Te?m", true, "te?m");
323: assertWildcardQueryEquals("TE?M", true, "te?m");
324: assertWildcardQueryEquals("Te?m*gerM", true, "te?m*germ");
325: // explicitly disable lowercase conversion:
326: assertWildcardQueryEquals("te?m", false, "te?m");
327: assertWildcardQueryEquals("Te?m", false, "Te?m");
328: assertWildcardQueryEquals("TE?M", false, "TE?M");
329: assertWildcardQueryEquals("Te?m*gerM", false, "Te?m*gerM");
330: // Fuzzy queries:
331: assertWildcardQueryEquals("Term~", "term~0.5");
332: assertWildcardQueryEquals("Term~", true, "term~0.5");
333: assertWildcardQueryEquals("Term~", false, "Term~0.5");
334: // Range queries:
335: assertWildcardQueryEquals("[A TO C]", "[a TO c]");
336: assertWildcardQueryEquals("[A TO C]", true, "[a TO c]");
337: assertWildcardQueryEquals("[A TO C]", false, "[A TO C]");
338: }
339:
340: public void testQPA() throws Exception {
341: assertQueryEquals("term term term", qpAnalyzer,
342: "term term term");
343: assertQueryEquals("term +stop term", qpAnalyzer, "term term");
344: assertQueryEquals("term -stop term", qpAnalyzer, "term term");
345: assertQueryEquals("drop AND stop AND roll", qpAnalyzer,
346: "+drop +roll");
347: assertQueryEquals("term phrase term", qpAnalyzer,
348: "term \"phrase1 phrase2\" term");
349: // note the parens in this next assertion differ from the original
350: // QueryParser behavior
351: assertQueryEquals("term AND NOT phrase term", qpAnalyzer,
352: "(+term -\"phrase1 phrase2\") term");
353: assertQueryEquals("stop", qpAnalyzer, "");
354: assertQueryEquals("stop OR stop AND stop", qpAnalyzer, "");
355: assertTrue(getQuery("term term term", qpAnalyzer) instanceof BooleanQuery);
356: assertTrue(getQuery("term +stop", qpAnalyzer) instanceof TermQuery);
357: }
358:
359: public void testRange() throws Exception {
360: assertQueryEquals("[ a TO z]", null, "[a TO z]");
361: assertTrue(getQuery("[ a TO z]", null) instanceof RangeQuery);
362: assertQueryEquals("[ a TO z ]", null, "[a TO z]");
363: assertQueryEquals("{ a TO z}", null, "{a TO z}");
364: assertQueryEquals("{ a TO z }", null, "{a TO z}");
365: assertQueryEquals("{ a TO z }^2.0", null, "{a TO z}^2.0");
366: assertQueryEquals("[ a TO z] OR bar", null, "[a TO z] bar");
367: assertQueryEquals("[ a TO z] AND bar", null, "+[a TO z] +bar");
368: assertQueryEquals("( bar blar { a TO z}) ", null,
369: "bar blar {a TO z}");
370: assertQueryEquals("gack ( bar blar { a TO z}) ", null,
371: "gack (bar blar {a TO z})");
372: }
373:
374: public String getDate(String s) throws Exception {
375: DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
376: return DateTools.dateToString(df.parse(s),
377: DateTools.Resolution.DAY);
378: }
379:
380: public String getLocalizedDate(int year, int month, int day) {
381: DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
382: Calendar calendar = Calendar.getInstance();
383: calendar.set(year, month, day);
384: return df.format(calendar.getTime());
385: }
386:
387: public void testDateRange() throws Exception {
388: String startDate = getLocalizedDate(2002, 1, 1);
389: String endDate = getLocalizedDate(2002, 1, 4);
390: assertQueryEquals("[ " + startDate + " TO " + endDate + "]",
391: null, "[" + getDate(startDate) + " TO "
392: + getDate(endDate) + "]");
393: assertQueryEquals(
394: "{ " + startDate + " " + endDate + " }", null,
395: "{" + getDate(startDate) + " TO " + getDate(endDate)
396: + "}");
397: }
398:
399: public void testEscaped() throws Exception {
400: Analyzer a = new WhitespaceAnalyzer();
401:
402: /*assertQueryEquals("\\[brackets", a, "\\[brackets");
403: assertQueryEquals("\\[brackets", null, "brackets");
404: assertQueryEquals("\\\\", a, "\\\\");
405: assertQueryEquals("\\+blah", a, "\\+blah");
406: assertQueryEquals("\\(blah", a, "\\(blah");
407:
408: assertQueryEquals("\\-blah", a, "\\-blah");
409: assertQueryEquals("\\!blah", a, "\\!blah");
410: assertQueryEquals("\\{blah", a, "\\{blah");
411: assertQueryEquals("\\}blah", a, "\\}blah");
412: assertQueryEquals("\\:blah", a, "\\:blah");
413: assertQueryEquals("\\^blah", a, "\\^blah");
414: assertQueryEquals("\\[blah", a, "\\[blah");
415: assertQueryEquals("\\]blah", a, "\\]blah");
416: assertQueryEquals("\\\"blah", a, "\\\"blah");
417: assertQueryEquals("\\(blah", a, "\\(blah");
418: assertQueryEquals("\\)blah", a, "\\)blah");
419: assertQueryEquals("\\~blah", a, "\\~blah");
420: assertQueryEquals("\\*blah", a, "\\*blah");
421: assertQueryEquals("\\?blah", a, "\\?blah");
422: //assertQueryEquals("foo \\&\\& bar", a, "foo \\&\\& bar");
423: //assertQueryEquals("foo \\|| bar", a, "foo \\|| bar");
424: //assertQueryEquals("foo \\AND bar", a, "foo \\AND bar");*/
425:
426: assertQueryEquals("a\\-b:c", a, "a-b:c");
427: assertQueryEquals("a\\+b:c", a, "a+b:c");
428: assertQueryEquals("a\\:b:c", a, "a:b:c");
429: assertQueryEquals("a\\\\b:c", a, "a\\b:c");
430:
431: assertQueryEquals("a:b\\-c", a, "a:b-c");
432: assertQueryEquals("a:b\\+c", a, "a:b+c");
433: assertQueryEquals("a:b\\:c", a, "a:b:c");
434: assertQueryEquals("a:b\\\\c", a, "a:b\\c");
435:
436: assertQueryEquals("a:b\\-c*", a, "a:b-c*");
437: assertQueryEquals("a:b\\+c*", a, "a:b+c*");
438: assertQueryEquals("a:b\\:c*", a, "a:b:c*");
439:
440: assertQueryEquals("a:b\\\\c*", a, "a:b\\c*");
441:
442: assertQueryEquals("a:b\\-?c", a, "a:b-?c");
443: assertQueryEquals("a:b\\+?c", a, "a:b+?c");
444: assertQueryEquals("a:b\\:?c", a, "a:b:?c");
445:
446: assertQueryEquals("a:b\\\\?c", a, "a:b\\?c");
447:
448: assertQueryEquals("a:b\\-c~", a, "a:b-c~0.5");
449: assertQueryEquals("a:b\\+c~", a, "a:b+c~0.5");
450: assertQueryEquals("a:b\\:c~", a, "a:b:c~0.5");
451: assertQueryEquals("a:b\\\\c~", a, "a:b\\c~0.5");
452:
453: assertQueryEquals("[ a\\- TO a\\+ ]", null, "[a- TO a+]");
454: assertQueryEquals("[ a\\: TO a\\~ ]", null, "[a: TO a~]");
455: assertQueryEquals("[ a\\\\ TO a\\* ]", null, "[a\\ TO a*]");
456: }
457:
458: public void testTabNewlineCarriageReturn() throws Exception {
459: assertQueryEqualsDOA("+weltbank +worlbank", null,
460: "+weltbank +worlbank");
461:
462: assertQueryEqualsDOA("+weltbank\n+worlbank", null,
463: "+weltbank +worlbank");
464: assertQueryEqualsDOA("weltbank \n+worlbank", null,
465: "+weltbank +worlbank");
466: assertQueryEqualsDOA("weltbank \n +worlbank", null,
467: "+weltbank +worlbank");
468:
469: assertQueryEqualsDOA("+weltbank\r+worlbank", null,
470: "+weltbank +worlbank");
471: assertQueryEqualsDOA("weltbank \r+worlbank", null,
472: "+weltbank +worlbank");
473: assertQueryEqualsDOA("weltbank \r +worlbank", null,
474: "+weltbank +worlbank");
475:
476: assertQueryEqualsDOA("+weltbank\r\n+worlbank", null,
477: "+weltbank +worlbank");
478: assertQueryEqualsDOA("weltbank \r\n+worlbank", null,
479: "+weltbank +worlbank");
480: assertQueryEqualsDOA("weltbank \r\n +worlbank", null,
481: "+weltbank +worlbank");
482: assertQueryEqualsDOA("weltbank \r \n +worlbank", null,
483: "+weltbank +worlbank");
484:
485: assertQueryEqualsDOA("+weltbank\t+worlbank", null,
486: "+weltbank +worlbank");
487: assertQueryEqualsDOA("weltbank \t+worlbank", null,
488: "+weltbank +worlbank");
489: assertQueryEqualsDOA("weltbank \t +worlbank", null,
490: "+weltbank +worlbank");
491: }
492:
493: public void testSimpleDAO() throws Exception {
494: assertQueryEqualsDOA("term term term", null,
495: "+term +term +term");
496: assertQueryEqualsDOA("term +term term", null,
497: "+term +term +term");
498: assertQueryEqualsDOA("term term +term", null,
499: "+term +term +term");
500: assertQueryEqualsDOA("term +term +term", null,
501: "+term +term +term");
502: assertQueryEqualsDOA("-term term term", null,
503: "-term +term +term");
504: }
505:
506: public void testBoost() throws Exception {
507: StandardAnalyzer oneStopAnalyzer = new StandardAnalyzer(
508: new String[] { "on" });
509: PrecedenceQueryParser qp = new PrecedenceQueryParser("field",
510: oneStopAnalyzer);
511: Query q = qp.parse("on^1.0");
512: assertNotNull(q);
513: q = qp.parse("\"hello\"^2.0");
514: assertNotNull(q);
515: assertEquals(q.getBoost(), (float) 2.0, (float) 0.5);
516: q = qp.parse("hello^2.0");
517: assertNotNull(q);
518: assertEquals(q.getBoost(), (float) 2.0, (float) 0.5);
519: q = qp.parse("\"on\"^1.0");
520: assertNotNull(q);
521:
522: q = getParser(new StandardAnalyzer()).parse("the^3");
523: assertNotNull(q);
524: }
525:
526: public void testException() throws Exception {
527: try {
528: assertQueryEquals("\"some phrase", null, "abc");
529: fail("ParseException expected, not thrown");
530: } catch (ParseException expected) {
531: }
532: }
533:
534: public void testCustomQueryParserWildcard() {
535: try {
536: new QPTestParser("contents", new WhitespaceAnalyzer())
537: .parse("a?t");
538: } catch (ParseException expected) {
539: return;
540: }
541: fail("Wildcard queries should not be allowed");
542: }
543:
544: public void testCustomQueryParserFuzzy() throws Exception {
545: try {
546: new QPTestParser("contents", new WhitespaceAnalyzer())
547: .parse("xunit~");
548: } catch (ParseException expected) {
549: return;
550: }
551: fail("Fuzzy queries should not be allowed");
552: }
553:
554: public void testBooleanQuery() throws Exception {
555: BooleanQuery.setMaxClauseCount(2);
556: try {
557: getParser(new WhitespaceAnalyzer()).parse("one two three");
558: fail("ParseException expected due to too many boolean clauses");
559: } catch (ParseException expected) {
560: // too many boolean clauses, so ParseException is expected
561: }
562: }
563:
564: /**
565: * This test differs from the original QueryParser, showing how the
566: * precedence issue has been corrected.
567: */
568: // failing tests disabled since PrecedenceQueryParser
569: // is currently unmaintained
570: public void _testPrecedence() throws Exception {
571: PrecedenceQueryParser parser = getParser(new WhitespaceAnalyzer());
572: Query query1 = parser.parse("A AND B OR C AND D");
573: Query query2 = parser.parse("(A AND B) OR (C AND D)");
574: assertEquals(query1, query2);
575:
576: query1 = parser.parse("A OR B C");
577: query2 = parser.parse("A B C");
578: assertEquals(query1, query2);
579:
580: query1 = parser.parse("A AND B C");
581: query2 = parser.parse("(+A +B) C");
582: assertEquals(query1, query2);
583:
584: query1 = parser.parse("A AND NOT B");
585: query2 = parser.parse("+A -B");
586: assertEquals(query1, query2);
587:
588: query1 = parser.parse("A OR NOT B");
589: query2 = parser.parse("A -B");
590: assertEquals(query1, query2);
591:
592: query1 = parser.parse("A OR NOT B AND C");
593: query2 = parser.parse("A (-B +C)");
594: assertEquals(query1, query2);
595: }
596:
597: public void tearDown() {
598: BooleanQuery.setMaxClauseCount(originalMaxClauses);
599: }
600:
601: }
|