add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / queries / src / test / org / apache / lucene / search / regex / TestRegexQuery.java
1 package org.apache.lucene.search.regex;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
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;
28
29 import org.apache.lucene.search.spans.SpanNearQuery;
30 import org.apache.lucene.search.spans.SpanQuery;
31 import org.apache.lucene.util.LuceneTestCase;
32
33 public class TestRegexQuery extends LuceneTestCase {
34   private IndexSearcher searcher;
35   private IndexReader reader;
36   private Directory directory;
37   private final String FN = "field";
38
39
40   @Override
41   public void setUp() throws Exception {
42     super.setUp();
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();
49     writer.close();
50     searcher = newSearcher(reader);
51   }
52
53   @Override
54   public void tearDown() throws Exception {
55     searcher.close();
56     reader.close();
57     directory.close();
58     super.tearDown();
59   }
60
61   private Term newTerm(String value) { return new Term(FN, value); }
62
63   private int  regexQueryNrHits(String regex, RegexCapabilities capability) throws Exception {
64     RegexQuery query = new RegexQuery( newTerm(regex));
65     
66     if ( capability != null )
67       query.setRegexImplementation(capability);
68     
69     return searcher.search(query, null, 1000).totalHits;
70   }
71
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);
76     
77     return searcher.search(query, null, 1000).totalHits;
78   }
79
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());
85   }
86
87   public void testRegex1() throws Exception {
88     assertEquals(1, regexQueryNrHits("^q.[aeiou]c.*$", null));
89   }
90
91   public void testRegex2() throws Exception {
92     assertEquals(0, regexQueryNrHits("^.[aeiou]c.*$", null));
93   }
94
95   public void testRegex3() throws Exception {
96     assertEquals(0, regexQueryNrHits("^q.[aeiou]c$", null));
97   }
98
99   public void testSpanRegex1() throws Exception {
100     assertEquals(1, spanRegexQueryNrHits("^q.[aeiou]c.*$", "dog", 6, true));
101   }
102
103   public void testSpanRegex2() throws Exception {
104     assertEquals(0, spanRegexQueryNrHits("^q.[aeiou]c.*$", "dog", 5, true));
105   }
106
107   public void testEquals() throws Exception {
108     RegexQuery query1 = new RegexQuery( newTerm("foo.*"));
109     query1.setRegexImplementation(new JakartaRegexpCapabilities());
110
111     RegexQuery query2 = new RegexQuery( newTerm("foo.*"));
112     assertFalse(query1.equals(query2));
113   }
114   
115   public void testJakartaCaseSensativeFail() throws Exception {
116     assertEquals(0, regexQueryNrHits("^.*DOG.*$", null));
117   }
118
119   public void testJavaUtilCaseSensativeFail() throws Exception {
120     assertEquals(0, regexQueryNrHits("^.*DOG.*$", null));
121   }
122   
123   public void testJakartaCaseInsensative() throws Exception {
124     assertEquals(1, regexQueryNrHits("^.*DOG.*$", new JakartaRegexpCapabilities(JakartaRegexpCapabilities.FLAG_MATCH_CASEINDEPENDENT)));
125   }
126   
127   public void testJavaUtilCaseInsensative() throws Exception {
128     assertEquals(1, regexQueryNrHits("^.*DOG.*$", new JavaUtilRegexCapabilities(JavaUtilRegexCapabilities.FLAG_CASE_INSENSITIVE)));
129   }
130
131 }
132