1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.components;
23
24 import java.util.Collections;
25 import java.util.Map;
26
27 import org.apache.struts2.StrutsTestCase;
28 import org.springframework.mock.web.MockHttpServletRequest;
29 import org.springframework.mock.web.MockHttpServletResponse;
30
31 import com.opensymphony.xwork2.ActionContext;
32 import com.opensymphony.xwork2.util.ValueStack;
33
34 /***
35 *
36 * @version $Date: 2009-03-16 16:09:51 -0400 (Mon, 16 Mar 2009) $ $Id: UIBeanTest.java 754994 2009-03-16 20:09:51Z musachy $
37 */
38 public class UIBeanTest extends StrutsTestCase {
39
40 public void testPopulateComponentHtmlId1() throws Exception {
41 ValueStack stack = ActionContext.getContext().getValueStack();
42 MockHttpServletRequest req = new MockHttpServletRequest();
43 MockHttpServletResponse res = new MockHttpServletResponse();
44
45 Form form = new Form(stack, req, res);
46 form.getParameters().put("id", "formId");
47
48 TextField txtFld = new TextField(stack, req, res);
49 txtFld.setId("txtFldId");
50
51 txtFld.populateComponentHtmlId(form);
52
53 assertEquals("txtFldId", txtFld.getParameters().get("id"));
54 }
55
56 public void testPopulateComponentHtmlIdWithOgnl() throws Exception {
57 ValueStack stack = ActionContext.getContext().getValueStack();
58 MockHttpServletRequest req = new MockHttpServletRequest();
59 MockHttpServletResponse res = new MockHttpServletResponse();
60
61 Form form = new Form(stack, req, res);
62 form.getParameters().put("id", "formId");
63
64 TextField txtFld = new TextField(stack, req, res);
65 txtFld.setName("txtFldName%{'1'}");
66
67 txtFld.populateComponentHtmlId(form);
68
69 assertEquals("formId_txtFldName1", txtFld.getParameters().get("id"));
70 }
71
72 public void testPopulateComponentHtmlId2() throws Exception {
73 ValueStack stack = ActionContext.getContext().getValueStack();
74 MockHttpServletRequest req = new MockHttpServletRequest();
75 MockHttpServletResponse res = new MockHttpServletResponse();
76
77 Form form = new Form(stack, req, res);
78 form.getParameters().put("id", "formId");
79
80 TextField txtFld = new TextField(stack, req, res);
81 txtFld.setName("txtFldName");
82
83 txtFld.populateComponentHtmlId(form);
84
85 assertEquals("formId_txtFldName", txtFld.getParameters().get("id"));
86 }
87
88 public void testEscape() throws Exception {
89 ValueStack stack = ActionContext.getContext().getValueStack();
90 MockHttpServletRequest req = new MockHttpServletRequest();
91 MockHttpServletResponse res = new MockHttpServletResponse();
92 UIBean bean = new UIBean(stack, req, res) {
93 protected String getDefaultTemplate() {
94 return null;
95 }
96 };
97
98 assertEquals(bean.escape("hello[world"), "hello_world");
99 assertEquals(bean.escape("hello.world"), "hello_world");
100 assertEquals(bean.escape("hello]world"), "hello_world");
101 assertEquals(bean.escape("hello!world"), "hello!world");
102 assertEquals(bean.escape("hello!@#$%^&*()world"), "hello!@#$%^&*()world");
103 }
104
105 public void testEscapeId() throws Exception {
106 ValueStack stack = ActionContext.getContext().getValueStack();
107 MockHttpServletRequest req = new MockHttpServletRequest();
108 MockHttpServletResponse res = new MockHttpServletResponse();
109
110 Form form = new Form(stack, req, res);
111 form.getParameters().put("id", "formId");
112
113 TextField txtFld = new TextField(stack, req, res);
114 txtFld.setName("foo/bar");
115 txtFld.populateComponentHtmlId(form);
116 assertEquals("formId_foo_bar", txtFld.getParameters().get("id"));
117 }
118
119 public void testGetThemeFromForm() throws Exception {
120 ValueStack stack = ActionContext.getContext().getValueStack();
121 MockHttpServletRequest req = new MockHttpServletRequest();
122 MockHttpServletResponse res = new MockHttpServletResponse();
123
124 Form form = new Form(stack, req, res);
125 form.setTheme("foo");
126
127 TextField txtFld = new TextField(stack, req, res);
128 assertEquals("foo", txtFld.getTheme());
129 }
130
131 public void testGetThemeFromContext() throws Exception {
132 ValueStack stack = ActionContext.getContext().getValueStack();
133 MockHttpServletRequest req = new MockHttpServletRequest();
134 MockHttpServletResponse res = new MockHttpServletResponse();
135 Map context = Collections.singletonMap("theme", "bar");
136 ActionContext.getContext().put("attr", context);
137
138 TextField txtFld = new TextField(stack, req, res);
139 assertEquals("bar", txtFld.getTheme());
140 }
141
142 public void testGetThemeFromContextNonString() throws Exception {
143 ValueStack stack = ActionContext.getContext().getValueStack();
144 MockHttpServletRequest req = new MockHttpServletRequest();
145 MockHttpServletResponse res = new MockHttpServletResponse();
146 Map context = Collections.singletonMap("theme", new Integer(12));
147 ActionContext.getContext().put("attr", context);
148
149 TextField txtFld = new TextField(stack, req, res);
150 assertEquals("12", txtFld.getTheme());
151 }
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170 }