blob: 60cd682a74a529caf148376e28cee006ebfc781d [file] [log] [blame]
<?xml version="1.0"?>
<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="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:assertTrue 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:assertTrue 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:assertTrue test="${i==4}"/>
<j:while test="${i != 10}">
<j:break test="${i==6}"/>
<j:set var="i" value="${i+1}"/>
</j:while>
<test:assertTrue 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>
</test:suite>