KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > StringEscapeUtilsTest


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
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 package org.apache.commons.lang;
17
18 import java.io.IOException JavaDoc;
19 import java.lang.reflect.Constructor JavaDoc;
20 import java.lang.reflect.Modifier JavaDoc;
21
22 import junit.framework.Test;
23 import junit.framework.TestCase;
24 import junit.framework.TestSuite;
25 import junit.textui.TestRunner;
26
27 /**
28  * Unit tests for {@link StringEscapeUtils}.
29  *
30  * @author of original StringUtilsTest.testEscape = ?
31  * @author <a HREF="mailto:alex@purpletech.com">Alexander Day Chaffee</a>
32  * @version $Id: StringEscapeUtilsTest.java 161244 2005-04-14 06:16:36Z ggregory $
33  */

34 public class StringEscapeUtilsTest extends TestCase {
35     private final static String JavaDoc FOO = "foo";
36
37     public StringEscapeUtilsTest(String JavaDoc name) {
38         super(name);
39     }
40
41     public static void main(String JavaDoc[] args) {
42         TestRunner.run(suite());
43     }
44
45     public static Test suite() {
46         TestSuite suite = new TestSuite(StringEscapeUtilsTest.class);
47         suite.setName("StringEscapeUtilsTest Tests");
48         return suite;
49     }
50
51     //-----------------------------------------------------------------------
52
public void testConstructor() {
53         assertNotNull(new StringEscapeUtils());
54         Constructor JavaDoc[] cons = StringEscapeUtils.class.getDeclaredConstructors();
55         assertEquals(1, cons.length);
56         assertEquals(true, Modifier.isPublic(cons[0].getModifiers()));
57         assertEquals(true, Modifier.isPublic(StringEscapeUtils.class.getModifiers()));
58         assertEquals(false, Modifier.isFinal(StringEscapeUtils.class.getModifiers()));
59     }
60     
61     //-----------------------------------------------------------------------
62
public void testEscapeJava() throws IOException JavaDoc {
63         assertEquals(null, StringEscapeUtils.escapeJava(null));
64         try {
65             StringEscapeUtils.escapeJava(null, null);
66             fail();
67         } catch (IOException JavaDoc ex) {
68             fail();
69         } catch (IllegalArgumentException JavaDoc ex) {
70         }
71         try {
72             StringEscapeUtils.escapeJava(null, "");
73             fail();
74         } catch (IOException JavaDoc ex) {
75             fail();
76         } catch (IllegalArgumentException JavaDoc ex) {
77         }
78         
79         assertEscapeJava("empty string", "", "");
80         assertEscapeJava(FOO, FOO);
81         assertEscapeJava("tab", "\\t", "\t");
82         assertEscapeJava("backslash", "\\\\", "\\");
83         assertEscapeJava("single quote should not be escaped", "'", "'");
84         assertEscapeJava("\\\\\\b\\t\\r", "\\\b\t\r");
85         assertEscapeJava("\\u1234", "\u1234");
86         assertEscapeJava("\\u0234", "\u0234");
87         assertEscapeJava("\\u00EF", "\u00ef");
88         assertEscapeJava("\\u0001", "\u0001");
89         assertEscapeJava("Should use capitalized unicode hex", "\\uABCD", "\uabcd");
90
91         assertEscapeJava("He didn't say, \\\"stop!\\\"",
92                 "He didn't say, \"stop!\"");
93         assertEscapeJava("non-breaking space", "This space is non-breaking:" + "\\u00A0",
94                 "This space is non-breaking:\u00a0");
95         assertEscapeJava("\\uABCD\\u1234\\u012C",
96                 "\uABCD\u1234\u012C");
97     }
98
99     private void assertEscapeJava(String JavaDoc escaped, String JavaDoc original) throws IOException JavaDoc {
100         assertEscapeJava(null, escaped, original);
101     }
102
103     private void assertEscapeJava(String JavaDoc message, String JavaDoc expected, String JavaDoc original) throws IOException JavaDoc {
104         String JavaDoc converted = StringEscapeUtils.escapeJava(original);
105         message = "escapeJava(String) failed" + (message == null ? "" : (": " + message));
106         assertEquals(message, expected, converted);
107
108         StringPrintWriter writer = new StringPrintWriter();
109         StringEscapeUtils.escapeJava(writer, original);
110         assertEquals(expected, writer.getString());
111     }
112
113     public void testUnescapeJava() throws IOException JavaDoc {
114         assertEquals(null, StringEscapeUtils.unescapeJava(null));
115         try {
116             StringEscapeUtils.unescapeJava(null, null);
117             fail();
118         } catch (IOException JavaDoc ex) {
119             fail();
120         } catch (IllegalArgumentException JavaDoc ex) {
121         }
122         try {
123             StringEscapeUtils.unescapeJava(null, "");
124             fail();
125         } catch (IOException JavaDoc ex) {
126             fail();
127         } catch (IllegalArgumentException JavaDoc ex) {
128         }
129         try {
130             String JavaDoc str = StringEscapeUtils.unescapeJava("\\u02-3");
131             fail();
132         } catch (RuntimeException JavaDoc ex) {
133         }
134         
135         assertUnescapeJava("", "");
136         assertUnescapeJava("test", "test");
137         assertUnescapeJava("\ntest\b", "\\ntest\\b");
138         assertUnescapeJava("\u123425foo\ntest\b", "\\u123425foo\\ntest\\b");
139         assertUnescapeJava("'\foo\teste\r", "\\'\\foo\\teste\\r");
140         assertUnescapeJava("\\", "\\");
141         //foo
142
assertUnescapeJava("lowercase unicode", "\uABCDx", "\\uabcdx");
143         assertUnescapeJava("uppercase unicode", "\uABCDx", "\\uABCDx");
144         assertUnescapeJava("unicode as final character", "\uABCD", "\\uabcd");
145     }
146
147     private void assertUnescapeJava(String JavaDoc unescaped, String JavaDoc original) throws IOException JavaDoc {
148         assertUnescapeJava(null, unescaped, original);
149     }
150
151     private void assertUnescapeJava(String JavaDoc message, String JavaDoc unescaped, String JavaDoc original) throws IOException JavaDoc {
152         String JavaDoc expected = unescaped;
153         String JavaDoc actual = StringEscapeUtils.unescapeJava(original);
154
155         assertEquals("unescape(String) failed" +
156                 (message == null ? "" : (": " + message)) +
157                 ": expected '" + StringEscapeUtils.escapeJava(expected) +
158                 // we escape this so we can see it in the error message
159
"' actual '" + StringEscapeUtils.escapeJava(actual) + "'",
160                 expected, actual);
161
162         StringPrintWriter writer = new StringPrintWriter();
163         StringEscapeUtils.unescapeJava(writer, original);
164         assertEquals(unescaped, writer.getString());
165
166     }
167
168     public void testEscapeJavaScript() {
169         assertEquals(null, StringEscapeUtils.escapeJavaScript(null));
170         try {
171             StringEscapeUtils.escapeJavaScript(null, null);
172             fail();
173         } catch (IOException JavaDoc ex) {
174             fail();
175         } catch (IllegalArgumentException JavaDoc ex) {
176         }
177         try {
178             StringEscapeUtils.escapeJavaScript(null, "");
179             fail();
180         } catch (IOException JavaDoc ex) {
181             fail();
182         } catch (IllegalArgumentException JavaDoc ex) {
183         }
184         
185         assertEquals("He didn\\'t say, \\\"stop!\\\"", StringEscapeUtils.escapeJavaScript("He didn't say, \"stop!\""));
186     }
187
188
189     // HTML and XML
190
//--------------------------------------------------------------
191

192     String JavaDoc[][] htmlEscapes = {
193         {"no escaping", "plain text", "plain text"},
194         {"no escaping", "plain text", "plain text"},
195         {"empty string", "", ""},
196         {"null", null, null},
197         {"ampersand", "bread &amp; butter", "bread & butter"},
198         {"quotes", "&quot;bread&quot; &amp; butter", "\"bread\" & butter"},
199         {"final character only", "greater than &gt;", "greater than >"},
200         {"first character only", "&lt; less than", "< less than"},
201         {"apostrophe", "Huntington's chorea", "Huntington's chorea"},
202         {"languages", "English,Fran&ccedil;ais,&#26085;&#26412;&#35486; (nihongo)", "English,Fran\u00E7ais,\u65E5\u672C\u8A9E (nihongo)"},
203         {"8-bit ascii doesn't number-escape", "~\u007F", "\u007E\u007F"},
204         {"8-bit ascii does number-escape", "&#128;&#159;", "\u0080\u009F"},
205     };
206
207     public void testEscapeHtml() {
208         for (int i = 0; i < htmlEscapes.length; ++i) {
209             String JavaDoc message = htmlEscapes[i][0];
210             String JavaDoc expected = htmlEscapes[i][1];
211             String JavaDoc original = htmlEscapes[i][2];
212             assertEquals(message, expected, StringEscapeUtils.escapeHtml(original));
213             // todo: add test for (and implement) Writer-based version, something like this:
214
// StringPrintWriter sw = new StringPrintWriter();
215
// StringEscapeUtils.escapeHtml(sw, original);
216
// assertEquals(expected, sw.getString());
217
}
218     }
219
220     public void testUnescapeHtml() {
221         for (int i = 0; i < htmlEscapes.length; ++i) {
222             assertEquals(htmlEscapes[i][0], htmlEscapes[i][2], StringEscapeUtils.unescapeHtml(htmlEscapes[i][1]));
223             // todo: add test for (and implement) Writer-based version
224
}
225         // \u00E7 is a cedilla (c with wiggle under)
226
// note that the test string must be 7-bit-clean (unicode escaped) or else it will compile incorrectly
227
// on some locales
228
assertEquals("funny chars pass through OK", "Fran\u00E7ais", StringEscapeUtils.unescapeHtml("Fran\u00E7ais"));
229         
230         assertEquals("Hello&;World", StringEscapeUtils.unescapeHtml("Hello&;World"));
231         assertEquals("Hello&#;World", StringEscapeUtils.unescapeHtml("Hello&#;World"));
232         assertEquals("Hello&# ;World", StringEscapeUtils.unescapeHtml("Hello&# ;World"));
233         assertEquals("Hello&##;World", StringEscapeUtils.unescapeHtml("Hello&##;World"));
234     }
235
236     public void testUnescapeHexCharsHtml() {
237         // Simple easy to grok test
238
assertEquals("hex number unescape", "\u0080\u009F", StringEscapeUtils.unescapeHtml("&#x80;&#x9F;"));
239         assertEquals("hex number unescape", "\u0080\u009F", StringEscapeUtils.unescapeHtml("&#X80;&#X9F;"));
240         // Test all Character values:
241
for (char i = Character.MIN_VALUE; i < Character.MAX_VALUE; i++) {
242             Character JavaDoc c1 = new Character JavaDoc(i);
243             Character JavaDoc c2 = new Character JavaDoc((char)(i+1));
244             String JavaDoc expected = c1.toString() + c2.toString();
245             String JavaDoc escapedC1 = "&#x" + Integer.toHexString((int)(c1.charValue())) + ";";
246             String JavaDoc escapedC2 = "&#x" + Integer.toHexString((int)(c2.charValue())) + ";";
247             assertEquals("hex number unescape index " + (int)i, expected, StringEscapeUtils.unescapeHtml(escapedC1 + escapedC2));
248         }
249     }
250
251     public void testUnescapeUnknownEntity() throws Exception JavaDoc
252     {
253         assertEquals("&zzzz;", StringEscapeUtils.unescapeHtml("&zzzz;"));
254     }
255
256     public void testEscapeHtmlVersions() throws Exception JavaDoc
257     {
258         assertEquals("&Beta;", StringEscapeUtils.escapeHtml("\u0392"));
259         assertEquals("\u0392", StringEscapeUtils.unescapeHtml("&Beta;"));
260
261         //todo: refine API for escaping/unescaping specific HTML versions
262

263     }
264
265     public void testEscapeXml() throws Exception JavaDoc {
266         assertEquals("&lt;abc&gt;", StringEscapeUtils.escapeXml("<abc>"));
267         assertEquals("<abc>", StringEscapeUtils.unescapeXml("&lt;abc&gt;"));
268
269         assertEquals("XML should use numbers, not names for HTML entities",
270                 "&#161;", StringEscapeUtils.escapeXml("\u00A1"));
271         assertEquals("XML should use numbers, not names for HTML entities",
272                 "\u00A0", StringEscapeUtils.unescapeXml("&#160;"));
273
274         assertEquals("ain't", StringEscapeUtils.unescapeXml("ain&apos;t"));
275         assertEquals("ain&apos;t", StringEscapeUtils.escapeXml("ain't"));
276         assertEquals("", StringEscapeUtils.escapeXml(""));
277         assertEquals(null, StringEscapeUtils.escapeXml(null));
278         assertEquals(null, StringEscapeUtils.unescapeXml(null));
279     }
280
281     // SQL
282
// see http://www.jguru.com/faq/view.jsp?EID=8881
283
//--------------------
284

285     public void testEscapeSql() throws Exception JavaDoc
286     {
287         assertEquals("don''t stop", StringEscapeUtils.escapeSql("don't stop"));
288         assertEquals("", StringEscapeUtils.escapeSql(""));
289         assertEquals(null, StringEscapeUtils.escapeSql(null));
290     }
291 }
292
293
Popular Tags