001: package org.apache.lucene.search;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import org.apache.lucene.util.LuceneTestCase;
021: import junit.framework.Test;
022: import junit.framework.TestSuite;
023: import junit.textui.TestRunner;
024: import org.apache.lucene.store.RAMDirectory;
025: import org.apache.lucene.index.IndexWriter;
026: import org.apache.lucene.index.Term;
027: import org.apache.lucene.index.IndexReader;
028: import org.apache.lucene.analysis.WhitespaceAnalyzer;
029: import org.apache.lucene.document.Document;
030: import org.apache.lucene.document.Field;
031: import org.apache.lucene.search.PrefixQuery;
032: import org.apache.lucene.search.Query;
033: import org.apache.lucene.search.BooleanQuery;
034:
035: import java.io.IOException;
036:
037: /**
038: *
039: * @version $Id: TestBooleanPrefixQuery.java 583534 2007-10-10 16:46:35Z mikemccand $
040: **/
041:
042: public class TestBooleanPrefixQuery extends LuceneTestCase {
043:
044: public static void main(String[] args) {
045: TestRunner.run(suite());
046: }
047:
048: public static Test suite() {
049: return new TestSuite(TestBooleanPrefixQuery.class);
050: }
051:
052: public TestBooleanPrefixQuery(String name) {
053: super (name);
054: }
055:
056: public void testMethod() {
057: RAMDirectory directory = new RAMDirectory();
058:
059: String[] categories = new String[] { "food", "foodanddrink",
060: "foodanddrinkandgoodtimes", "food and drink" };
061:
062: Query rw1 = null;
063: Query rw2 = null;
064: try {
065: IndexWriter writer = new IndexWriter(directory,
066: new WhitespaceAnalyzer(), true);
067: for (int i = 0; i < categories.length; i++) {
068: Document doc = new Document();
069: doc.add(new Field("category", categories[i],
070: Field.Store.YES, Field.Index.UN_TOKENIZED));
071: writer.addDocument(doc);
072: }
073: writer.close();
074:
075: IndexReader reader = IndexReader.open(directory);
076: PrefixQuery query = new PrefixQuery(new Term("category",
077: "foo"));
078:
079: rw1 = query.rewrite(reader);
080:
081: BooleanQuery bq = new BooleanQuery();
082: bq.add(query, BooleanClause.Occur.MUST);
083:
084: rw2 = bq.rewrite(reader);
085: } catch (IOException e) {
086: fail(e.getMessage());
087: }
088:
089: BooleanQuery bq1 = null;
090: if (rw1 instanceof BooleanQuery) {
091: bq1 = (BooleanQuery) rw1;
092: }
093:
094: BooleanQuery bq2 = null;
095: if (rw2 instanceof BooleanQuery) {
096: bq2 = (BooleanQuery) rw2;
097: } else {
098: fail("Rewrite");
099: }
100:
101: assertEquals("Number of Clauses Mismatch",
102: bq1.getClauses().length, bq2.getClauses().length);
103: }
104: }
|