vladimir    2003/04/05 21:39:13

  Modified:    java/tests/src/org/apache/xindice/integration/client/services
                        XUpdateQueryTest.java
  Added:       java/tests/src/org/apache/xindice/integration/client/services
                        XPathQueryTest.java
  Log:
  new tests for XUpdate.
  It will be easier to test for XUpdate problems now
  
  Revision  Changes    Path
  1.2       +79 -36    
xml-xindice/java/tests/src/org/apache/xindice/integration/client/services/XUpdateQueryTest.java
  
  Index: XUpdateQueryTest.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xindice/java/tests/src/org/apache/xindice/integration/client/services/XUpdateQueryTest.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- XUpdateQueryTest.java     28 Nov 2002 08:12:26 -0000      1.1
  +++ XUpdateQueryTest.java     6 Apr 2003 05:39:13 -0000       1.2
  @@ -71,54 +71,97 @@
   public class XUpdateQueryTest
           extends AbstractXmlDbClientTest {
   
  -    public void testUpdateDocument()
  +    protected void setUp()
               throws Exception {
  -        String document = "<?xml version=\"1.0\"?>" +
  -                "<person>" +
  -                "<fname>John</fname>" +
  -                "<lname>Smith</lname>" +
  -                "<phone type=\"work\">563-456-7890</phone>" +
  -                "<phone type=\"home\">534-567-8901</phone>" +
  -                "<email type=\"home\">[EMAIL PROTECTED]</email>" +
  -                "<email type=\"work\">[EMAIL PROTECTED]</email>" +
  -                "<address type=\"home\">34 S. Colon St.</address>" +
  -                "<address type=\"work\">9967 W. Shrimp Ave.</address>" +
  +        super.setUp();
  +
  +        String document1 = "<?xml version=\"1.0\"?>" +
  +                "<person status=\"single\">" +
  +                "<first>John</first>" +
  +                "<last>Smith</last>" +
  +                "<phone type=\"work\">555-345-6789</phone>" +
  +                "</person>";
  +        String document2 = "<?xml version=\"1.0\"?>" +
  +                "<person status=\"married\">" +
  +                "<first>Sally</first>" +
  +                "<last>Benton</last>" +
  +                "<phone type=\"work\">555-345-6789</phone>" +
                   "</person>";
   
  -        this.client.insertDocument(TEST_COLLECTION_PATH, "xupdatedoc", 
document);
  -        assertEquals(1, this.client.countDocument(TEST_COLLECTION_PATH));
  +        this.client.insertDocument(TEST_COLLECTION_PATH, "doc1", document1);
  +        this.client.insertDocument(TEST_COLLECTION_PATH, "doc2", document2);
  +    }
  +
  +    protected void tearDown()
  +            throws Exception {
  +        this.client.removeDocument(TEST_COLLECTION_PATH, "doc1");
  +        this.client.removeDocument(TEST_COLLECTION_PATH, "doc2");
  +
  +        super.tearDown();
  +    }
   
  -        String xupdate = "<xu:modifications version=\"1.0\"" +
  -                "      xmlns:xu=\"http://www.xmldb.org/xupdate\";>" +
  -                "   <xu:remove select=\"/person/[EMAIL PROTECTED] = 
'home']\"/>" +
  -                "   <xu:update select=\"/person/[EMAIL PROTECTED] = 
'work']\">" +
  -                "       480-300-3003" +
  -                "   </xu:update>" +
  -                "</xu:modifications>";
  -
  -        String updatedDocument = "<?xml version=\"1.0\"?>" +
  -                "<person>" +
  -                "<fname>John</fname>" +
  -                "<lname>Smith</lname>" +
  -                "<phone type=\"work\">408-300-3003</phone>" +
  -                "<email type=\"home\">[EMAIL PROTECTED]</email>" +
  -                "<email type=\"work\">[EMAIL PROTECTED]</email>" +
  -                "<address type=\"home\">34 S. Colon St.</address>" +
  -                "<address type=\"work\">9967 W. Shrimp Ave.</address>" +
  +    public void testUpdateDocument()
  +            throws Exception {
  +        String query =
  +                "<xupdate:modifications version=\"1.0\" 
xmlns:xupdate=\"http://www.xmldb.org/xupdate\";>" +
  +                "   <xupdate:update 
select=\"/person/@status\">married</xupdate:update>" +
  +                "   <xupdate:update 
select=\"/person/first\">Ben</xupdate:update>" +
  +                "   <xupdate:update select=\"/person/[EMAIL PROTECTED] = 
'work']\">480-300-3003</xupdate:update>" +
  +                "</xupdate:modifications>";
  +
  +        String updatedDocument = "<?xml version=\"1.0\"?>\n" +
  +                "<person status=\"married\">" +
  +                "<first>Ben</first>" +
  +                "<last>Smith</last>" +
  +                "<phone type=\"work\">480-300-3003</phone>" +
                   "</person>";
   
           Collection col = this.client.getCollection(TEST_COLLECTION_PATH);
           XUpdateQueryService service = (XUpdateQueryService) 
