KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > util > HtmlUtilsTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.util;
18
19 import junit.framework.TestCase;
20
21 /**
22  * @author Alef Arendsen
23  * @author Martin Kersten
24  */

25 public class HtmlUtilsTests extends TestCase {
26
27     public void testHtmlEscape() {
28         String JavaDoc unescaped = "\"This is a quote";
29         String JavaDoc escaped = HtmlUtils.htmlEscape(unescaped);
30         assertEquals(""This is a quote", escaped);
31         escaped = HtmlUtils.htmlEscapeDecimal(unescaped);
32         assertEquals(""This is a quote", escaped);
33         escaped = HtmlUtils.htmlEscapeHex(unescaped);
34         assertEquals(""This is a quote", escaped);
35     }
36
37     public void testHtmlUnescape() {
38         String JavaDoc escaped = ""This is a quote";
39         String JavaDoc unescaped = HtmlUtils.htmlUnescape(escaped);
40         assertEquals(unescaped, "\"This is a quote");
41     }
42
43     public void testEncodeIntoHtmlCharacterSet() {
44         assertNull("A null string should be converted to a null string",
45                 HtmlUtils.htmlEscape(null));
46         assertEquals("An empty string should be converted to an empty string",
47                 "", HtmlUtils.htmlEscape(""));
48         assertEquals("A string containing no special characters should not be affected",
49                 "A sentence containing no special characters.",
50                 HtmlUtils.htmlEscape("A sentence containing no special characters."));
51
52         assertEquals("'< >' should be encoded to '&lt; &gt;'",
53                 "&lt; &gt;", HtmlUtils.htmlEscape("< >"));
54         assertEquals("'< >' should be encoded to '&#60; &#62;'",
55                 "&#60; &#62;", HtmlUtils.htmlEscapeDecimal("< >"));
56
57         assertEquals("The special character 8709 should be encoded to '&empty;'",
58                 "&empty;", HtmlUtils.htmlEscape("" + (char) 8709));
59         assertEquals("The special character 8709 should be encoded to '&#8709;'",
60                 "&#8709;", HtmlUtils.htmlEscapeDecimal("" + (char) 8709));
61
62         assertEquals("The special character 977 should be encoded to '&thetasym;'",
63                 "&thetasym;", HtmlUtils.htmlEscape("" + (char) 977));
64         assertEquals("The special character 977 should be encoded to '&#977;'",
65                 "&#977;", HtmlUtils.htmlEscapeDecimal("" + (char) 977));
66     }
67
68     public void testDecodeFromHtmlCharacterSet() {
69         assertNull("A null string should be converted to a null string",
70                 HtmlUtils.htmlUnescape(null));
71         assertEquals("An empty string should be converted to an empty string",
72                 "", HtmlUtils.htmlUnescape(""));
73         assertEquals("A string containing no special characters should not be affected",
74                 "This is a sentence containing no special characters.",
75                 HtmlUtils.htmlUnescape("This is a sentence containing no special characters."));
76
77         assertEquals("'A&nbsp;B' should be decoded to 'A B'",
78                 "A" + (char) 160 + "B", HtmlUtils.htmlUnescape("A&nbsp;B"));
79
80         assertEquals("'&lt; &gt;' should be decoded to '< >'",
81                 "< >", HtmlUtils.htmlUnescape("&lt; &gt;"));
82         assertEquals("'&#60; &#62;' should be decoded to '< >'",
83                 "< >", HtmlUtils.htmlUnescape("&#60; &#62;"));
84
85         assertEquals("'&#x41;&#X42;&#x43;' should be decoded to 'ABC'",
86                 "ABC", HtmlUtils.htmlUnescape("&#x41;&#X42;&#x43;"));
87
88         assertEquals("'&phi;' should be decoded to uni-code character 966",
89                 "" + (char) 966, HtmlUtils.htmlUnescape("&phi;"));
90
91         assertEquals("'&Prime;' should be decoded to uni-code character 8243",
92                 "" + (char) 8243, HtmlUtils.htmlUnescape("&Prime;"));
93
94         assertEquals("A not supported named reference leads should be ingnored",
95                 "&prIme;", HtmlUtils.htmlUnescape("&prIme;"));
96
97         assertEquals("An empty reference '&;' should be survive the decoding",
98                 "&;", HtmlUtils.htmlUnescape("&;"));
99
100         assertEquals("The longest character entity reference '&thetasym;' should be processable",
101                 "" + (char) 977, HtmlUtils.htmlUnescape("&thetasym;"));
102
103         assertEquals("A malformed decimal reference should survive the decoding",
104                 "&#notADecimalNumber;", HtmlUtils.htmlUnescape("&#notADecimalNumber;"));
105         assertEquals("A malformed hex reference should survive the decoding",
106                 "&#XnotAHexNumber;", HtmlUtils.htmlUnescape("&#XnotAHexNumber;"));
107
108         assertEquals("The numerical reference '&#1;' should be converted to char 1",
109                 "" + (char) 1, HtmlUtils.htmlUnescape("&#1;"));
110
111         assertEquals("The malformed hex reference '&#x;' should remain '&#x;'",
112                 "&#x;", HtmlUtils.htmlUnescape("&#x;"));
113     }
114
115 }
116
Popular Tags