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.junit;
016    
017    import static org.easymock.EasyMock.expect;
018    
019    import org.apache.tapestry.BaseComponent;
020    import org.apache.tapestry.BaseComponentTestCase;
021    import org.apache.tapestry.IMarkupWriter;
022    import org.apache.tapestry.IRender;
023    import org.apache.tapestry.IRequestCycle;
024    import org.apache.tapestry.engine.NullWriter;
025    import org.apache.tapestry.services.ResponseBuilder;
026    import org.apache.tapestry.services.impl.DefaultResponseBuilder;
027    import org.apache.tapestry.test.Creator;
028    import org.testng.annotations.Test;
029    
030    /**
031     * Test a few random things in {@link org.apache.tapestry.AbstractComponent}and
032     * {@link  org.apache.tapestry.BaseComponent}.
033     * 
034     * @author Howard Lewis Ship
035     * @since 3.0
036     */
037    @Test
038    public class TestComponent extends BaseComponentTestCase
039    {
040        private static class TestRender implements IRender
041        {
042            private boolean rendered = false;
043    
044            public void render(IMarkupWriter writer, IRequestCycle cycle)
045            {
046                rendered = true;
047            }
048    
049        }
050    
051        public abstract static class FakeComponent extends BaseComponent
052        {
053            void addOuterTest(IRender render)
054            {
055                addOuter(render);
056            }
057    
058            void testRenderComponent(IMarkupWriter write, IRequestCycle cycle)
059            {
060                renderComponent(write, cycle);
061            }
062        }
063    
064        /**
065         * Test the ability of {@link org.apache.tapestry.BaseComponent#addOuter(IRender)}to add a
066         * large number of objects.
067         */
068    
069        public void testOuter() throws Exception
070        {
071            IMarkupWriter writer = new NullWriter();
072            IRequestCycle cycle = newMock(IRequestCycle.class);
073            
074            Creator creator = new Creator();
075            
076            FakeComponent c = (FakeComponent) creator.newInstance(FakeComponent.class);
077            
078            TestRender[] list = new TestRender[50];
079            
080            ResponseBuilder builder = 
081                new DefaultResponseBuilder(writer);
082            
083            for (int i = 0; i < list.length; i++)
084            {
085                list[i] = new TestRender();
086                c.addOuterTest(list[i]);
087                
088                expect(cycle.getResponseBuilder()).andReturn(builder);
089            }
090            
091            replay();
092            
093            c.testRenderComponent(writer, cycle);
094            
095            verify();
096            
097            for (int i = 0; i < list.length; i++)
098                assertTrue(list[i].rendered);
099        }
100    }