001    // Copyright 2004, 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.script;
016    
017    import static org.easymock.EasyMock.expect;
018    
019    import org.apache.hivemind.ApplicationRuntimeException;
020    import org.apache.hivemind.Location;
021    import org.apache.tapestry.BaseComponentTestCase;
022    import org.testng.annotations.Test;
023    
024    /**
025     * Tests {@link org.apache.tapestry.script.IfToken}, the basis for the <if> and
026     * <if-not> elements.
027     * 
028     * @author Howard M. Lewis Ship
029     * @since 4.0
030     */
031    @Test
032    public class TestIfToken extends BaseComponentTestCase
033    {
034        public void testEvaluateTrue()
035        {
036            ScriptSession s = newMock(ScriptSession.class);
037    
038            IScriptToken nested = newMock(IScriptToken.class);
039    
040            expect(s.evaluate("EXPRESSION", Boolean.class)).andReturn(Boolean.TRUE);
041    
042            StringBuffer buffer = new StringBuffer();
043    
044            nested.write(buffer, s);
045    
046            replay();
047    
048            IfToken t = new IfToken(true, "EXPRESSION", null);
049            t.addToken(nested);
050    
051            t.write(buffer, s);
052    
053            verify();
054        }
055    
056        public void testEvaluateFalse()
057        {
058            ScriptSession s = newMock(ScriptSession.class);
059    
060            IScriptToken nested = newMock(IScriptToken.class);
061    
062            expect(s.evaluate("EXPRESSION", Boolean.class)).andReturn(Boolean.FALSE);
063    
064            StringBuffer buffer = new StringBuffer();
065    
066            replay();
067    
068            IfToken t = new IfToken(true, "EXPRESSION", null);
069            t.addToken(nested);
070    
071            t.write(buffer, s);
072    
073            verify();
074        }
075    
076        public void testEvaluateMatch()
077        {
078            ScriptSession s = newMock(ScriptSession.class);
079    
080            IScriptToken nested = newMock(IScriptToken.class);
081    
082            expect(s.evaluate("EXPRESSION", Boolean.class)).andReturn(Boolean.FALSE);
083    
084            StringBuffer buffer = new StringBuffer();
085    
086            nested.write(buffer, s);
087    
088            replay();
089    
090            IfToken t = new IfToken(false, "EXPRESSION", null);
091            t.addToken(nested);
092    
093            t.write(buffer, s);
094    
095            verify();
096        }
097    
098        public void testEvaluateFailure()
099        {
100            Location l = fabricateLocation(8);
101            Throwable inner = new ApplicationRuntimeException("Simulated error.");
102    
103            ScriptSession s = newMock(ScriptSession.class);
104    
105            expect(s.evaluate("EXPRESSION", Boolean.class)).andThrow(inner);
106    
107            replay();
108    
109            IfToken t = new IfToken(false, "EXPRESSION", l);
110    
111            try
112            {
113                t.write(null, s);
114                
115                unreachable();
116            }
117            catch (ApplicationRuntimeException ex)
118            {
119                assertEquals(inner.getMessage(), ex.getMessage());
120                assertSame(l, ex.getLocation());
121                assertSame(inner, ex.getRootCause());
122            }
123        }
124    }