Moved tests from testsuite to web

This commit is contained in:
Arjen Poutsma
2008-11-03 10:42:27 +00:00
parent 88efc06a23
commit 25dab06804
6 changed files with 672 additions and 163 deletions

View File

@@ -1,165 +0,0 @@
/*
* Copyright 2002-2005 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 java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.HashMap;
import java.util.Map;
import junit.framework.TestCase;
/**
* @author Martin Kersten
* @author Juergen Hoeller
*/
public class HtmlCharacterEntityReferencesTests extends TestCase {
private static final String DTD_FILE = "HtmlCharacterEntityReferences.dtd";
public void testSupportsAllCharacterEntityReferencesDefinedByHtml() {
HtmlCharacterEntityReferences entityReferences = new HtmlCharacterEntityReferences();
Map referenceCharactersMap = getReferenceCharacterMap();
for (int character = 0; character < 10000; character++) {
String referenceName = (String) referenceCharactersMap.get(new Integer(character));
if (referenceName != null) {
String fullReference =
HtmlCharacterEntityReferences.REFERENCE_START +
referenceName +
HtmlCharacterEntityReferences.REFERENCE_END;
assertTrue("The unicode character " + character + " should be mapped to a reference",
entityReferences.isMappedToReference((char) character));
assertEquals("The reference of unicode character " + character + " should be entity " + referenceName,
fullReference, entityReferences.convertToReference((char) character));
assertEquals("The entity reference [" + referenceName + "] should be mapped to unicode character " + character,
(char) character, entityReferences.convertToCharacter(referenceName));
}
else {
assertFalse("The unicode character " + character + " should not be mapped to a reference",
entityReferences.isMappedToReference((char) character));
assertNull("No entity reference of unicode character " + character + " should exist",
entityReferences.convertToReference((char) character));
}
}
assertEquals("The registered entity count of entityReferences should match the number of entity references",
referenceCharactersMap.size(), entityReferences.getSupportedReferenceCount());
assertEquals("The HTML 4.0 Standard defines 252 entity references so do entityReferences",
252, entityReferences.getSupportedReferenceCount());
assertEquals("Invalid entity reference names should not be convertable",
(char) -1, entityReferences.convertToCharacter("invalid"));
}
private Map getReferenceCharacterMap() {
CharacterEntityResourceIterator entityIterator = new CharacterEntityResourceIterator();
Map referencedCharactersMap = new HashMap();
while (entityIterator.hasNext()) {
int character = entityIterator.getReferredCharacter();
String entityName = entityIterator.nextEntry();
referencedCharactersMap.put(new Integer(character), entityName);
}
return referencedCharactersMap;
}
private static class CharacterEntityResourceIterator {
private final StreamTokenizer tokenizer;
private String currentEntityName = null;
private int referredCharacter = -1;
public CharacterEntityResourceIterator() {
try {
InputStream inputStream = getClass().getResourceAsStream(DTD_FILE);
if (inputStream == null) {
throw new IOException("Cannot find definition resource [" + DTD_FILE + "]");
}
tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(inputStream, "UTF-8")));
}
catch (IOException ex) {
throw new IllegalStateException("Failed to open definition resource [" + DTD_FILE + "]");
}
}
public boolean hasNext() {
return (currentEntityName != null ? true : readNextEntity());
}
public String nextEntry() {
if (hasNext()) {
String entityName = currentEntityName;
currentEntityName = null;
return entityName;
}
return null;
}
public int getReferredCharacter() {
return referredCharacter;
}
private boolean readNextEntity() {
try {
while (navigateToNextEntity()) {
String entityName = nextWordToken();
if ("CDATA".equals(nextWordToken())) {
int referredCharacter = nextReferredCharacterId();
if (entityName != null && referredCharacter != -1) {
this.currentEntityName = entityName;
this.referredCharacter = referredCharacter;
return true;
}
}
}
return false;
}
catch (IOException ex) {
throw new IllegalStateException("Could not parse defintion resource: " + ex.getMessage());
}
}
private boolean navigateToNextEntity() throws IOException {
while (tokenizer.nextToken() != StreamTokenizer.TT_WORD || !"ENTITY".equals(tokenizer.sval)) {
if (tokenizer.ttype == StreamTokenizer.TT_EOF) {
return false;
}
}
return true;
}
private int nextReferredCharacterId() throws IOException {
String reference = nextWordToken();
if (reference != null && reference.startsWith("&#") && reference.endsWith(";")) {
return Integer.parseInt(reference.substring(2, reference.length() - 1));
}
return -1;
}
private String nextWordToken() throws IOException {
tokenizer.nextToken();
return tokenizer.sval;
}
}
}

