blob: 421fae8643d9d981dba38cd9a76caef6509e6403 [file] [log] [blame]
<?xml version="1.0"?>
<!--
Copyright 2002-2004 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.
-->
<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>
<x:set var="blopSingle" select="$blopElements/root/blop" single="true"/>
<!-- should return the second -->
<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 returns a string -->
<x:set var="blopId" select="$blopElements/root/blop/@id" asString="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())}"/>
</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>