blob: ce937567e19e7cad2c121ede4973c04f29010f42 [file] [log] [blame]
<?xml version="1.0"?>
<!--
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.
-->
<test:suite
xmlns:j="jelly:core"
xmlns:x="jelly:xml"
xmlns:test="jelly:junit">
<test:case name="testUriParse">
<x:parse var="doc" xml="dummy.xml"/>
<test:assert xpath="$doc/dummy/entry[@id='2']"/>
</test:case>
<test:case name="testBodyParse">
<x:parse var="doc">
<foo>
<bar a="1"/>
</foo>
</x:parse>
<test:assert xpath="$doc/foo/bar[@a='1']"/>
</test:case>
<test:case name="testTextParse">
<j:set var="text" encode="false">
<foo>
<bar a="1"/>
</foo>
</j:set>
<x:parse var="doc" text="${text}"/>
<test:assert xpath="$doc/foo/bar[@a='1']"/>
</test:case>
<test:case name="testElementAndAttribute">
<x:parse var="doc">
<x:element name="foo">
<x:attribute name="x">1234</x:attribute>
<x:element name="bar">
<x:attribute name="y">ABC</x:attribute>
hello
</x:element>
</x:element>
</x:parse>
<test:assert xpath="$doc/foo"/>
<test:assert xpath="$doc/foo[@x='1234']"/>
<test:assert xpath="count($doc/bar) = 0"/>
<!-- test nested element and attributes -->
<test:assert xpath="$doc/foo[@x='1234']/bar[@y='ABC']"/>
<test:assert xpath="$doc/foo[@x='1234']/bar[@y='ABC']='hello'"/>
</test:case>
<test:case name="testBadElementAndAttribute">
<j:catch var="ex">
<x:element name="foo">
some text
<x:attribute name="x">1234</x:attribute>
</x:element>
</j:catch>
<test:assert test="${ex != null}">
We should have created an exception as some text is output before the attributes
</test:assert>
</test:case>
<test:case name="assertXPathTests">
<x:parse var="doc">
<foo>
<bar>cheese</bar>
</foo>
</x:parse>
<test:assert xpath="$doc/foo/bar">This should never fail</test:assert>
<test:assert xpath="$doc/foo/bar = 'cheese'">This should never fail</test:assert>
<j:catch var="ex">
<test:assert xpath="$doc/foo/xyz">This should always fail</test:assert>
</j:catch>
<test:assert test="${ex != null}">We should have created an exception</test:assert>
The exception was: ${ex.message}
</test:case>
<!-- test the use of namespaces with XPath -->
<test:case name="assertXPathWithNamespaceTests" xmlns:a="fooURI" xmlns:z="doesNotMatch" xmlns="different">
<x:parse var="doc">
<foo xmlns="fooURI">
<bar>cheese</bar>
</foo>
</x:parse>
<test:assert xpath="$doc/a:foo/a:bar">This should never fail</test:assert>
<test:assert xpath="$doc/b:foo/b:bar" xmlns:b="fooURI">This should never fail</test:assert>
<j:catch var="ex">
<test:assert xpath="$doc/z:foo/z:bar">This should always fail</test:assert>
</j:catch>
<test:assert test="${ex != null}">We should have created an exception</test:assert>
<j:catch var="ex">
<test:assert xpath="$doc/foo/bar">This should always fail, since foo is not in the empty namespace</test:assert>
</j:catch>
<test:assert test="${ex != null}">We should have created an exception</test:assert>
<!--
|| now lets test that the default namespace 'different' is ignored by the XPath expressions
|| since XPath should ignore the default namespace - you must use a prefix in XPath to denote
|| a namespace
-->
<test:assert xpath="$doc/*[local-name()='foo']"/>
</test:case>
<!-- test xpath sorting -->
<test:case name="testXpathSorting">
<x:parse var="nums">
<a>
<b v="3"/>
<b v="2"/>
<b v="1"/>
<b v="11"/>
<b v="1.4"/>
<b v="1.2"/>
</a>
</x:parse>
<x:parse var="deeper">
<a>
<b><c><d>3<e>1</e></d></c></b>
<b><c><d>2<e>11</e></d></c></b>
<b><c><d>1</d></c></b>
<b><c><d>11</d></c></b>
</a>
</x:parse>
<!-- test ascending -->
<j:set var="result" value=""/>
<x:forEach select="$nums/a/b" var="x" sort="number(@v)">
<x:set var="num" select="$x/@v"/>
<j:set var="result" value="${result} ${num.get(0).getText()}"/>
</x:forEach>
<test:assertEquals expected=" 1 1.2 1.4 2 3 11" actual="${result}"/>
<!-- test descending -->
<j:set var="result" value=""/>
<x:forEach select="$nums/a/b" var="x" sort="number(@v)" descending="true">
<x:set var="num" select="$x/@v"/>
<j:set var="result" value="${result} ${num.get(0).getText()}"/>
</x:forEach>
<test:assertEquals expected=" 11 3 2 1.4 1.2 1" actual="${result}"/>
<!-- test deeper nesting -->
<j:set var="result" value=""/>
<x:forEach select="$deeper/a/b" var="x" sort="number(c/d/text())">
<j:set var="result" value="${result} ${x.getStringValue()}"/>
</x:forEach>
<test:assertEquals expected=" 1 211 31 11" actual="${result}"/>
<!-- test sort as strings -->
<j:set var="result" value=""/>
<x:forEach select="$nums/a/b" var="x" sort="@v">
<x:set var="num" select="$x/@v"/>
<j:set var="result" value="${result} ${num.get(0).getText()}"/>
</x:forEach>
<test:assertEquals expected=" 1 1.2 1.4 11 2 3" actual="${result}"/>
<!-- test x:set with sort -->
<j:set var="result" value=""/>
<x:set var="rset" select="$nums/a/b" sort="number(@v)"/>
<j:forEach var="num" items="${rset.iterator()}">
<j:set var="result" value="${result} ${num.attributeValue('v')}"/>
</j:forEach>
<test:assertEquals expected=" 1 1.2 1.4 2 3 11" actual="${result}"/>
<!-- test x:set with sort -->
<j:set var="result" value=""/>
<x:set var="rset" select="$nums/a/b"/>
<x:sort list="${rset}" sort="number(@v)"/>
<j:forEach var="num" items="${rset.iterator()}">
<j:set var="result" value="${result} ${num.attributeValue('v')}"/>
</j:forEach>
<test:assertEquals expected=" 1 1.2 1.4 2 3 11" actual="${result}"/>
</test:case>
<test:case name="testCommentWithLexical">
<x:parse var="doc">
<foo>
<!-- this will not be output -->
<x:comment text="this is a comment"/>
</foo>
</x:parse>
<test:assert xpath="$doc/foo"/>
<j:set var="t"><x:copyOf select="$doc/foo/comment()" lexical="true"/></j:set>
<test:assertEquals expected="&lt;!--this is a comment--&gt;" actual="${t.trim()}"/>
</test:case>
<test:case name="testCommentWithoutLexical">
<x:parse var="doc">
<foo>
<!-- this will not be output -->
<x:comment text="this is a comment"/>
</foo>
</x:parse>
<test:assert xpath="$doc/foo"/>
<j:set var="t"><x:copyOf select="string($doc/foo/comment())" lexical="true"/></j:set>
<test:assertEquals expected="this is a comment" actual="${t.trim()}"/>
</test:case>
<test:case name="testCommentWithTextAttributeWithLexical">
<x:parse var="doc">
<foo>
<!-- this will not be output -->
<x:comment text="this is a comment"/>
</foo>
</x:parse>
<test:assert xpath="$doc/foo"/>
<j:set var="t"><x:copyOf select="$doc/foo/comment()" lexical="true"/></j:set>
<test:assertEquals expected="&lt;!--this is a comment--&gt;" actual="${t.trim()}"/>
</test:case>
<test:case name="testFileParse">
<j:new var="theSuite" className="java.io.File">
<j:arg value="${basedir}/src/test/org/apache/commons/jelly/tags/xml/suite.jelly"/>
</j:new>
exists = ${theSuite.exists()}, readable = ${theSuite.canRead()}, class=${theSuite.class.name}
<!-- this test fails with beanutils 1.6
<x:parse var="suite" xml="${theSuite}"/> -->
</test:case>
<test:case name="testSetSingleNodeAndAsString">
<x:parse var="blopElements">
<root>
<blop>blop1</blop>
<blip/>
<blop id="bla">blop0</blop></root>
</x:parse>
<!-- should return the second -->
<x:set var="blopSingle" select="$blopElements/root/blop" single="true"/>
<j:invokeStatic var="eltClass" className="java.lang.Class" method="forName"><j:arg value="org.dom4j.Element"/></j:invokeStatic>
<test:assert test="${eltClass.isAssignableFrom(blopSingle.getClass())}"/>
<j:set var="blopSingleText"><x:expr select="$blopSingle/text()"/></j:set>
<test:assertEquals actual="${blopSingleText}" expected="blop1"/>
<!-- check if selecting root/blip returns a list -->
<x:set var="blip" select="$blopElements/root/blip" single="false"/>
<j:invokeStatic var="listClass" className="java.lang.Class" method="forName"><j:arg value="java.util.List"/></j:invokeStatic>
<test:assert test="${listClass.isAssignableFrom(blip.getClass())}"/>
<!-- check if selecting blop/@id asString and single returns a string -->
<x:set var="blopId" select="$blopElements/root/blop/@id" asString="true" single="true"/>
<j:invokeStatic var="stringClass" className="java.lang.Class" method="forName"><j:arg value="java.lang.String"/></j:invokeStatic>
<test:assert test="${stringClass.isAssignableFrom(blopId.getClass())}"/>
<!-- check if select blop/blurp with false single returns an empty list -->
<x:set var="blurp" select="$blopElements/root/blurp" single="false"/>
<j:invokeStatic var="listClass" className="java.lang.Class" method="forName"><j:arg value="java.util.List"/></j:invokeStatic>
<test:assert test="${listClass.isAssignableFrom(blip.getClass())}"/>
<!-- check if select blop/blurp with no single or asString returns null -->
<x:set var="blurp" select="$blopElements/root/blurp"/>
<j:invokeStatic var="listClass" className="java.lang.Class" method="forName"><j:arg value="java.util.List"/></j:invokeStatic>
<j:set var="blurpAndX" value="${blurp}X"/>
blurp=${blurp}, blurpAndX=${blurpAndX}
<test:assert test="${'X' eq blurpAndX}"/>
</test:case>
<test:case name="testSetStringLists">
<j:invokeStatic var="listClass" className="java.lang.Class" method="forName"><j:arg value="java.util.List"/></j:invokeStatic>
<j:invokeStatic var="stringClass" className="java.lang.Class" method="forName"><j:arg value="java.lang.String"/></j:invokeStatic>
<x:parse var="blopElements">
<root>
<blop>blop1</blop>
<blip/>
<blop id="bla">blop0</blop></root>
</x:parse>
<!-- check if selecting root/blop asString returns a list of strings -->
<x:set var="blopList" select="$blopElements/root/blop" asString="true" />
<test:assert test="${listClass.isAssignableFrom(blopList.getClass())}"/>
<test:assert test="${stringClass.isAssignableFrom(blopList.get(0).getClass())}"/>
<test:assertEquals expected="blop1" actual="${blopList.get(0)}"/>
<!-- check if selecting root/blop asString + delim returns a delimited string -->
<x:set var="blopString" select="$blopElements/root/blop" asString="true" delim=","/>
<test:assert test="${stringClass.isAssignableFrom(blopString.getClass())}"/>
<test:assertEquals expected="blop1,blop0" actual="${blopString}"/>
</test:case>
<test:case name="testEntities">
<x:parse var="doc" xml="entity.xml"/>
<!--just the entity is output-->
<j:set var="value"><x:copyOf select="$doc/a/node()"/></j:set>
<test:assertEquals actual="${value}" expected="y"/>
<!--both the entity and it's lexical name are properly output-->
<j:set var="value"><x:copyOf select="$doc/a/node()" lexical="true"/></j:set>
<test:assertEquals actual="${value}" expected="&amp;x;y"/>
</test:case>
</test:suite>