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.components;
016    
017    import static org.easymock.EasyMock.expect;
018    
019    import org.apache.tapestry.BaseComponentTestCase;
020    import org.apache.tapestry.IBinding;
021    import org.apache.tapestry.IComponent;
022    import org.apache.tapestry.IMarkupWriter;
023    import org.apache.tapestry.IRender;
024    import org.apache.tapestry.IRequestCycle;
025    import org.testng.annotations.Test;
026    
027    /**
028     * Tests for {@link org.apache.tapestry.components.Block}.
029     * 
030     * @author Howard M. Lewis Ship
031     * @since 4.0
032     */
033    @Test
034    public class TestBlock extends BaseComponentTestCase
035    {
036        public void testRender()
037        {
038            final IComponent invoker = newComponent();
039    
040            final Block block = (Block) newInstance(Block.class);
041    
042            IMarkupWriter writer = newWriter();
043            IRequestCycle cycle = newCycle(writer);
044    
045            replay();
046    
047            IRender body = new IRender()
048            {
049                public void render(IMarkupWriter pwriter, IRequestCycle pcycle)
050                {
051                    assertSame(block.getInvoker(), invoker);
052                }
053            };
054    
055            assertNull(block.getInvoker());
056    
057            block.addBody(body);
058    
059            block.renderForComponent(writer, cycle, invoker);
060    
061            assertNull(block.getInvoker());
062    
063            verify();
064        }
065    
066        public void testGetParameter()
067        {
068            Object parameterValue = new Object();
069    
070            IBinding binding = newBinding(parameterValue);
071            IComponent component = newComponent();
072            
073            expect(component.getBinding("fred")).andReturn(binding);
074            
075            replay();
076            
077            Block block = (Block) newInstance(Block.class);
078            
079            block.setInvoker(component);
080            
081            assertSame(parameterValue, block.getParameter("fred"));
082    
083            verify();
084        }
085    
086    }