/*
* 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.
*
* $Id: SearcherTest.java 584478 2007-10-14 02:47:06Z natalia $
*/
package org.apache.xindice.core.query.ftsearch;
import junit.framework.TestCase;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.apache.xindice.xml.dom.DOMParser;
import org.apache.xindice.core.data.NodeSet;
import org.apache.lucene.analysis.SimpleAnalyzer;
/**
* Tests for full text search on DOM nodes
*
* @version $Revision: 584478 $, $Date: 2007-10-13 22:47:06 -0400 (Sat, 13 Oct 2007) $
*
*/
public class SearcherTest extends TestCase {
String str = "<?xml version=\"1.0\"?>\n" +
"<doc>" +
" <chapter>" +
" <title>Chapter</title>\n" +
" <content>" +
" Invokes the underlying method represented by this Method object, on the specified \n" +
" object with the specified parameters. Individual parameters are automatically \n" +
" unwrapped to match primitive formal parameters, and both primitive and reference \n" +
" parameters are subject to method invocation conversions as necessary.\n" +
" </content>\n" +
" <title>Paragraph</title>\n" +
" <content>" +
" If the underlying method is an instance method, it is invoked using dynamic method \n" +
" lookup as documented in The Java Language Specification, Second Edition, section \n" +
" 15.12.4.4; in particular, overriding based on the runtime type of the target object \n" +
" will occur. \n" +
" </content>\n" +
" <title>Part</title>\n" +
" <content>" +
" If the method completes normally, the value it returns is returned to the caller of \n" +
" invoke; if the value has a primitive type, it is first appropriately wrapped in an \n" +
" object. If the underlying method return type is void, the invocation returns null.\n" +
" </content>" +
" </chapter>" +
"</doc>";
Document doc;
NodeList list;
public void setUp() throws Exception {
doc = DOMParser.toDocument(str);
list = doc.getDocumentElement().getElementsByTagName("content");
}
public void testTermQuery() throws Exception {
Searcher searcher = new Searcher(list, new SimpleAnalyzer());
NodeSet res = searcher.search("underlying");
int count = 0;
while (res.hasMoreNodes()) {
count++;
res.getNextNode();
}
assertTrue(count == 3);
}
public void testBooleanQuery() throws Exception {
Searcher searcher = new Searcher(list, new SimpleAnalyzer());
NodeSet res = searcher.search("underlying AND automatically");
int count = 0;
while (res.hasMoreNodes()) {
count++;
res.getNextNode();
}
assertTrue(count == 1);
}
public void testPhraseQuery() throws Exception {
Searcher searcher = new Searcher(list, new SimpleAnalyzer());
NodeSet res = searcher.search("\"underlying method\"");
int count = 0;
while (res.hasMoreNodes()) {
count++;
res.getNextNode();
}
assertTrue(count == 2);
}
public void testWildcardQuery1() throws Exception {
Searcher searcher = new Searcher(list, new SimpleAnalyzer());
NodeSet res = searcher.search("invoke?");
int count = 0;
while (res.hasMoreNodes()) {
count++;
res.getNextNode();
}
assertTrue(count == 2);
}
public void testWildcardQuery2() throws Exception {
Searcher searcher = new Searcher(list, new SimpleAnalyzer());
NodeSet res = searcher.search("invoke*");
int count = 0;
while (res.hasMoreNodes()) {
count++;
res.getNextNode();
}
assertTrue(count == 3);
}
public void testWildcardQuery3() throws Exception {
Searcher searcher = new Searcher(list, new SimpleAnalyzer());
NodeSet res = searcher.search("test*");
int count = 0;
while (res.hasMoreNodes()) {
count++;
res.getNextNode();
}
assertTrue(count == 0);
}
public void testWildcardQuery4() throws Exception {
Searcher searcher = new Searcher(list, new SimpleAnalyzer());
NodeSet res = searcher.search("s*d"); // matches "second" and "specified"
int count = 0;
while (res.hasMoreNodes()) {
count++;
res.getNextNode();
}
assertTrue(count == 2);
}
public void testProximityQuery() throws Exception {
Searcher searcher = new Searcher(list, new SimpleAnalyzer());
NodeSet res = searcher.search("\"java edition\"~5");
int count = 0;
while (res.hasMoreNodes()) {
count++;
res.getNextNode();
}
assertTrue(count == 1);
}
public void testFuzzyQuery() throws Exception {
Searcher searcher = new Searcher(list, new SimpleAnalyzer());
NodeSet res = searcher.search("on~0.2"); // matches "on" and "in"
int count = 0;
while (res.hasMoreNodes()) {
count++;
res.getNextNode();
}
assertTrue(count == 3);
}
public void testRangeQuery() throws Exception {
doc = DOMParser.toDocument(str);
list = doc.getDocumentElement().getElementsByTagName("title");
Searcher searcher = new Searcher(list, new SimpleAnalyzer());
NodeSet res = searcher.search("{chapter TO part}");
int count = 0;
while (res.hasMoreNodes()) {
count++;
res.getNextNode();
}
assertTrue(count == 1);
}
}