Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
39   104   4   19.5
4   65   0.1   2
2     2  
1    
 
  TestPhrasePrefixQuery       Line # 38 39 4 100% 1.0
 
  (1)
 
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.SimpleAnalyzer;
21    import org.apache.lucene.document.Document;
22    import org.apache.lucene.document.Field;
23    import org.apache.lucene.index.IndexReader;
24    import org.apache.lucene.index.IndexWriter;
25    import org.apache.lucene.index.Term;
26    import org.apache.lucene.index.TermEnum;
27    import org.apache.lucene.store.RAMDirectory;
28   
29    import java.io.IOException;
30    import java.util.LinkedList;
31   
32    /**
33    * This class tests PhrasePrefixQuery class.
34    *
35    * @author Otis Gospodnetic
36    * @version $Id: TestPhrasePrefixQuery.java 387550 2006-03-21 15:36:32Z yonik $
37    */
 
38    public class TestPhrasePrefixQuery
39    extends TestCase
40    {
 
41  1 toggle public TestPhrasePrefixQuery(String name)
42    {
43  1 super(name);
44    }
45   
46    /**
47    *
48    */
 
49  1 toggle public void testPhrasePrefix()
50    throws IOException
51    {
52  1 RAMDirectory indexStore = new RAMDirectory();
53  1 IndexWriter writer = new IndexWriter(indexStore, new SimpleAnalyzer(), true);
54  1 Document doc1 = new Document();
55  1 Document doc2 = new Document();
56  1 Document doc3 = new Document();
57  1 Document doc4 = new Document();
58  1 Document doc5 = new Document();
59  1 doc1.add(new Field("body", "blueberry pie", Field.Store.YES, Field.Index.TOKENIZED));
60  1 doc2.add(new Field("body", "blueberry strudel", Field.Store.YES, Field.Index.TOKENIZED));
61  1 doc3.add(new Field("body", "blueberry pizza", Field.Store.YES, Field.Index.TOKENIZED));
62  1 doc4.add(new Field("body", "blueberry chewing gum", Field.Store.YES, Field.Index.TOKENIZED));
63  1 doc5.add(new Field("body", "piccadilly circus", Field.Store.YES, Field.Index.TOKENIZED));
64  1 writer.addDocument(doc1);
65  1 writer.addDocument(doc2);
66  1 writer.addDocument(doc3);
67  1 writer.addDocument(doc4);
68  1 writer.addDocument(doc5);
69  1 writer.optimize();
70  1 writer.close();
71   
72  1 IndexSearcher searcher = new IndexSearcher(indexStore);
73   
74    //PhrasePrefixQuery query1 = new PhrasePrefixQuery();
75  1 MultiPhraseQuery query1 = new MultiPhraseQuery();
76    //PhrasePrefixQuery query2 = new PhrasePrefixQuery();
77  1 MultiPhraseQuery query2 = new MultiPhraseQuery();
78  1 query1.add(new Term("body", "blueberry"));
79  1 query2.add(new Term("body", "strawberry"));
80   
81  1 LinkedList termsWithPrefix = new LinkedList();
82  1 IndexReader ir = IndexReader.open(indexStore);
83   
84    // this TermEnum gives "piccadilly", "pie" and "pizza".
85  1 String prefix = "pi";
86  1 TermEnum te = ir.terms(new Term("body", prefix + "*"));
87  1 do {
88  4 if (te.term().text().startsWith(prefix))
89    {
90  3 termsWithPrefix.add(te.term());
91    }
92  4 } while (te.next());
93   
94  1 query1.add((Term[])termsWithPrefix.toArray(new Term[0]));
95  1 query2.add((Term[])termsWithPrefix.toArray(new Term[0]));
96   
97  1 Hits result;
98  1 result = searcher.search(query1);
99  1 assertEquals(2, result.length());
100   
101  1 result = searcher.search(query2);
102  1 assertEquals(0, result.length());
103    }
104    }