col.getService("XUpdateQueryService", "1.0");
   
  -        long count = service.update(xupdate);
  +        long count = service.updateResource("doc1", query);
           assertEquals(1, count);
   
  -        String doc = this.client.getDocument(TEST_COLLECTION_PATH, 
"xupdatedoc");
  +        String doc = this.client.getDocument(TEST_COLLECTION_PATH, "doc1");
           assertNotNull(doc);
           assertEquals(updatedDocument, doc);
  +    }
   
  -        this.client.removeDocument(TEST_COLLECTION_PATH, "xupdatedoc");
  -        assertEquals(0, this.client.countDocument(TEST_COLLECTION_PATH));
  +    public void testUpdateCollection()
  +            throws Exception {
  +        String query =
  +                "<xupdate:modifications version=\"1.0\" 
xmlns:xupdate=\"http://www.xmldb.org/xupdate\";>" +
  +                "   <xupdate:update 
select=\"/person/@status\">divorced</xupdate:update>" +
  +                "   <xupdate:update 
select=\"/person/first\">Ben</xupdate:update>" +
  +                "   <xupdate:update select=\"/person/[EMAIL PROTECTED] = 
'work']\">480-300-3003</xupdate:update>" +
  +                "</xupdate:modifications>";
  +
  +        String updatedDocument1 = "<?xml version=\"1.0\"?>\n" +
  +                "<person status=\"divorced\">" +
  +                "<first>Ben</first>" +
  +                "<last>Smith</last>" +
  +                "<phone type=\"work\">480-300-3003</phone>" +
  +                "</person>";
  +        String updatedDocument2 = "<?xml version=\"1.0\"?>\n" +
  +                "<person status=\"divorced\">" +
  +                "<first>Ben</first>" +
  +                "<last>Benton</last>" +
  +                "<phone type=\"work\">480-300-3003</phone>" +
  +                "</person>";
  +
  +        Collection col = this.client.getCollection(TEST_COLLECTION_PATH);
  +        XUpdateQueryService service = (XUpdateQueryService) 
col.getService("XUpdateQueryService", "1.0");
  +
  +        long count = service.update(query);
  +        assertEquals(6, count);
  +
  +        String doc = this.client.getDocument(TEST_COLLECTION_PATH, "doc1");
  +        assertNotNull(doc);
  +        assertEquals(updatedDocument1, doc);
  +
  +        doc = this.client.getDocument(TEST_COLLECTION_PATH, "doc2");
  +        assertNotNull(doc);
  +        assertEquals(updatedDocument2, doc);
       }
   
   }
  
  
  
  1.1                  
xml-xindice/java/tests/src/org/apache/xindice/integration/client/services/XPathQueryTest.java
  
  Index: XPathQueryTest.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Xindice" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation and was
   * originally based on software copyright (c) 1999-2001, The dbXML
   * Group, L.L.C., http://www.dbxmlgroup.com.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * $Id: XPathQueryTest.java,v 1.1 2003/04/06 05:39:13 vladimir Exp $
   */
  
  package org.apache.xindice.integration.client.services;
  
  import org.apache.xindice.integration.client.AbstractXmlDbClientTest;
  
  import org.xmldb.api.base.Collection;
  import org.xmldb.api.base.ResourceIterator;
  import org.xmldb.api.base.ResourceSet;
  import org.xmldb.api.modules.XPathQueryService;
  
  import java.util.ArrayList;
  import java.util.List;
  
  /**
   * @version $Revision: 1.1 $, $Date: 2003/04/06 05:39:13 $
   * @author Vladimir R. Bossicard <[EMAIL PROTECTED]>
   */
  public class XPathQueryTest
          extends AbstractXmlDbClientTest {
  
      protected void setUp()
              throws Exception {
          super.setUp();
  
          String document1 = "<?xml version=\"1.0\"?>" +
                  "<person>" +
                  "<first>John</first>" +
                  "<last>Smith</last>" +
                  "<age>30</age>" +
                  "<phone type=\"work\">555-345-6789</phone>" +
                  "</person>";
          String document2 = "<?xml version=\"1.0\"?>" +
                  "<person>" +
                  "<first>Sally</first>" +
                  "<last>Smith</last>" +
                  "<age>20</age>" +
                  "<phone type=\"work\">555-345-6789</phone>" +
                  "</person>";
  
          this.client.insertDocument(TEST_COLLECTION_PATH, "doc1", document1);
          this.client.insertDocument(TEST_COLLECTION_PATH, "doc2", document2);
      }
  
      protected void tearDown()
              throws Exception {
          this.client.removeDocument(TEST_COLLECTION_PATH, "doc1");
          this.client.removeDocument(TEST_COLLECTION_PATH, "doc2");
  
          super.tearDown();
      }
  
      public void testSimpleQuery()
              throws Exception {
          String query = "//person[last='Smith']";
  
          Collection col = this.client.getCollection(TEST_COLLECTION_PATH);
          XPathQueryService xpathservice = (XPathQueryService) 
col.getService("XPathQueryService", "1.0");
          ResourceSet resultSet = xpathservice.query(query);
  
          ResourceIterator results = resultSet.getIterator();
  
          List res = asList(results);
          assertEquals(2, res.size());
      }
  
      public void testSimpleAndQuery()
              throws Exception {
          String query = "//person[first='John' and last='Smith']";
  
          Collection col = this.client.getCollection(TEST_COLLECTION_PATH);
          XPathQueryService xpathservice = (XPathQueryService) 
col.getService("XPathQueryService", "1.0");
          ResourceSet resultSet = xpathservice.query(query);
  
          ResourceIterator results = resultSet.getIterator();
  
          List res = asList(results);
          assertEquals(1, res.size());
      }
  
      public void testSimpleOrQuery()
              throws Exception {
          String query = "//person[first='John' or last='Smith']";
  
          Collection col = this.client.getCollection(TEST_COLLECTION_PATH);
          XPathQueryService xpathservice = (XPathQueryService) 
col.getService("XPathQueryService", "1.0");
          ResourceSet resultSet = xpathservice.query(query);
  
          ResourceIterator results = resultSet.getIterator();
  
          List res = asList(results);
          assertEquals(2, res.size());
      }
  
      public void testMultipleOrQuery()
              throws Exception {
          String query = "//person[first='John' or first='Sally']";
  
          Collection col = this.client.getCollection(TEST_COLLECTION_PATH);
          XPathQueryService xpathservice = (XPathQueryService) 
col.getService("XPathQueryService", "1.0");
          ResourceSet resultSet = xpathservice.query(query);
  
          ResourceIterator results = resultSet.getIterator();
  
          List res = asList(results);
          assertEquals(2, res.size());
      }
  
      public void testAndOrQuery()
              throws Exception {
          String query = "//person[last='Smith' and (first='John' or 
first='Sally')]";
  
          Collection col = this.client.getCollection(TEST_COLLECTION_PATH);
          XPathQueryService xpathservice = (XPathQueryService) 
