1   // 
2   //Copyright (c) 2003, 2004 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  
29  package org.objectledge.encodings.encoders;
30  
31  import junit.framework.TestCase;
32  
33  import org.objectledge.encodings.HTMLEntityEncoder;
34  
35  /**
36   * @author <a href="mailto:dgajda@caltha.pl">Damian Gajda</a>
37   * @version $Id: HTMLEntityEncoderTest.java,v 1.7 2005/05/18 04:59:34 rafal Exp $
38   */
39  public class HTMLEntityEncoderTest extends TestCase
40  {
41  	private String srcPart1 = "\"&'<> ";
42  
43  	private String srcPart2 =
44  		"\u00A1\u00A2\u00A3\u00A4\u00A5\u00A6\u00A7\u00A8\u00A9\u00AA\u00AB" +
45  		"\u00AC\u00AD\u00AE\u00AF\u00B0\u00B1\u00B2\u00B3\u00B4\u00B5\u00B6\u00B7\u00B8" +
46  		"\u00B9\u00BA\u00BB\u00BC\u00BD\u00BE\u00BF\u00C0\u00C1\u00C2\u00C3\u00C4\u00C5" +
47  		"\u00C6\u00C7\u00C8\u00C9\u00CA\u00CB\u00CC\u00CD\u00CE\u00CF\u00D0\u00D1\u00D2" +
48  		"\u00D3\u00D4\u00D5\u00D6\u00D7\u00D8\u00D9\u00DA\u00DB\u00DC\u00DD\u00DE\u00DF" +
49  		"\u00E0\u00E1\u00E2\u00E3\u00E4\u00E5\u00E6\u00E7\u00E8\u00E9\u00EA\u00EB\u00EC" +
50  		"\u00ED\u00EE\u00EF\u00F0\u00F1\u00F2\u00F3\u00F4\u00F5\u00F6\u00F7\u00F8\u00F9" +
51  		"\u00FA\u00FB\u00FC\u00FD\u00FE\u00FF\u0152\u0153\u0160\u0161\u0178\u0192\u02C6" +
52  		"\u02DC\u0391\u0392\u0393\u0394\u0395\u0396\u0397\u0398\u0399\u039A\u039B\u039C" +
53  		"\u039D\u039E\u039F\u03A0\u03A1\u03A3\u03A4\u03A5\u03A6\u03A7\u03A8\u03A9\u03B1" +
54  		"\u03B2\u03B3\u03B4\u03B5\u03B6\u03B7\u03B8\u03B9\u03BA\u03BB\u03BC\u03BD\u03BE" +
55  		"\u03BF\u03C0\u03C1\u03C2\u03C3\u03C4\u03C5\u03C6\u03C7\u03C8\u03C9\u03D1\u03D2" +
56  		"\u03D6\u2002\u2003\u2009\u200C\u200D\u200E\u200F\u2013\u2014\u2018\u2019\u201A" +
57  		"\u201C\u201D\u201E\u2020\u2021\u2022\u2026\u2030\u2032\u2033\u2039\u203A\u203E" +
58  		"\u2044\u20AC\u2111\u2118\u211C\u2122\u2135\u2190\u2191\u2192\u2193\u2194\u21B5" +
59  		"\u21D0\u21D1\u21D2\u21D3\u21D4\u2200\u2202\u2203\u2205\u2207\u2208\u2209\u220B" +
60  		"\u220F\u2211\u2212\u2217\u221A\u221D\u221E\u2220\u2227\u2228\u2229\u222A\u222B" +
61  		"\u2234\u223C\u2245\u2248\u2260\u2261\u2264\u2265\u2282\u2283\u2284\u2286\u2287" +
62  		"\u2295\u2297\u22A5\u22C5\u2308\u2309\u230A\u230B\u2329\u232A\u25CA\u2660\u2663" +
63  		"\u2665\u2666\u0414\u0413";
64  
65  	private String outPart2Iso2 =
66  		"&iexcl;&cent;&pound;\u00A4&yen;&brvbar;\u00A7\u00A8&copy;&ordf;" +
67  		"&laquo;&not;\u00AD&reg;&macr;\u00B0&plusmn;&sup2;&sup3;\u00B4&micro;&para;&middot;" +
68  		"\u00B8&sup1;&ordm;&raquo;&frac14;&frac12;&frac34;&iquest;&Agrave;\u00C1\u00C2" +
69  		"&Atilde;\u00C4&Aring;&AElig;\u00C7&Egrave;\u00C9&Ecirc;\u00CB&Igrave;\u00CD\u00CE" +
70  		"&Iuml;&ETH;&Ntilde;&Ograve;\u00D3\u00D4&Otilde;\u00D6\u00D7&Oslash;&Ugrave;\u00DA" +
71  		"&Ucirc;\u00DC\u00DD&THORN;\u00DF&agrave;\u00E1\u00E2&atilde;\u00E4&aring;&aelig;" +
72  		"\u00E7&egrave;\u00E9&ecirc;\u00EB&igrave;\u00ED\u00EE&iuml;&eth;&ntilde;&ograve;" +
73  		"\u00F3\u00F4&otilde;\u00F6\u00F7&oslash;&ugrave;\u00FA&ucirc;\u00FC\u00FD&thorn;" +
74  		"&yuml;&OElig;&oelig;\u0160\u0161&Yuml;&fnof;&circ;&tilde;&Alpha;&Beta;&Gamma;" +
75  		"&Delta;&Epsilon;&Zeta;&Eta;&Theta;&Iota;&Kappa;&Lambda;&Mu;&Nu;&Xi;&Omicron;&Pi;" +
76  		"&Rho;&Sigma;&Tau;&Upsilon;&Phi;&Chi;&Psi;&Omega;&alpha;&beta;&gamma;&delta;&epsilon;" +
77  		"&zeta;&eta;&theta;&iota;&kappa;&lambda;&mu;&nu;&xi;&omicron;&pi;&rho;&sigmaf;&sigma;" +
78  		"&tau;&upsilon;&phi;&chi;&psi;&omega;&thetasym;&upsih;&piv;&ensp;&emsp;&thinsp;&zwnj;" +
79  		"&zwj;&lrm;&rlm;&ndash;&mdash;&lsquo;&rsquo;&sbquo;&ldquo;&rdquo;&bdquo;&dagger;" +
80  		"&Dagger;&bull;&hellip;&permil;&prime;&Prime;&lsaquo;&rsaquo;&oline;&frasl;&euro;" +
81  		"&image;&weierp;&real;&trade;&alefsym;&larr;&uarr;&rarr;&darr;&harr;&crarr;&lArr;" +
82  		"&uArr;&rArr;&dArr;&hArr;&forall;&part;&exist;&empty;&nabla;&isin;&notin;&ni;&prod;" +
83  		"&sum;&minus;&lowast;&radic;&prop;&infin;&ang;&and;&or;&cap;&cup;&int;&there4;&sim;" +
84  		"&cong;&asymp;&ne;&equiv;&le;&ge;&sub;&sup;&nsub;&sube;&supe;&oplus;&otimes;&perp;" +
85  		"&sdot;&lceil;&rceil;&lfloor;&rfloor;&lang;&rang;&loz;&spades;&clubs;&hearts;&diams;" +
86  		"&#1044;&#1043;";
87  
88      /***
89       * Constructor for HTMLEntityEncoderTest.
90       * @param arg0
91       */
92      public HTMLEntityEncoderTest(String arg0)
93      {
94          super(arg0);
95      }
96  
97      /*
98       * Test for String encodeAttribute(String, String, boolean)
99       * &amp; String encodeAttribute(String, String)
100      */
101     public void testEncodeAttribute()
102     {
103 		HTMLEntityEncoder encoder = new HTMLEntityEncoder();
104 
105 		String src = srcPart1+srcPart2;
106 		String outDblQuote1 = "&quot;&amp;'&lt;&gt; ";
107 		String outDblQuote = outDblQuote1+outPart2Iso2;
108 		String outQuote = "\"&amp;&apos;&lt;&gt; "+outPart2Iso2;
109 		
110 		String realDblQuote = encoder.encodeAttribute(src, "ISO-8859-2", true);
111 		assertEquals(realDblQuote, outDblQuote);
112 
113 		String realQuote = encoder.encodeAttribute(src, "ISO-8859-2", false);
114 		assertEquals(realQuote, outQuote);
115 
116 		realDblQuote = encoder.encodeAttribute(src, "ISO-8859-2");
117 		assertEquals(realDblQuote, outDblQuote);
118 		
119 		realDblQuote = encoder.encodeAttribute(null, "ISO-8859-2");
120 		assertNull(realDblQuote);
121 
122 		realDblQuote = encoder.encodeAttribute("", "ISO-8859-2");
123 		assertEquals("", realDblQuote);
124 
125 		realDblQuote = encoder.encodeAttribute(src, null);
126 		assertEquals(realDblQuote, outDblQuote1+srcPart2);
127     }
128 
129     public void testEncodeHTML()
130     {
131 		HTMLEntityEncoder encoder = new HTMLEntityEncoder();
132 
133 		String src = srcPart1+srcPart2;
134 		String out = "\"&'<> "+outPart2Iso2;
135 
136 		String realOut = encoder.encodeHTML(src, "ISO-8859-2");
137 		assertEquals(realOut, out);
138 
139 		realOut = encoder.encodeHTML(null, "ISO-8859-2");
140 		assertNull(realOut);
141 
142 		realOut = encoder.encodeHTML("", "ISO-8859-2");
143 		assertEquals("", realOut);
144 
145 		realOut = encoder.encodeHTML(src, null);
146 		assertEquals(realOut, src);
147     }
148 }