1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.custom.transform;
21
22 import org.apache.shale.test.base.AbstractJsfTestCase;
23 import org.apache.shale.test.mock.MockResponseWriter;
24 import org.apache.shale.test.mock.MockRenderKitFactory;
25 import org.apache.shale.test.mock.MockHttpServletRequest;
26 import org.apache.shale.test.mock.MockValueBinding;
27
28 import junit.framework.Test;
29 import junit.framework.TestSuite;
30
31 import java.io.*;
32 import java.net.URL;
33 import java.net.URI;
34
35
36
37
38
39
40 public class XmlTransformTest extends AbstractJsfTestCase
41 {
42 private static final String PETS_STYLESHEET = "pets.xsl";
43 private static final String PETS_CONTENT = "pets.xml";
44 private static final String EXPECTED_TEXT = "Iguana";
45
46 private XmlTransform xmlTransform;
47 private ManagedFoo fooBean;
48 private String stylesheet;
49 private String stylesheetLocation = PETS_STYLESHEET;
50 private InputStream styleStream;
51 private String content;
52 private String contentLocation = PETS_CONTENT;
53 private InputStream contentStream;
54
55 private StringWriter mockWriter = new StringWriter();
56
57
58
59
60
61 public XmlTransformTest(String name)
62 {
63 super(name);
64 }
65
66
67
68
69 public void setUp() throws Exception
70 {
71 super.setUp();
72
73 xmlTransform = new XmlTransform();
74
75
76 facesContext.setResponseWriter(new MockResponseWriter(mockWriter, null, null));
77
78
79 facesContext.getViewRoot().setRenderKitId(MockRenderKitFactory.HTML_BASIC_RENDER_KIT);
80
81 fooBean = new ManagedFoo();
82 fooBean.setContentLocation(PETS_CONTENT);
83 fooBean.setStylesheetLocation(PETS_STYLESHEET);
84
85
86 MockHttpServletRequest request = (MockHttpServletRequest)facesContext.getExternalContext().getRequest();
87 request.setAttribute("foo", fooBean);
88
89
90 ClassLoader loader = Thread.currentThread().getContextClassLoader();
91 if (loader == null)
92 {
93 loader = XmlTransform.class.getClassLoader();
94 }
95
96 URL url = loader.getResource(PETS_CONTENT);
97 try
98 {
99 contentStream = new FileInputStream(new File(URI.create(url.toString())));
100
101
102 FileInputStream cs = new FileInputStream(new File(URI.create(url.toString())));
103 int x= cs.available();
104 byte b[]= new byte[x];
105
106 cs.read(b);
107 content = new String(b);
108 }
109 catch (IOException io)
110 {}
111
112
113 url = loader.getResource(PETS_STYLESHEET);
114 try
115 {
116 styleStream = new FileInputStream(new File(URI.create(url.toString())));
117
118
119 FileInputStream ss = new FileInputStream(new File(URI.create(url.toString())));
120 int x= ss.available();
121 byte b[]= new byte[x];
122
123 ss.read(b);
124 stylesheet = new String(b);
125 }
126 catch (IOException io)
127 {}
128
129 fooBean.setContent(content);
130 fooBean.setStylesheet(stylesheet);
131 fooBean.setContentStream(contentStream);
132 fooBean.setStyleStream(styleStream);
133 }
134
135
136
137
138 public void testSaveAndRestoreState()
139 {
140
141
142
143
144 xmlTransform.setContent(content);
145 xmlTransform.setContentLocation(contentLocation);
146 xmlTransform.setStylesheet(stylesheet);
147 xmlTransform.setStylesheetLocation(stylesheetLocation);
148 xmlTransform.setContentStream(contentStream);
149 xmlTransform.setStyleStream(styleStream);
150
151 Object state = xmlTransform.saveState(facesContext);
152 xmlTransform = new XmlTransform();
153 xmlTransform.restoreState(facesContext, state);
154
155 assertEquals(content, xmlTransform.getContent());
156 assertEquals(contentLocation, xmlTransform.getContentLocation());
157 assertEquals(stylesheet, xmlTransform.getStylesheet());
158 assertEquals(stylesheetLocation, xmlTransform.getStylesheetLocation());
159 assertNull("contentStream should be null since it cannot be serialized", xmlTransform.getContentStream());
160 assertNull("styleStream should be null since it cannot be serialized", xmlTransform.getStyleStream());
161 }
162
163
164
165
166
167 public void testSaveAndRestoreStateValueBinding()
168 {
169
170
171
172
173 xmlTransform.setValueBinding("content", new MockValueBinding(application, "#{foo.content}"));
174 xmlTransform.setValueBinding("contentLocation", new MockValueBinding(application, "#{foo.contentLocation}"));
175 xmlTransform.setValueBinding("stylesheet", new MockValueBinding(application, "#{foo.stylesheet}"));
176 xmlTransform.setValueBinding("stylesheetLocation", new MockValueBinding(application, "#{foo.stylesheetLocation}"));
177 xmlTransform.setValueBinding("contentStream", new MockValueBinding(application, "#{foo.contentStream}"));
178 xmlTransform.setValueBinding("styleStream", new MockValueBinding(application, "#{foo.styleStream}"));
179
180 Object state = xmlTransform.saveState(facesContext);
181 xmlTransform = new XmlTransform();
182 xmlTransform.restoreState(facesContext, state);
183
184 assertEquals(content, xmlTransform.getContent());
185 assertEquals(contentLocation, xmlTransform.getContentLocation());
186 assertEquals(stylesheet, xmlTransform.getStylesheet());
187 assertEquals(stylesheetLocation, xmlTransform.getStylesheetLocation());
188 assertEquals(contentStream, xmlTransform.getContentStream());
189 }
190
191 public void testSetContent()
192 {
193 xmlTransform.setContent("foo");
194 assertEquals("foo", xmlTransform.getContent());
195
196
197 xmlTransform.setContent(null);
198 xmlTransform.setValueBinding("content", new MockValueBinding(application, "#{foo.content}"));
199 assertEquals(fooBean.getContent(), xmlTransform.getContent());
200 }
201
202 public void testSetContentStream()
203 {
204 xmlTransform.setContentStream(fooBean.getContentStream());
205 assertEquals(fooBean.getContentStream(), xmlTransform.getContentStream());
206 }
207
208 public void testSetContentStreamValueBinding()
209 {
210 xmlTransform.setValueBinding("contentStream", new MockValueBinding(application, "#{foo.contentStream}"));
211 assertEquals(fooBean.getContentStream(), xmlTransform.getContentStream());
212 }
213
214 public void testSetStylesheet()
215 {
216 xmlTransform.setStylesheet("foo");
217 assertEquals("foo", xmlTransform.getStylesheet());
218
219
220 xmlTransform.setStylesheet(null);
221 xmlTransform.setValueBinding("stylesheet", new MockValueBinding(application, "#{foo.stylesheet}"));
222 assertEquals(fooBean.getStylesheet(), xmlTransform.getStylesheet());
223 }
224
225
226
227
228 public void testNoTransformInfo() throws IOException
229 {
230 try
231 {
232 xmlTransform.encodeBegin(facesContext);
233 }
234 catch (NullPointerException e)
235 {
236 return;
237 }
238
239 fail("Expected exception when no Transform or stylesheet provided");
240 }
241
242
243
244
245 public void testStylesheetNoContent() throws IOException
246 {
247
248 xmlTransform.setStylesheet("blah");
249
250 try
251 {
252 xmlTransform.encodeBegin(facesContext);
253 }
254 catch (NullPointerException e)
255 {
256 return;
257 }
258
259 fail("Expected exception when stylesheet but no content provided");
260 }
261
262 public void testStyleSheet() throws IOException
263 {
264 xmlTransform.setContentLocation(contentLocation);
265 xmlTransform.setStylesheet(stylesheet);
266 xmlTransform.encodeBegin(facesContext);
267
268 String responseText = mockWriter.toString();
269 assertEquals("Unexpected response text from content transformation", EXPECTED_TEXT, responseText);
270 }
271
272 public void testStyleSheetValueBinding() throws IOException
273 {
274 xmlTransform.setContent(content);
275 xmlTransform.setValueBinding("stylesheet", new MockValueBinding(application, "#{foo.stylesheet}"));
276 xmlTransform.encodeBegin(facesContext);
277
278 String responseText = mockWriter.toString();
279 assertEquals("Unexpected response text from content transformation", EXPECTED_TEXT, responseText);
280 }
281
282 public void testStylesheetLocation() throws IOException
283 {
284 xmlTransform.setContent(content);
285 xmlTransform.setStylesheetLocation(stylesheetLocation);
286 xmlTransform.encodeBegin(facesContext);
287
288 String responseText = mockWriter.toString();
289 assertEquals("Unexpected response text from content transformation", EXPECTED_TEXT, responseText);
290 }
291
292 public void testStylesheetLocationValueBinding() throws IOException
293 {
294 xmlTransform.setContent(content);
295 xmlTransform.setValueBinding("stylesheetLocation", new MockValueBinding(application, "#{foo.stylesheetLocation}"));
296 xmlTransform.encodeBegin(facesContext);
297
298 String responseText = mockWriter.toString();
299 assertEquals("Unexpected response text from content transformation", EXPECTED_TEXT, responseText);
300 }
301
302 public void testStylesheetStream() throws IOException
303 {
304 xmlTransform.setStyleStream(styleStream);
305 xmlTransform.setContent(content);
306 xmlTransform.encodeBegin(facesContext);
307
308 String responseText = mockWriter.toString();
309 assertEquals("Unexpected response text from content transformation", EXPECTED_TEXT, responseText);
310 }
311
312 public void testStylesheetStreamValueBinding() throws IOException
313 {
314 xmlTransform.setValueBinding("styleStream", new MockValueBinding(application, "#{foo.styleStream}"));
315 xmlTransform.setContent(content);
316 xmlTransform.encodeBegin(facesContext);
317
318 String responseText = mockWriter.toString();
319 assertEquals("Unexpected response text from content transformation", EXPECTED_TEXT, responseText);
320 }
321
322 public void testContent() throws IOException
323 {
324 xmlTransform.setContent(content);
325 xmlTransform.setStylesheet(stylesheet);
326 xmlTransform.encodeBegin(facesContext);
327
328 String responseText = mockWriter.toString();
329 assertEquals("Unexpected response text from content transformation", EXPECTED_TEXT, responseText);
330 }
331
332 public void testContentValueBinding() throws IOException
333 {
334 xmlTransform.setValueBinding("content", new MockValueBinding(application, "#{foo.content}"));
335 xmlTransform.setStylesheet(stylesheet);
336 xmlTransform.encodeBegin(facesContext);
337
338 String responseText = mockWriter.toString();
339 assertEquals("Unexpected response text from content transformation", EXPECTED_TEXT, responseText);
340 }
341
342 public void testContentLocation() throws IOException
343 {
344 xmlTransform.setContentLocation(contentLocation);
345 xmlTransform.setStylesheet(stylesheet);
346 xmlTransform.encodeBegin(facesContext);
347
348 String responseText = mockWriter.toString();
349 assertEquals("Unexpected response text from content transformation", EXPECTED_TEXT, responseText);
350 }
351
352 public void testContentLocationValueBinding() throws IOException
353 {
354 xmlTransform.setValueBinding("contentLocation", new MockValueBinding(application, "#{foo.contentLocation}"));
355 xmlTransform.setStylesheet(stylesheet);
356 xmlTransform.encodeBegin(facesContext);
357
358 String responseText = mockWriter.toString();
359 assertEquals("Unexpected response text from content transformation", EXPECTED_TEXT, responseText);
360 }
361
362
363
364
365
366 public void testContentStream() throws IOException
367 {
368 xmlTransform.setContentStream(contentStream);
369 xmlTransform.setStylesheet(stylesheet);
370 xmlTransform.encodeBegin(facesContext);
371
372 String responseText = mockWriter.toString();
373 assertEquals("Unexpected response text from content transformation", EXPECTED_TEXT, responseText);
374 }
375
376
377
378
379
380
381
382
383 public void testContentStreamValueBinding() throws IOException
384 {
385 xmlTransform.setValueBinding("contentStream", new MockValueBinding(application, "#{foo.contentStream}"));
386 xmlTransform.setStylesheetLocation(stylesheetLocation);
387 xmlTransform.encodeBegin(facesContext);
388
389 String responseText = mockWriter.toString();
390 assertEquals("Unexpected response text from content transformation", EXPECTED_TEXT, responseText);
391 }
392
393 public static Test suite()
394 {
395 return new TestSuite(XmlTransformTest.class);
396 }
397
398 }