001: package org.apache.lucene.search.regex;
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 junit.framework.TestCase;
021: import org.apache.lucene.store.RAMDirectory;
022: import org.apache.lucene.index.IndexWriter;
023: import org.apache.lucene.index.Term;
024: import org.apache.lucene.analysis.SimpleAnalyzer;
025: import org.apache.lucene.document.Document;
026: import org.apache.lucene.document.Field;
027: import org.apache.lucene.search.IndexSearcher;
028:
029: import org.apache.lucene.search.spans.SpanNearQuery;
030: import org.apache.lucene.search.spans.SpanQuery;
031:
032: public class TestRegexQuery extends TestCase {
033: private IndexSearcher searcher;
034: private final String FN = "field";
035:
036: public void setUp() {
037: RAMDirectory directory = new RAMDirectory();
038: try {
039: IndexWriter writer = new IndexWriter(directory,
040: new SimpleAnalyzer(), true);
041: Document doc = new Document();
042: doc.add(new Field(FN,
043: "the quick brown fox jumps over the lazy dog",
044: Field.Store.NO, Field.Index.TOKENIZED));
045: writer.addDocument(doc);
046: writer.optimize();
047: writer.close();
048: searcher = new IndexSearcher(directory);
049: } catch (Exception e) {
050: fail(e.toString());
051: }
052: }
053:
054: public void tearDown() {
055: try {
056: searcher.close();
057: } catch (Exception e) {
058: fail(e.toString());
059: }
060: }
061:
062: private Term newTerm(String value) {
063: return new Term(FN, value);
064: }
065:
066: private int regexQueryNrHits(String regex) throws Exception {
067: RegexQuery query = new RegexQuery(newTerm(regex));
068: return searcher.search(query).length();
069: }
070:
071: private int spanRegexQueryNrHits(String regex1, String regex2,
072: int slop, boolean ordered) throws Exception {
073: SpanRegexQuery srq1 = new SpanRegexQuery(newTerm(regex1));
074: SpanRegexQuery srq2 = new SpanRegexQuery(newTerm(regex2));
075: SpanNearQuery query = new SpanNearQuery(new SpanQuery[] { srq1,
076: srq2 }, slop, ordered);
077: return searcher.search(query).length();
078: }
079:
080: public void testRegex1() throws Exception {
081: assertEquals(1, regexQueryNrHits("^q.[aeiou]c.*$"));
082: }
083:
084: public void testRegex2() throws Exception {
085: assertEquals(0, regexQueryNrHits("^.[aeiou]c.*$"));
086: }
087:
088: public void testRegex3() throws Exception {
089: assertEquals(0, regexQueryNrHits("^q.[aeiou]c$"));
090: }
091:
092: public void testSpanRegex1() throws Exception {
093: assertEquals(1, spanRegexQueryNrHits("^q.[aeiou]c.*$", "dog",
094: 6, true));
095: }
096:
097: public void testSpanRegex2() throws Exception {
098: assertEquals(0, spanRegexQueryNrHits("^q.[aeiou]c.*$", "dog",
099: 5, true));
100: }
101:
102: public void testEquals() throws Exception {
103: RegexQuery query1 = new RegexQuery(newTerm("foo.*"));
104: query1.setRegexImplementation(new JakartaRegexpCapabilities());
105:
106: RegexQuery query2 = new RegexQuery(newTerm("foo.*"));
107: assertFalse(query1.equals(query2));
108: }
109:
110: }
|