[exo-jcr-commits] exo-jcr SVN: r1298 - jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/test/java/org/exoplatform/services/jcr/cluster/functional.

do-not-reply at jboss.org do-not-reply at jboss.org
Tue Jan 5 11:50:17 EST 2010


Author: nzamosenchuk
Date: 2010-01-05 11:50:17 -0500 (Tue, 05 Jan 2010)
New Revision: 1298

Modified:
   jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/test/java/org/exoplatform/services/jcr/cluster/functional/WebdavQueryTest.java
Log:
EXOJCR-340: Added fulltext search test.

Modified: jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/test/java/org/exoplatform/services/jcr/cluster/functional/WebdavQueryTest.java
===================================================================
--- jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/test/java/org/exoplatform/services/jcr/cluster/functional/WebdavQueryTest.java	2010-01-05 15:54:13 UTC (rev 1297)
+++ jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/test/java/org/exoplatform/services/jcr/cluster/functional/WebdavQueryTest.java	2010-01-05 16:50:17 UTC (rev 1298)
@@ -19,6 +19,7 @@
 package org.exoplatform.services.jcr.cluster.functional;
 
 import org.exoplatform.common.http.client.HTTPResponse;
+import org.exoplatform.common.http.client.ModuleException;
 import org.exoplatform.services.jcr.cluster.BaseClusteringFunctionalTest;
 import org.exoplatform.services.jcr.cluster.JCRWebdavConnection;
 
@@ -26,9 +27,12 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
-import java.util.HashSet;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
 import java.util.List;
-import java.util.Set;
+import java.util.Map;
+import java.util.Map.Entry;
 
 import javax.xml.namespace.QName;
 import javax.xml.stream.FactoryConfigurationError;
