KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > search > TestBooleanPrefixQuery


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 junit.framework.Test;
21 import junit.framework.TestSuite;
22 import junit.textui.TestRunner;
23 import org.apache.lucene.store.RAMDirectory;
24 import org.apache.lucene.index.IndexWriter;
25 import org.apache.lucene.index.Term;
26 import org.apache.lucene.index.IndexReader;
27 import org.apache.lucene.analysis.WhitespaceAnalyzer;
28 import org.apache.lucene.document.Document;
29 import org.apache.lucene.document.Field;
30 import org.apache.lucene.search.PrefixQuery;
31 import org.apache.lucene.search.Query;
32 import org.apache.lucene.search.BooleanQuery;
33
34 import java.io.IOException JavaDoc;
35
36 /**
37  * @author schnee
38  * @version $Id: TestBooleanPrefixQuery.java,v 1.2 2004/03/29 22:48:06 cutting Exp $
39  **/

40
41 public class TestBooleanPrefixQuery extends TestCase {
42
43   public static void main(String JavaDoc[] args) {
44     TestRunner.run(suite());
45   }
46
47   public static Test suite() {
48     return new TestSuite(TestBooleanPrefixQuery.class);
49   }
50
51   public TestBooleanPrefixQuery(String JavaDoc name) {
52     super(name);
53   }
54
55   public void testMethod() {
56     RAMDirectory directory = new RAMDirectory();
57
58     String JavaDoc[] categories = new String JavaDoc[]{"food",
59                                        "foodanddrink",
60                                        "foodanddrinkandgoodtimes",
61                                        "food and drink"};
62
63     Query rw1 = null;
64     Query rw2 = null;
65     try {
66       IndexWriter writer = new IndexWriter(directory, new
67                                            WhitespaceAnalyzer(), true);
68       for (int i = 0; i < categories.length; i++) {
69         Document doc = new Document();
70         doc.add(Field.Keyword("category", categories[i]));
71         writer.addDocument(doc);
72       }
73       writer.close();
74       
75       IndexReader reader = IndexReader.open(directory);
76       PrefixQuery query = new PrefixQuery(new Term("category", "foo"));
77       
78       rw1 = query.rewrite(reader);
79       
80       BooleanQuery bq = new BooleanQuery();
81       bq.add(query, true, false);
82       
83       rw2 = bq.rewrite(reader);
84     } catch (IOException JavaDoc e) {
85       fail(e.getMessage());
86     }
87
88     BooleanQuery bq1 = null;
89     if (rw1 instanceof BooleanQuery) {
90       bq1 = (BooleanQuery) rw1;
91     }
92
93     BooleanQuery bq2 = null;
94     if (rw2 instanceof BooleanQuery) {
95         bq2 = (BooleanQuery) rw2;
96     } else {
97       fail("Rewrite");
98     }
99
100     assertEquals("Number of Clauses Mismatch", bq1.getClauses().length,
101                  bq2.getClauses().length);
102   }
103 }
104
105
Popular Tags