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.tapestry.*;
018    import org.apache.tapestry.services.ResponseBuilder;
019    import static org.easymock.EasyMock.expect;
020    import org.testng.annotations.Test;
021    
022    import java.util.List;
023    
024    /**
025     * Tests for the {@link org.apache.tapestry.html.Shell}  component.
026     * 
027     */
028    @Test
029    public class TestShell extends BaseComponentTestCase
030    {
031    
032        /**
033         * Test that Shell does very little when the entire page is rewinding (which itself is a
034         * holdback to the action service).
035         */
036    
037        public void test_Rewinding()
038        {
039            IMarkupWriter writer = newWriter();
040            NestedMarkupWriter nested = newNestedWriter();
041            ResponseBuilder builder = newMock(ResponseBuilder.class);
042            IRequestCycle cycle = newCycle(true, writer);
043            IRender body = newRender();
044            
045            Shell shell = (Shell) newInstance(Shell.class, "builder", builder);
046            
047            expect(cycle.renderStackPush(shell)).andReturn(shell);
048            shell.addBody(body);
049    
050            trainStoreShellInCycle(cycle, shell);
051            expect(builder.isDynamic()).andReturn(false);
052            trainGetNestedWriter(writer, nested);
053            
054            body.render(nested, cycle);
055            
056            nested.close();
057            
058            trainRemoveShellFromCycle(cycle);
059            expect(cycle.renderStackPop()).andReturn(shell);
060            
061            replay();
062    
063            shell.render(writer, cycle);
064    
065            verify();
066        }
067        
068        public void test_Add_Relation()
069        {        
070            Shell shell = newInstance(Shell.class, null);
071            RelationBean css1 = new RelationBean();
072            css1.setHref("temp");
073            RelationBean css2 = new RelationBean();
074            css2.setHref("temp");
075            shell.addRelation(css1);
076            shell.addRelation(css2);
077            
078            List all = shell.getRelations();
079            assertEquals(all.size(), 1);   
080        }
081        
082        public void test_Include_Additional_Content_Null()
083        {
084            StringBuffer sb = new StringBuffer();
085            Shell shell = (Shell) newInstance(Shell.class, "contentBuffer", sb);
086            shell.includeAdditionalContent(null);
087            assertEquals(sb.length(), 0);
088        }
089        
090        public void test_Include_Additional_Content()
091        {
092            StringBuffer sb = new StringBuffer();
093            Shell shell = (Shell) newInstance(Shell.class, "contentBuffer", sb);
094            shell.includeAdditionalContent("data");
095            assertEquals(sb.toString(), "data");
096        }    
097    
098        protected void trainStoreShellInCycle(IRequestCycle cycle, Shell shell)
099        {
100            expect(cycle.getAttribute(Shell.SHELL_ATTRIBUTE)).andReturn(null);
101            cycle.setAttribute(Shell.SHELL_ATTRIBUTE, shell);
102        }
103    
104        protected void trainRemoveShellFromCycle(IRequestCycle cycle)
105        {
106            cycle.removeAttribute(Shell.SHELL_ATTRIBUTE);
107        }
108    }