1 15 package org.apache.tapestry.portlet; 16 17 import java.io.CharArrayWriter ; 18 import java.io.PrintWriter ; 19 20 import org.apache.hivemind.test.AggregateArgumentsMatcher; 21 import org.apache.hivemind.test.ArgumentMatcher; 22 import org.apache.hivemind.test.HiveMindTestCase; 23 import org.apache.hivemind.test.RegexpMatcher; 24 import org.apache.hivemind.test.TypeMatcher; 25 import org.apache.tapestry.IMarkupWriter; 26 import org.apache.tapestry.IPage; 27 import org.apache.tapestry.IRequestCycle; 28 import org.apache.tapestry.NestedMarkupWriter; 29 import org.apache.tapestry.asset.AssetFactory; 30 import org.apache.tapestry.engine.IEngineService; 31 import org.apache.tapestry.markup.MarkupWriterSource; 32 import org.apache.tapestry.util.ContentType; 33 import org.apache.tapestry.util.PageRenderSupportImpl; 34 import org.apache.tapestry.web.WebResponse; 35 import org.easymock.MockControl; 36 37 43 public class TestPortletRenderer extends HiveMindTestCase 44 { 45 private NestedMarkupWriter newNestedWriter() 46 { 47 return (NestedMarkupWriter) newMock(NestedMarkupWriter.class); 48 } 49 50 private PrintWriter newPrintWriter() 51 { 52 return new PrintWriter (new CharArrayWriter ()); 53 } 54 55 private AssetFactory newAssetFactory() 56 { 57 return (AssetFactory) newMock(AssetFactory.class); 58 } 59 60 private WebResponse newWebResponse(ContentType contentType, PrintWriter writer) 61 throws Exception 62 { 63 MockControl control = newControl(WebResponse.class); 64 WebResponse response = (WebResponse) control.getMock(); 65 66 response.getPrintWriter(contentType); 67 control.setReturnValue(writer); 68 69 response.getNamespace(); 70 control.setReturnValue("NAMESPACE"); 71 72 return response; 73 } 74 75 private MarkupWriterSource newSource(PrintWriter printWriter, ContentType contentType, 76 IMarkupWriter writer) 77 { 78 MockControl control = newControl(MarkupWriterSource.class); 79 MarkupWriterSource source = (MarkupWriterSource) control.getMock(); 80 81 source.newMarkupWriter(printWriter, contentType); 82 control.setReturnValue(writer); 83 84 return source; 85 } 86 87 private IPage newPage(ContentType contentType) 88 { 89 MockControl control = newControl(IPage.class); 90 IPage page = (IPage) control.getMock(); 91 92 page.getResponseContentType(); 93 control.setReturnValue(contentType); 94 95 page.getPageName(); 96 control.setReturnValue("ZePage"); 97 98 return page; 99 } 100 101 private IRequestCycle newCycle(String pageName, IPage page) 102 { 103 MockControl control = newControl(IRequestCycle.class); 104 IRequestCycle cycle = (IRequestCycle) control.getMock(); 105 106 cycle.activate(pageName); 107 108 cycle.getPage(); 109 control.setReturnValue(page); 110 111 cycle.getAttribute("org.apache.tapestry.PageRenderSupport"); 112 control.setReturnValue(null); 113 114 cycle.setAttribute("org.apache.tapestry.PageRenderSupport", new PageRenderSupportImpl( 117 newAssetFactory(), "", null)); 118 control.setMatcher(new AggregateArgumentsMatcher(new ArgumentMatcher[] 119 { null, new TypeMatcher() })); 120 121 return cycle; 122 } 123 124 public void testSuccess() throws Exception 125 { 126 ContentType ct = new ContentType("text/html"); 127 PrintWriter pw = newPrintWriter(); 128 WebResponse response = newWebResponse(ct, pw); 129 IMarkupWriter nested = newNestedWriter(); 130 131 MockControl control = newControl(IMarkupWriter.class); 132 IMarkupWriter writer = (IMarkupWriter) control.getMock(); 133 134 writer.getNestedWriter(); 135 control.setReturnValue(nested); 136 137 MarkupWriterSource source = newSource(pw, ct, writer); 138 IPage page = newPage(ct); 139 AssetFactory assetFactory = newAssetFactory(); 140 141 IRequestCycle cycle = newCycle("ZePage", page); 142 143 cycle.renderPage(nested); 144 145 writer.comment("BEGIN Tapestry Portlet appId NAMESPACE"); 146 writer.comment("Page: ZePage"); 147 148 writer.comment("Generated:.*"); 149 control.setMatcher(new RegexpMatcher()); 150 151 writer.comment("Framework version:.*"); 152 153 nested.close(); 154 155 writer.comment("END Tapestry Portlet appId NAMESPACE"); 156 157 writer.close(); 158 159 replayControls(); 160 161 PortletRendererImpl r = new PortletRendererImpl(); 162 r.setMarkupWriterSource(source); 163 r.setResponse(response); 164 r.setAssetFactory(assetFactory); 165 r.setApplicationId("appId"); 166 167 r.renderPage(cycle, "ZePage"); 168 169 verifyControls(); 170 } 171 172 } | Popular Tags |