Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
62   182   17   3.88
2   130   0.27   16
16     1.06  
1    
 
  TestNearSpansOrdered       Line # 38 62 17 100% 1.0
 
  (10)
 
1    package org.apache.lucene.search.spans;
2   
3    /**
4    * Copyright 2006 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.search.Explanation;
20    import org.apache.lucene.search.IndexSearcher;
21    import org.apache.lucene.search.Scorer;
22    import org.apache.lucene.search.Weight;
23    import org.apache.lucene.search.CheckHits;
24    import org.apache.lucene.store.RAMDirectory;
25   
26    import org.apache.lucene.index.IndexWriter;
27    import org.apache.lucene.index.Term;
28   
29    import org.apache.lucene.analysis.WhitespaceAnalyzer;
30   
31    import org.apache.lucene.document.Document;
32    import org.apache.lucene.document.Field;
33   
34    import org.apache.lucene.queryParser.QueryParser;
35   
36    import junit.framework.TestCase;
37   
 
38    public class TestNearSpansOrdered extends TestCase {
39    protected IndexSearcher searcher;
40   
41    public static final String FIELD = "field";
42    public static final QueryParser qp =
43    new QueryParser(FIELD, new WhitespaceAnalyzer());
44   
 
45  10 toggle public void tearDown() throws Exception {
46  10 searcher.close();
47    }
48   
 
49  10 toggle public void setUp() throws Exception {
50  10 RAMDirectory directory = new RAMDirectory();
51  10 IndexWriter writer= new IndexWriter(directory, new WhitespaceAnalyzer(), true);
52  50 for (int i = 0; i < docFields.length; i++) {
53  40 Document doc = new Document();
54  40 doc.add(new Field(FIELD, docFields[i], Field.Store.NO, Field.Index.TOKENIZED));
55  40 writer.addDocument(doc);
56    }
57  10 writer.close();
58  10 searcher = new IndexSearcher(directory);
59    }
60   
61    protected String[] docFields = {
62    "w1 w2 w3 w4 w5",
63    "w1 w3 w2 w3 zz",
64    "w1 xx w2 yy w3",
65    "w1 w3 xx w2 yy w3 zz"
66    };
67   
 
68  10 toggle protected SpanNearQuery makeQuery(String s1, String s2, String s3,
69    int slop, boolean inOrder) {
70  10 return new SpanNearQuery
71    (new SpanQuery[] {
72    new SpanTermQuery(new Term(FIELD, s1)),
73    new SpanTermQuery(new Term(FIELD, s2)),
74    new SpanTermQuery(new Term(FIELD, s3)) },
75    slop,
76    inOrder);
77    }
 
78  10 toggle protected SpanNearQuery makeQuery() {
79  10 return makeQuery("w1","w2","w3",1,true);
80    }
81   
 
82  1 toggle public void testSpanNearQuery() throws Exception {
83  1 SpanNearQuery q = makeQuery();
84  1 CheckHits.checkHits(q, FIELD, searcher, new int[] {0,1});
85    }
86   
 
87  9 toggle public String s(Spans span) {
88  9 return s(span.doc(), span.start(), span.end());
89    }
 
90  18 toggle public String s(int doc, int start, int end) {
91  18 return "s(" + doc + "," + start + "," + end +")";
92    }
93   
 
94  1 toggle public void testNearSpansNext() throws Exception {
95  1 SpanNearQuery q = makeQuery();
96  1 Spans span = q.getSpans(searcher.getIndexReader());
97  1 assertEquals(true, span.next());
98  1 assertEquals(s(0,0,3), s(span));
99  1 assertEquals(true, span.next());
100  1 assertEquals(s(1,0,4), s(span));
101  1 assertEquals(false, span.next());
102    }
103   
104    /**
105    * test does not imply that skipTo(doc+1) should work exactly the
106    * same as next -- it's only applicable in this case since we know doc
107    * does not contain more than one span
108    */
 
109  1 toggle public void testNearSpansSkipToLikeNext() throws Exception {
110  1 SpanNearQuery q = makeQuery();
111  1 Spans span = q.getSpans(searcher.getIndexReader());
112  1 assertEquals(true, span.skipTo(0));
113  1 assertEquals(s(0,0,3), s(span));
114  1 assertEquals(true, span.skipTo(1));
115  1 assertEquals(s(1,0,4), s(span));
116  1 assertEquals(false, span.skipTo(2));
117    }
118   
 
119  1 toggle public void testNearSpansNextThenSkipTo() throws Exception {
120  1