From d741210f2000c718fb89bd85438c7741228117b9 Mon Sep 17 00:00:00 2001 From: "Donald J. Brown" Date: Fri, 25 Jan 2008 12:05:37 +0000 Subject: [PATCH] Making retrieval of theme and templateDir more safe WW-2366 git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@615195 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/struts2/components/UIBean.java | 4 +- .../apache/struts2/components/UIBeanTest.java | 37 +++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/apache/struts2/components/UIBean.java b/core/src/main/java/org/apache/struts2/components/UIBean.java index 25c06ac9b..ef4f561da 100644 --- a/core/src/main/java/org/apache/struts2/components/UIBean.java +++ b/core/src/main/java/org/apache/struts2/components/UIBean.java @@ -552,7 +552,7 @@ public abstract class UIBean extends Component { // If templateDir is not explicitly given, // try to find attribute which states the dir set to use if ((templateDir == null) || (templateDir.equals(""))) { - templateDir = (String) stack.findValue("#attr.templateDir"); + templateDir = stack.findString("#attr.templateDir"); } // Default template set @@ -585,7 +585,7 @@ public abstract class UIBean extends Component { // If theme set is not explicitly given, // try to find attribute which states the theme set to use if ((theme == null) || (theme.equals(""))) { - theme = (String) stack.findValue("#attr.theme"); + theme = stack.findString("#attr.theme"); } // Default theme set diff --git a/core/src/test/java/org/apache/struts2/components/UIBeanTest.java b/core/src/test/java/org/apache/struts2/components/UIBeanTest.java index 2924224df..94c104789 100644 --- a/core/src/test/java/org/apache/struts2/components/UIBeanTest.java +++ b/core/src/test/java/org/apache/struts2/components/UIBeanTest.java @@ -28,6 +28,9 @@ import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.util.ValueStack; import com.opensymphony.xwork2.util.ValueStackFactory; +import java.util.Map; +import java.util.Collections; + /** * * @version $Date$ $Id$ @@ -81,4 +84,38 @@ public class UIBeanTest extends StrutsTestCase { assertEquals("formId_txtFldName", txtFld.getParameters().get("id")); } + + public void testGetThemeFromForm() throws Exception { + ValueStack stack = ActionContext.getContext().getValueStack(); + MockHttpServletRequest req = new MockHttpServletRequest(); + MockHttpServletResponse res = new MockHttpServletResponse(); + + Form form = new Form(stack, req, res); + form.setTheme("foo"); + + TextField txtFld = new TextField(stack, req, res); + assertEquals("foo", txtFld.getTheme()); + } + + public void testGetThemeFromContext() throws Exception { + ValueStack stack = ActionContext.getContext().getValueStack(); + MockHttpServletRequest req = new MockHttpServletRequest(); + MockHttpServletResponse res = new MockHttpServletResponse(); + Map context = Collections.singletonMap("theme", "bar"); + ActionContext.getContext().put("attr", context); + + TextField txtFld = new TextField(stack, req, res); + assertEquals("bar", txtFld.getTheme()); + } + + public void testGetThemeFromContextNonString() throws Exception { + ValueStack stack = ActionContext.getContext().getValueStack(); + MockHttpServletRequest req = new MockHttpServletRequest(); + MockHttpServletResponse res = new MockHttpServletResponse(); + Map context = Collections.singletonMap("theme", new Integer(12)); + ActionContext.getContext().put("attr", context); + + TextField txtFld = new TextField(stack, req, res); + assertEquals("12", txtFld.getTheme()); + } }