Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
79   256   23   4.94
14   162   0.29   2.29
16     1.44  
7    
 
  TestMultiAnalyzer       Line # 38 36 3 100% 1.0
  TestMultiAnalyzer.MultiAnalyzer       Line # 128 4 2 100% 1.0
  TestMultiAnalyzer.TestFilter       Line # 141 18 6 100% 1.0
  TestMultiAnalyzer.PosIncrementAnalyzer       Line # 180 4 2 100% 1.0
  TestMultiAnalyzer.TestPosIncrementFilter       Line # 193 11 5 100% 1.0
  TestMultiAnalyzer.DumbQueryParser       Line # 222 3 3 100% 1.0
  TestMultiAnalyzer.DumbQueryWrapper       Line # 244 3 2 100% 1.0
 
  (3)
 
1    package org.apache.lucene.queryParser;
2   
3    /**
4    * Copyright 2004 The Apache Software Foundation
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10    * http://www.apache.org/licenses/LICENSE-2.0
11    *
12    * Unless required by applicable law or agreed to in writing, software
13    * distributed under the License is distributed on an "AS IS" BASIS,
14    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    * See the License for the specific language governing permissions and
16    * limitations under the License.
17    */
18   
19    import java.io.Reader;
20   
21    import junit.framework.TestCase;
22   
23    import org.apache.lucene.search.Query;
24    import org.apache.lucene.analysis.Analyzer;
25    import org.apache.lucene.analysis.LowerCaseFilter;
26    import org.apache.lucene.analysis.Token;
27    import org.apache.lucene.analysis.TokenFilter;
28    import org.apache.lucene.analysis.TokenStream;
29    import org.apache.lucene.analysis.standard.StandardTokenizer;
30   
31    /**
32    * Test QueryParser's ability to deal with Analyzers that return more
33    * than one token per position or that return tokens with a position
34    * increment > 1.
35    *
36    * @author Daniel Naber
37    */
 
38    public class TestMultiAnalyzer extends TestCase {
39   
40    private static int multiToken = 0;
41   
 
42  1 toggle public void testMultiAnalyzer() throws ParseException {
43   
44  1 QueryParser qp = new QueryParser("", new MultiAnalyzer());
45   
46    // trivial, no multiple tokens:
47  1 assertEquals("foo", qp.parse("foo").toString());
48  1 assertEquals("foo", qp.parse("\"foo\"").toString());
49  1 assertEquals("foo foobar", qp.parse("foo foobar").toString());
50  1 assertEquals("\"foo foobar\"", qp.parse("\"foo foobar\"").toString());
51  1 assertEquals("\"foo foobar blah\"", qp.parse("\"foo foobar blah\"").toString());
52   
53    // two tokens at the same position:
54  1 assertEquals("(multi multi2) foo", qp.parse("multi foo").toString());
55  1 assertEquals("foo (multi multi2)", qp.parse("foo multi").toString());
56  1 assertEquals("(multi multi2) (multi multi2)", qp.parse("multi multi").toString());
57  1 assertEquals("+(foo (multi multi2)) +(bar (multi multi2))",
58    qp.parse("+(foo multi) +(bar multi)").toString());
59  1 assertEquals("+(foo (multi multi2)) field:\"bar (multi multi2)\"",
60    qp.parse("+(foo multi) field:\"bar multi\"").toString());
61   
62    // phrases:
63  1 assertEquals("\"(multi multi2) foo\"", qp.parse("\"multi foo\"").toString());
64  1 assertEquals("\"foo (multi multi2)\"", qp.parse("\"foo multi\"").toString());
65  1 assertEquals("\"foo (multi multi2) foobar (multi multi2)\"",
66    qp.parse("\"foo multi foobar multi\"").toString());
67   
68    // fields:
69  1 assertEquals("(field:multi field:multi2) field:foo", qp.parse("field:multi field:foo").toString());
70  1 assertEquals("field:\"(multi multi2) foo\"", qp.parse("field:\"multi foo\"").toString());
71   
72    // three tokens at one position:
73  1 assertEquals("triplemulti multi3 multi2", qp.parse("triplemulti").toString());
74  1 assertEquals("foo (triplemulti multi3 multi2) foobar",
75    qp.parse("foo triplemulti foobar").toString());
76   
77    // phrase with non-default slop:
78  1 assertEquals("\"(multi multi2) foo\"~10", qp.parse("\"multi foo\"~10").toString());
79   
80    // phrase with non-default boost:
81  1 assertEquals("\"(multi multi2) foo\"^2.0", qp.parse("\"multi foo\"^2").toString());
82   
83    // phrase after changing default slop
84  1 qp.setPhraseSlop(99);
85  1 assertEquals("\"(multi multi2) foo\"~99 bar",
86    qp.parse("\"multi foo\" bar").toString());
87  1 assertEquals("\"(multi multi2) foo\"~99 \"foo bar\"~2",
88    qp.parse("\"multi foo\" \"foo bar\"~2").toString());
89  1 qp.setPhraseSlop(0);
90   
91    // non-default operator:
92  1 qp.setDefaultOperator(QueryParser.AND_OPERATOR);
93  1 assertEquals("+(multi multi2) +foo", qp.parse("multi foo").toString());
94   
95    }
96   
 
97  1 toggle public void testMultiAnalyzerWithSubclassOfQueryParser() throws ParseException {
98   
99  1 DumbQueryParser qp = new DumbQueryParser("", new MultiAnalyzer());
100  1 qp.setPhraseSlop(99); // modified default slop
101   
102    // direct call to (super's) getFieldQuery to demonstrate differnce
103    // between phrase and multiphrase with modified default slop
104  1 assertEquals("\"foo bar\"~99",
105    qp.getSuperFieldQuery("","foo bar").toString());
106  1 assertEquals("\"(multi multi2) bar\"~99",
107    qp.getSuperFieldQuery("","multi bar").toString());
108   
109   
110    // ask sublcass to parse phrase with modified default slop
111  1 assertEquals("\"(multi multi2) foo\"~99 bar",
112    qp.parse("\"multi foo\" bar").toString());
113   
114    }
115   
 
116  1 toggle public void testPosIncrementAnalyzer() throws ParseException {
117  1 QueryParser qp = new QueryParser("", new PosIncrementAnalyzer());
118  1 assertEquals("quick brown", qp.parse("the quick brown").toString());
119  1 assertEquals("\"quick brown\"", qp.parse("\"the quick brown\"").toString());
120  1 assertEquals("quick brown fox", qp.parse("the quick brown fox").toString());
121  1 assertEquals("\"quick brown fox\"", qp.parse("\"the quick brown fox\"").toString());
122    }
123   
124    /**
125    * Expands "multi" to "multi" and "multi2", both at the same position,
126    * and expands "triplemulti" to "triplemulti", "multi3", and "multi2".
127    */
 
128    private class MultiAnalyzer extends Analyzer {
129   
 
130  2 toggle public MultiAnalyzer() {
131  &n