/* * Copyright 2002-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.web.util; import junit.framework.TestCase; /** * @author Alef Arendsen * @author Martin Kersten * @author Rick Evans */ public class HtmlUtilsTests extends TestCase { public void testHtmlEscape() { String unescaped = "\"This is a quote"; String escaped = HtmlUtils.htmlEscape(unescaped); assertEquals(""This is a quote", escaped); escaped = HtmlUtils.htmlEscapeDecimal(unescaped); assertEquals(""This is a quote", escaped); escaped = HtmlUtils.htmlEscapeHex(unescaped); assertEquals(""This is a quote", escaped); } public void testHtmlUnescape() { String escaped = ""This is a quote"; String unescaped = HtmlUtils.htmlUnescape(escaped); assertEquals(unescaped, "\"This is a quote"); } public void testEncodeIntoHtmlCharacterSet() { assertNull("A null string should be converted to a null string", HtmlUtils.htmlEscape(null)); assertEquals("An empty string should be converted to an empty string", "", HtmlUtils.htmlEscape("")); assertEquals("A string containing no special characters should not be affected", "A sentence containing no special characters.", HtmlUtils.htmlEscape("A sentence containing no special characters.")); assertEquals("'< >' should be encoded to '< >'", "< >", HtmlUtils.htmlEscape("< >")); assertEquals("'< >' should be encoded to '< >'", "< >", HtmlUtils.htmlEscapeDecimal("< >")); assertEquals("The special character 8709 should be encoded to '∅'", "∅", HtmlUtils.htmlEscape("" + (char) 8709)); assertEquals("The special character 8709 should be encoded to '∅'", "∅", HtmlUtils.htmlEscapeDecimal("" + (char) 8709)); assertEquals("The special character 977 should be encoded to 'ϑ'", "ϑ", HtmlUtils.htmlEscape("" + (char) 977)); assertEquals("The special character 977 should be encoded to 'ϑ'", "ϑ", HtmlUtils.htmlEscapeDecimal("" + (char) 977)); } public void testDecodeFromHtmlCharacterSet() { assertNull("A null string should be converted to a null string", HtmlUtils.htmlUnescape(null)); assertEquals("An empty string should be converted to an empty string", "", HtmlUtils.htmlUnescape("")); assertEquals("A string containing no special characters should not be affected", "This is a sentence containing no special characters.", HtmlUtils.htmlUnescape("This is a sentence containing no special characters.")); assertEquals("'A B' should be decoded to 'A B'", "A" + (char) 160 + "B", HtmlUtils.htmlUnescape("A B")); assertEquals("'< >' should be decoded to '< >'", "< >", HtmlUtils.htmlUnescape("< >")); assertEquals("'< >' should be decoded to '< >'", "< >", HtmlUtils.htmlUnescape("< >")); assertEquals("'ABC' should be decoded to 'ABC'", "ABC", HtmlUtils.htmlUnescape("ABC")); assertEquals("'φ' should be decoded to uni-code character 966", "" + (char) 966, HtmlUtils.htmlUnescape("φ")); assertEquals("'″' should be decoded to uni-code character 8243", "" + (char) 8243, HtmlUtils.htmlUnescape("″")); assertEquals("A not supported named reference leads should be ingnored", "&prIme;", HtmlUtils.htmlUnescape("&prIme;")); assertEquals("An empty reference '&;' should be survive the decoding", "&;", HtmlUtils.htmlUnescape("&;")); assertEquals("The longest character entity reference 'ϑ' should be processable", "" + (char) 977, HtmlUtils.htmlUnescape("ϑ")); assertEquals("A malformed decimal reference should survive the decoding", "&#notADecimalNumber;", HtmlUtils.htmlUnescape("&#notADecimalNumber;")); assertEquals("A malformed hex reference should survive the decoding", "&#XnotAHexNumber;", HtmlUtils.htmlUnescape("&#XnotAHexNumber;")); assertEquals("The numerical reference '' should be converted to char 1", "" + (char) 1, HtmlUtils.htmlUnescape("")); assertEquals("The malformed hex reference '&#x;' should remain '&#x;'", "&#x;", HtmlUtils.htmlUnescape("&#x;")); } }