/*
* 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: XPathQueryResolverTest.java 511423 2007-02-25 03:21:05Z vgritsenko $
*/
package org.apache.xindice.core.query;
import org.apache.xindice.core.Collection;
import org.apache.xindice.core.Database;
import org.apache.xindice.core.data.NodeSet;
import org.apache.xindice.util.Configuration;
import org.apache.xindice.xml.dom.DOMParser;
import junit.framework.TestCase;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
/**
* This TestCase should test the correct Work of the XPathQueryResolver.
*
* During setUp it generates a database and adds one Collection.
* The two Tests Simple and Complex query will be raun against this Collection.
* The complex query string is taken from the Bug 14878 in Bugzilla
*
* @version $Revision: 511423 $, $Date: 2007-02-24 22:21:05 -0500 (Sat, 24 Feb 2007) $
* @author Alexander Sterff <sterff@in.tum.de>
*/
public class XPathQueryResolverTest extends TestCase {
final String DATABASE =
"<root-collection dbroot=\"db/\" name=\"db\">" +
"<queryengine>" +
"<resolver autoindex=\"false\" class=\"org.apache.xindice.core.query.XPathQueryResolver\" />" +
"<resolver class=\"org.apache.xindice.core.xupdate.XUpdateQueryResolver\" />" +
"</queryengine>" +
"</root-collection>";
final String COLLECTION =
"<collection compressed=\"true\" name=\"testcol\">" +
"<filer class=\"org.apache.xindice.core.filer.BTreeFiler\" />" +
"<indexes />" +
"</collection>";
final String DOCUMENT =
"<terrainmap>" +
"<coordinates>" +
"some text" +
"<top-left>" +
"<latlong>" +
"some more text" +
"<latitude>80</latitude>" +
"<longitude>-100</longitude>" +
"</latlong>" +
"</top-left>" +
"<bottom-right>" +
"<latlong>" +
"a" +
"<latitude>-200</latitude>" +
"b" +
"<longitude>90</longitude>" +
"c" +
"</latlong>" +
"</bottom-right>" +
"</coordinates>" +
"</terrainmap>";
final String[] ELEMENT_QUERY = {
"/terrainmap",
"/terrainxmap",
"/terrainmap/coordinates",
"/terrainmap/coordinates/top-left",
"/terrainmap/coordinates/*",
"/terrainmap/coordinates/bottom-right/latlong",
"/terrainmap/coordinates/bottom-right/latlong/*",
"/terrainmap/coordinates/bottom-right/latlong/..",
};
final int[] ELEMENT_COUNT = {
1,
0,
1,
1,
2,
1,
2,
1,
};
final String[] ELEMENT_NAMES = {
"terrainmap",
null,
"coordinates",
"top-left",
"bottom-right",
"latlong",
"longitude",
"bottom-right",
};
final String[] NUMBER_QUERY = {
"2 + 2",
"count(/terrainmap)",
"count(/terrainmap/coordinates) + count(//latlong)",
"number(//latitude[1]/text())",
"number(//longitude[ancestor::bottom-right]/text())",
};
final int[] NUMBER_VALUE = {
4,
1,
3,
80,
90,
};
final String[] TEXT_QUERY = {
"/terrainmap/coordinates/text()",
"/terrainmap/coordinates//text()[contains(., 'more')]",
"//text()",
"//latlong/text()",
"//latlong/text()[2]",
};
final int[] TEXT_COUNT = {
1,
1,
9,
4,
1,
};
final String[] TEXT_VALUE = {
"some text",
"some more text",
"c",
"c",
"b",
};
private static int tests;
private static Database db;
private static Collection col;
private XPathQueryResolver resolver;
public XPathQueryResolverTest(String name) {
super(name);
tests++;
}
public void setUp() throws Exception {
if (db == null) {
Database db = new Database();
db.setConfig(new Configuration(DOMParser.toDocument(DATABASE)));
col = db.createCollection("testcol", new Configuration(DOMParser.toDocument(COLLECTION)));
col.insertDocument(DOMParser.toDocument(DOCUMENT));
XPathQueryResolverTest.db = db;
}
resolver = new XPathQueryResolver();
}
public void testElementXPathQuery() throws Exception {
for (int i = 0; i < ELEMENT_QUERY.length; i++) {
NodeSet nodeSet = resolver.query(col, ELEMENT_QUERY[i], null, null);
int results = 0;
Element element = null;
while(nodeSet.hasMoreNodes()) {
element = (Element)nodeSet.getNextNode();
assertNotNull("NodeSet.getNextNode must be not null", element);
results++;
}
assertEquals("Results count", ELEMENT_COUNT[i], results);
if (results > 0) {
assertEquals("Result element name", ELEMENT_NAMES[i], element.getNodeName());
}
}
}
public void testNumberXPathQuery() throws Exception {
for (int i = 0; i < NUMBER_QUERY.length; i++) {
NodeSet nodeSet = resolver.query(col, NUMBER_QUERY[i], null, null);
assertTrue("Expected one result", nodeSet.hasMoreNodes());
Text element = (Text)nodeSet.getNextNode();
assertNotNull("NodeSet.getNextNode must be not null", element);
assertEquals("Result value", new Double(NUMBER_VALUE[i]).toString(), element.getNodeValue());
}
}
public void testTextXPathQuery() throws Exception {
for (int i = 0; i < TEXT_QUERY.length; i++) {
NodeSet nodeSet = resolver.query(col, TEXT_QUERY[i], null, null);
int results = 0;
Text element = null;
while(nodeSet.hasMoreNodes()) {
element = (Text)nodeSet.getNextNode();
assertNotNull("NodeSet.getNextNode must be not null", element);
results++;
}
assertEquals("Results count for " + TEXT_QUERY[i], TEXT_COUNT[i], results);
if (results > 0) {
assertEquals("Result value for " + TEXT_QUERY[i], TEXT_VALUE[i], element.getNodeValue());
}
}
}
public void testComplexXPathQuery() throws Exception {
Object node = null;
XPathQueryResolver queryResolv = new XPathQueryResolver();
NodeSet nodeSet =
queryResolv.query(
col,
"/terrainmap[coordinates/top-left/latlong/latitude[(number(text()) + 180) >= 0] and coordinates/top-left/latlong/longitude[(number(text()) + 90) <= 0] and coordinates/bottom-right/latlong/latitude[(number(text()) + 180) <= 0] and coordinates/bottom-right/latlong/longitude[(number(text()) + 90) >= 0]]",
null,
null);
if (nodeSet.hasMoreNodes()) {
node = nodeSet.getNextNode();
}
assertNotNull("Complex query didn't return a node", node);
}
public void tearDown() throws Exception {
if (--tests == 0) {
db.dropCollection(col);
}
}
}