1 16 17 package org.springframework.web.util; 18 19 import junit.framework.TestCase; 20 21 25 public class HtmlUtilsTests extends TestCase { 26 27 public void testHtmlEscape() { 28 String unescaped = "\"This is a quote"; 29 String 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 escaped = ""This is a quote"; 39 String 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 '< >'", 53 "< >", HtmlUtils.htmlEscape("< >")); 54 assertEquals("'< >' should be encoded to '< >'", 55 "< >", HtmlUtils.htmlEscapeDecimal("< >")); 56 57 assertEquals("The special character 8709 should be encoded to '∅'", 58 "∅", HtmlUtils.htmlEscape("" + (char) 8709)); 59 assertEquals("The special character 8709 should be encoded to '∅'", 60 "∅", HtmlUtils.htmlEscapeDecimal("" + (char) 8709)); 61 62 assertEquals("The special character 977 should be encoded to 'ϑ'", 63 "ϑ", HtmlUtils.htmlEscape("" + (char) 977)); 64 assertEquals("The special character 977 should be encoded to 'ϑ'", 65 "ϑ", 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 B' should be decoded to 'A B'", 78 "A" + (char) 160 + "B", HtmlUtils.htmlUnescape("A B")); 79 80 assertEquals("'< >' should be decoded to '< >'", 81 "< >", HtmlUtils.htmlUnescape("< >")); 82 assertEquals("'< >' should be decoded to '< >'", 83 "< >", HtmlUtils.htmlUnescape("< >")); 84 85 assertEquals("'ABC' should be decoded to 'ABC'", 86 "ABC", HtmlUtils.htmlUnescape("ABC")); 87 88 assertEquals("'φ' should be decoded to uni-code character 966", 89 "" + (char) 966, HtmlUtils.htmlUnescape("φ")); 90 91 assertEquals("'″' should be decoded to uni-code character 8243", 92 "" + (char) 8243, HtmlUtils.htmlUnescape("″")); 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 'ϑ' should be processable", 101 "" + (char) 977, HtmlUtils.htmlUnescape("ϑ")); 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 '' should be converted to char 1", 109 "" + (char) 1, HtmlUtils.htmlUnescape("")); 110 111 assertEquals("The malformed hex reference '&#x;' should remain '&#x;'", 112 "&#x;", HtmlUtils.htmlUnescape("&#x;")); 113 } 114 115 } 116 | Popular Tags |