Package org.apache.xindice.core.query

Source Code of org.apache.xindice.core.query.TextQueryResolverTest

/*
* 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: TextQueryResolverTest.java 564825 2007-08-11 02:49:10Z natalia $
*/

package org.apache.xindice.core.query;

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.data.NodeSet;
import org.apache.xindice.core.data.Key;
import org.apache.xindice.core.indexer.LuceneIndexer;
import org.apache.xindice.core.indexer.Indexer;
import org.apache.xindice.util.Configuration;
import org.apache.xindice.xml.dom.DOMParser;
import org.w3c.dom.Document;

import java.util.HashMap;
import java.util.Iterator;

/**
* Tests indexed queries
*
* @version $Revision: 564825 $, $Date: 2007-08-10 22:49:10 -0400 (Fri, 10 Aug 2007) $
*/
public class TextQueryResolverTest extends TestCase {

    private Database db;
    private Collection collection;
    private Indexer idx;
    private String indexClass;


    public TextQueryResolverTest(String name) {
        super(name);
        indexClass = "org.apache.xindice.core.indexer.LuceneIndexer";
    }

    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
        ));

        Document document = DOMParser.toDocument("<doc><a>test</a> <b>foo</b></doc>");
        collection.insertDocument("key1", document);

        document = DOMParser.toDocument("<doc><a>test</a> <b>bar</b></doc>");
        collection.insertDocument("key2", document);

        document = DOMParser.toDocument("<doc><a>test</a> <b>test</b></doc>");
        collection.insertDocument("key3", document);

        document = DOMParser.toDocument("<doc><a>text</a> <b>test</b></doc>");
        collection.insertDocument("key4", document);

        document = DOMParser.toDocument("<doc><a>foo</a> <b>test</b></doc>");
        collection.insertDocument("key5", document);

        HashMap patterns = new HashMap();
        patterns.put("test1", "a@*");
        patterns.put("test2", "b@*");
        idx = createIndex("test", patterns);
    }

    public void tearDown() throws Exception {
        db.dropCollection(collection);
        db.close();
    }

    private LuceneIndexer createIndex(String name, HashMap patterns) throws Exception {
        String config = "<index name='" + name + "' " +
                        "class='" + indexClass + "' " +
                        "analyzer='org.apache.lucene.analysis.SimpleAnalyzer'>";
        for (Iterator i = patterns.keySet().iterator(); i.hasNext(); ) {
            String alias = (String) i.next();
            config += "<pattern pattern='" + patterns.get(alias) + "' alias='" + alias + "' />";
        }
        config += "</index>";

        LuceneIndexer ind = (LuceneIndexer) collection.createIndexer(new Configuration(DOMParser.toDocument(config)));
        Thread.sleep(2000);
        return ind;
    }

    public void testNoTextIndexer() throws Exception {
        try {
            collection.dropIndexer(idx);

            TextQueryResolver res = new TextQueryResolver();
            res.query(collection, "test", null, null);

            assertTrue("Text query requires LuceneIndexer", false);
        } catch (QueryException e) {
            // correct behavior
        }
    }

    public void testQuery() throws Exception {
        TextQueryResolver res = new TextQueryResolver();

        NodeSet set = res.query(collection, "test1:test", null, null);
        int count = 0;
        while (set.hasMoreNodes()) {
            set.getNextNode();
            count++;
        }
        assertEquals("Number of elements in a result", 3, count);

        collection.remove("key1");
        set = res.query(collection, "test1:test", null, null);

        count = 0;
        while (set.hasMoreNodes()) {
            set.getNextNode();
            count++;
        }
        assertEquals("Number of elements in a result", 2, count);
    }

    public void testCompiledQuery() throws Exception {
        TextQueryResolver res = new TextQueryResolver();

        Query query = res.compileQuery(collection, "test1:test", null, null);
        NodeSet set = query.execute();
        int count = 0;
        while (set.hasMoreNodes()) {
            set.getNextNode();
            count++;
        }
        assertEquals("Number of elements in a result", 3, count);
    }

    public void testQueryWithKeySet() throws Exception {
        TextQueryResolver res = new TextQueryResolver();

        Key[] keys = new Key[] {new Key("key1"), new Key("key3"), new Key("key4")};
        NodeSet set = res.query(collection, "test1:test", null, keys);
        int count = 0;
        while (set.hasMoreNodes()) {
            set.getNextNode();
            count++;
        }
        assertEquals("Number of elements in a result", 2, count);
    }
}
TOP

Related Classes of org.apache.xindice.core.query.TextQueryResolverTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.