Author: natalia
Date: Fri Aug 10 19:49:10 2007
New Revision: 564825
URL: http://svn.apache.org/viewvc?view=rev&rev=564825
Log:
Test indexer's tests
Added:
xml/xindice/trunk/java/tests/src/org/apache/xindice/core/indexer/LuceneIndexerTest.java
(with props)
xml/xindice/trunk/java/tests/src/org/apache/xindice/core/query/TextQueryResolverTest.java
(with props)
Modified:
xml/xindice/trunk/java/tests/src/org/apache/xindice/util/ConfigurationTest.java
Added:
xml/xindice/trunk/java/tests/src/org/apache/xindice/core/indexer/LuceneIndexerTest.java
URL:
http://svn.apache.org/viewvc/xml/xindice/trunk/java/tests/src/org/apache/xindice/core/indexer/LuceneIndexerTest.java?view=auto&rev=564825
==============================================================================
---
xml/xindice/trunk/java/tests/src/org/apache/xindice/core/indexer/LuceneIndexerTest.java
(added)
+++
xml/xindice/trunk/java/tests/src/org/apache/xindice/core/indexer/LuceneIndexerTest.java
Fri Aug 10 19:49:10 2007
@@ -0,0 +1,154 @@
+/*
+ * 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$
+ */
+
+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.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$, $Date$
+ */
+public class LuceneIndexerTest extends TestCase {
+
+ private Database db;
+ private Collection collection;
+ protected String indexClass;
+
+
+ public LuceneIndexerTest(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);
+ }
+
+ 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(1000);
+ return ind;
+ }
+
+ private IndexMatch[] query(Indexer idx, String query) throws Exception {
+ return idx.queryMatches(new IndexQuery(null, IndexQuery.TQ, query));
+ }
+
+ public void testSingleIndex() throws Exception {
+ HashMap patterns = new HashMap();
+ patterns.put("test", "a");
+ LuceneIndexer idx = createIndex("test", patterns);
+
+ String query = "test:test";
+ IndexMatch[] match = query(idx, query);
+
+ assertEquals(3, match.length);
+ }
+
+ public void testDuplicate() throws Exception {
+ try {
+ HashMap patterns = new HashMap();
+ patterns.put("test1", "[EMAIL PROTECTED]");
+ patterns.put("test2", "[EMAIL PROTECTED]");
+ createIndex("test1", patterns);
+ createIndex("test2", patterns);
+ assertTrue("There can be only one full text index per collection",
false);
+ } catch (DuplicateIndexException e) {
+ // correct behavior
+ }
+ }
+
+ public void testMultipleIndexes() throws Exception {
+ HashMap patterns = new HashMap();
+ patterns.put("test1", "[EMAIL PROTECTED]");
+ patterns.put("test2", "[EMAIL PROTECTED]");
+ LuceneIndexer idx = createIndex("test", patterns);
+
+ IndexMatch[] match = query(idx, "test1:test");
+ assertEquals(3, match.length);
+
+ match = query(idx, "test2:test");
+ assertEquals(3, match.length);
+
+ match = query(idx, "test1:test AND test2:test");
+ assertEquals(1, match.length);
+
+ match = query(idx, "test1:test OR test2:test");
+ assertEquals(5, match.length);
+ }
+
+ public void testNestedContent() throws Exception {
+ HashMap patterns = new HashMap();
+ patterns.put("doc", "doc");
+ LuceneIndexer idx = createIndex("test", patterns);
+
+ String query = "doc:foo";
+ IndexMatch[] match = query(idx, query);
+
+ assertEquals(2, match.length);
+ }
+}
Propchange:
xml/xindice/trunk/java/tests/src/org/apache/xindice/core/indexer/LuceneIndexerTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
xml/xindice/trunk/java/tests/src/org/apache/xindice/core/indexer/LuceneIndexerTest.java
------------------------------------------------------------------------------
svn:keywords = Id Revision Author Date
Added:
xml/xindice/trunk/java/tests/src/org/apache/xindice/core/query/TextQueryResolverTest.java
URL:
http://svn.apache.org/viewvc/xml/xindice/trunk/java/tests/src/org/apache/xindice/core/query/TextQueryResolverTest.java?view=auto&rev=564825
==============================================================================
---
xml/xindice/trunk/java/tests/src/org/apache/xindice/core/query/TextQueryResolverTest.java
(added)
+++
xml/xindice/trunk/java/tests/src/org/apache/xindice/core/query/TextQueryResolverTest.java
Fri Aug 10 19:49:10 2007
@@ -0,0 +1,167 @@
+/*
+ * 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$
+ */
+
+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$, $Date$
+ */
+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", "[EMAIL PROTECTED]");
+ patterns.put("test2", "[EMAIL PROTECTED]");
+ 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);
+ }
+}
Propchange:
xml/xindice/trunk/java/tests/src/org/apache/xindice/core/query/TextQueryResolverTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
xml/xindice/trunk/java/tests/src/org/apache/xindice/core/query/TextQueryResolverTest.java
------------------------------------------------------------------------------
svn:keywords = Id Revision Author Date
Modified:
xml/xindice/trunk/java/tests/src/org/apache/xindice/util/ConfigurationTest.java
URL:
http://svn.apache.org/viewvc/xml/xindice/trunk/java/tests/src/org/apache/xindice/util/ConfigurationTest.java?view=diff&rev=564825&r1=564824&r2=564825
==============================================================================
---
xml/xindice/trunk/java/tests/src/org/apache/xindice/util/ConfigurationTest.java
(original)
+++
xml/xindice/trunk/java/tests/src/org/apache/xindice/util/ConfigurationTest.java
Fri Aug 10 19:49:10 2007
@@ -58,9 +58,10 @@
assertEquals("queryengine", config.getName());
Configuration[] children = config.getChildren();
- assertEquals(2, children.length);
+ assertEquals(3, children.length);
assertEquals("resolver", children[0].getName());
assertEquals("resolver", children[1].getName());
+ assertEquals("resolver", children[2].getName());
}
public void testEditReadonly() throws Exception {