KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > servlet > HtmlEncoderTest


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.servlet;
4
5 import junit.framework.*;
6
7 public class HtmlEncoderTest extends TestCase {
8
9     public void testEncode() {
10         String JavaDoc html = "< & \" ' > \r\n \n \t";
11
12         assertEquals("&lt; &amp; &quot; &#039; &gt; \r\n \n \t", HtmlEncode.text(html));
13
14         html = "";
15         assertEquals("", HtmlEncode.text(html));
16
17         html = null;
18         assertEquals("", HtmlEncode.text(html));
19
20         html = new String JavaDoc(new char[] {128, 257});
21         assertEquals(html, HtmlEncode.text(html));
22     }
23
24     public void testEncodeText() {
25         String JavaDoc html = "< & \" ' > \r\n \n \t";
26
27         assertEquals("&lt; &amp; &quot; &#039; &gt; <br> <br> \t", HtmlEncode.block(html));
28
29         html = "";
30         assertEquals("", HtmlEncode.block(html));
31
32         html = null;
33         assertEquals("", HtmlEncode.block(html));
34
35         html = new String JavaDoc(new char[] {128, 257});
36         assertEquals(html, HtmlEncode.block(html));
37
38         html = "\r\n\n\r";
39         assertEquals("<br><br><br>", HtmlEncode.block(html));
40
41         html = "\r\n\r\n";
42         assertEquals("<br><br>", HtmlEncode.block(html));
43
44         html = "\n\r";
45         assertEquals("<br><br>", HtmlEncode.block(html));
46     }
47
48
49     public void testEncodeTextStrict() {
50         String JavaDoc html = "< & \" ' > \r\n \n \t";
51
52         assertEquals("&lt; &amp; &quot; &#039; &gt; <br> <br> &nbsp;\t", HtmlEncode.strict(html));
53
54         html = "";
55         assertEquals("", HtmlEncode.strict(html));
56
57         html = null;
58         assertEquals("", HtmlEncode.strict(html));
59
60         html = new String JavaDoc(new char[] {128, 257});
61         assertEquals(html, HtmlEncode.strict(html));
62
63         html = "\r\n\n\r";
64         assertEquals("<br><br><br>", HtmlEncode.strict(html));
65
66         html = "\r\n\r\n";
67         assertEquals("<br><br>", HtmlEncode.strict(html));
68
69         html = "\n\r";
70         assertEquals("<br><br>", HtmlEncode.strict(html));
71
72
73         html = " ";
74         assertEquals(" ", HtmlEncode.strict(html));
75         html = " ";
76         assertEquals(" &nbsp;", HtmlEncode.strict(html));
77         html = " ";
78         assertEquals(" &nbsp; ", HtmlEncode.strict(html));
79         html = " ";
80         assertEquals(" &nbsp; &nbsp;", HtmlEncode.strict(html));
81         html = " ";
82         assertEquals(" &nbsp; &nbsp; ", HtmlEncode.strict(html));
83
84         html = " a";
85         assertEquals(" a", HtmlEncode.strict(html));
86         html = " a";
87         assertEquals(" &nbsp;a", HtmlEncode.strict(html));
88         html = " a";
89         assertEquals(" &nbsp; a", HtmlEncode.strict(html));
90         html = " a";
91         assertEquals(" &nbsp; &nbsp;a", HtmlEncode.strict(html));
92         html = " a";
93         assertEquals(" &nbsp; &nbsp; a", HtmlEncode.strict(html));
94         
95         html = "a ";
96         assertEquals("a ", HtmlEncode.strict(html));
97         html = "a ";
98         assertEquals("a &nbsp;", HtmlEncode.strict(html));
99         html = "a ";
100         assertEquals("a &nbsp; ", HtmlEncode.strict(html));
101         html = "a ";
102         assertEquals("a &nbsp; &nbsp;", HtmlEncode.strict(html));
103         html = "a ";
104         assertEquals("a &nbsp; &nbsp; ", HtmlEncode.strict(html));
105         
106         html = " a ";
107         assertEquals(" a ", HtmlEncode.strict(html));
108         html = " a ";
109         assertEquals(" &nbsp;a &nbsp;", HtmlEncode.strict(html));
110         html = " a ";
111         assertEquals(" a &nbsp;", HtmlEncode.strict(html));
112         html = " a ";
113         assertEquals(" &nbsp;a ", HtmlEncode.strict(html));
114         html = " a b c d e";
115         assertEquals(" &nbsp;a b &nbsp; c &nbsp;d e", HtmlEncode.strict(html));
116     }
117
118     public void testEncodeUrl() {
119         assertEquals("/aaa", HtmlEncode.url("/aaa"));
120         assertEquals("/aaa?", HtmlEncode.url("/aaa?"));
121         assertEquals("/aaa?b", HtmlEncode.url("/aaa?b"));
122         assertEquals("/aaa?b=", HtmlEncode.url("/aaa?b="));
123         assertEquals("/aaa?b=c", HtmlEncode.url("/aaa?b=c"));
124         assertEquals("/aaa?b=+c", HtmlEncode.url("/aaa?b= c"));
125         assertEquals("/aaa?b=+c&", HtmlEncode.url("/aaa?b= c&"));
126         assertEquals("/aaa?b=+c&dd", HtmlEncode.url("/aaa?b= c&dd"));
127         assertEquals("/aaa?b=+c&dd=", HtmlEncode.url("/aaa?b= c&dd="));
128         assertEquals("/aaa?b=++c&dd=%3D", HtmlEncode.url("/aaa?b= c&dd=="));
129         assertEquals("/aaa?b=++c&dd=%3D&=", HtmlEncode.url("/aaa?b= c&dd==&="));
130         assertEquals("?data=The+string+%C3%BC%40foo-bar", HtmlEncode.url("?data=The string ü@foo-bar"));
131     }
132
133     public void testEncodeBaseUrl() {
134         assertEquals("/aaa", HtmlEncode.baseUrl("/aaa").toString());
135         assertEquals("/aaa?", HtmlEncode.baseUrl("/aaa").p("").toString());
136         assertEquals("/aaa?b", HtmlEncode.baseUrl("/aaa").p("b").toString());
137         assertEquals("/aaa?b", HtmlEncode.baseUrl("/aaa").p("b", null).toString());
138         assertEquals("/aaa?b=c", HtmlEncode.baseUrl("/aaa").p("b", "c").toString());
139         assertEquals("/aaa?b=c", HtmlEncode.baseUrl("/aaa").p("b=c").toString());
140         assertEquals("/aaa?b=+c", HtmlEncode.baseUrl("/aaa").p("b", " c").toString());
141         assertEquals("/aaa?b=+c", HtmlEncode.baseUrl("/aaa").p("b= c").toString());
142         assertEquals("/aaa?b=+c&", HtmlEncode.baseUrl("/aaa").p("b", " c").p("").toString());
143         assertEquals("/aaa?b=+c&dd", HtmlEncode.baseUrl("/aaa").p("b", " c").p("dd").toString());
144         assertEquals("/aaa?b=+c&dd", HtmlEncode.baseUrl("/aaa").p("b", " c").p("dd", null).toString());
145         assertEquals("/aaa?b=+c&dd=%3D", HtmlEncode.baseUrl("/aaa").p("b", " c").p("dd==").toString());
146         assertEquals("/aaa?b=+c&dd=%3D", HtmlEncode.baseUrl("/aaa").p("b", " c").p("dd","=").toString());
147         assertEquals("?data=The+string+%C3%BC%40foo-bar", HtmlEncode.baseUrl("").p("data", "The string ü@foo-bar").toString());
148     }
149     
150
151 }
152
Popular Tags