You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xmlbeans.apache.org by ra...@apache.org on 2006/07/14 00:10:08 UTC

svn commit: r421715 - in /xmlbeans/trunk/test: cases/xbean/xmlcursor/xquery/bookstore.xml cases/xbean/xmlcursor/xquery/employees.xml src/xmlcursor/xquery/detailed/XQueryVariableBindingTest.java

Author: radup
Date: Thu Jul 13 15:10:07 2006
New Revision: 421715

URL: http://svn.apache.org/viewvc?rev=421715&view=rev
Log:
Contributed by Wing Yew Poon. Tests for use of XmlOptions in binding XQuery variables.

Added:
    xmlbeans/trunk/test/cases/xbean/xmlcursor/xquery/bookstore.xml
    xmlbeans/trunk/test/cases/xbean/xmlcursor/xquery/employees.xml
    xmlbeans/trunk/test/src/xmlcursor/xquery/detailed/XQueryVariableBindingTest.java

Added: xmlbeans/trunk/test/cases/xbean/xmlcursor/xquery/bookstore.xml
URL: http://svn.apache.org/viewvc/xmlbeans/trunk/test/cases/xbean/xmlcursor/xquery/bookstore.xml?rev=421715&view=auto
==============================================================================
--- xmlbeans/trunk/test/cases/xbean/xmlcursor/xquery/bookstore.xml (added)
+++ xmlbeans/trunk/test/cases/xbean/xmlcursor/xquery/bookstore.xml Thu Jul 13 15:10:07 2006
@@ -0,0 +1,35 @@
+<bookstore>
+
+  <book category="COOKING">
+    <title lang="en">Everyday Italian</title>
+    <author>Giada De Laurentiis</author>
+    <year>2005</year>
+    <price>30.00</price>
+  </book>
+
+  <book category="CHILDREN">
+    <title lang="en">Harry Potter</title>
+    <author>J K. Rowling</author>
+    <year>2005</year>
+    <price>29.99</price>
+  </book>
+
+  <book category="WEB">
+    <title lang="en">XQuery Kick Start</title>
+    <author>James McGovern</author>
+    <author>Per Bothner</author>
+    <author>Kurt Cagle</author>
+    <author>James Linn</author>
+    <author>Vaidyanathan Nagarajan</author>
+    <year>2003</year>
+    <price>49.99</price>
+  </book>
+
+  <book category="WEB">
+    <title lang="en">Learning XML</title>
+    <author>Erik T. Ray</author>
+    <year>2003</year>
+    <price>39.95</price>
+  </book>
+
+</bookstore>

Added: xmlbeans/trunk/test/cases/xbean/xmlcursor/xquery/employees.xml
URL: http://svn.apache.org/viewvc/xmlbeans/trunk/test/cases/xbean/xmlcursor/xquery/employees.xml?rev=421715&view=auto
==============================================================================
--- xmlbeans/trunk/test/cases/xbean/xmlcursor/xquery/employees.xml (added)
+++ xmlbeans/trunk/test/cases/xbean/xmlcursor/xquery/employees.xml Thu Jul 13 15:10:07 2006
@@ -0,0 +1,56 @@
+<employees>
+  <employee>
+    <name>Fred Jones</name>
+    <address location="home">
+      <street>900 Aurora Ave.</street>
+      <city>Seattle</city>
+      <state>WA</state>
+      <zip>98115</zip>
+    </address>
+    <address location="work">
+      <street>2011 152nd Avenue NE</street>
+      <city>Redmond</city>
+      <state>WA</state>
+      <zip>98052</zip>
+    </address>
+    <phone location="work">(425)555-5665</phone>
+    <phone location="home">(206)555-5555</phone>
+    <phone location="mobile">(206)555-4321</phone>
+  </employee>
+  <employee>
+    <name>Sally Smith</name>
+    <address location="home">
+      <street>1430 Oak Place</street>
+      <city>Salem</city>
+      <state>OR</state>
+      <zip>97125</zip>
+    </address>
+    <address location="work">
+      <street>765 Main St.</street>
+      <city>Keizer</city>
+      <state>OR</state>
+      <zip>97303</zip>
+    </address>
+    <phone location="work">(503)555-3856</phone>
+    <phone location="home">(503)555-6951</phone>
+    <phone location="mobile">(503)555-5152</phone>
+  </employee>
+  <employee>
+    <name>Gladys Kravitz</name>
+    <address location="home">
+      <street>1313 Mockingbird Lane</street>
+      <city>Seattle</city>
+      <state>WA</state>
+      <zip>98115</zip>
+    </address>
+    <address location="work">
+      <street>2011 152nd Avenue NE</street>
+      <city>Redmond</city>
+      <state>WA</state>
+      <zip>98052</zip>
+    </address>
+    <phone location="work">(425)555-6897</phone>
+    <phone location="home">(206)555-6594</phone>
+    <phone location="mobile">(206)555-7894</phone>
+  </employee>
+</employees>

