Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
60   134   7   15
4   92   0.12   4
4     1.75  
1    
 
  TestPositionIncrement       Line # 43 60 7 100% 1.0
 
  (2)
 
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 org.apache.lucene.index.Term;
20    import org.apache.lucene.index.IndexWriter;
21    import org.apache.lucene.search.PhraseQuery;
22    import org.apache.lucene.search.Hits;
23    import org.apache.lucene.search.IndexSearcher;
24    import org.apache.lucene.store.RAMDirectory;
25    import org.apache.lucene.analysis.Analyzer;
26    import org.apache.lucene.analysis.Token;
27    import org.apache.lucene.analysis.TokenStream;
28    import org.apache.lucene.analysis.WhitespaceAnalyzer;
29    import org.apache.lucene.document.Document;
30    import org.apache.lucene.document.Field;
31   
32    import java.io.Reader;
33    import java.io.StringReader;
34   
35    import junit.framework.TestCase;
36   
37    /**
38    * Term position unit test.
39    *
40    * @author Doug Cutting
41    * @version $Revision: 150585 $
42    */
 
43    public class TestPositionIncrement extends TestCase {
44   
 
45  1 toggle public void testSetPosition() throws Exception {
46  1 Analyzer analyzer = new Analyzer() {
 
47  1 toggle public TokenStream tokenStream(String fieldName, Reader reader) {
48  1 return new TokenStream() {
49    private final String[] TOKENS = {"1", "2", "3", "4", "5"};
50    private final int[] INCREMENTS = {1, 2, 1, 0, 1};
51    private int i = 0;
52   
 
53  6 toggle public Token next() {
54  6 if (i == TOKENS.length)
55  1 return null;
56  5 Token t = new Token(TOKENS[i], i, i);
57  5 t.setPositionIncrement(INCREMENTS[i]);
58  5 i++;
59  5 return t;
60    }
61    };
62    }
63    };
64  1 RAMDirectory store = new RAMDirectory();
65  1 IndexWriter writer = new IndexWriter(store, analyzer, true);
66  1 Document d = new Document();
67  1 d.add(new Field("field", "bogus", Field.Store.YES, Field.Index.TOKENIZED));
68  1 writer.addDocument(d);
69  1 writer.optimize();
70  1 writer.close();
71   
72  1 IndexSearcher searcher = new IndexSearcher(store);
73  1 PhraseQuery q;
74  1 Hits hits;
75   
76  1 q = new PhraseQuery();
77  1 q.add(new Term("field", "1"));
78  1 q.add(new Term("field", "2"));
79  1 hits = searcher.search(q);
80  1 assertEquals(0, hits.length());
81   
82  1 q = new PhraseQuery();
83  1 q.add(new Term("field", "2"));
84  1 q.add(new Term("field", "3"));
85  1 hits = searcher.search(q);
86  1 assertEquals(1, hits.length());
87   
88  1 q = new PhraseQuery();
89  1 q.add(new Term("field", "3"));
90  1 q.add(new Term("field", "4"));
91  1 hits = searcher.search(q);
92  1 assertEquals(0, hits.length());
93   
94  1 q = new PhraseQuery();
95  1 q.add(new Term("field", "2"));
96  1 q.add(new Term("field", "4"));
97  1 hits = searcher.search(q);
98  1 assertEquals(1, hits.length());
99   
100  1 q = new PhraseQuery();
101  1 q.add(new Term("field", "3"));
102  1 q.add(new Term("field", "5"));
103  1 hits = searcher.search(q);
104  1 assertEquals(1, hits.length());
105   
106  1 q = new PhraseQuery();
107  1 q.add(new Term("field", "4"));
108  1 q.add(new Term("field", "5"));
109  1 hits = searcher.search(q);
110  1 assertEquals(1, hits.length());
111   
112  1 q = new PhraseQuery();
113  1 q.add(new Term("field", "2"));
114  1 q.add(new Term("field", "5"));
115  1 hits = searcher.search(q);
116  1 assertEquals(0, hits.length());
117    }
118   
119    /**
120    * Basic analyzer behavior should be to keep sequential terms in one
121    * increment from one another.
122    */
 
123  1 toggle public void testIncrementingPositions() throws Exception {
124  1 Analyzer analyzer = new WhitespaceAnalyzer();
125  1 TokenStream ts = analyzer.tokenStream("field",
126    new StringReader("one two three four five"));
127   
128  1 while (true) {
129  6 Token token = ts.next();
130  6 if (token == null) break;
131  5 assertEquals(token.termText(), 1, token.getPositionIncrement());
132    }
133    }
134    }