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 java.io.File;
23  import java.io.FileInputStream;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.io.StringWriter;
27  import java.net.URI;
28  import java.net.URL;
29  
30  import junit.framework.Test;
31  import junit.framework.TestSuite;
32  
33  import org.apache.myfaces.test.base.AbstractJsfTestCase;
34  import org.apache.myfaces.test.mock.MockHttpServletRequest;
35  import org.apache.myfaces.test.mock.MockRenderKitFactory;
36  import org.apache.myfaces.test.mock.MockResponseWriter;
37  import org.apache.myfaces.test.mock.MockValueBinding;
38  
39  /**
40   * Test case for XmlTransform
41   *
42   * @author Sean Schofield
43   */
44  public class XmlTransformTest extends AbstractJsfTestCase
45  {
46      private static final String PETS_STYLESHEET = "pets.xsl";
47      private static final String PETS_CONTENT = "pets.xml";
48      private static final String EXPECTED_TEXT = "Iguana";
49  
50      private XmlTransform xmlTransform;
51      private ManagedFoo fooBean;
52      private String stylesheet;
53      private String stylesheetLocation = PETS_STYLESHEET;
54      private InputStream styleStream;
55      private String content;
56      private String contentLocation = PETS_CONTENT;
57      private InputStream contentStream;
58  
59      private StringWriter mockWriter = new StringWriter();
60  
61      /**
62       * Constructor
63       * @param name String
64       */
65      public XmlTransformTest(String name)
66      {
67          super(name);
68      }
69  
70      /**
71       * See abstract class
72       */
73      public void setUp() throws Exception
74      {
75          super.setUp();
76  
77          xmlTransform = new XmlTransform();
78  
79          // additional setup not provided automatically by the shale mock stuff
80          facesContext.setResponseWriter(new MockResponseWriter(mockWriter, null, null));
81  
82          // TODO remove these two lines once shale-test goes alpha, see MYFACES-1155
83          facesContext.getViewRoot().setRenderKitId(MockRenderKitFactory.HTML_BASIC_RENDER_KIT);
84  
85          fooBean = new ManagedFoo();
86          fooBean.setContentLocation(PETS_CONTENT);
87          fooBean.setStylesheetLocation(PETS_STYLESHEET);
88  
89          // put the foo bean in the request scope
90          MockHttpServletRequest request = (MockHttpServletRequest)facesContext.getExternalContext().getRequest();
91          request.setAttribute("foo", fooBean);
92  
93          // setup input stream to be used in some of the test cases
94          ClassLoader loader = Thread.currentThread().getContextClassLoader();
95          if (loader == null)
96          {
97              loader = XmlTransform.class.getClassLoader();
98          }
99  
100         URL url = loader.getResource(PETS_CONTENT);
101         try
102         {
103             contentStream = new FileInputStream(new File(URI.create(url.toString())));
104 
105             // now also populate the String variable for certain test cases
106             FileInputStream cs = new FileInputStream(new File(URI.create(url.toString())));
107             int x= cs.available();
108             byte b[]= new byte[x];
109             //noinspection ResultOfMethodCallIgnored
110             cs.read(b);
111             content = new String(b);
112         }
113         catch (IOException io)
114         {}  // do nothing - test will fail eventually
115 
116         // setup stylesheet text to be used in some of the test cases
117         url = loader.getResource(PETS_STYLESHEET);
118         try
119         {
120             styleStream = new FileInputStream(new File(URI.create(url.toString())));
121 
122             // now also populate the String variable for certain test cases
123             FileInputStream ss = new FileInputStream(new File(URI.create(url.toString())));
124             int x= ss.available();
125             byte b[]= new byte[x];
126             //noinspection ResultOfMethodCallIgnored
127             ss.read(b);
128             stylesheet = new String(b);
129         }
130         catch (IOException io)
131         {}  // do nothing - test will fail eventually
132 
133         fooBean.setContent(content);
134         fooBean.setStylesheet(stylesheet);
135         fooBean.setContentStream(contentStream);
136         fooBean.setStyleStream(styleStream);
137     }
138 
139     /**
140      * Test the save and restore state methods.
141      */
142     public void testSaveAndRestoreState()
143     {
144         /**
145          * NOTE: Normally you would not set all of these properties together in the component, but for the
146          * purposes of testing, we want to make sure all fields are saved and restored properly.
147          */
148         xmlTransform.setContent(content);
149         xmlTransform.setContentLocation(contentLocation);
150         xmlTransform.setStylesheet(stylesheet);
151         xmlTransform.setStylesheetLocation(stylesheetLocation);
152         xmlTransform.setContentStream(contentStream);
153         xmlTransform.setStyleStream(styleStream);
154 
155         Object state = xmlTransform.saveState(facesContext);
156         xmlTransform = new XmlTransform();
157         xmlTransform.restoreState(facesContext, state);
158 
159         assertEquals(content, xmlTransform.getContent());
160         assertEquals(contentLocation, xmlTransform.getContentLocation());
161         assertEquals(stylesheet, xmlTransform.getStylesheet());
162         assertEquals(stylesheetLocation, xmlTransform.getStylesheetLocation());
163         assertNull("contentStream should be null since it cannot be serialized", xmlTransform.getContentStream());
164         assertNull("styleStream should be null since it cannot be serialized", xmlTransform.getStyleStream());
165     }
166 
167     /**
168      * Test the save and restore state methods with all values as value binding expressions.  This also tests
169      * the value bindings themselves to make sure they are working.
170      */
171     public void testSaveAndRestoreStateValueBinding()
172     {
173         /**
174          * NOTE: Normally you would not set all of these properties together in the component, but for the
175          * purposes of testing, we want to make sure all fields are saved and restored properly.
176          */
177         xmlTransform.setValueBinding("content", new MockValueBinding(application, "#{foo.content}"));
178         xmlTransform.setValueBinding("contentLocation", new MockValueBinding(application, "#{foo.contentLocation}"));
179         xmlTransform.setValueBinding("stylesheet", new MockValueBinding(application, "#{foo.stylesheet}"));
180         xmlTransform.setValueBinding("stylesheetLocation", new MockValueBinding(application, "#{foo.stylesheetLocation}"));
181         xmlTransform.setValueBinding("contentStream", new MockValueBinding(application, "#{foo.contentStream}"));
182         xmlTransform.setValueBinding("styleStream", new MockValueBinding(application, "#{foo.styleStream}"));
183 
184         Object state = xmlTransform.saveState(facesContext);
185         xmlTransform = new XmlTransform();
186         xmlTransform.restoreState(facesContext, state);
187 
188         assertEquals(content, xmlTransform.getContent());
189         assertEquals(contentLocation, xmlTransform.getContentLocation());
190         assertEquals(stylesheet, xmlTransform.getStylesheet());
191         assertEquals(stylesheetLocation, xmlTransform.getStylesheetLocation());
192         assertEquals(contentStream, xmlTransform.getContentStream());
193     }
194 
195     public void testSetContent()
196     {
197         xmlTransform.setContent("foo");
198         assertEquals("foo", xmlTransform.getContent());
199 
200         // reset property so we can test value binding
201         xmlTransform.setContent(null);
202         xmlTransform.setValueBinding("content", new MockValueBinding(application, "#{foo.content}"));
203         assertEquals(fooBean.getContent(), xmlTransform.getContent());
204     }
205 
206     public void testSetContentStream()
207     {
208         xmlTransform.setContentStream(fooBean.getContentStream());
209         assertEquals(fooBean.getContentStream(), xmlTransform.getContentStream());
210     }
211 
212     public void testSetContentStreamValueBinding()
213     {
214         xmlTransform.setValueBinding("contentStream", new MockValueBinding(application, "#{foo.contentStream}"));
215         assertEquals(fooBean.getContentStream(), xmlTransform.getContentStream());
216     }
217 
218     public void testSetStylesheet()
219     {
220         xmlTransform.setStylesheet("foo");
221         assertEquals("foo", xmlTransform.getStylesheet());
222 
223         // reset property so we can test value binding
224         xmlTransform.setStylesheet(null);
225         xmlTransform.setValueBinding("stylesheet", new MockValueBinding(application, "#{foo.stylesheet}"));
226         assertEquals(fooBean.getStylesheet(), xmlTransform.getStylesheet());
227     }
228 
229     /**
230      * Component should throw NPE if no stylesheet or Transform is provided
231      */
232     public void testNoTransformInfo() throws IOException
233     {
234         try
235         {
236             xmlTransform.encodeBegin(facesContext);
237         }
238         catch (NullPointerException e)
239         {
240             return;
241         }
242 
243         fail("Expected exception when no Transform or stylesheet provided");
244     }
245 
246     /**
247      * Component should throw NPE if no content is provided
248      */
249     public void testStylesheetNoContent() throws IOException
250     {
251         // stylesheet is not sufficient, content must be provided
252         xmlTransform.setStylesheet("blah");
253 
254         try
255         {
256             xmlTransform.encodeBegin(facesContext);
257         }
258         catch (NullPointerException e)
259         {
260             return;
261         }
262 
263         fail("Expected exception when stylesheet but no content provided");
264     }
265 
266     public void testStyleSheet() throws IOException
267     {
268         xmlTransform.setContentLocation(contentLocation);
269         xmlTransform.setStylesheet(stylesheet);
270         xmlTransform.encodeBegin(facesContext);
271 
272         String responseText = mockWriter.toString();
273         assertEquals("Unexpected response text from content transformation", EXPECTED_TEXT, responseText);
274     }
275 
276     public void testStyleSheetValueBinding() throws IOException
277     {
278         xmlTransform.setContent(content);
279         xmlTransform.setValueBinding("stylesheet", new MockValueBinding(application, "#{foo.stylesheet}"));
280         xmlTransform.encodeBegin(facesContext);
281 
282         String responseText = mockWriter.toString();
283         assertEquals("Unexpected response text from content transformation", EXPECTED_TEXT, responseText);
284     }
285 
286     public void testStylesheetLocation() throws IOException
287     {
288         xmlTransform.setContent(content);
289         xmlTransform.setStylesheetLocation(stylesheetLocation);
290         xmlTransform.encodeBegin(facesContext);
291 
292         String responseText = mockWriter.toString();
293         assertEquals("Unexpected response text from content transformation", EXPECTED_TEXT, responseText);
294     }
295 
296     public void testStylesheetLocationValueBinding() throws IOException
297     {
298         xmlTransform.setContent(content);
299         xmlTransform.setValueBinding("stylesheetLocation", new MockValueBinding(application, "#{foo.stylesheetLocation}"));
300         xmlTransform.encodeBegin(facesContext);
301 
302         String responseText = mockWriter.toString();
303         assertEquals("Unexpected response text from content transformation", EXPECTED_TEXT, responseText);
304     }
305 
306     public void testStylesheetStream() throws IOException
307     {
308         xmlTransform.setStyleStream(styleStream);
309         xmlTransform.setContent(content);
310         xmlTransform.encodeBegin(facesContext);
311 
312         String responseText = mockWriter.toString();
313         assertEquals("Unexpected response text from content transformation", EXPECTED_TEXT, responseText);
314     }
315 
316     public void testStylesheetStreamValueBinding() throws IOException
317     {
318         xmlTransform.setValueBinding("styleStream", new MockValueBinding(application, "#{foo.styleStream}"));
319         xmlTransform.setContent(content);
320         xmlTransform.encodeBegin(facesContext);
321 
322         String responseText = mockWriter.toString();
323         assertEquals("Unexpected response text from content transformation", EXPECTED_TEXT, responseText);
324     }
325 
326     public void testContent() throws IOException
327     {
328         xmlTransform.setContent(content);
329         xmlTransform.setStylesheet(stylesheet);
330         xmlTransform.encodeBegin(facesContext);
331 
332         String responseText = mockWriter.toString();
333         assertEquals("Unexpected response text from content transformation", EXPECTED_TEXT, responseText);
334     }
335 
336     public void testContentValueBinding() throws IOException
337     {
338         xmlTransform.setValueBinding("content", new MockValueBinding(application, "#{foo.content}"));
339         xmlTransform.setStylesheet(stylesheet);
340         xmlTransform.encodeBegin(facesContext);
341 
342         String responseText = mockWriter.toString();
343         assertEquals("Unexpected response text from content transformation", EXPECTED_TEXT, responseText);
344     }
345 
346     public void testContentLocation() throws IOException
347     {
348         xmlTransform.setContentLocation(contentLocation);
349         xmlTransform.setStylesheet(stylesheet);
350         xmlTransform.encodeBegin(facesContext);
351 
352         String responseText = mockWriter.toString();
353         assertEquals("Unexpected response text from content transformation", EXPECTED_TEXT, responseText);
354     }
355 
356     public void testContentLocationValueBinding() throws IOException
357     {
358         xmlTransform.setValueBinding("contentLocation", new MockValueBinding(application, "#{foo.contentLocation}"));
359         xmlTransform.setStylesheet(stylesheet);
360         xmlTransform.encodeBegin(facesContext);
361 
362         String responseText = mockWriter.toString();
363         assertEquals("Unexpected response text from content transformation", EXPECTED_TEXT, responseText);
364     }
365 
366     /**
367      * Tests the actual transformation process using an InputStream as the source for content.
368      * @throws IOException
369      */
370     public void testContentStream() throws IOException
371     {
372         xmlTransform.setContentStream(contentStream);
373         xmlTransform.setStylesheet(stylesheet);
374         xmlTransform.encodeBegin(facesContext);
375 
376         String responseText = mockWriter.toString();
377         assertEquals("Unexpected response text from content transformation", EXPECTED_TEXT, responseText);
378     }
379 
380     /**
381      * Tests the actual transformation process using an InputStream as the source for content.  This test
382      * sets the property to a value binding (as you would through use of XmlTransformTag) instead of setting
383      * the value of the contentStream on the component directly.
384      *
385      * @throws IOException
386      */
387     public void testContentStreamValueBinding() throws IOException
388     {
389         xmlTransform.setValueBinding("contentStream", new MockValueBinding(application, "#{foo.contentStream}"));
390         xmlTransform.setStylesheetLocation(stylesheetLocation);
391         xmlTransform.encodeBegin(facesContext);
392 
393         String responseText = mockWriter.toString();
394         assertEquals("Unexpected response text from content transformation", EXPECTED_TEXT, responseText);
395     }
396 
397     public static Test suite()
398     {
399         return new TestSuite(XmlTransformTest.class);
400     }
401 
402 }