1   //
2   // Copyright (c) 2005 Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. 
3   // All rights reserved. 
4   // 
5   // Redistribution and use in source and binary forms, with or without modification,  
6   // are permitted provided that the following conditions are met: 
7   // 
8   // * Redistributions of source code must retain the above copyright notice,  
9   //   this list of conditions and the following disclaimer. 
10  // * Redistributions in binary form must reproduce the above copyright notice,  
11  //   this list of conditions and the following disclaimer in the documentation  
12  //   and/or other materials provided with the distribution. 
13  // * Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.  
14  //   nor the names of its contributors may be used to endorse or promote products  
15  //   derived from this software without specific prior written permission. 
16  //
17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  
18  // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  
19  // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
20  // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,  
21  // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  
22  // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
23  // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
24  // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  
25  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  
26  // POSSIBILITY OF SUCH DAMAGE. 
27  //
28  package org.objectledge.encodings;
29  
30  import java.io.StringReader;
31  
32  import javax.xml.parsers.SAXParser;
33  import javax.xml.parsers.SAXParserFactory;
34  
35  import junit.framework.TestCase;
36  
37  import org.xml.sax.InputSource;
38  import org.xml.sax.ext.DefaultHandler2;
39  
40  /***
41   * @author <a href="mailto:dgajda@caltha.pl">Damian Gajda</a>
42   * @version $Id: HTMLEntityDecoderTest.java,v 1.2 2005/04/26 13:33:54 zwierzem Exp $
43   */
44  public class HTMLEntityDecoderTest extends TestCase
45  {
46      private HTMLEntityDecoder entParser = new HTMLEntityDecoder();
47  
48      /***
49       * Constructor for TestHTMLEntityDecoder.
50       * @param arg0
51       */
52      public HTMLEntityDecoderTest(String arg0)
53      {
54          super(arg0);
55      }
56  
57      public void testDecode()
58      {
59          String src1 = "<meta> &amp; &egrave; &#8222; &#x22AB; &dsds &#12 &#x12Ag &ap&amp; </meta>";
60          String src2 = entParser.decode(src1);
61          assertEquals("<meta> & \u00E8 \u201E \u22AB &dsds &#12 &#x12Ag &ap& </meta>", src2);
62      }
63      
64      public void testDecodeXML()
65      throws Exception
66      {
67          String src1 = "<meta> &amp; &egrave; &#8222; &#x22AB; &amp; </meta>";
68          String src2 = entParser.decodeXML(src1); 
69          assertEquals("<meta> &amp; \u00E8 \u201E \u22AB &amp; </meta>", src2);
70  
71          SAXParserFactory factory = SAXParserFactory.newInstance();
72          factory.setValidating( false );
73          factory.setNamespaceAware( true );
74          SAXParser parser = factory.newSAXParser();
75          DefaultHandler2 dhandler = new DefaultHandler2();
76          InputSource is = new InputSource(new StringReader(src2));
77          parser.parse(is, dhandler);
78      }
79  
80      public void testDecodeAndFixXML()
81      throws Exception
82      {
83          String src1 = "<meta> &amp; &egrave; &#8222; & word &#x22AB; &amp; &broken &egrave &&& </meta>";
84          String src2 = entParser.decodeAndFixXML(src1); 
85          assertEquals("<meta> &amp; \u00E8 \u201E &amp; word \u22AB &amp; &amp;broken &amp;egrave &amp;&amp;&amp; </meta>", src2);
86  
87          SAXParserFactory factory = SAXParserFactory.newInstance();
88          factory.setValidating( false );
89          factory.setNamespaceAware( true );
90          SAXParser parser = factory.newSAXParser();
91          DefaultHandler2 dhandler = new DefaultHandler2();
92          InputSource is = new InputSource(new StringReader(src2));
93          parser.parse(is, dhandler);
94      }
95  }