Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
60   139   6   12
2   89   0.1   5
5     1.2  
1    
 
  TestWildcard       Line # 35 60 6 100% 1.0
 
  (3)
 
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.IndexWriter;
24    import org.apache.lucene.index.Term;
25    import org.apache.lucene.store.RAMDirectory;
26   
27    import java.io.IOException;
28   
29    /**
30    * TestWildcard tests the '*' and '?' wildcard characters.
31    *
32    * @version $Id: TestWildcard.java 329860 2005-10-31 17:06:29Z bmesser $
33    * @author Otis Gospodnetic
34    */
 
35    public class TestWildcard
36    extends TestCase {
 
37  1 toggle public void testEquals() {
38  1 WildcardQuery wq1 = new WildcardQuery(new Term("field", "b*a"));
39  1 WildcardQuery wq2 = new WildcardQuery(new Term("field", "b*a"));
40  1 WildcardQuery wq3 = new WildcardQuery(new Term("field", "b*a"));
41   
42    // reflexive?
43  1 assertEquals(wq1, wq2);
44  1 assertEquals(wq2, wq1);
45   
46    // transitive?
47  1 assertEquals(wq2, wq3);
48  1 assertEquals(wq1, wq3);
49   
50  1 assertFalse(wq1.equals(null));
51   
52  1 FuzzyQuery fq = new FuzzyQuery(new Term("field", "b*a"));
53  1 assertFalse(wq1.equals(fq));
54  1 assertFalse(fq.equals(wq1));
55    }
56   
57    /**
58    * Tests Wildcard queries with an asterisk.
59    */
 
60  1 toggle public void testAsterisk()
61    throws IOException {
62  1 RAMDirectory indexStore = getIndexStore("body", new String[]
63    {"metal", "metals"});
64  1 IndexSearcher searcher = new IndexSearcher(indexStore);
65  1 Query query1 = new TermQuery(new Term("body", "metal"));
66  1 Query query2 = new WildcardQuery(new Term("body", "metal*"));
67  1 Query query3 = new WildcardQuery(new Term("body", "m*tal"));
68  1 Query query4 = new WildcardQuery(new Term("body", "m*tal*"));
69  1 Query query5 = new WildcardQuery(new Term("body", "m*tals"));
70   
71  1 BooleanQuery query6 = new BooleanQuery();
72  1 query6.add(query5, BooleanClause.Occur.SHOULD);
73   
74  1 BooleanQuery query7 = new BooleanQuery();
75  1 query7.add(query3, BooleanClause.Occur.SHOULD);
76  1 query7.add(query5, BooleanClause.Occur.SHOULD);
77   
78    // Queries do not automatically lower-case search terms:
79  1 Query query8 = new WildcardQuery(new Term("body", "M*tal*"));
80   
81  1 assertMatches(searcher, query1, 1);
82  1 assertMatches(searcher, query2, 2);
83  1 assertMatches(searcher, query3, 1);
84  1 assertMatches(searcher, query4, 2);
85  1 assertMatches(searcher, query5, 1);
86  1 assertMatches(searcher, query6, 1);
87  1 assertMatches(searcher, query7, 2);
88  1 assertMatches(searcher, query8, 0);
89  1 assertMatches(searcher, new WildcardQuery(new Term("body", "*tall")), 0);
90  1 assertMatches(searcher, new WildcardQuery(new Term("body", "*tal")), 1);
91  1 assertMatches(searcher, new WildcardQuery(new Term("body", "*tal*")), 2);
92    }
93   
94    /**
95    * Tests Wildcard queries with a question mark.
96    *
97    * @throws IOException if an error occurs
98    */
 
99  1 toggle public void testQuestionmark()
100    throws IOException {
101  1 RAMDirectory indexStore = getIndexStore("body", new String[]
102    {"metal", "metals", "mXtals", "mXtXls"});
103  1 IndexSearcher searcher = new IndexSearcher(indexStore);
104  1 Query query1 = new WildcardQuery(new Term("body", "m?tal"));
105  1 Query query2 = new WildcardQuery(new Term("body", "metal?"));
106  1 Query query3 = new WildcardQuery(new Term("body", "metals?"));
107  1 Query query4 = new WildcardQuery(new Term("body", "m?t?ls"));
108  1 Query query5 = new WildcardQuery(new Term("body", "M?t?ls"));
109  1 Query query6 = new WildcardQuery(new Term("body", "meta??"));
110   
111  1 assertMatches(searcher, query1, 1);
112  1 assertMatches(searcher, query2, 1);
113  1 assertMatches(searcher, query3, 0);
114  1 assertMatches(searcher, query4, 3);
115  1 assertMatches(searcher, query5, 0);
116  1 assertMatches(searcher, query6, 1); // Query: 'meta??' matches 'metals' not 'metal'
117    }
118   
 
119  2 toggle private RAMDirectory getIndexStore(String field, String[] contents)
120    throws IOException {
121  2 RAMDirectory indexStore = new RAMDirectory();
122  2 IndexWriter writer = new IndexWriter(indexStore, new SimpleAnalyzer(), true);
123  8 for (int i = 0; i < contents.length; ++i) {
124  6 Document doc = new Document();
125  6 doc.add(new Field(field, contents[i], Field.Store.YES, Field.Index.TOKENIZED));
126  6 writer.addDocument(doc);
127    }
128  2 writer.optimize();
129  2 writer.close();
130   
131  2 return indexStore;
132    }
133   
 
134  17 toggle private void assertMatches(IndexSearcher searcher, Query q, int expectedMatches)
135    throws IOException {
136  17 Hits result = searcher.search(q);
137  17 assertEquals(expectedMatches, result.length());
138    }
139    }