001: /*
002: * $Id: StrutsUtilTest.java 471756 2006-11-06 15:01:43Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.portlet.util;
022:
023: import java.util.ArrayList;
024: import java.util.List;
025:
026: import javax.servlet.RequestDispatcher;
027: import javax.servlet.ServletRequest;
028: import javax.servlet.ServletResponse;
029:
030: import org.apache.struts2.StrutsTestCase;
031: import org.apache.struts2.TestAction;
032: import org.apache.struts2.util.ListEntry;
033: import org.apache.struts2.util.StrutsUtil;
034: import org.springframework.mock.web.MockHttpServletRequest;
035: import org.springframework.mock.web.MockHttpServletResponse;
036: import org.springframework.mock.web.MockRequestDispatcher;
037:
038: import com.opensymphony.xwork2.util.ValueStack;
039: import com.opensymphony.xwork2.util.ValueStackFactory;
040:
041: /**
042: * Test case for StrutsUtil.
043: *
044: */
045: public class StrutsUtilTest extends StrutsTestCase {
046:
047: protected ValueStack stack = null;
048: protected InternalMockHttpServletRequest request = null;
049: protected MockHttpServletResponse response = null;
050: protected StrutsUtil strutsUtil = null;
051:
052: public void testBeanMethod() throws Exception {
053: Object o = strutsUtil.bean("org.apache.struts2.TestAction");
054: assertNotNull(o);
055: assertTrue(o instanceof TestAction);
056: }
057:
058: public void testIsTrueMethod() throws Exception {
059: stack.push(new Object() {
060: public String getMyString() {
061: return "myString";
062: }
063:
064: public boolean getMyBoolean(boolean bool) {
065: return bool;
066: }
067: });
068: assertTrue(strutsUtil.isTrue("myString == 'myString'"));
069: assertFalse(strutsUtil.isTrue("myString == 'myOtherString'"));
070: assertTrue(strutsUtil.isTrue("getMyBoolean(true)"));
071: assertFalse(strutsUtil.isTrue("getMyBoolean(false)"));
072: }
073:
074: public void testFindStringMethod() throws Exception {
075: stack.push(new Object() {
076: public String getMyString() {
077: return "myString";
078: }
079:
080: public boolean getMyBoolean(boolean bool) {
081: return bool;
082: }
083: });
084:
085: assertEquals(strutsUtil.findString("myString"), "myString");
086: assertNull(strutsUtil.findString("myOtherString"));
087: assertEquals(strutsUtil.findString("getMyBoolean(true)"),
088: "true");
089: }
090:
091: public void testIncludeMethod() throws Exception {
092: strutsUtil.include("/some/includedJspFile.jsp");
093:
094: // with include, this must have been created and should not be null
095: assertNotNull(request.getDispatcher());
096: // this must be true, indicating we actaully ask container to do an include
097: assertTrue(request.getDispatcher().included);
098: }
099:
100: public void testUrlEncodeMethod() throws Exception {
101: assertEquals(
102: strutsUtil
103: .urlEncode("http://www.opensymphony.com/action2/index.jsp?param1=value1"),
104: "http%3A%2F%2Fwww.opensymphony.com%2Faction2%2Findex.jsp%3Fparam1%3Dvalue1");
105: }
106:
107: public void testBuildUrlMethod() throws Exception {
108: request.setContextPath("/myContextPath");
109: assertEquals(strutsUtil.buildUrl("/someUrl?param1=value1"),
110: "/myContextPath/someUrl?param1=value1");
111: }
112:
113: public void testFindValueMethod() throws Exception {
114: stack.push(new Object() {
115: public String getMyString() {
116: return "myString";
117: }
118:
119: public boolean getMyBoolean(boolean bool) {
120: return bool;
121: }
122: });
123: Object obj1 = strutsUtil.findValue("myString",
124: "java.lang.String");
125: Object obj2 = strutsUtil.findValue("getMyBoolean(true)",
126: "java.lang.Boolean");
127:
128: assertNotNull(obj1);
129: assertNotNull(obj2);
130: assertTrue(obj1 instanceof String);
131: assertTrue(obj2 instanceof Boolean);
132: assertEquals(obj1, "myString");
133: assertEquals(obj2, Boolean.TRUE);
134: }
135:
136: public void testGetTextMethod() throws Exception {
137: // this should be in xwork-messages.properties (included by default
138: // by LocalizedTextUtil
139: assertNotNull(strutsUtil
140: .getText("xwork.error.action.execution"));
141: assertEquals(
142: strutsUtil.getText("xwork.error.action.execution"),
143: "Error during Action invocation");
144: }
145:
146: public void testGetContextMethod() throws Exception {
147: request.setContextPath("/myContext");
148: assertEquals(strutsUtil.getContext(), "/myContext");
149: }
150:
151: public void testMakeSelectListMethod() throws Exception {
152: String[] selectedList = new String[] { "Car", "Airplane", "Bus" };
153: List list = new ArrayList();
154: list.add("Lorry");
155: list.add("Car");
156: list.add("Helicopter");
157:
158: stack.getContext().put("mySelectedList", selectedList);
159: stack.getContext().put("myList", list);
160:
161: List listMade = strutsUtil.makeSelectList("#mySelectedList",
162: "#myList", null, null);
163:
164: assertEquals(listMade.size(), 3);
165: assertEquals(((ListEntry) listMade.get(0)).getKey(), "Lorry");
166: assertEquals(((ListEntry) listMade.get(0)).getValue(), "Lorry");
167: assertEquals(((ListEntry) listMade.get(0)).getIsSelected(),
168: false);
169: assertEquals(((ListEntry) listMade.get(1)).getKey(), "Car");
170: assertEquals(((ListEntry) listMade.get(1)).getValue(), "Car");
171: assertEquals(((ListEntry) listMade.get(1)).getIsSelected(),
172: true);
173: assertEquals(((ListEntry) listMade.get(2)).getKey(),
174: "Helicopter");
175: assertEquals(((ListEntry) listMade.get(2)).getValue(),
176: "Helicopter");
177: assertEquals(((ListEntry) listMade.get(2)).getIsSelected(),
178: false);
179: }
180:
181: public void testHtmlEncode() throws Exception {
182: assertEquals(
183: strutsUtil
184: .htmlEncode("<html><head><title>some title</title><body>some content</body></html>"),
185: "<html><head><title>some title</title><body>some content</body></html>");
186: }
187:
188: public void testToInt() throws Exception {
189: assertEquals(strutsUtil.toInt(11l), 11);
190: }
191:
192: public void testToLong() throws Exception {
193: assertEquals(strutsUtil.toLong(11), 11l);
194: }
195:
196: public void testToString() throws Exception {
197: assertEquals(strutsUtil.toString(1), "1");
198: assertEquals(strutsUtil.toString(11l), "11");
199: }
200:
201: // === Junit Hook
202:
203: protected void setUp() throws Exception {
204: super .setUp();
205: stack = ValueStackFactory.getFactory().createValueStack();
206: request = new InternalMockHttpServletRequest();
207: response = new MockHttpServletResponse();
208: strutsUtil = new StrutsUtil(stack, request, response);
209: }
210:
211: protected void tearDown() throws Exception {
212: stack = null;
213: request = null;
214: response = null;
215: strutsUtil = null;
216: super .tearDown();
217: }
218:
219: // === internal class to assist in testing
220:
221: class InternalMockHttpServletRequest extends MockHttpServletRequest {
222: InternalMockRequestDispatcher dispatcher = null;
223:
224: public RequestDispatcher getRequestDispatcher(String path) {
225: dispatcher = new InternalMockRequestDispatcher(path);
226: return dispatcher;
227: }
228:
229: public InternalMockRequestDispatcher getDispatcher() {
230: return dispatcher;
231: }
232: }
233:
234: class InternalMockRequestDispatcher extends MockRequestDispatcher {
235: private String url;
236: boolean included = false;
237:
238: public InternalMockRequestDispatcher(String url) {
239: super (url);
240: this .url = url;
241: }
242:
243: public void include(ServletRequest servletRequest,
244: ServletResponse servletResponse) {
245: if (servletResponse instanceof MockHttpServletResponse) {
246: ((MockHttpServletResponse) servletResponse)
247: .setIncludedUrl(this .url);
248: }
249: included = true;
250: }
251: }
252:
253: }
|