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.portlet;
016    
017    import org.apache.tapestry.*;
018    import org.apache.tapestry.asset.AssetFactory;
019    import org.apache.tapestry.markup.MarkupWriterSource;
020    import org.apache.tapestry.services.ResponseBuilder;
021    import org.apache.tapestry.util.ContentType;
022    import org.apache.tapestry.web.WebResponse;
023    import static org.easymock.EasyMock.*;
024    import org.testng.annotations.Test;
025    
026    import java.io.CharArrayWriter;
027    import java.io.PrintWriter;
028    
029    /**
030     * Tests for {@link org.apache.tapestry.portlet.PortletRendererImpl}.
031     * 
032     * @author Howard M. Lewis Ship
033     * @since 4.0
034     */
035    @Test
036    public class TestPortletRenderer extends BaseComponentTestCase
037    {
038        
039        private PrintWriter newPrintWriter()
040        {
041            return new PrintWriter(new CharArrayWriter());
042        }
043    
044        private AssetFactory newAssetFactory()
045        {
046            return newMock(AssetFactory.class);
047        }
048    
049        private WebResponse newWebResponse(ContentType contentType, PrintWriter writer)
050                throws Exception
051        {
052            WebResponse response = newMock(WebResponse.class);
053            checkOrder(response, false);
054            
055            expect(response.getPrintWriter(contentType)).andReturn(writer);
056    
057            expect(response.getNamespace()).andReturn("NAMESPACE");
058            
059            return response;
060        }
061    
062        private MarkupWriterSource newSource(PrintWriter printWriter, ContentType contentType,
063                IMarkupWriter writer)
064        {
065            MarkupWriterSource source = newMock(MarkupWriterSource.class);
066            
067            expect(source.newMarkupWriter(printWriter, contentType)).andReturn(writer);
068            
069            return source;
070        }
071    
072        private IPage newPage(ContentType contentType)
073        {
074            IPage page = newMock(IPage.class);
075    
076            expect(page.getResponseContentType()).andReturn(contentType);
077            expect(page.getPageName()).andReturn("ZePage");
078    
079            return page;
080        }
081    
082        private IRequestCycle newCycle(String pageName, IPage page)
083        {
084            return newCycle(pageName, page, null);
085        }
086        
087        private IRequestCycle newCycle(String pageName, IPage page, IMarkupWriter writer)
088        {   
089            IRequestCycle cycle = newCycle();
090            
091            cycle.activate(pageName);
092            
093            expect(cycle.getPage()).andReturn(page).anyTimes();                
094            expect(cycle.getAttribute("org.apache.tapestry.PageRenderSupport")).andReturn(null);
095            
096            // We can check that an instance of PageRenderSupport is passed in, but
097            // we can't (easily) check thta it's configured the way we want.
098            
099            cycle.setAttribute(eq("org.apache.tapestry.PageRenderSupport"),  isA(PageRenderSupport.class));
100            
101            cycle.renderPage(isA(ResponseBuilder.class));
102    
103            cycle.removeAttribute("org.apache.tapestry.PageRenderSupport");
104            
105            return cycle;
106        }
107    
108        public void test_Success() throws Exception
109        {
110            ContentType ct = new ContentType("text/html");
111            PrintWriter pw = newPrintWriter();
112            
113            WebResponse response = newWebResponse(ct, pw);
114            IMarkupWriter nested = newNestedWriter();
115            checkOrder(nested, false);
116            
117            IMarkupWriter writer = newWriter();
118    
119            expect(writer.getNestedWriter()).andReturn((NestedMarkupWriter)nested);
120    
121            nested.flush();
122    
123            MarkupWriterSource source = newSource(pw, ct, writer);
124            IPage page = newPage(ct);
125            AssetFactory assetFactory = newAssetFactory();
126            
127            IRequestCycle cycle = newCycle("ZePage", page);
128            
129            writer.comment("BEGIN Tapestry Portlet appId NAMESPACE");
130            writer.comment("Page: ZePage");
131            
132            writer.comment(matches("Generated:.*"));
133            writer.comment(matches("Framework version:.*"));
134    
135            nested.close();
136    
137            writer.comment("END Tapestry Portlet appId NAMESPACE");
138    
139            writer.close();
140    
141            replay();
142            
143            PortletRendererImpl r = new PortletRendererImpl();
144            r.setMarkupWriterSource(source);
145            r.setResponse(response);
146            r.setAssetFactory(assetFactory);
147            r.setApplicationId("appId");
148            
149            r.renderPage(cycle, "ZePage");
150            
151            verify();
152        }
153    
154        // TODO: Tests that prove the RenderPageSupport is working properly.
155    }