Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
201   351   14   14.36
0   245   0.07   14
14     1  
1    
 
  TestPhraseQuery       Line # 36 201 14 100% 1.0
 
  (10)
 
1    package org.apache.lucene.search;
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 junit.framework.TestCase;
20    import org.apache.lucene.analysis.*;
21    import org.apache.lucene.document.*;
22    import org.apache.lucene.index.IndexWriter;
23    import org.apache.lucene.index.Term;
24    import org.apache.lucene.store.Directory;
25    import org.apache.lucene.store.RAMDirectory;
26   
27    import java.io.IOException;
28    import java.io.Reader;
29   
30    /**
31    * Tests {@link PhraseQuery}.
32    *
33    * @see TestPositionIncrement
34    * @author Erik Hatcher
35    */
 
36    public class TestPhraseQuery extends TestCase {
37    private IndexSearcher searcher;
38    private PhraseQuery query;
39    private RAMDirectory directory;
40   
 
41  10 toggle public void setUp() throws Exception {
42  10 directory = new RAMDirectory();
43  10 Analyzer analyzer = new Analyzer() {
 
44  30 toggle public TokenStream tokenStream(String fieldName, Reader reader) {
45  30 return new WhitespaceTokenizer(reader);
46    }
47   
 
48  10 toggle public int getPositionIncrementGap(String fieldName) {
49  10 return 100;
50    }
51    };
52  10 IndexWriter writer = new IndexWriter(directory, analyzer, true);
53   
54  10 Document doc = new Document();
55  10 doc.add(new Field("field", "one two three four five", Field.Store.YES, Field.Index.TOKENIZED));
56  10 doc.add(new Field("repeated", "this is a repeated field - first part", Field.Store.YES, Field.Index.TOKENIZED));
57  10 Fieldable repeatedField = new Field("repeated", "second part of a repeated field", Field.Store.YES, Field.Index.TOKENIZED);
58  10 doc.add(repeatedField);
59  10 writer.addDocument(doc);
60   
61  10 writer.optimize();
62  10 writer.close();
63   
64  10 searcher = new IndexSearcher(directory);
65  10 query = new PhraseQuery();
66    }
67   
 
68  10 toggle public void tearDown() throws Exception {
69  10 searcher.close();
70  10 directory.close();
71    }
72   
 
73  1 toggle public void testNotCloseEnough() throws Exception {
74  1 query.setSlop(2);
75  1 query.add(new Term("field", "one"));
76  1 query.add(new Term("field", "five"));
77  1 Hits hits = searcher.search(query);
78  1 assertEquals(0, hits.length());
79  1 QueryUtils.check(query,searcher);
80    }
81   
 
82  1 toggle public void testBarelyCloseEnough() throws Exception {
83  1 query.setSlop(3);
84  1 query.add(new Term("field", "one"));
85  1 query.add(new Term("field", "five"));
86  1 Hits hits = searcher.search(query);
87  1 assertEquals(1, hits.length());
88  1 QueryUtils.check(query,searcher);
89    }
90   
91    /**
92    * Ensures slop of 0 works for exact matches, but not reversed
93    */
 
94  1 toggle public void testExact() throws Exception {
95    // slop is zero by default
96  1 query.add(new Term("field", "four"));
97  1 query.add(new Term("field", "five"));
98  1 Hits hits = searcher.search(query);
99  1 assertEquals("exact match", 1, hits.length());
100  1 QueryUtils.check(query,searcher);
101   
102   
103  1 query = new PhraseQuery();
104  1 query.add(new Term("field", "two"));
105  1 query.add(new Term("field", "one"));
106  1 hits = searcher.search(query);
107  1 assertEquals("reverse not exact", 0, hits.length());
108  1 QueryUtils.check(query,searcher);
109    }
110   
 
111  1 toggle public void testSlop1() throws Exception {
112    // Ensures slop of 1 works with terms in order.
113  1 query.setSlop(1);
114  1 query.add(new Term("field", "one"));
115  1 query.add(new Term("field", "two"));
116  1 Hits hits = searcher.search(query);
117  1 assertEquals("in order", 1, hits.length());
118  1 QueryUtils.check(query,searcher);
119   
120   
121    // Ensures slop of 1 does not work for phrases out of order;
122    // must be at least 2.
123  1 query = new PhraseQuery();
124  1 query.setSlop(1);
125  1 query.add(new Term("field", "two"));
126  1 query.add(new Term("field", "one"));
127  1 hits = searcher.search(query);
128  1 assertEquals("reversed, slop not 2 or more", 0, hits.length());
129  1 QueryUtils.check(query,searcher);
130    }
131   
132