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.util;
23
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import javax.servlet.RequestDispatcher;
28 import javax.servlet.ServletRequest;
29 import javax.servlet.ServletResponse;
30
31 import org.apache.struts2.StrutsTestCase;
32 import org.apache.struts2.TestAction;
33 import org.apache.struts2.util.ListEntry;
34 import org.apache.struts2.util.StrutsUtil;
35 import org.springframework.mock.web.MockHttpServletRequest;
36 import org.springframework.mock.web.MockHttpServletResponse;
37 import org.springframework.mock.web.MockRequestDispatcher;
38
39 import com.opensymphony.xwork2.ActionContext;
40 import com.opensymphony.xwork2.util.ValueStack;
41 import com.opensymphony.xwork2.util.ValueStackFactory;
42
43 /***
44 * Test case for StrutsUtil.
45 *
46 */
47 public class StrutsUtilTest extends StrutsTestCase {
48
49 protected ValueStack stack = null;
50 protected InternalMockHttpServletRequest request = null;
51 protected MockHttpServletResponse response = null;
52 protected StrutsUtil strutsUtil = null;
53
54 public void testBeanMethod() throws Exception {
55 Object o = strutsUtil.bean("org.apache.struts2.TestAction");
56 assertNotNull(o);
57 assertTrue(o instanceof TestAction);
58 }
59
60 public void testIsTrueMethod() throws Exception {
61 stack.push(new Object() {
62 public String getMyString() {
63 return "myString";
64 }
65 public boolean getMyBoolean(boolean bool) {
66 return bool;
67 }
68 });
69 assertTrue(strutsUtil.isTrue("myString == 'myString'"));
70 assertFalse(strutsUtil.isTrue("myString == 'myOtherString'"));
71 assertTrue(strutsUtil.isTrue("getMyBoolean(true)"));
72 assertFalse(strutsUtil.isTrue("getMyBoolean(false)"));
73 }
74
75 public void testFindStringMethod() throws Exception {
76 stack.push(new Object() {
77 public String getMyString() {
78 return "myString";
79 }
80 public boolean getMyBoolean(boolean bool) {
81 return bool;
82 }
83 });
84
85 assertEquals(strutsUtil.findString("myString"), "myString");
86 assertNull(strutsUtil.findString("myOtherString"));
87 assertEquals(strutsUtil.findString("getMyBoolean(true)"), "true");
88 }
89
90 public void testIncludeMethod() throws Exception {
91 strutsUtil.include("/some/includedJspFile.jsp");
92
93
94 assertNotNull(request.getDispatcher());
95
96 assertTrue(request.getDispatcher().included);
97 }
98
99
100 public void testUrlEncodeMethod() throws Exception {
101 assertEquals(
102 strutsUtil.urlEncode("http://www.opensymphony.com/action2/index.jsp?param1=value1"),
103 "http%3A%2F%2Fwww.opensymphony.com%2Faction2%2Findex.jsp%3Fparam1%3Dvalue1");
104 }
105
106 public void testBuildUrlMethod() throws Exception {
107 request.setContextPath("/myContextPath");
108 assertEquals(strutsUtil.buildUrl("/someUrl?param1=value1"), "/myContextPath/someUrl?param1=value1");
109 }
110
111
112 public void testFindValueMethod() throws Exception {
113 stack.push(new Object() {
114 public String getMyString() {
115 return "myString";
116 }
117 public boolean getMyBoolean(boolean bool) {
118 return bool;
119 }
120 });
121 Object obj1 = strutsUtil.findValue("myString", "java.lang.String");
122 Object obj2 = strutsUtil.findValue("getMyBoolean(true)", "java.lang.Boolean");
123
124 assertNotNull(obj1);
125 assertNotNull(obj2);
126 assertTrue(obj1 instanceof String);
127 assertTrue(obj2 instanceof Boolean);
128 assertEquals(obj1, "myString");
129 assertEquals(obj2, Boolean.TRUE);
130 }
131
132
133
134 public void testGetTextMethod() throws Exception {
135
136
137 assertNotNull(strutsUtil.getText("xwork.error.action.execution"));
138 assertEquals(strutsUtil.getText("xwork.error.action.execution"), "Error during Action invocation");
139 }
140
141
142 public void testGetContextMethod() throws Exception {
143 request.setContextPath("/myContext");
144 assertEquals(strutsUtil.getContext(), "/myContext");
145 }
146
147
148 public void testMakeSelectListMethod() throws Exception {
149 String[] selectedList = new String[] { "Car", "Airplane", "Bus" };
150 List list = new ArrayList();
151 list.add("Lorry");
152 list.add("Car");
153 list.add("Helicopter");
154
155 stack.getContext().put("mySelectedList", selectedList);
156 stack.getContext().put("myList", list);
157
158 List listMade = strutsUtil.makeSelectList("#mySelectedList", "#myList", null, null);
159
160 assertEquals(listMade.size(), 3);
161 assertEquals(((ListEntry)listMade.get(0)).getKey(), "Lorry");
162 assertEquals(((ListEntry)listMade.get(0)).getValue(), "Lorry");
163 assertEquals(((ListEntry)listMade.get(0)).getIsSelected(), false);
164 assertEquals(((ListEntry)listMade.get(1)).getKey(), "Car");
165 assertEquals(((ListEntry)listMade.get(1)).getValue(), "Car");
166 assertEquals(((ListEntry)listMade.get(1)).getIsSelected(), true);
167 assertEquals(((ListEntry)listMade.get(2)).getKey(), "Helicopter");
168 assertEquals(((ListEntry)listMade.get(2)).getValue(), "Helicopter");
169 assertEquals(((ListEntry)listMade.get(2)).getIsSelected(), false);
170 }
171
172 public void testToInt() throws Exception {
173 assertEquals(strutsUtil.toInt(11l), 11);
174 }
175
176
177 public void testToLong() throws Exception {
178 assertEquals(strutsUtil.toLong(11), 11l);
179 }
180
181
182 public void testToString() throws Exception {
183 assertEquals(strutsUtil.toString(1), "1");
184 assertEquals(strutsUtil.toString(11l), "11");
185 }
186
187
188
189
190 protected void setUp() throws Exception {
191 super.setUp();
192 stack = ActionContext.getContext().getValueStack();
193 request = new InternalMockHttpServletRequest();
194 response = new MockHttpServletResponse();
195 strutsUtil = new StrutsUtil(stack, request, response);
196 }
197
198 protected void tearDown() throws Exception {
199 stack = null;
200 request = null;
201 response = null;
202 strutsUtil = null;
203 super.tearDown();
204 }
205
206
207
208
209
210 class InternalMockHttpServletRequest extends MockHttpServletRequest {
211 InternalMockRequestDispatcher dispatcher = null;
212 public RequestDispatcher getRequestDispatcher(String path) {
213 dispatcher = new InternalMockRequestDispatcher(path);
214 return dispatcher;
215 }
216
217 public InternalMockRequestDispatcher getDispatcher() {
218 return dispatcher;
219 }
220 }
221
222 class InternalMockRequestDispatcher extends MockRequestDispatcher {
223 private String url;
224 boolean included = false;
225 public InternalMockRequestDispatcher(String url) {
226 super(url);
227 this.url = url;
228 }
229 public void include(ServletRequest servletRequest, ServletResponse servletResponse) {
230 if (servletResponse instanceof MockHttpServletResponse) {
231 ((MockHttpServletResponse) servletResponse).setIncludedUrl(this.url);
232 }
233 included = true;
234 }
235 }
236
237 }