KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > servlet > TestHtmlEncoder


1 package jodd.servlet;
2
3 import junit.framework.*;
4
5 public class TestHtmlEncoder extends TestCase {
6
7     public void testEncode() {
8         String html = "< & \" ' > \r\n \n \t";
9
10         assertEquals("&lt; &amp; &quot; &#039; &gt; &#013;&#010; &#010; &#009;", HtmlEncoder.encode(html));
11
12         html = "";
13         assertEquals("", HtmlEncoder.encode(html));
14
15         html = null;
16         assertEquals("", HtmlEncoder.encode(html));
17
18         html = new String(new char[] {128, 257});
19         assertEquals("&#128;&#257;", HtmlEncoder.encode(html));
20     }
21
22     public void testEncodeText() {
23         String html = "< & \" ' > \r\n \n \t";
24
25         assertEquals("&lt; &amp; &quot; &#039; &gt; <br> <br> &#009;", HtmlEncoder.encodeText(html));
26
27         html = "";
28         assertEquals("", HtmlEncoder.encodeText(html));
29
30         html = null;
31         assertEquals("", HtmlEncoder.encodeText(html));
32
33         html = new String(new char[] {128, 257});
34         assertEquals("&#128;&#257;", HtmlEncoder.encodeText(html));
35
36         html = "\r\n\n\r";
37         assertEquals("<br><br><br>", HtmlEncoder.encodeText(html));
38
39         html = "\r\n\r\n";
40         assertEquals("<br><br>", HtmlEncoder.encodeText(html));
41
42         html = "\n\r";
43         assertEquals("<br><br>", HtmlEncoder.encodeText(html));
44     }
45
46
47     public void testEncodeTextStrict() {
48         String html = "< & \" ' > \r\n \n \t";
49
50         assertEquals("&lt;&nbsp;&amp;&nbsp;&quot;&nbsp;&#039;&nbsp;&gt;&nbsp;<br>&nbsp;<br>&nbsp;&nbsp;&#009;", HtmlEncoder.encodeTextStrict(html));
51
52         html = "";
53         assertEquals("", HtmlEncoder.encodeTextStrict(html));
54
55         html = null;
56         assertEquals("", HtmlEncoder.encodeTextStrict(html));
57
58         html = new String(new char[] {128, 257});
59         assertEquals("&#128;&#257;", HtmlEncoder.encodeTextStrict(html));
60
61         html = "\r\n\n\r";
62         assertEquals("<br><br><br>", HtmlEncoder.encodeTextStrict(html));
63
64         html = "\r\n\r\n";
65         assertEquals("<br><br>", HtmlEncoder.encodeTextStrict(html));
66
67         html = "\n\r";
68         assertEquals("<br><br>", HtmlEncoder.encodeTextStrict(html));
69     }
70     
71     
72     public void testEncodeTextSmart() {
73         String html = "< & \" ' > \r\n \n \t";
74
75         assertEquals("&lt; &amp; &quot; &#039; &gt; <br> <br> &nbsp;&#009;", HtmlEncoder.encodeTextSmart(html));
76
77         html = "";
78         assertEquals("", HtmlEncoder.encodeTextSmart(html));
79
80         html = null;
81         assertEquals("", HtmlEncoder.encodeTextSmart(html));
82
83         html = new String(new char[] {128, 257});
84         assertEquals("&#128;&#257;", HtmlEncoder.encodeTextSmart(html));
85
86         html = "\r\n\n\r";
87         assertEquals("<br><br><br>", HtmlEncoder.encodeTextSmart(html));
88
89         html = "\r\n\r\n";
90         assertEquals("<br><br>", HtmlEncoder.encodeTextSmart(html));
91
92         html = "\n\r";
93         assertEquals("<br><br>", HtmlEncoder.encodeTextSmart(html));
94         
95         html = " ";
96         assertEquals(" ", HtmlEncoder.encodeTextSmart(html));
97         html = " ";
98         assertEquals(" &nbsp;", HtmlEncoder.encodeTextSmart(html));
99         html = " ";
100         assertEquals(" &nbsp; ", HtmlEncoder.encodeTextSmart(html));
101         html = " ";
102         assertEquals(" &nbsp; &nbsp;", HtmlEncoder.encodeTextSmart(html));
103         html = " ";
104         assertEquals(" &nbsp; &nbsp; ", HtmlEncoder.encodeTextSmart(html));
105         
106         html = " a";
107         assertEquals(" a", HtmlEncoder.encodeTextSmart(html));
108         html = " a";
109         assertEquals(" &nbsp;a", HtmlEncoder.encodeTextSmart(html));
110         html = " a";
111         assertEquals(" &nbsp; a", HtmlEncoder.encodeTextSmart(html));
112         html = " a";
113         assertEquals(" &nbsp; &nbsp;a", HtmlEncoder.encodeTextSmart(html));
114         html = " a";
115         assertEquals(" &nbsp; &nbsp; a", HtmlEncoder.encodeTextSmart(html));
116         
117         html = "a ";
118         assertEquals("a ", HtmlEncoder.encodeTextSmart(html));
119         html = "a ";
120         assertEquals("a &nbsp;", HtmlEncoder.encodeTextSmart(html));
121         html = "a ";
122         assertEquals("a &nbsp; ", HtmlEncoder.encodeTextSmart(html));
123         html = "a ";
124         assertEquals("a &nbsp; &nbsp;", HtmlEncoder.encodeTextSmart(html));
125         html = "a ";
126         assertEquals("a &nbsp; &nbsp; ", HtmlEncoder.encodeTextSmart(html));
127         
128         html = " a ";
129         assertEquals(" a ", HtmlEncoder.encodeTextSmart(html));
130         html = " a ";
131         assertEquals(" &nbsp;a &nbsp;", HtmlEncoder.encodeTextSmart(html));
132         html = " a ";
133         assertEquals(" a &nbsp;", HtmlEncoder.encodeTextSmart(html));
134         html = " a ";
135         assertEquals(" &nbsp;a ", HtmlEncoder.encodeTextSmart(html));
136         html = " a b c d e";
137         assertEquals(" &nbsp;a b &nbsp; c &nbsp;d e", HtmlEncoder.encodeTextSmart(html));
138     }
139
140
141 }
142
Popular Tags