View File

@@ -1,116 +0,0 @@
/*
* 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("&quot;This is a quote", escaped);
escaped = HtmlUtils.htmlEscapeDecimal(unescaped);
assertEquals("&#34;This is a quote", escaped);
escaped = HtmlUtils.htmlEscapeHex(unescaped);
assertEquals("&#x22;This is a quote", escaped);
}
public void testHtmlUnescape() {
String escaped = "&quot;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 '&lt; &gt;'",
"&lt; &gt;", HtmlUtils.htmlEscape("< >"));
assertEquals("'< >' should be encoded to '&#60; &#62;'",
"&#60; &#62;", HtmlUtils.htmlEscapeDecimal("< >"));
assertEquals("The special character 8709 should be encoded to '&empty;'",
"&empty;", HtmlUtils.htmlEscape("" + (char) 8709));
assertEquals("The special character 8709 should be encoded to '&#8709;'",
"&#8709;", HtmlUtils.htmlEscapeDecimal("" + (char) 8709));
assertEquals("The special character 977 should be encoded to '&thetasym;'",
"&thetasym;", HtmlUtils.htmlEscape("" + (char) 977));
assertEquals("The special character 977 should be encoded to '&#977;'",
"&#977;", 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&nbsp;B' should be decoded to 'A B'",
"A" + (char) 160 + "B", HtmlUtils.htmlUnescape("A&nbsp;B"));
assertEquals("'&lt; &gt;' should be decoded to '< >'",
"< >", HtmlUtils.htmlUnescape("&lt; &gt;"));
assertEquals("'&#60; &#62;' should be decoded to '< >'",
"< >", HtmlUtils.htmlUnescape("&#60; &#62;"));
assertEquals("'&#x41;&#X42;&#x43;' should be decoded to 'ABC'",
"ABC", HtmlUtils.htmlUnescape("&#x41;&#X42;&#x43;"));
assertEquals("'&phi;' should be decoded to uni-code character 966",
"" + (char) 966, HtmlUtils.htmlUnescape("&phi;"));
assertEquals("'&Prime;' should be decoded to uni-code character 8243",
"" + (char) 8243, HtmlUtils.htmlUnescape("&Prime;"));
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 '&thetasym;' should be processable",
"" + (char) 977, HtmlUtils.htmlUnescape("&thetasym;"));
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 '&#1;' should be converted to char 1",
"" + (char) 1, HtmlUtils.htmlUnescape("&#1;"));
assertEquals("The malformed hex reference '&#x;' should remain '&#x;'",
"&#x;", HtmlUtils.htmlUnescape("&#x;"));
}
}

View File

@@ -1,162 +0,0 @@
/*
* Copyright 2002-2006 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;
import org.springframework.test.AssertThrows;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;
/**
* Unit tests for the {@link TagUtils} class.
*
* @author Alef Arendsen
* @author Rick Evans
*/
public final class TagUtilsTests extends TestCase {
public void testGetScopeSunnyDay() {
assertEquals(TagUtils.SCOPE_PAGE, "page");
assertEquals(TagUtils.SCOPE_APPLICATION, "application");
assertEquals(TagUtils.SCOPE_SESSION, "session");
assertEquals(TagUtils.SCOPE_REQUEST, "request");
assertEquals(TagUtils.getScope("page"), PageContext.PAGE_SCOPE);
assertEquals(TagUtils.getScope("request"), PageContext.REQUEST_SCOPE);
assertEquals(TagUtils.getScope("session"), PageContext.SESSION_SCOPE);
assertEquals(TagUtils.getScope("application"), PageContext.APPLICATION_SCOPE);
// non-existent scope
assertEquals("TagUtils.getScope(..) with a non-existent scope argument must " +
"just return the default scope (PageContext.PAGE_SCOPE).",
TagUtils.getScope("bla"), PageContext.PAGE_SCOPE);
}
public void testGetScopeWithNullScopeArgument() {
new AssertThrows(IllegalArgumentException.class) {
public void test() throws Exception {
TagUtils.getScope(null);
}
}.runTest();
}
public void testHasAncestorOfTypeWhereAncestorTagIsNotATagType() throws Exception {
new AssertThrows(IllegalArgumentException.class) {
public void test() throws Exception {
assertFalse(TagUtils.hasAncestorOfType(
new TagSupport(), String.class));
}
}.runTest();
}
public void testHasAncestorOfTypeWithNullTagArgument() throws Exception {
new AssertThrows(IllegalArgumentException.class) {
public void test() throws Exception {
assertFalse(TagUtils.hasAncestorOfType(null, TagSupport.class));
}
}.runTest();
}
public void testHasAncestorOfTypeWithNullAncestorTagClassArgument() throws Exception {
new AssertThrows(IllegalArgumentException.class) {
public void test() throws Exception {
assertFalse(TagUtils.hasAncestorOfType(new TagSupport(), null));
}
}.runTest();
}
public void testHasAncestorOfTypeTrueScenario() throws Exception {
Tag a = new TagA();
Tag b = new TagB();
Tag c = new TagC();
a.setParent(b);
b.setParent(c);
assertTrue(TagUtils.hasAncestorOfType(a, TagC.class));
}
public void testHasAncestorOfTypeFalseScenario() throws Exception {
Tag a = new TagA();
Tag b = new TagB();
Tag anotherB = new TagB();
a.setParent(b);
b.setParent(anotherB);
assertFalse(TagUtils.hasAncestorOfType(a, TagC.class));
}
public void testHasAncestorOfTypeWhenTagHasNoParent() throws Exception {
assertFalse(TagUtils.hasAncestorOfType(new TagA(), TagC.class));
}
public void testAssertHasAncestorOfTypeWithNullTagName() throws Exception {
new AssertThrows(IllegalArgumentException.class) {
public void test() throws Exception {
TagUtils.assertHasAncestorOfType(new TagA(), TagC.class, null, "c");
}
}.runTest();
}
public void testAssertHasAncestorOfTypeWithNullAncestorTagName() throws Exception {
new AssertThrows(IllegalArgumentException.class) {
public void test() throws Exception {
TagUtils.assertHasAncestorOfType(new TagA(), TagC.class, "a", null);
}
}.runTest();
}
public void testAssertHasAncestorOfTypeThrowsExceptionOnFail() throws Exception {
new AssertThrows(IllegalStateException.class) {
public void test() throws Exception {
Tag a = new TagA();
Tag b = new TagB();
Tag anotherB = new TagB();
a.setParent(b);
b.setParent(anotherB);
TagUtils.assertHasAncestorOfType(a, TagC.class, "a", "c");
}
}.runTest();
}
public void testAssertHasAncestorOfTypeDoesNotThrowExceptionOnPass() throws Exception {
Tag a = new TagA();
Tag b = new TagB();
Tag c = new TagC();
a.setParent(b);
b.setParent(c);
TagUtils.assertHasAncestorOfType(a, TagC.class, "a", "c");
}
private static final class TagA extends TagSupport {
}
private static final class TagB extends TagSupport {
}
private static final class TagC extends TagSupport {
}
}

