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.html;
016    
017    import org.apache.hivemind.ApplicationRuntimeException;
018    import org.apache.hivemind.Location;
019    import org.apache.tapestry.BaseComponentTestCase;
020    import org.apache.tapestry.IAsset;
021    import org.apache.tapestry.IBinding;
022    import org.apache.tapestry.IMarkupWriter;
023    import org.apache.tapestry.IPage;
024    import org.apache.tapestry.IRequestCycle;
025    import org.apache.tapestry.spec.IComponentSpecification;
026    import org.testng.annotations.Test;
027    
028    /**
029     * Tests for {@link org.apache.tapestry.html.Image} component.
030     * 
031     * @author Howard M. Lewis Ship
032     * @since 4.0
033     */
034    @Test(sequential=true)
035    public class TestImage extends BaseComponentTestCase
036    {
037        public void testRewinding()
038        {
039            IMarkupWriter writer = newWriter();
040            IRequestCycle cycle = newCycle();
041    
042            trainIsRewinding(cycle, true);
043    
044            Image image = (Image) newInstance(Image.class);
045    
046            replay();
047    
048            image.renderComponent(writer, cycle);
049    
050            verify();
051        }
052    
053        public void testNoImage()
054        {
055            Location l = newLocation();
056            IMarkupWriter writer = newWriter();
057            IRequestCycle cycle = newCycle();
058            IPage page = newPage();
059    
060            trainIsRewinding(cycle, false);
061    
062            trainGetPageName(page, "Fred");
063            trainGetIdPath(page, null);
064    
065            Image image = newInstance(Image.class, new Object[]
066            { "location", l, "id", "barney", "page", page, "container", page });
067    
068            replay();
069    
070            try
071            {
072                image.renderComponent(writer, cycle);
073            }
074            catch (ApplicationRuntimeException ex)
075            {
076                assertEquals(
077                        "Value for parameter 'image' in component Fred/barney is null, and a non-null value is required.",
078                        ex.getMessage());
079                assertSame(image, ex.getComponent());
080                assertSame(l, ex.getLocation());
081            }
082    
083            verify();
084        }
085    
086        public void testRender()
087        {
088            IMarkupWriter writer = newWriter();
089            IRequestCycle cycle = newCycle(false);
090            
091            IAsset asset = newAsset();
092            trainBuildURL(asset, cycle, "/foo.gif");
093            
094            writer.beginEmpty("img");
095            writer.attribute("src", "/foo.gif");
096            
097            IComponentSpecification spec = newSpec("border", null);
098            IBinding informal = newBinding("0");
099            
100            writer.attribute("border", "0");
101    
102            writer.closeTag();
103            
104            replay();
105            
106            Image image = newInstance(Image.class, new Object[]
107            { "image", asset, "specification", spec });
108            
109            image.setBinding("border", informal);
110    
111            image.renderComponent(writer, cycle);
112    
113            verify();
114        }
115    }