@@ -38,7 +42,7 @@
 import javax.xml.stream.events.StartElement;
 
 /**
- * Class contains set of query tests for clustered environment
+ * Class contains set of query tests for cluster environment
  * 
  * @author <a href="mailto:nikolazius at gmail.com">Nikolay Zamosenchuk</a>
  * @version $Id: WebdavQueryTest.java 34360 2009-07-22 23:58:59Z nzamosenchuk $
@@ -47,38 +51,164 @@
 public class WebdavQueryTest extends BaseClusteringFunctionalTest
 {
 
-   public void testSimpleGetAllSql() throws Exception
+   /**
+    * Fulltext queries
+    */
+   public void testFullTextSearch() throws Exception
    {
-      String testLocalRootName = "node";
       JCRWebdavConnection conn = getConnection();
+      // Nodes with some text, with unique words in each one.
+      Map<String, String> expected = new HashMap<String, String>();
+      expected
+         .put(
+            "JCR_Overview",
+            "A JCR is a type of Object Database tailored to the storage, searching, and retrieval of hierarchical data. The JCR API grew o"
+               + "ut of the needs of content management systems, which require storage of documents and other binary objects with associated me"
+               + "tadata; however, the API is applicable to many additional types of application. In addition to object storage, the JCR provid"
+               + "es: APIs for versioning of data; transactions; observation of changes in data; and import or export of data to XML in a standard way.");
+      expected
+         .put(
+            "JCR_Structure",
+            "The data in a JCR consists of a tree of Nodes with associated Properties. Data is stored in the Properties, which may hold simple "
+               + "values such as numbers and strings or binary data of arbitrary length. Nodes may optionally have one or more types associated with"
+               + " them which dictate the kinds of properties, number and type of child nodes, and certain behavioral characteristics of the nodes. API");
+
+      expected
+         .put(
+            "JCR_Queries",
+            "A JCR can be queried with XPathQuery, can export portions of its tree to XML in two standard formats and can import hierarchies directly"
+               + " from XML. A JCR may optionally support a standardized form of SQL for queries. The Apache Jackrabbit reference implementation of "
+               + "JCR also supports the integration of the Apache Lucene search engine to give full text searches of data in the repository.");
+
+      expected.put("JCR_Impl",
+         "eXo Platform JCR implementation on the company wiki. eXo Platform 2 article on theserverside");
+
+      // add nodes
+      for (Entry<String, String> entry : expected.entrySet())
+      {
+         conn.addNode(entry.getKey(), entry.getValue().getBytes(), "text/plain");
+      }
+
+      // map containing test-case: <SQL query> : <expected nodes>
+      Map<String, String[]> sqlCases = new HashMap<String, String[]>();
+      sqlCases.put("SELECT * FROM nt:base WHERE CONTAINS(*,'tailored')", new String[]{"JCR_Overview"});
+      sqlCases.put("SELECT * FROM nt:base WHERE CONTAINS(*,'XPathQuery')", new String[]{"JCR_Queries"});
+      sqlCases.put("SELECT * FROM nt:resource WHERE CONTAINS(*,'API')", new String[]{"JCR_Structure", "JCR_Overview"});
+
+      // SQL
+      for (Entry<String, String[]> entry : sqlCases.entrySet())
+      {
+         HTTPResponse response = conn.sqlQuery(entry.getKey());
+         assertEquals(207, response.getStatusCode());
+         assertResponse(entry.getValue(), response);
+      }
+
+      // map containing test-case: <XPATH query> : <expected nodes>
+      Map<String, String[]> xpathCases = new HashMap<String, String[]>();
+      xpathCases.put("//element(*, nt:base)[jcr:contains(.,'tailored')]", new String[]{"JCR_Overview"});
+      xpathCases.put("//element(*, nt:base)[jcr:contains(.,'XPathQuery')]", new String[]{"JCR_Queries"});
+      xpathCases.put("//element(*, nt:resource)[jcr:contains(.,'API')]", new String[]{"JCR_Structure", "JCR_Overview"});
+
+      // XPATH
+      for (Entry<String, String[]> entry : xpathCases.entrySet())
+      {
+         HTTPResponse response = conn.xpathQuery(entry.getKey());
+         assertEquals(207, response.getStatusCode());
+         assertResponse(entry.getValue(), response);
+      }
+      // remove created nodes
+      for (Entry<String, String> entry : expected.entrySet())
+      {
+         conn.removeNode(entry.getKey());
+      }
+   }
+
+   /**
+    * Simple test, searching nodes by given path 
+    */
+   public void testPathSearch() throws Exception
+   {
+      String testLocalRootName = "testSimpleGetAll";
+      JCRWebdavConnection conn = getConnection();
       conn.addDir(testLocalRootName);
       List<String> expected = new ArrayList<String>();
-      expected.add("exo_String");
-      expected.add("exo_Boolean");
-      expected.add("exo_Integer");
-      expected.add("exo_Long");
-      expected.add("exo_Float");
-      expected.add("exo_Double");
+      expected.add("exoString");
+      expected.add("exoBoolean");
+      expected.add("exoInteger");
+      expected.add("exoLong");
+      expected.add("exoFloat");
+      expected.add("exoDouble");
 
       for (String name : expected)
       {
          conn.addNode(testLocalRootName + "/" + name, "_data_".getBytes());
-         System.out.println("added: " + name);
       }
-
+      // SQL
       HTTPResponse response =
-         conn.sqlQuery("SELECT * FROM nt:base WHERE jcr:path LIKE '/" + testLocalRootName + "[%]/%'");
+         conn.sqlQuery("SELECT * FROM nt:base WHERE jcr:path LIKE '/" + testLocalRootName
+            + "[%]/%' AND NOT jcr:path LIKE '/" + testLocalRootName + "[%]/%/%' ");
       assertEquals(207, response.getStatusCode());
-      List<String> found = parseNodeNames(response.getData());
-      assertTrue("Some nodes not found", assertLists(expected, found));
+      assertResponse(expected, response);
+      // XPath
+      response = conn.xpathQuery("/jcr:root/" + testLocalRootName + "/ element(*, nt:base)");
+      assertEquals(207, response.getStatusCode());
+      assertResponse(expected, response);
       conn.removeNode(testLocalRootName);
    }
 
    /**
+    * @param expected
+    * @param response
+    * @throws IOException
+    * @throws ModuleException
+    * @throws XMLStreamException
+    * @throws FactoryConfigurationError
+    */
+   private void assertResponse(Collection<String> expected, HTTPResponse response) throws IOException, ModuleException,
+      XMLStreamException, FactoryConfigurationError
+   {
+      List<String> found;
+      assertEquals(207, response.getStatusCode());
+      found = parseNodeNames(response.getData());
+      assertTrue("Lists are not equals:\n*found:\t" + found + "\n*expected:\t" + expected,
+         compareLists(expected, found));
+   }
+
+   /**
+    * @param expected
+    * @param response
+    * @throws IOException
+    * @throws ModuleException
+    * @throws XMLStreamException
+    * @throws FactoryConfigurationError
+    */
+   private void assertResponse(String[] expected, HTTPResponse response) throws IOException, ModuleException,
+      XMLStreamException, FactoryConfigurationError
+   {
+      assertResponse(Arrays.asList(expected), response);
+   }
+
+   /**
+    * returns true if lists are equals (order doesn't matter)
+    * 
+    * @param expected
+    * @param found
+    * @return
+    */
+   private boolean compareLists(Collection<String> expected, Collection<String> found)
+   {
+      if (expected == null || found == null)
+      {
+         return false;
+      }
+      return expected.containsAll(found) && found.containsAll(expected);
+   }
+
+   /**
     * Extracts names of nodes from response XML
     * 
     * TODO: fix
-    * /!\ During parsing method accumulates nodes in SET, because of https://jira.jboss.org/jira/browse/EXOJCR-364 /!\
+    * /!\ Method accumulates nodes in SET, because of https://jira.jboss.org/jira/browse/EXOJCR-364 /!\
     * 
     * @param data
     * @return
@@ -86,11 +216,12 @@
     * @throws FactoryConfigurationError
     * @throws IOException
     */
-   public List<String> parseNodeNames(byte[] data) throws XMLStreamException, FactoryConfigurationError, IOException
+   private List<String> parseNodeNames(byte[] data) throws XMLStreamException, FactoryConfigurationError, IOException
    {
       // flag, that notifies when parser is inside <D:displayname></D:displayname> 
       boolean displayName = false;
-      Set<String> nodes = new HashSet<String>();
+      //Set<String> nodes = new HashSet<String>();
+      List<String> nodes = new ArrayList<String>();
       InputStream input = new ByteArrayInputStream(data);
       XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(input);
       QName name = QName.valueOf("{DAV:}displayname");
@@ -136,20 +267,4 @@
       }
       return new ArrayList<String>(nodes);
    }
-
-   /**
-    * returns true if lists are equals (order doesn't matter)
-    * 
-    * @param expected
-    * @param found
-    * @return
-    */
-   public boolean assertLists(List<String> expected, List<String> found)
-   {
-      if (expected == null || found == null)
-      {
-         return false;
-      }
-      return expected.containsAll(found) && found.containsAll(expected);
-   }
 }



More information about the exo-jcr-commits mailing list