Added: xmlbeans/trunk/test/src/xmlcursor/xquery/detailed/XQueryVariableBindingTest.java
URL: http://svn.apache.org/viewvc/xmlbeans/trunk/test/src/xmlcursor/xquery/detailed/XQueryVariableBindingTest.java?rev=421715&view=auto
==============================================================================
--- xmlbeans/trunk/test/src/xmlcursor/xquery/detailed/XQueryVariableBindingTest.java (added)
+++ xmlbeans/trunk/test/src/xmlcursor/xquery/detailed/XQueryVariableBindingTest.java Thu Jul 13 15:10:07 2006
@@ -0,0 +1,205 @@
+/*   Copyright 2006 The Apache Software Foundation
+ *
+ *   Licensed 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.
+ */
+package xmlcursor.xquery.detailed;
+
+import java.io.File;
+import java.util.Map;
+import java.util.HashMap;
+
+import org.apache.xmlbeans.XmlObject;
+import org.apache.xmlbeans.XmlOptions;
+import org.apache.xmlbeans.XmlCursor;
+
+import common.Common;
+
+/** This class tests the use of XmlOptions in binding XQuery variables */
+public class XQueryVariableBindingTest extends Common
+{
+    public static final String XQUERY_CASE_DIR =
+        XBEAN_CASE_ROOT + P + "xmlcursor" + P + "xquery";
+    public static File dir = new File(XQUERY_CASE_DIR);
+
+    public XQueryVariableBindingTest(String name)
+    {
+        super(name);
+    }
+
+    private XmlCursor _testDocCursor1() throws Exception
+    {
+        String xml =
+            "<elem1>" +
+            "<elem11 id=\"123\">text11</elem11>" +
+            "<elem21 id=\"456\">text11</elem21>" +
+            "<elem12 idRef=\"123\"/>" +
+            "<elem13 idRef=\"456\"/>" +
+            "<elem14 idRef=\"123\"/>" +
+            "<elem15 idRef=\"456\"/>" +
+            "<elem16 idRef=\"123\"/>" +
+            "<elem17 idRef=\"789\"/>" +
+            "</elem1>";
+        XmlObject doc = XmlObject.Factory.parse(xml);
+        XmlCursor xc = doc.newCursor();
+        return xc;
+    }
+
+    private void _verifySelection(XmlCursor xc)
+    {
+        assertEquals(3, xc.getSelectionCount());
+        assertTrue(xc.toNextSelection());
+        assertEquals("<elem12 idRef=\"123\"/>", xc.xmlText());
+        assertTrue(xc.toNextSelection());
+        assertEquals("<elem14 idRef=\"123\"/>", xc.xmlText());
+        assertTrue(xc.toNextSelection());
+        assertEquals("<elem16 idRef=\"123\"/>", xc.xmlText());
+    }
+
+    /** test the automatic binding of $this to the current node: selectPath() */
+    public void testThisVariable1() throws Exception
+    {
+        XmlCursor xc = _testDocCursor1();
+        xc.toFirstChild(); //<elem1>
+        xc.toFirstChild(); //<elem11>
+        xc.selectPath("//*[@idRef=$this/@id]");
+        _verifySelection(xc);
+        xc.clearSelections();
+        xc.dispose();
+    }
+
+    // this fails: see JIRA issue XMLBEANS-276
+    /** test the binding of a variable to the current node: selectPath() */
+    public void testCurrentNodeVariable1() throws Exception
+    {
+        XmlCursor xc = _testDocCursor1();
+        xc.toFirstChild();
+        xc.toFirstChild();
+        XmlOptions opts = new XmlOptions();
+        opts.setXqueryCurrentNodeVar("cur");
+        //String varDecl = "declare variable $cur external; ";
+        //xc.selectPath(varDecl + "//*[@idRef=$cur/@id]", opts);
+        xc.selectPath("//*[@idRef=$cur/@id]", opts);
+        _verifySelection(xc);
+        xc.clearSelections();
+        xc.dispose();
+    }
+
+    private XmlCursor _testDocCursor2() throws Exception
+    {
+        File f = new File(dir, "employees.xml");
+        XmlObject doc = XmlObject.Factory.parse(f);
+        XmlCursor xc = doc.newCursor();
+        return xc;
+    }
+
+    public void _verifyQueryResult(XmlCursor qc)
+    {
+        System.out.println(qc.xmlText());
+        assertTrue(qc.toFirstChild());
+        assertEquals("<phone location=\"work\">(425)555-5665</phone>", 
+                     qc.xmlText());
+        assertTrue(qc.toNextSibling());
+        assertEquals("<phone location=\"work\">(425)555-6897</phone>", 
+                     qc.xmlText());
+        assertFalse(qc.toNextSibling());
+    }
+
+    /** test the automatic binding of $this to the current node: execQuery() */
+    public void testThisVariable2() throws Exception
+    {
+        XmlCursor xc = _testDocCursor2();
+        xc.toNextToken();
+        String q =
+            "for $e in $this/employees/employee " +
+            "let $s := $e/address/state " +
+            "where $s = 'WA' " +
+            "return $e//phone[@location='work']";
+        XmlCursor qc = xc.execQuery(q);
+        _verifyQueryResult(qc);
+        xc.dispose();
+        qc.dispose();
+    }
+
+    /** test the binding of a variable to the current node: execQuery() */
+    public void testCurrentNodeVariable2() throws Exception
+    {
+        XmlCursor xc = _testDocCursor2();
+        xc.toNextToken();
+        String q =
+            "for $e in $cur/employees/employee " +
+            "let $s := $e/address/state " +
+            "where $s = 'WA' " +
+            "return $e//phone[@location='work']";
+        XmlOptions opts = new XmlOptions();
+        opts.setXqueryCurrentNodeVar("cur");
+        //String varDecl = "declare variable $cur external; ";
+        //XmlCursor qc = xc.execQuery(varDecl + q, opts);
+        XmlCursor qc = xc.execQuery(q, opts);
+        _verifyQueryResult(qc);
+        xc.dispose();
+        qc.dispose();
+    }
+
+    private XmlObject[] _execute(XmlObject xo, Map m, String q)
+    {
+        XmlOptions opts = new XmlOptions();
+        opts.setXqueryVariables(m);
+        XmlObject[] results = xo.execQuery(q, opts);
+        return results;
+    }
+
+    /** test the binding of a variable to an XmlTokenSource using a map */
+    public void testOneVariable() throws Exception
+    {
+        File f = new File(dir, "bookstore.xml");
+        XmlObject doc = XmlObject.Factory.parse(f);
+        String q =
+            "declare variable $rt external; " +
+            "for $x in $rt/book " +
+            "where $x/price > 30 " +
+            "return $x/title";
+        Map m = new HashMap();
+        m.put("rt", doc.selectChildren("", "bookstore")[0]);
+        XmlObject[] results = _execute(doc, m, q);
+        assertNotNull(results);
+        assertEquals(2, results.length);
+        assertEquals("<title lang=\"en\">XQuery Kick Start</title>",
+                     results[0].xmlText());
+        assertEquals("<title lang=\"en\">Learning XML</title>",
+                     results[1].xmlText());
+    }
+    
+    /** test the binding of multiple variables using a map;
+        at the same time, test the binding of a variable to a String
+     */
+    public void testMultipleVariables() throws Exception
+    {
+        File f = new File(dir, "bookstore.xml");
+        XmlObject doc = XmlObject.Factory.parse(f);
+        String q =
+            "declare variable $rt external; " +
+            "declare variable $c external; " +
+            "for $x in $rt/book " +
+            "where $x[@category=$c] " +
+            "return $x/title";
+        Map m = new HashMap();
+        m.put("rt", doc.selectChildren("", "bookstore")[0]);
+        m.put("c", "CHILDREN");
+        XmlObject[] results = _execute(doc, m, q);
+        assertNotNull(results);
+        assertEquals(1, results.length);
+        assertEquals("<title lang=\"en\">Harry Potter</title>",
+                     results[0].xmlText());
+    }
+
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: commits-help@xmlbeans.apache.org