View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Test case for XmlTransform
37   *
38   * @author Sean Schofield
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       * Constructor
59       * @param name String
60       */
61      public XmlTransformTest(String name)
62      {
63          super(name);
64      }
65  
66      /**
67       * See abstract class
68       */
69      public void setUp() throws Exception
70      {
71          super.setUp();
72  
73          xmlTransform = new XmlTransform();
74  
75          // additional setup not provided automatically by the shale mock stuff
76          facesContext.setResponseWriter(new MockResponseWriter(mockWriter, null, null));
77  
78          // TODO remove these two lines once shale-test goes alpha, see MYFACES-1155
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          // put the foo bean in the request scope
86          MockHttpServletRequest request = (MockHttpServletRequest)facesContext.getExternalContext().getRequest();
87          request.setAttribute("foo", fooBean);
88  
89          // setup input stream to be used in some of the test cases
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             // now also populate the String variable for certain test cases
102             FileInputStream cs = new FileInputStream(new File(URI.create(url.toString())));
103             int x= cs.available();
104             byte b[]= new byte[x];
105             //noinspection ResultOfMethodCallIgnored
106             cs.read(b);
107             content = new String(b);
108         }
109         catch (IOException io)
110         {}  // do nothing - test will fail eventually
111 
112         // setup stylesheet text to be used in some of the test cases
113         url = loader.getResource(PETS_STYLESHEET);
114         try
115         {
116             styleStream = new FileInputStream(new File(URI.create(url.toString())));
117 
118             // now also populate the String variable for certain test cases
119             FileInputStream ss = new FileInputStream(new File(URI.create(url.toString())));
120             int x= ss.available();
121             byte b[]= new byte[x];
122             //noinspection ResultOfMethodCallIgnored
123             ss.read(b);
124             stylesheet = new String(b);
125         }
126         catch (IOException io)
127         {}  // do nothing - test will fail eventually
128 
129         fooBean.setContent(content);
130         fooBean.setStylesheet(stylesheet);
131         fooBean.setContentStream(contentStream);
132         fooBean.setStyleStream(styleStream);
133     }
134 
135     /**
136      * Test the save and restore state methods.
137      */
138     public void testSaveAndRestoreState()
139     {
140         /**
141          * NOTE: Normally you would not set all of these properties together in the component, but for the
142          * purposes of testing, we want to make sure all fields are saved and restored properly.
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      * Test the save and restore state methods with all values as value binding expressions.  This also tests
165      * the value bindings themselves to make sure they are working.
166      */
167     public void testSaveAndRestoreStateValueBinding()
168     {
169         /**
170          * NOTE: Normally you would not set all of these properties together in the component, but for the
171          * purposes of testing, we want to make sure all fields are saved and restored properly.
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         // reset property so we can test value binding
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         // reset property so we can test value binding
220         xmlTransform.setStylesheet(null);
221         xmlTransform.setValueBinding("stylesheet", new MockValueBinding(application, "#{foo.stylesheet}"));
222         assertEquals(fooBean.getStylesheet(), xmlTransform.getStylesheet());
223     }
224 
225     /**
226      * Component should throw NPE if no stylesheet or Transform is provided
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      * Component should throw NPE if no content is provided
244      */
245     public void testStylesheetNoContent() throws IOException
246     {
247         // stylesheet is not sufficient, content must be provided
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      * Tests the actual transformation process using an InputStream as the source for content.
364      * @throws IOException
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      * Tests the actual transformation process using an InputStream as the source for content.  This test
378      * sets the property to a value binding (as you would through use of XmlTransformTag) instead of setting
379      * the value of the contentStream on the component directly.
380      *
381      * @throws IOException
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 }