KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > util > TestPageRenderSupport


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.util;
16
17 import java.io.CharArrayWriter JavaDoc;
18 import java.io.PrintWriter JavaDoc;
19
20 import org.apache.hivemind.Location;
21 import org.apache.hivemind.Resource;
22 import org.apache.hivemind.test.HiveMindTestCase;
23 import org.apache.tapestry.IAsset;
24 import org.apache.tapestry.IMarkupWriter;
25 import org.apache.tapestry.IRequestCycle;
26 import org.apache.tapestry.asset.AssetFactory;
27 import org.apache.tapestry.engine.ILink;
28 import org.apache.tapestry.markup.AsciiMarkupFilter;
29 import org.apache.tapestry.markup.MarkupWriterImpl;
30 import org.easymock.MockControl;
31
32 /**
33  * Tests for {@link org.apache.tapestry.util.PageRenderSupportImpl}.
34  *
35  * @author Howard M. Lewis Ship
36  * @since 4.0
37  */

38 public class TestPageRenderSupport extends HiveMindTestCase
39 {
40     private AssetFactory newAssetFactory()
41     {
42         return (AssetFactory) newMock(AssetFactory.class);
43     }
44
45     private IRequestCycle newCycle()
46     {
47         return (IRequestCycle) newMock(IRequestCycle.class);
48     }
49
50     private Resource newResource()
51     {
52         return (Resource) newMock(Resource.class);
53     }
54
55     private IAsset newAsset(IRequestCycle cycle, String JavaDoc url)
56     {
57         MockControl control = newControl(IAsset.class);
58         IAsset asset = (IAsset) control.getMock();
59
60         asset.buildURL(cycle);
61         control.setReturnValue(url);
62
63         return asset;
64     }
65
66     private ILink newLink(String JavaDoc URL)
67     {
68         MockControl control = newControl(ILink.class);
69         ILink link = (ILink) control.getMock();
70
71         link.getURL();
72         control.setReturnValue(URL);
73
74         return link;
75     }
76
77     private CharArrayWriter JavaDoc _writer;
78
79     private IMarkupWriter newWriter()
80     {
81         _writer = new CharArrayWriter JavaDoc();
82
83         return new MarkupWriterImpl("text/html", new PrintWriter JavaDoc(_writer), new AsciiMarkupFilter());
84     }
85
86     private void assertOutput(String JavaDoc[] expectedLines)
87     {
88         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
89         for (int i = 0; i < expectedLines.length; i++)
90         {
91             // Note: PageRenderSupport is a bit sloppy; a lot of code just uses \n for
92
// a newline seperator, other parts go through IMarkupWriter.println() and get
93
// a proper newline seperator (which may be different).
94

95             if (i > 0)
96                 buffer.append("\n");
97
98             buffer.append(expectedLines[i]);
99         }
100
101         assertOutput(buffer.toString());
102     }
103
104     private void assertOutput(String JavaDoc expected)
105     {
106         String JavaDoc actual = _writer.toString();
107
108         assertEquals(expected, actual);
109
110         _writer.reset();
111     }
112
113     public void testGetLocation()
114     {
115         AssetFactory factory = newAssetFactory();
116         Location l = newLocation();
117
118         replayControls();
119
120         PageRenderSupportImpl prs = new PageRenderSupportImpl(factory, "", l);
121
122         assertSame(l, prs.getLocation());
123
124         verifyControls();
125     }
126
127     public void testGetPreloadedImageReference()
128     {
129         AssetFactory factory = newAssetFactory();
130         Location l = newLocation();
131         IRequestCycle cycle = newCycle();
132         IMarkupWriter writer = newWriter();
133
134         replayControls();
135
136         PageRenderSupportImpl prs = new PageRenderSupportImpl(factory, "", l);
137
138         assertEquals("tapestry_preload[0].src", prs.getPreloadedImageReference("/foo/bar.gif"));
139         assertEquals("tapestry_preload[1].src", prs.getPreloadedImageReference("/zip/zap.png"));
140         assertEquals("tapestry_preload[0].src", prs.getPreloadedImageReference("/foo/bar.gif"));
141
142         prs.addBodyScript("myBodyScript();");
143
144         prs.writeBodyScript(writer, cycle);
145
146         assertOutput(new String JavaDoc[]
147         { "<script language=\"JavaScript\" type=\"text/javascript\"><!--", "",
148                 "var tapestry_preload = new Array();", "if (document.images)", "{",
149                 " tapestry_preload[0] = new Image();",
150                 " tapestry_preload[0].src = \"/foo/bar.gif\";",
151                 " tapestry_preload[1] = new Image();",
152                 " tapestry_preload[1].src = \"/zip/zap.png\";", "}", "", "", "myBodyScript();",
153                 "", "// --></script>" });
154
155         verifyControls();
156     }
157
158     public void testPreloadedImagesInNamespace()
159     {
160         AssetFactory factory = newAssetFactory();
161         Location l = newLocation();
162         IRequestCycle cycle = newCycle();
163         IMarkupWriter writer = newWriter();
164
165         replayControls();
166
167         PageRenderSupportImpl prs = new PageRenderSupportImpl(factory, "NAMESPACE", l);
168
169         assertEquals("NAMESPACE_preload[0].src", prs.getPreloadedImageReference("/foo/bar.gif"));
170
171         prs.writeBodyScript(writer, cycle);
172
173         assertOutput(new String JavaDoc[]
174         { "<script language=\"JavaScript\" type=\"text/javascript\"><!--", "",
175                 "var NAMESPACE_preload = new Array();", "if (document.images)", "{",
176                 " NAMESPACE_preload[0] = new Image();",
177                 " NAMESPACE_preload[0].src = \"/foo/bar.gif\";", "}", "", "", "// --></script>" });
178
179         verifyControls();
180     }
181
182     public void testAddBodyScript()
183     {
184         AssetFactory factory = newAssetFactory();
185         Location l = newLocation();
186         IRequestCycle cycle = newCycle();
187         IMarkupWriter writer = newWriter();
188
189         replayControls();
190
191         PageRenderSupportImpl prs = new PageRenderSupportImpl(factory, "", l);
192
193         prs.addBodyScript("myBodyScript();");
194
195         prs.writeBodyScript(writer, cycle);
196
197         assertOutput(new String JavaDoc[]
198         { "<script language=\"JavaScript\" type=\"text/javascript\"><!--", "", "myBodyScript();",
199                 "", "// --></script>" });
200
201         verifyControls();
202     }
203
204     public void testGetUniqueString()
205     {
206         AssetFactory factory = newAssetFactory();
207         Location l = newLocation();
208
209         replayControls();
210
211         PageRenderSupportImpl prs = new PageRenderSupportImpl(factory, "", l);
212
213         assertEquals("foo", prs.getUniqueString("foo"));
214         assertEquals("foo$0", prs.getUniqueString("foo"));
215         assertEquals("bar", prs.getUniqueString("bar"));
216         assertEquals("foo$1", prs.getUniqueString("foo"));
217
218         verifyControls();
219     }
220
221     public void testGetUniqueStringWithNamespace()
222     {
223         AssetFactory factory = newAssetFactory();
224         Location l = newLocation();
225
226         replayControls();
227
228         PageRenderSupportImpl prs = new PageRenderSupportImpl(factory, "NAMESPACE", l);
229
230         assertEquals("fooNAMESPACE", prs.getUniqueString("foo"));
231         assertEquals("fooNAMESPACE$0", prs.getUniqueString("foo"));
232         assertEquals("barNAMESPACE", prs.getUniqueString("bar"));
233         assertEquals("fooNAMESPACE$1", prs.getUniqueString("foo"));
234
235         verifyControls();
236     }
237
238     public void testAddInitializationScript()
239     {
240         AssetFactory factory = newAssetFactory();
241         Location l = newLocation();
242         IMarkupWriter writer = newWriter();
243
244         replayControls();
245
246         PageRenderSupportImpl prs = new PageRenderSupportImpl(factory, "", l);
247
248         prs.addInitializationScript("myInitializationScript1();");
249         prs.addInitializationScript("myInitializationScript2();");
250
251         prs.writeInitializationScript(writer);
252
253         assertOutput(new String JavaDoc[]
254         { "<script language=\"JavaScript\" type=\"text/javascript\"><!--",
255                 "myInitializationScript1();", "myInitializationScript2();", "", "// --></script>" });
256
257         verifyControls();
258     }
259
260     public void testAddExternalScript() throws Exception JavaDoc
261     {
262         String JavaDoc newline = System.getProperty("line.separator");
263
264         IRequestCycle cycle = newCycle();
265
266         IMarkupWriter writer = newWriter();
267
268         MockControl assetFactoryc = newControl(AssetFactory.class);
269         AssetFactory assetFactory = (AssetFactory) assetFactoryc.getMock();
270
271         Resource script1 = newResource();
272         IAsset asset1 = newAsset(cycle, "/script1.js");
273         Resource script2 = newResource();
274         IAsset asset2 = newAsset(cycle, "/script2.js");
275
276         assetFactory.createAsset(script1, null);
277         assetFactoryc.setReturnValue(asset1);
278
279         assetFactory.createAsset(script2, null);
280         assetFactoryc.setReturnValue(asset2);
281
282         replayControls();
283
284         PageRenderSupportImpl prs = new PageRenderSupportImpl(assetFactory, "", null);
285
286         prs.addExternalScript(script1);
287         prs.addExternalScript(script2);
288         prs.addExternalScript(script1);
289
290         prs.writeBodyScript(writer, cycle);
291
292         // PageRenderSupport is a little sloppy about using \n for a newline, vs. using
293
// the property line seperator sequence and it bites us right here.
294

295         assertOutput(scriptTagFor("/script1.js") + newline + scriptTagFor("/script2.js") + newline);
296
297         verifyControls();
298     }
299
300     private String JavaDoc scriptTagFor(String JavaDoc url)
301     {
302         return "<script language=\"JavaScript\" type=\"text/javascript\" SRC=\"" + url
303                 + "\"></script>";
304     }
305 }
Popular Tags