alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Spring Framework example source code file (HtmlUtilsTests.java)

This example Spring Framework source code file (HtmlUtilsTests.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Spring Framework tags/keywords

a, a, abc, an, b, prime, string, string, the, the, this, this, x42, xnotahexnumber

The Spring Framework HtmlUtilsTests.java source code

/*
 * 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 '?' should remain '?'",
				"?", HtmlUtils.htmlUnescape("?"));
	}

}

Other Spring Framework examples (source code examples)

Here is a short list of links related to this Spring Framework HtmlUtilsTests.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.