📄 testtagutils.java
字号:
/*
* $Id: $
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.struts.taglib;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.config.ActionConfig;
import org.apache.struts.config.ModuleConfig;
import org.apache.struts.config.impl.ModuleConfigImpl;
import org.apache.struts.mock.MockFormBean;
import org.apache.struts.mock.MockHttpServletRequest;
import org.apache.struts.mock.MockHttpServletResponse;
import org.apache.struts.mock.MockPageContext;
import org.apache.struts.mock.MockServletConfig;
import org.apache.struts.mock.MockServletContext;
import org.apache.struts.taglib.html.Constants;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
/**
* Unit tests for the TagUtils.
*/
public class TestTagUtils extends TagTestBase {
private static final Log log = LogFactory.getLog(TestTagUtils.class);
/**
* Defines the testcase name for JUnit.
*
* @param theName the testcase's name.
*/
public TestTagUtils(String theName) {
super(theName);
}
/**
* Start the tests.
*
* @param theArgs the arguments. Not used
*/
public static void main(String[] theArgs) {
junit.awtui.TestRunner.main(new String[] { TestTagUtils.class.getName() });
}
/**
* @return a test suite (<code>TestSuite</code>) that includes all methods
* starting with "test"
*/
public static Test suite() {
// All methods starting with "test" will be executed in the test suite.
return new TestSuite(TestTagUtils.class);
}
/**
* Test Operators.
*/
public void testFilter() {
assertNull("Null", null);
// Test Null
assertNull("Filter Test 1", tagutils.filter(null));
// Test Empty String
assertEquals("Filter Test 2", "", tagutils.filter(""));
// Test Single Character
assertEquals("Filter Test 3", "a", tagutils.filter("a"));
// Test Multiple Characters
assertEquals("Filter Test 4", "adhdfhdfhadhf",
tagutils.filter("adhdfhdfhadhf"));
// Test Each filtered Character
assertEquals("Filter Test 5", "<", tagutils.filter("<"));
assertEquals("Filter Test 6", ">", tagutils.filter(">"));
assertEquals("Filter Test 7", "&", tagutils.filter("&"));
assertEquals("Filter Test 8", """, tagutils.filter("\""));
assertEquals("Filter Test 9", "'", tagutils.filter("'"));
// Test filtering beginning, middle, end
assertEquals("Filter Test 10", "a<", tagutils.filter("a<"));
assertEquals("Filter Test 11", "<a", tagutils.filter("<a"));
assertEquals("Filter Test 12", "a<a", tagutils.filter("a<a"));
// Test filtering beginning, middle, end
assertEquals("Filter Test 13", "abc<", tagutils.filter("abc<"));
assertEquals("Filter Test 14", "<abc", tagutils.filter("<abc"));
assertEquals("Filter Test 15", "abc<abc", tagutils.filter("abc<abc"));
// Test Multiple Characters
assertEquals("Filter Test 16",
"<input type="text" value='Me & You'>",
tagutils.filter("<input type=\"text\" value='Me & You'>"));
}
// ---------------------------------------------------- computeParameters()
// No parameters and no transaction token
public void testComputeParameters0a() {
Map map = null;
try {
map = tagutils.computeParameters(pageContext, null, null, null,
null, null, null, null, false);
} catch (JspException e) {
fail("JspException: " + e);
}
assertNull("Map is null", map);
}
// No parameters but add transaction token
public void testComputeParameters0b() {
request.getSession().setAttribute(Globals.TRANSACTION_TOKEN_KEY, "token");
Map map = null;
try {
map = tagutils.computeParameters(pageContext, null, null, null,
null, null, null, null, true);
} catch (JspException e) {
fail("JspException: " + e);
}
assertNotNull("Map is not null", map);
assertEquals("One parameter in the returned map", 1, map.size());
assertTrue("Transaction token parameter present",
map.containsKey(Constants.TOKEN_KEY));
assertEquals("Transaction token parameter value", "token",
(String) map.get(Constants.TOKEN_KEY));
}
// invalid scope name is requested
public void testComputeParametersInvalidScope() {
Map map = null;
try {
map = tagutils.computeParameters(pageContext, null, null, null,
"session", "i-do-not-exist", null, null, false);
fail("JspException not thrown");
} catch (JspException e) {
assertNull("map is null", map);
}
}
// specified bean is not found
public void testComputeParametersBeanNotFound() {
Map map = null;
try {
map = tagutils.computeParameters(pageContext, null, null, null,
null, "i-do-not-exist", null, null, false);
fail("JspException not thrown");
} catch (JspException e) {
assertNull("map is null", map);
}
}
// accessing this property causes an exception
public void testComputeParametersPropertyThrowsException() {
request.getSession().setAttribute("SomeBean", new MockFormBean(true));
Map map = null;
try {
map = tagutils.computeParameters(pageContext, null, null, null,
null, "SomeBean", "justThrowAnException", null, false);
fail("JspException not thrown");
} catch (JspException e) {
assertNull("map is null", map);
}
}
public void testComputeParametersParamIdParamPropThrowException() {
request.getSession().setAttribute("SomeBean", new MockFormBean(true));
Map map = null;
try {
map = tagutils.computeParameters(pageContext, "paramId",
"SomeBean", "justThrowAnException", null, null, null, null,
false);
fail("JspException not thrown");
} catch (JspException e) {
assertNull("map is null", map);
}
}
public void testComputeParametersParamValueToString() {
request.getSession().setAttribute("SomeBean",
new MockFormBean(false, false, new Double(1)));
Map map = null;
try {
map = tagutils.computeParameters(pageContext, "paramId",
"SomeBean", "doubleValue", null, null, null, null, false);
assertNotNull("map is null", map);
String val = (String) map.get("paramId");
assertTrue("paramId should be 1.0", "1.0".equals(val));
} catch (JspException e) {
fail("JspException not thrown");
}
}
public void skiptestComputeParametersParamIdAsStringArray() {
Map params = new HashMap();
// String[] vals = new String[]{"test0, test1"};
params.put("fooParamId", "fooParamValue");
request.getSession().setAttribute("SomeBean", params);
Map map = null;
try {
map = tagutils.computeParameters(pageContext, "fooParamId",
"SomeBean", null, null, "SomeBean", null, null, false);
// map = tagutils.computeParameters(
// page, "paramId", "SomeBean", "stringArray", null,
// null, null, null, false);
assertNotNull("map is null", map);
String val = (String) map.get("key0");
assertTrue("paramId should be \"test\"", "1.0".equals(val));
} catch (JspException e) {
fail("JspException not thrown");
}
}
// Single parameter -- name
public void testComputeParameters1a() {
request.getSession().setAttribute("attr", "bar");
Map map = null;
try {
map = tagutils.computeParameters(pageContext, "foo", "attr", null,
null, null, null, null, false);
} catch (JspException e) {
fail("JspException: " + e);
}
assertNotNull("Map is not null", map);
assertEquals("One parameter in the returned map", 1, map.size());
assertTrue("Parameter present", map.containsKey("foo"));
assertEquals("Parameter value", "bar", (String) map.get("foo"));
}
// Single parameter -- scope + name
public void testComputeParameters1b() {
request.setAttribute("attr", "bar");
Map map = null;
try {
map = tagutils.computeParameters(pageContext, "foo", "attr", null,
"request", null, null, null, false);
} catch (JspException e) {
fail("JspException: " + e);
}
assertNotNull("Map is not null", map);
assertEquals("One parameter in the returned map", 1, map.size());
assertTrue("Parameter present", map.containsKey("foo"));
assertEquals("Parameter value", "bar", (String) map.get("foo"));
}
// Single parameter -- scope + name + property
public void testComputeParameters1c() {
request.setAttribute("attr", new MockFormBean("bar"));
Map map = null;
try {
map = tagutils.computeParameters(pageContext, "foo", "attr",
"stringProperty", "request", null, null, null, false);
} catch (JspException e) {
fail("JspException: " + e);
}
assertNotNull("Map is not null", map);
assertEquals("One parameter in the returned map", 1, map.size());
assertTrue("Parameter present", map.containsKey("foo"));
assertEquals("Parameter value", "bar", (String) map.get("foo"));
}
// Provided map -- name
public void testComputeParameters2a() {
Map map = new HashMap();
map.put("foo1", "bar1");
map.put("foo2", "bar2");
request.getSession().setAttribute("attr", map);
try {
map = tagutils.computeParameters(pageContext, null, null, null,
null, "attr", null, null, false);
} catch (JspException e) {
fail("JspException: " + e);
}
assertNotNull("Map is not null", map);
assertEquals("Two parameter in the returned map", 2, map.size());
assertTrue("Parameter foo1 present", map.containsKey("foo1"));
assertEquals("Parameter foo1 value", "bar1", (String) map.get("foo1"));
assertTrue("Parameter foo2 present", map.containsKey("foo2"));
assertEquals("Parameter foo2 value", "bar2", (String) map.get("foo2"));
}
// Provided map -- scope + name
public void testComputeParameters2b() {
Map map = new HashMap();
map.put("foo1", "bar1");
map.put("foo2", "bar2");
request.setAttribute("attr", map);
try {
map = tagutils.computeParameters(pageContext, null, null, null,
null, "attr", null, "request", false);
} catch (JspException e) {
fail("JspException: " + e);
}
assertNotNull("Map is not null", map);
assertEquals("Two parameter in the returned map", 2, map.size());
assertTrue("Parameter foo1 present", map.containsKey("foo1"));
assertEquals("Parameter foo1 value", "bar1", (String) map.get("foo1"));
assertTrue("Parameter foo2 present", map.containsKey("foo2"));
assertEquals("Parameter foo2 value", "bar2", (String) map.get("foo2"));
}
// Provided map -- scope + name + property
public void testComputeParameters2c() {
request.setAttribute("attr", new MockFormBean());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -