/*
* 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: NameIndexerTest.java 559284 2007-07-25 02:22:18Z natalia $
*/
package org.apache.xindice.core.indexer;
import junit.framework.TestCase;
import org.apache.xindice.core.Database;
import org.apache.xindice.core.Collection;
import org.apache.xindice.core.DatabaseTest;
import org.apache.xindice.core.DBException;
import org.apache.xindice.core.indexer.helpers.IndexQueryANY;
import org.apache.xindice.util.Configuration;
import org.apache.xindice.xml.dom.DOMParser;
import org.w3c.dom.Document;
/**
* Tests indexed queries
*
* @version $Revision: 559284 $, $Date: 2007-07-24 22:22:18 -0400 (Tue, 24 Jul 2007) $
*/
public class NameIndexerTest extends TestCase {
private Database db;
private Collection collection;
protected String indexClass;
public NameIndexerTest(String name) {
super(name);
indexClass = "org.apache.xindice.core.indexer.NameIndexer";
}
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
));
}
public void tearDown() throws Exception {
db.dropCollection(collection);
db.close();
}
private Indexer createIndex(String name, String pattern) throws Exception {
String config = "<index name='" + name + "' " +
"class='" + indexClass + "' " +
"pattern='" + pattern + "'/>";
Indexer ind = collection.createIndexer(new
Configuration(DOMParser.toDocument(config)));
Thread.sleep(100);
return ind;
}
private IndexMatch[] query(Indexer ind, String pattern) throws DBException {
return ind.queryMatches(new IndexQueryANY(new IndexPattern(collection.getSymbols(), pattern, null)));
}
public void testElementIndex() throws Exception {
Indexer ind = createIndex("index", "test");
Document document = DOMParser.toDocument("<a><test>q</test></a>");
collection.insertDocument("key1", document);
document = DOMParser.toDocument("<a><test/></a>");
collection.insertDocument("key2", document);
document = DOMParser.toDocument("<a><test value='abc'/></a>");
collection.insertDocument("key3", document);
IndexMatch[] match = query(ind, "test");
assertEquals(3, match.length);
}
public void testAttributeIndex() throws Exception {
Indexer ind = createIndex("index", "test@value");
Document document = DOMParser.toDocument("<a><test value='abc'>q</test></a>");
collection.insertDocument("key1", document);
document = DOMParser.toDocument("<a><test/></a>");
collection.insertDocument("key2", document);
document = DOMParser.toDocument("<a><test value=''/></a>");
collection.insertDocument("key3", document);
IndexMatch[] match = query(ind, "test@value");
assertEquals(2, match.length);
}
public void testWildcardElementIndex() throws Exception {
Indexer ind = createIndex("index", "*");
Document document = DOMParser.toDocument("<a><test value='abc'>text</test></a>");
collection.insertDocument("key1", document);
document = DOMParser.toDocument("<b><test value=''>text</test></b>");
collection.insertDocument("key2", document);
document = DOMParser.toDocument("<c><test value='bcd'>text</test></c>");
collection.insertDocument("key3", document);
document = DOMParser.toDocument("<d><test value='aac'>text</test></d>");
collection.insertDocument("key4", document);
IndexMatch[] match = query(ind, "a");
assertEquals(1, match.length);
}
public void testWildcardAttributeIndex() throws Exception {
Indexer ind = createIndex("index", "test@*");
Document document = DOMParser.toDocument("<test value1='abc'></test>");
collection.insertDocument("key1", document);
document = DOMParser.toDocument("<test value2=''>q</test>");
collection.insertDocument("key2", document);
document = DOMParser.toDocument("<test value3='bcd'>q</test>");
collection.insertDocument("key3", document);
document = DOMParser.toDocument("<test value4='aac'>q</test>");
collection.insertDocument("key4", document);
IndexMatch[] match = query(ind, "test@value3");
assertEquals(1, match.length);
}
}