1 16 package org.apache.commons.lang; 17 18 import java.io.IOException ; 19 import java.lang.reflect.Constructor ; 20 import java.lang.reflect.Modifier ; 21 22 import junit.framework.Test; 23 import junit.framework.TestCase; 24 import junit.framework.TestSuite; 25 import junit.textui.TestRunner; 26 27 34 public class StringEscapeUtilsTest extends TestCase { 35 private final static String FOO = "foo"; 36 37 public StringEscapeUtilsTest(String name) { 38 super(name); 39 } 40 41 public static void main(String [] 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 public void testConstructor() { 53 assertNotNull(new StringEscapeUtils()); 54 Constructor [] 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 public void testEscapeJava() throws IOException { 63 assertEquals(null, StringEscapeUtils.escapeJava(null)); 64 try { 65 StringEscapeUtils.escapeJava(null, null); 66 fail(); 67 } catch (IOException ex) { 68 fail(); 69 } catch (IllegalArgumentException ex) { 70 } 71 try { 72 StringEscapeUtils.escapeJava(null, ""); 73 fail(); 74 } catch (IOException ex) { 75 fail(); 76 } catch (IllegalArgumentException 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 escaped, String original) throws IOException { 100 assertEscapeJava(null, escaped, original); 101 } 102 103 private void assertEscapeJava(String message, String expected, String original) throws IOException { 104 String 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 { 114 assertEquals(null, StringEscapeUtils.unescapeJava(null)); 115 try { 116 StringEscapeUtils.unescapeJava(null, null); 117 fail(); 118 } catch (IOException ex) { 119 fail(); 120 } catch (IllegalArgumentException ex) { 121 } 122 try { 123 StringEscapeUtils.unescapeJava(null, ""); 124 fail(); 125 } catch (IOException ex) { 126 fail(); 127 } catch (IllegalArgumentException ex) { 128 } 129 try { 130 String str = StringEscapeUtils.unescapeJava("\\u02-3"); 131 fail(); 132 } catch (RuntimeException 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 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 unescaped, String original) throws IOException { 148 assertUnescapeJava(null, unescaped, original); 149 } 150 151 private void assertUnescapeJava(String message, String unescaped, String original) throws IOException { 152 String expected = unescaped; 153 String actual = StringEscapeUtils.unescapeJava(original); 154 155 assertEquals("unescape(String) failed" + 156 (message == null ? "" : (": " + message)) + 157 ": expected '" + StringEscapeUtils.escapeJava(expected) + 158 "' 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 ex) { 174 fail(); 175 } catch (IllegalArgumentException ex) { 176 } 177 try { 178 StringEscapeUtils.escapeJavaScript(null, ""); 179 fail(); 180 } catch (IOException ex) { 181 fail(); 182 } catch (IllegalArgumentException ex) { 183 } 184 185 assertEquals("He didn\\'t say, \\\"stop!\\\"", StringEscapeUtils.escapeJavaScript("He didn't say, \"stop!\"")); 186 } 187 188 189 192 String [][] 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 & butter", "bread & butter"}, 198 {"quotes", ""bread" & butter", "\"bread\" & butter"}, 199 {"final character only", "greater than >", "greater than >"}, 200 {"first character only", "< less than", "< less than"}, 201 {"apostrophe", "Huntington's chorea", "Huntington's chorea"}, 202 {"languages", "English,Français,日本語 (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", "€Ÿ", "\u0080\u009F"}, 205 }; 206 207 public void testEscapeHtml() { 208 for (int i = 0; i < htmlEscapes.length; ++i) { 209 String message = htmlEscapes[i][0]; 210 String expected = htmlEscapes[i][1]; 211 String original = htmlEscapes[i][2]; 212 assertEquals(message, expected, StringEscapeUtils.escapeHtml(original)); 213 } 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 } 225 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 assertEquals("hex number unescape", "\u0080\u009F", StringEscapeUtils.unescapeHtml("€Ÿ")); 239 assertEquals("hex number unescape", "\u0080\u009F", StringEscapeUtils.unescapeHtml("€Ÿ")); 240 for (char i = Character.MIN_VALUE; i < Character.MAX_VALUE; i++) { 242 Character c1 = new Character (i); 243 Character c2 = new Character ((char)(i+1)); 244 String expected = c1.toString() + c2.toString(); 245 String escapedC1 = "&#x" + Integer.toHexString((int)(c1.charValue())) + ";"; 246 String 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 252 { 253 assertEquals("&zzzz;", StringEscapeUtils.unescapeHtml("&zzzz;")); 254 } 255 256 public void testEscapeHtmlVersions() throws Exception 257 { 258 assertEquals("Β", StringEscapeUtils.escapeHtml("\u0392")); 259 assertEquals("\u0392", StringEscapeUtils.unescapeHtml("Β")); 260 261 263 } 264 265 public void testEscapeXml() throws Exception { 266 assertEquals("<abc>", StringEscapeUtils.escapeXml("<abc>")); 267 assertEquals("<abc>", StringEscapeUtils.unescapeXml("<abc>")); 268 269 assertEquals("XML should use numbers, not names for HTML entities", 270 "¡", StringEscapeUtils.escapeXml("\u00A1")); 271 assertEquals("XML should use numbers, not names for HTML entities", 272 "\u00A0", StringEscapeUtils.unescapeXml(" ")); 273 274 assertEquals("ain't", StringEscapeUtils.unescapeXml("ain't")); 275 assertEquals("ain't", StringEscapeUtils.escapeXml("ain't")); 276 assertEquals("", StringEscapeUtils.escapeXml("")); 277 assertEquals(null, StringEscapeUtils.escapeXml(null)); 278 assertEquals(null, StringEscapeUtils.unescapeXml(null)); 279 } 280 281 285 public void testEscapeSql() throws Exception 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 |