FileDocCategorySizeDatePackage
TestRegexQuery.javaAPI DocApache Lucene 2.1.03529Wed Feb 14 10:46:32 GMT 2007org.apache.lucene.search.regex

TestRegexQuery

public class TestRegexQuery extends TestCase
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Fields Summary
private IndexSearcher
searcher
private final String
FN
Constructors Summary
Methods Summary
private org.apache.lucene.index.TermnewTerm(java.lang.String value)

 return new Term(FN, value); 
private intregexQueryNrHits(java.lang.String regex)

    RegexQuery query = new RegexQuery( newTerm(regex));
    return searcher.search(query).length();
  
public voidsetUp()


     
    RAMDirectory directory = new RAMDirectory();
    try {
      IndexWriter writer = new IndexWriter(directory, new SimpleAnalyzer(), true);
      Document doc = new Document();
      doc.add(new Field(FN, "the quick brown fox jumps over the lazy dog", Field.Store.NO, Field.Index.TOKENIZED));
      writer.addDocument(doc);
      writer.optimize();
      writer.close();
      searcher = new IndexSearcher(directory);
    } catch (Exception e) {
      fail(e.toString());
    }
  
private intspanRegexQueryNrHits(java.lang.String regex1, java.lang.String regex2, int slop, boolean ordered)

    SpanRegexQuery srq1 = new SpanRegexQuery( newTerm(regex1));
    SpanRegexQuery srq2 = new SpanRegexQuery( newTerm(regex2));
    SpanNearQuery query = new SpanNearQuery( new SpanQuery[]{srq1, srq2}, slop, ordered);
    return searcher.search(query).length();
  
public voidtearDown()

    try {
      searcher.close();
    } catch (Exception e) {
      fail(e.toString());
    }
  
public voidtestEquals()

    RegexQuery query1 = new RegexQuery( newTerm("foo.*"));
    query1.setRegexImplementation(new JakartaRegexpCapabilities());

    RegexQuery query2 = new RegexQuery( newTerm("foo.*"));
    assertFalse(query1.equals(query2));
  
public voidtestRegex1()

    assertEquals(1, regexQueryNrHits("^q.[aeiou]c.*$"));
  
public voidtestRegex2()

    assertEquals(0, regexQueryNrHits("^.[aeiou]c.*$"));
  
public voidtestRegex3()

    assertEquals(0, regexQueryNrHits("^q.[aeiou]c$"));
  
public voidtestSpanRegex1()

    assertEquals(1, spanRegexQueryNrHits("^q.[aeiou]c.*$", "dog", 6, true));
  
public voidtestSpanRegex2()

    assertEquals(0, spanRegexQueryNrHits("^q.[aeiou]c.*$", "dog", 5, true));