001    // Copyright 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.enhance;
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.hivemind.Resource;
022    import org.apache.tapestry.BaseComponentTestCase;
023    import org.apache.tapestry.IScript;
024    import org.apache.tapestry.engine.IScriptSource;
025    import org.testng.annotations.Test;
026    
027    /**
028     * Tests for {@link org.apache.tapestry.enhance.DeferredScriptImpl}.
029     * 
030     * @author Howard M. Lewis Ship
031     * @since 4.0
032     */
033    @Test
034    public class TestDeferredScript extends BaseComponentTestCase
035    {
036        public void testSuccess()
037        {
038            IScriptSource source = newMock(IScriptSource.class);
039    
040            Resource r = newMock(Resource.class);
041            IScript script = newMock(IScript.class);
042    
043            expect(source.getScript(r)).andReturn(script);
044    
045            replay();
046    
047            DeferredScript ds = new DeferredScriptImpl(r, source, null);
048    
049            assertSame(script, ds.getScript());
050    
051            // Cheating a little for code coverage. The script's resource
052            // is part of ds's toString()
053    
054            assertTrue(ds.toString().indexOf(r.toString()) > 0);
055    
056            verify();
057        }
058    
059        public void testFailure()
060        {
061            IScriptSource source = newMock(IScriptSource.class);
062    
063            Resource newResource = newMock(Resource.class);
064            Resource r = newResource;
065    
066            Location l = newLocation();
067            Throwable t = new RuntimeException("Woops!");
068    
069            expect(source.getScript(r)).andThrow(t);
070    
071            replay();
072    
073            DeferredScript ds = new DeferredScriptImpl(r, source, l);
074    
075            try
076            {
077                ds.getScript();
078                unreachable();
079            }
080            catch (ApplicationRuntimeException ex)
081            {
082                assertEquals("Woops!", ex.getMessage());
083                assertSame(l, ex.getLocation());
084                assertSame(t, ex.getRootCause());
085            }
086    
087            verify();
088    
089        }
090    }