col.getService("XPathQueryService", "1.0");
          ResourceSet resultSet = xpathservice.query(query);
  
          ResourceIterator results = resultSet.getIterator();
  
          List res = asList(results);
          assertEquals(2, res.size());
      }
  
      public void testSimpleWildcardSearchQuery() 
              throws Exception {
          // search all records whose last name contains 'Smi'
          String query = "//person[contains(last, 'Smi')]";
          
          Collection col = this.client.getCollection(TEST_COLLECTION_PATH);
          XPathQueryService xpathservice = (XPathQueryService) 
col.getService("XPathQueryService", "1.0");
          ResourceSet resultSet = xpathservice.query(query);
  
          ResourceIterator results = resultSet.getIterator();
          List res = asList(results);
          assertEquals(2, res.size());
      }
      
      public void testStartsWithSearchQuery() 
              throws Exception {
          String document3 = "<?xml version=\"1.0\"?>" +
                  "<person>" +
                  "<first>Sally</first>" +
                  "<last>aSm</last>" +
                  "<phone type=\"work\">555-345-6789</phone>" +
                  "</person>";
          this.client.insertDocument(TEST_COLLECTION_PATH, "doc3", document3);
  
          // search all records whose last name begins with 'Smi'
          String query = "//person[(string-length(//person/last) >= 3) and 
(substring(//person/last, 1, 3)='Smi')]";
         
          Collection col = this.client.getCollection(TEST_COLLECTION_PATH);
          XPathQueryService xpathservice = (XPathQueryService) 
col.getService("XPathQueryService", "1.0");
          ResourceSet resultSet = xpathservice.query(query);
  
          ResourceIterator results = resultSet.getIterator();
          List res = asList(results);
          assertEquals(2, res.size());
  
          this.client.removeDocument(TEST_COLLECTION_PATH, "doc3");
      }
      
      public void testEndsWithSearchQuery() 
              throws Exception {
          // search all records whose last name begins with 'ith'
          String query = "//person[(string-length(//person/last) >= 4) and 
(substring(//person/last, 2)='mith')]";
         
          Collection col = this.client.getCollection(TEST_COLLECTION_PATH);
          XPathQueryService xpathservice = (XPathQueryService) 
col.getService("XPathQueryService", "1.0");
          ResourceSet resultSet = xpathservice.query(query);
  
          ResourceIterator results = resultSet.getIterator();
          List res = asList(results);
          assertEquals(2, res.size());
      }
      
      public void testGreaterSearchQuery()
              throws Exception {
          String query = "//person[age > 25]";
                  
          Collection col = this.client.getCollection(TEST_COLLECTION_PATH);
          XPathQueryService xpathservice = (XPathQueryService) 
col.getService("XPathQueryService", "1.0");
          ResourceSet resultSet = xpathservice.query(query);
  
          ResourceIterator results = resultSet.getIterator();
          List res = asList(results);
          assertEquals(1, res.size());
      }
      
      public static List asList(ResourceIterator iter)
              throws Exception {
          if (iter == null) {
              return null;
          }
                  
          List result = null;
          result = new ArrayList();
          while (iter.hasMoreResources()) {
              result.add(iter.nextResource());
          }
          return result;
      }
  
  }
  
  
  
  

Reply via email to