1 package org.apache.lucene.search.regex;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import org.apache.lucene.store.Directory;
21 import org.apache.lucene.index.IndexReader;
22 import org.apache.lucene.index.RandomIndexWriter;
23 import org.apache.lucene.index.Term;
24 import org.apache.lucene.document.Document;
25 import org.apache.lucene.document.Field;
26 import org.apache.lucene.search.IndexSearcher;
27 import org.apache.lucene.index.TermEnum;
29 import org.apache.lucene.search.spans.SpanNearQuery;
30 import org.apache.lucene.search.spans.SpanQuery;
31 import org.apache.lucene.util.LuceneTestCase;
33 public class TestRegexQuery extends LuceneTestCase {
34 private IndexSearcher searcher;
35 private IndexReader reader;
36 private Directory directory;
37 private final String FN = "field";
41 public void setUp() throws Exception {
43 directory = newDirectory();
44 RandomIndexWriter writer = new RandomIndexWriter(random, directory);
45 Document doc = new Document();
46 doc.add(newField(FN, "the quick brown fox jumps over the lazy dog", Field.Store.NO, Field.Index.ANALYZED));
47 writer.addDocument(doc);
48 reader = writer.getReader();
50 searcher = newSearcher(reader);
54 public void tearDown() throws Exception {
61 private Term newTerm(String value) { return new Term(FN, value); }
63 private int regexQueryNrHits(String regex, RegexCapabilities capability) throws Exception {
64 RegexQuery query = new RegexQuery( newTerm(regex));
66 if ( capability != null )
67 query.setRegexImplementation(capability);
69 return searcher.search(query, null, 1000).totalHits;
72 private int spanRegexQueryNrHits(String regex1, String regex2, int slop, boolean ordered) throws Exception {
73 SpanRegexQuery srq1 = new SpanRegexQuery( newTerm(regex1));
74 SpanRegexQuery srq2 = new SpanRegexQuery( newTerm(regex2));
75 SpanNearQuery query = new SpanNearQuery( new SpanQuery[]{srq1, srq2}, slop, ordered);
77 return searcher.search(query, null, 1000).totalHits;
80 public void testMatchAll() throws Exception {
81 TermEnum terms = new RegexQuery(new Term(FN, "jum.")).getEnum(searcher.getIndexReader());
82 // no term should match
83 assertNull(terms.term());
84 assertFalse(terms.next());
87 public void testRegex1() throws Exception {
88 assertEquals(1, regexQueryNrHits("^q.[aeiou]c.*$", null));
91 public void testRegex2() throws Exception {
92 assertEquals(0, regexQueryNrHits("^.[aeiou]c.*$", null));
95 public void testRegex3() throws Exception {
96 assertEquals(0, regexQueryNrHits("^q.[aeiou]c$", null));
99 public void testSpanRegex1() throws Exception {
100 assertEquals(1, spanRegexQueryNrHits("^q.[aeiou]c.*$", "dog", 6, true));
103 public void testSpanRegex2() throws Exception {
104 assertEquals(0, spanRegexQueryNrHits("^q.[aeiou]c.*$", "dog", 5, true));
107 public void testEquals() throws Exception {
108 RegexQuery query1 = new RegexQuery( newTerm("foo.*"));
109 query1.setRegexImplementation(new JakartaRegexpCapabilities());
111 RegexQuery query2 = new RegexQuery( newTerm("foo.*"));
112 assertFalse(query1.equals(query2));
115 public void testJakartaCaseSensativeFail() throws Exception {
116 assertEquals(0, regexQueryNrHits("^.*DOG.*$", null));
119 public void testJavaUtilCaseSensativeFail() throws Exception {
120 assertEquals(0, regexQueryNrHits("^.*DOG.*$", null));
123 public void testJakartaCaseInsensative() throws Exception {
124 assertEquals(1, regexQueryNrHits("^.*DOG.*$", new JakartaRegexpCapabilities(JakartaRegexpCapabilities.FLAG_MATCH_CASEINDEPENDENT)));
127 public void testJavaUtilCaseInsensative() throws Exception {
128 assertEquals(1, regexQueryNrHits("^.*DOG.*$", new JavaUtilRegexCapabilities(JavaUtilRegexCapabilities.FLAG_CASE_INSENSITIVE)));