/*
* 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: IndexedQueryTest.java 511408 2007-02-25 02:42:45Z vgritsenko $
*/
package org.apache.xindice.core.query;
import org.apache.xindice.core.Collection;
import org.apache.xindice.core.Database;
import org.apache.xindice.core.DatabaseTest;
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.Document;
/**
* Tests indexed queries
*
* @version $Revision: 511408 $, $Date: 2007-02-24 21:42:45 -0500 (Sat, 24 Feb 2007) $
*/
public class IndexedQueryTest extends TestCase {
private Database db;
private Collection collection;
public IndexedQueryTest(String name) {
super(name);
}
public void setUp() throws Exception {
String name = getClass().getName();
db = new Database();
db.setConfig(new Configuration(DOMParser.toDocument(DatabaseTest.DATABASE)));
collection = db.createCollection(name, new Configuration(
DOMParser.toDocument(
"<collection compressed=\"true\" name=\"" + name + "\" inline-metadata=\"true\">" +
"<filer class=\"org.apache.xindice.core.filer.BTreeFiler\" />" +
"</collection>"), false
));
String config = "<index name='Test' " +
" class='org.apache.xindice.core.indexer.ValueIndexer' " +
" pattern='test1' />";
collection.createIndexer(new Configuration(DOMParser.toDocument(config)));
}
public void tearDown() throws Exception {
db.dropCollection(collection);
db.close();
}
public void testStartsWithAndLTGTQuery() throws Exception {
int iterations = 10;
for (int i = 0; i < iterations; i++) {
Document document = DOMParser.toDocument("<test string='true'><test1>" + i +
"</test1>This is a test document</test>");
collection.insertDocument("key" + i, document);
}
assertTrue(collection.getFiler().getRecordCount() == iterations);
XPathQueryResolver service = new XPathQueryResolver();
NodeSet result = service.query(collection, "//test1[starts-with(., '8')]", null, null);
int count = 0;
while (result.hasMoreNodes()) {
result.getNextNode();
count++;
}
assertEquals(1, count);
result = service.query(collection, "//test1[. < '8' or . > '8']", null, null);
count = 0;
while (result.hasMoreNodes()) {
result.getNextNode();
count++;
}
assertEquals(9, count);
}
}