View File

@@ -1,52 +0,0 @@
/*
* Copyright 2002-2008 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 java.util.HashMap;
import java.util.Map;
import junit.framework.TestCase;
/**
* @author Juergen Hoeller
*/
public class WebUtilsTests extends TestCase {
public void testFindParameterValue() {
Map params = new HashMap();
params.put("myKey1", "myValue1");
params.put("myKey2_myValue2", "xxx");
params.put("myKey3_myValue3.x", "xxx");
params.put("myKey4_myValue4.y", "yyy");
assertNull(WebUtils.findParameterValue(params, "myKey0"));
assertEquals("myValue1", WebUtils.findParameterValue(params, "myKey1"));
assertEquals("myValue2", WebUtils.findParameterValue(params, "myKey2"));
assertEquals("myValue3", WebUtils.findParameterValue(params, "myKey3"));
assertEquals("myValue4", WebUtils.findParameterValue(params, "myKey4"));
}
public void testExtractFilenameFromUrlPath() {
assertEquals("index", WebUtils.extractFilenameFromUrlPath("index.html"));
assertEquals("index", WebUtils.extractFilenameFromUrlPath("/index.html"));
assertEquals("view", WebUtils.extractFilenameFromUrlPath("/products/view.html"));
assertEquals("view", WebUtils.extractFilenameFromUrlPath("/products/view.html?param=a"));
assertEquals("view", WebUtils.extractFilenameFromUrlPath("/products/view.html?param=/path/a"));
assertEquals("view", WebUtils.extractFilenameFromUrlPath("/products/view.html?param=/path/a.do"));
}
}