blob: c0d52f14798a24b100f7896ea0ea5d5b5c801728 [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:test="jelly:junit">
<test:case name="testSetAndRemove">
<j:set var="x" value="abc"/>
<test:assertEquals expected="abc" actual="${x}"/>
<j:remove var="x"/>
<test:assert test="${x == null}"/>
<test:assert test="${empty x}"/>
</test:case>
<test:case name="testSetWithDefault">
<j:set var="foo" value="abc"/>
<j:set var="x" value="${doesNotExist}" defaultValue="${foo}"/>
<test:assertEquals expected="abc" actual="${x}"/>
<j:set var="doesNotExist" value="def"/>
<j:set var="x" value="${doesNotExist}" defaultValue="${foo}"/>
<test:assertEquals expected="def" actual="${x}"/>
</test:case>
<test:case name="testScope">
<j:set var="outer" value="def"/>
<test:assertEquals expected="def" actual="${outer}"/>
<j:scope>
<j:set var="x" value="abc"/>
<test:assertEquals expected="abc" actual="${x}"/>
<test:assertEquals expected="def" actual="${outer}"/>
</j:scope>
<test:assert test="${x == null}"/>
<test:assert test="${empty x}"/>
</test:case>
<test:case name="testChoose">
<j:set var="x" value="abc"/>
<j:choose>
<j:when test="${x == 'abc'}">
this should always work
</j:when>
<j:when test="${x == 'xyz'}">
<fail>This should never fail</fail>
</j:when>
<j:otherwise>
<fail>This should never fail</fail>
</j:otherwise>
</j:choose>
<j:choose>
<j:when test="${x == 'xyz'}">
<fail>This should never fail</fail>
</j:when>
<j:when test="${x == 'abc'}">
this should always work
</j:when>
<j:otherwise>
<fail>This should never fail</fail>
</j:otherwise>
</j:choose>
<j:choose>
<j:when test="${x == 'xyz'}">
<fail>This should never fail</fail>
</j:when>
<j:when test="${x == 'zzz'}">
<fail>This should never fail</fail>
</j:when>
<j:otherwise>
this should always work
</j:otherwise>
</j:choose>
</test:case>
<test:case name="testIf">
<j:set var="x" value="abc"/>
<j:set var="worked" value="f"/>
<j:if test="${x == 'abc'}">
this should always work
<j:set var="worked" value="t"/>
</j:if>
<test:assertEquals expected="t" actual="${worked}"/>
<j:if test="${x == 'zzz'}">
<fail>This should never fail</fail>
</j:if>
</test:case>
<test:case name="testNewAndSetProperties">
<j:new className="org.apache.commons.jelly.core.Customer" var="customer"/>
<j:setProperties object="${customer}" name="James" city="London" />
Created a new bean: ${customer}
<test:assert test="${customer != null}">Created a customer bean</test:assert>
<test:assertEquals
expected="James"
actual="${customer.name}"/>
<test:assertEquals
expected="London"
actual="${customer.city}"/>
<test:assertEquals
expected="org.apache.commons.jelly.core.Customer"
actual="${customer.class.name}"/>
</test:case>
<test:case name="testUseBean">
<j:useBean var="customer" class="org.apache.commons.jelly.core.Customer" name="James" city="London" />
Created a new bean: ${customer}
<test:assert test="${customer != null}">Created a customer bean</test:assert>
<test:assertEquals
expected="James"
actual="${customer.name}"/>
<test:assertEquals
expected="London"
actual="${customer.city}"/>
<test:assertEquals
expected="org.apache.commons.jelly.core.Customer"
actual="${customer.class.name}"/>
</test:case>
<test:case name="testUseBeanWithSetProperties">
<j:useBean var="customer" class="org.apache.commons.jelly.core.Customer" name="James">
<!-- typically the following tag might be nested inside some conditional logic -->
<j:setProperties name="Bob" city="Atlanta"/>
</j:useBean>
Created a new bean: ${customer}
<test:assert test="${customer != null}">Created a customer bean</test:assert>
<test:assertEquals
expected="Bob"
actual="${customer.name}"/>
<test:assertEquals
expected="Atlanta"
actual="${customer.city}"/>
<test:assertEquals
expected="org.apache.commons.jelly.core.Customer"
actual="${customer.class.name}"/>
</test:case>
<test:case name="testSetWithNoEncoding">
<j:set var="foo" encode="false">
<foo x="1">hello</foo>
</j:set>
<test:assertEquals
expected='&lt;foo x="1"&gt;hello&lt;/foo&gt;'
actual="${foo}"/>
</test:case>
<test:case name="testFileToVar">
<j:file var="foo" omitXmlDeclaration="true">
<foo x="1">hello</foo>
</j:file>
<test:assertEquals
expected='&lt;foo x="1"&gt;hello&lt;/foo&gt;'
actual="${foo}"/>
</test:case>
<!-- whitespace trimming tests -->
<test:case name="testTrim">
<j:set var="foo">
<j:forEach var="i" begin="1" end="3" trim="false"> ${i} </j:forEach>
</j:set>
<test:assertEquals
expected=" 1 2 3 "
actual="${foo}"/>
<j:set var="foo">
<j:forEach var="i" begin="1" end="3" trim="true"> ${i} </j:forEach>
</j:set>
<test:assertEquals
expected="123"
actual="${foo}"/>
<j:set var="foo">
<j:forEach var="i" begin="1" end="3" trim="true"> foo ${i} </j:forEach>
</j:set>
<test:assertEquals
expected="foo 1foo 2foo 3"
actual="${foo}"/>
<j:set var="foo">
<j:forEach var="i" begin="1" end="3" trim="true"> foo ${i} bar </j:forEach>
</j:set>
<test:assertEquals
expected="foo 1 barfoo 2 barfoo 3 bar"
actual="${foo}"/>
</test:case>
<test:case name="testBreak">
<j:forEach var="i" begin="1" end="10">
<j:if test="${i==4}">
<j:break/>
</j:if>
</j:forEach>
<test:assert test="${i==4}"/>
<j:while test="${i != 10}">
<j:if test="${i==6}">
<j:break/>
</j:if>
<j:set var="i" value="${i+1}"/>
</j:while>
<test:assert test="${i==6}"/>
</test:case>
<test:case name="testBreakWithIf">
<j:forEach var="i" begin="1" end="10">
<j:break test="${i==4}"/>
</j:forEach>
<test:assert test="${i==4}"/>
<j:while test="${i != 10}">
<j:break test="${i==6}"/>
<j:set var="i" value="${i+1}"/>
</j:while>
<test:assert test="${i==6}"/>
</test:case>
<test:case name="testVariablesWithDots">
<j:set var="page.name" value="a"/>
<j:set var="page.foo" value="b"/>
<test:assertEquals expected="a" actual="${page.name}"/>
<test:assertEquals expected="b" actual="${page.foo}"/>
<j:set var="outer" value="def"/>
<test:assertEquals expected="def" actual="${outer}"/>
<j:scope>
<test:assertEquals expected="a" actual="${page.name}"/>
<test:assertEquals expected="b" actual="${page.foo}"/>
</j:scope>
</test:case>
<!-- tests to make sure import works -->
<test:case name="testImport">
<j:set var="parentVar" value="12" />
<!-- imported scripts by default do not inherit parent vars,
but export to them -->
<j:set var="test1" value="original"/>
<j:import uri="import1.jelly"/>
<test:assert test="${test1 == null}"/>
<!-- inherit parent vars, and export to them -->
<j:set var="test1" value="original"/>
<j:import uri="import1.jelly" inherit="true"/>
<test:assertEquals expected="12" actual="${test1}"/>
<!-- explicitly do not inherit parent vars,
but export to them -->
<j:set var="test1" value="original"/>
<j:import uri="import1.jelly" inherit="false"/>
<test:assert test="${test1 == null}"/>
</test:case>
<!-- tests whether the mute tag indeed outputs nothing -->
<test:case name="testMuteTag">
<j:set var="shouldBeNothing"><j:mute>blop</j:mute></j:set>
<test:assertEquals expected="" actual="${shouldBeNothing}"/>
</test:case>
<test:case name="testForEachWithString">
<j:set var="j" value=""/>
<j:forEach var="i" items="a,b,c">
<j:set var="j" value="${j}${i}"/>
</j:forEach>
<test:assertEquals expected="abc" actual="${j}"/>
</test:case>
<!-- tests the file output provided by j:file -->
<test:case name="testFileTag">
<j:file name="target/test_iso.xml" encoding="ISO-8859-1" omitXmlDeclaration="true">
<l>é#\ü</l></j:file>
<!-- how to check result without xml-unit ? -->
<j:new var="f" className="java.io.File"><j:arg value="target/test_iso.xml"/></j:new>
<j:set var="singleSize" value="${f.length()}"/>
<!-- try outputting a second time -->
<j:file name="target/test_iso.xml" encoding="ISO-8859-1"
omitXmlDeclaration="true" append="true">
<l>é#\ü</l></j:file>
<j:set var="doubleSize" value="${f.length()}"/>
<test:assertEquals expected="${singleSize*2}" actual="${doubleSize}"/>
</test:case>
</test:suite>