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.context;
21  
22  import java.io.File;
23  import java.io.FileNotFoundException;
24  import java.io.IOException;
25  import java.io.StringReader;
26  import java.io.StringWriter;
27  import java.net.URL;
28  import java.util.logging.Logger;
29  
30  import javax.xml.parsers.DocumentBuilder;
31  import javax.xml.parsers.DocumentBuilderFactory;
32  
33  import org.apache.myfaces.shared.renderkit.html.HtmlResponseWriterImpl;
34  import org.apache.myfaces.test.base.AbstractJsfTestCase;
35  import org.w3c.dom.Document;
36  import org.w3c.dom.Node;
37  import org.xml.sax.InputSource;
38  
39  /**
40   * Test cases for our impl, which tests for the CDATA nesting
41   *
42   * @author Werner Punz (latest modification by $Author: struberg $)
43   * @version $Revision: 1188694 $ $Date: 2011-10-25 10:07:44 -0500 (Tue, 25 Oct 2011) $
44   */
45  
46  public class PartialResponseWriterImplTest extends AbstractJsfTestCase {
47  
48      static Logger _log = Logger.getLogger(PartialResponseWriterImplTest.class.getName());
49  
50      private final String filePath = this.getDirectory();
51      
52      PartialResponseWriterImpl _writer;
53      StringWriter _contentCollector;
54      private static final String STD_UPDATE_RESULT = "<changes><update id=\"blaId\"><![CDATA[testing]]></update>";
55      private static final String CORR_OUTPUT = "checking for correct output: ";
56  
57      public PartialResponseWriterImplTest() {
58          super("PartialResponseWriterImplTest");
59      }
60  
61      protected void setUp() throws Exception {
62          super.setUp();
63          _contentCollector = new StringWriter(100);
64      }
65      
66      protected void tearDown() throws Exception {
67          super.tearDown();
68          _contentCollector = null;
69      }
70  
71      private void checkOutput(File expected, String output) throws Exception
72      {
73          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
74          dbf.setNamespaceAware(false);
75          dbf.setCoalescing(true);
76          dbf.setIgnoringElementContentWhitespace(true);
77          dbf.setIgnoringComments(true);
78          DocumentBuilder db = dbf.newDocumentBuilder();
79          Document doc1 = db.parse(expected.toURI().toString());
80          doc1.normalizeDocument();
81          InputSource is2 = new InputSource();
82          is2.setCharacterStream(new StringReader(output));
83          Document doc2 = db.parse(is2);
84          doc2.normalizeDocument();
85          assertTrue(doc1.isEqualNode(doc2));
86      }
87  
88      /**
89       * Get the node path
90       */
91      public String getPath( Node node )
92      {
93          StringBuilder path = new StringBuilder();
94  
95          do
96          {           
97              path.insert(0, node.getNodeName() );
98              path.insert( 0, "/" );
99          }
100         while( ( node = node.getParentNode() ) != null );
101 
102         return path.toString();
103     }
104 
105 
106     protected URL getLocalFile(String name) throws FileNotFoundException
107     {
108         ClassLoader cl = Thread.currentThread().getContextClassLoader();
109         URL url = cl.getResource(this.filePath + "/" + name);
110         if (url == null)
111         {
112             throw new FileNotFoundException(cl.getResource("").getFile() + name
113                     + " was not found");
114         }
115         return url;
116     }
117     
118     protected String getDirectory()
119     {
120         return this.getClass().getName().substring(0,
121                 this.getClass().getName().lastIndexOf('.')).replace('.', '/')
122                 + "/";
123     }
124     
125     public void testNestedScriptCDATA() throws Exception {
126         _writer = createTestProbe();
127         try {
128             _writer.startDocument();
129             _writer.startUpdate("blaId");
130             _writer.startElement("script", null);
131             _writer.writeAttribute("type", "text/javascript", null);
132             _writer.write("\n// <![CDATA[\n");
133             _writer.write("var a && b;");
134             _writer.write("\n// ]]>\n");
135             _writer.endElement("script");
136             _writer.endUpdate();
137             _writer.endDocument();
138             
139             checkOutput(new File(getLocalFile("nestedScriptCDATA.xml").toURI()), _contentCollector.toString());
140         } catch (IOException e) {
141             fail(e.toString());
142         }
143     }
144     
145     public void testBasicWriteTest() {
146         _writer = createTestProbe();
147         try {
148             //_writer.startCDATA();
149             //_writer.startCDATA();
150             _writer.write("testing");
151             // _writer.endCDATA();
152             // _writer.endCDATA();
153             _writer.flush();
154             _writer.close();
155             assertTrue(CORR_OUTPUT, _contentCollector.toString().equals("testing"));
156         } catch (IOException e) {
157             fail(e.toString());
158         }
159     }
160 
161     public void teststandardNestingTest() {
162         _writer = createTestProbe();
163         try {
164             //_writer.startCDATA();
165             _writer.startCDATA();
166             _writer.write("testing");
167             _writer.endCDATA();
168             // _writer.endCDATA();
169             _writer.flush();
170             _writer.close();
171             assertTrue(CORR_OUTPUT, _contentCollector.toString().equals("<![CDATA[testing]]>"));
172         } catch (IOException e) {
173             fail(e.toString());
174         }
175     }
176 
177     public void testIllegalNestingResolvementTest() {
178         _writer = createTestProbe();
179         try {
180             _writer.startCDATA();
181             _writer.startCDATA();
182             _writer.write("testing");
183             _writer.endCDATA();
184             _writer.endCDATA();
185             _writer.flush();
186             _writer.close();
187             assertTrue(CORR_OUTPUT+ _contentCollector.toString(), _contentCollector.toString().equals("<![CDATA[<![CDATA[testing]]><![CDATA[]]]]><![CDATA[>]]>"));
188         } catch (IOException e) {
189             fail(e.toString());
190         }
191     }
192 
193     public void testIllegalNestingResolvementTest2() {
194         _writer = createTestProbe();
195         try {
196             _writer.startCDATA();
197             _writer.startCDATA();
198             _writer.write("testing");
199             _writer.flush();
200             _writer.close();
201             assertTrue(CORR_OUTPUT+ _contentCollector.toString(), _contentCollector.toString().equals("<![CDATA[<![CDATA[testing]]>"));
202         } catch (IOException e) {
203             fail(e.toString());
204         }
205     }
206 
207 
208 
209     public void testStandardUpdate() {
210         _writer = createTestProbe();
211         try {
212             _writer.startUpdate("blaId");
213             _writer.write("testing");
214             _writer.endUpdate();
215             assertTrue(CORR_OUTPUT, _contentCollector.toString().equals(STD_UPDATE_RESULT));
216         } catch (IOException e) {
217             fail(e.toString());
218         }
219     }
220 
221     public void testStandardUpdateNestedCDATA() {
222         _writer = createTestProbe();
223         try {
224             _writer.startUpdate("blaId");
225             _writer.startCDATA();
226             _writer.write("testing");
227             _writer.endCDATA();
228             _writer.endUpdate();
229             assertTrue(CORR_OUTPUT+_contentCollector.toString(), _contentCollector.toString().equals("<changes><update id=\"blaId\"><![CDATA[<![CDATA[testing]]><![CDATA[]]]]><![CDATA[>]]></update>"));
230         } catch (IOException e) {
231             fail(e.toString());
232         }
233     }
234 
235 
236     public void testComponentAuthorNestingFailureTest() {
237         _writer = createTestProbe();
238         try {
239             _writer.startUpdate("blaId");
240             _writer.startCDATA();
241             _writer.startCDATA();
242             _writer.write("testing");
243             _writer.endUpdate();
244             assertTrue(CORR_OUTPUT+_contentCollector.toString(), _contentCollector.toString().equals("<changes><update id=\"blaId\"><![CDATA[<![CDATA[<![CDATA[testing]]></update>"));
245         } catch (IOException e) {
246             fail(e.toString());
247         }
248     }
249 
250     public void testStandardInsertAfter() {
251         _writer = createTestProbe();
252         try {
253             _writer.startInsertAfter("blaId");
254             _writer.write("testing");
255             _writer.endInsert();
256             assertTrue(CORR_OUTPUT, _contentCollector.toString().equals("<changes><insert><after id=\"blaId\"><![CDATA[testing]]></after></insert>"));
257         } catch (IOException e) {
258             fail(e.toString());
259         }
260     }
261 
262     public void testStandardInsertBefore() {
263         _writer = createTestProbe();
264         try {
265             _writer.startInsertBefore("blaId");
266             _writer.write("testing");
267             _writer.endInsert();
268             assertTrue(CORR_OUTPUT, _contentCollector.toString().equals("<changes><insert><before id=\"blaId\"><![CDATA[testing]]></before></insert>"));
269         } catch (IOException e) {
270             fail(e.toString());
271         }
272     }
273 
274     public void testBrokenUserInput() {
275         _writer = createTestProbe();
276         try {
277             _writer.startInsertBefore("blaId");
278             _writer.startElement("input", null);
279             _writer.writeAttribute("type","text", null);
280             _writer.writeAttribute("value","]]>", null);
281             _writer.endElement("input");
282             _writer.endInsert();
283             assertTrue(CORR_OUTPUT+_contentCollector.toString(), _contentCollector.toString().contains("value=\"]]&gt;\""));
284         } catch (IOException e) {
285             fail(e.toString());
286         }
287     }
288 
289 
290 
291     public void testDelete() {
292         _writer = createTestProbe();
293         try {
294             _writer.delete("blaId");
295             assertTrue(CORR_OUTPUT+_contentCollector.toString(), _contentCollector.toString().equals("<changes><delete id=\"blaId\"></delete>"));
296         } catch (IOException e) {
297             fail(e.toString());
298         }
299     }
300 
301 
302 
303     /**
304      * creates a new test probe (aka response writer)
305      *
306      * @return
307      */
308     private PartialResponseWriterImpl createTestProbe() {
309         return new PartialResponseWriterImpl(new HtmlResponseWriterImpl(_contentCollector, null, "UTF-8"));
310     }
311 
312 }