KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > portlet > TestPortletRenderer


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.portlet;
16
17 import java.io.CharArrayWriter JavaDoc;
18 import java.io.PrintWriter JavaDoc;
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 /**
38  * Tests for {@link org.apache.tapestry.portlet.PortletRendererImpl}.
39  *
40  * @author Howard M. Lewis Ship
41  * @since 4.0
42  */

43 public class TestPortletRenderer extends HiveMindTestCase
44 {
45     private NestedMarkupWriter newNestedWriter()
46     {
47         return (NestedMarkupWriter) newMock(NestedMarkupWriter.class);
48     }
49
50     private PrintWriter JavaDoc newPrintWriter()
51     {
52         return new PrintWriter JavaDoc(new CharArrayWriter JavaDoc());
53     }
54
55     private AssetFactory newAssetFactory()
56     {
57         return (AssetFactory) newMock(AssetFactory.class);
58     }
59
60     private WebResponse newWebResponse(ContentType contentType, PrintWriter JavaDoc writer)
61             throws Exception JavaDoc
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 JavaDoc 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 JavaDoc 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         // We can check that an instance of PageRenderSupport is passed in, but
115
// we can't (easily) check thta it's configured the way we want.
116
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 JavaDoc
125     {
126         ContentType ct = new ContentType("text/html");
127         PrintWriter JavaDoc 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     // TODO: Tests that prove the RenderPageSupport is working properly.
173
}
Popular Tags