add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / queryparser / src / test / org / apache / lucene / queryParser / ext / TestExtendableQueryParser.java
1 package org.apache.lucene.queryParser.ext;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import org.apache.lucene.analysis.Analyzer;
21 import org.apache.lucene.analysis.MockAnalyzer;
22 import org.apache.lucene.analysis.MockTokenizer;
23 import org.apache.lucene.queryParser.ParseException;
24 import org.apache.lucene.queryParser.QueryParser;
25 import org.apache.lucene.queryParser.QueryParserTestBase;
26 import org.apache.lucene.search.BooleanClause;
27 import org.apache.lucene.search.BooleanQuery;
28 import org.apache.lucene.search.Query;
29 import org.apache.lucene.search.TermQuery;
30
31 /**
32  * Testcase for the class {@link ExtendableQueryParser}
33  */
34 public class TestExtendableQueryParser extends QueryParserTestBase {
35   private static char[] DELIMITERS = new char[] {
36       Extensions.DEFAULT_EXTENSION_FIELD_DELIMITER, '-', '|' };
37
38   @Override
39   public QueryParser getParser(Analyzer a) throws Exception {
40     return getParser(a, null);
41   }
42
43   public QueryParser getParser(Analyzer a, Extensions extensions)
44       throws Exception {
45     if (a == null)
46       a = new MockAnalyzer(random, MockTokenizer.SIMPLE, true);
47     QueryParser qp = extensions == null ? new ExtendableQueryParser(
48         TEST_VERSION_CURRENT, "field", a) : new ExtendableQueryParser(
49         TEST_VERSION_CURRENT, "field", a, extensions);
50     qp.setDefaultOperator(QueryParser.OR_OPERATOR);
51     return qp;
52   }
53
54   public void testUnescapedExtDelimiter() throws Exception {
55     Extensions ext = newExtensions(':');
56     ext.add("testExt", new ExtensionStub());
57     ExtendableQueryParser parser = (ExtendableQueryParser) getParser(null, ext);
58     try {
59       parser.parse("aField:testExt:\"foo \\& bar\"");
60       fail("extension field delimiter is not escaped");
61     } catch (ParseException e) {
62     }
63   }
64
65   public void testExtFieldUnqoted() throws Exception {
66     for (int i = 0; i < DELIMITERS.length; i++) {
67       Extensions ext = newExtensions(DELIMITERS[i]);
68       ext.add("testExt", new ExtensionStub());
69       ExtendableQueryParser parser = (ExtendableQueryParser) getParser(null,
70           ext);
71       String field = ext.buildExtensionField("testExt", "aField");
72       Query query = parser.parse(String.format("%s:foo bar", field));
73       assertTrue("expected instance of BooleanQuery but was "
74           + query.getClass(), query instanceof BooleanQuery);
75       BooleanQuery bquery = (BooleanQuery) query;
76       BooleanClause[] clauses = bquery.getClauses();
77       assertEquals(2, clauses.length);
78       BooleanClause booleanClause = clauses[0];
79       query = booleanClause.getQuery();
80       assertTrue("expected instance of TermQuery but was " + query.getClass(),
81           query instanceof TermQuery);
82       TermQuery tquery = (TermQuery) query;
83       assertEquals("aField", tquery.getTerm()
84           .field());
85       assertEquals("foo", tquery.getTerm().text());
86
87       booleanClause = clauses[1];
88       query = booleanClause.getQuery();
89       assertTrue("expected instance of TermQuery but was " + query.getClass(),
90           query instanceof TermQuery);
91       tquery = (TermQuery) query;
92       assertEquals("field", tquery.getTerm().field());
93       assertEquals("bar", tquery.getTerm().text());
94     }
95   }
96
97   public void testExtDefaultField() throws Exception {
98     for (int i = 0; i < DELIMITERS.length; i++) {
99       Extensions ext = newExtensions(DELIMITERS[i]);
100       ext.add("testExt", new ExtensionStub());
101       ExtendableQueryParser parser = (ExtendableQueryParser) getParser(null,
102           ext);
103       String field = ext.buildExtensionField("testExt");
104       Query parse = parser.parse(String.format("%s:\"foo \\& bar\"", field));
105       assertTrue("expected instance of TermQuery but was " + parse.getClass(),
106           parse instanceof TermQuery);
107       TermQuery tquery = (TermQuery) parse;
108       assertEquals("field", tquery.getTerm().field());
109       assertEquals("foo & bar", tquery.getTerm().text());
110     }
111   }
112
113   public Extensions newExtensions(char delimiter) {
114     return new Extensions(delimiter);
115   }
116
117   public void testExtField() throws Exception {
118     for (int i = 0; i < DELIMITERS.length; i++) {
119       Extensions ext = newExtensions(DELIMITERS[i]);
120       ext.add("testExt", new ExtensionStub());
121       ExtendableQueryParser parser = (ExtendableQueryParser) getParser(null,
122           ext);
123       String field = ext.buildExtensionField("testExt", "afield");
124       Query parse = parser.parse(String.format("%s:\"foo \\& bar\"", field));
125       assertTrue("expected instance of TermQuery but was " + parse.getClass(),
126           parse instanceof TermQuery);
127       TermQuery tquery = (TermQuery) parse;
128       assertEquals("afield", tquery.getTerm().field());
129       assertEquals("foo & bar", tquery.getTerm().text());
130     }
131   }
132
133 }