001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.portlet.util;
006:
007: import java.util.ArrayList;
008: import java.util.List;
009:
010: import javax.servlet.RequestDispatcher;
011: import javax.servlet.ServletRequest;
012: import javax.servlet.ServletResponse;
013:
014: import org.springframework.mock.web.MockHttpServletRequest;
015: import org.springframework.mock.web.MockHttpServletResponse;
016: import org.springframework.mock.web.MockRequestDispatcher;
017:
018: import com.opensymphony.webwork.TestAction;
019: import com.opensymphony.webwork.util.ListEntry;
020: import com.opensymphony.webwork.util.WebWorkUtil;
021: import com.opensymphony.xwork.util.OgnlValueStack;
022:
023: import junit.framework.TestCase;
024:
025: /**
026: * Test case for WebWorkUtil.
027: *
028: * @author tm_jee
029: * @version $Date: 2006-03-14 17:48:21 +0100 (Tue, 14 Mar 2006) $ $Id: WebWorkUtilTest.java 2404 2006-03-14 16:48:21Z tmjee $
030: */
031: public class WebWorkUtilTest extends TestCase {
032:
033: protected OgnlValueStack stack = null;
034: protected InternalMockHttpServletRequest request = null;
035: protected MockHttpServletResponse response = null;
036: protected WebWorkUtil webWorkUtil = null;
037:
038: public void testBeanMethod() throws Exception {
039: Object o = webWorkUtil
040: .bean("com.opensymphony.webwork.TestAction");
041: assertNotNull(o);
042: assertTrue(o instanceof TestAction);
043: }
044:
045: public void testIsTrueMethod() throws Exception {
046: stack.push(new Object() {
047: public String getMyString() {
048: return "myString";
049: }
050:
051: public boolean getMyBoolean(boolean bool) {
052: return bool;
053: }
054: });
055: assertTrue(webWorkUtil.isTrue("myString == 'myString'"));
056: assertFalse(webWorkUtil.isTrue("myString == 'myOtherString'"));
057: assertTrue(webWorkUtil.isTrue("getMyBoolean(true)"));
058: assertFalse(webWorkUtil.isTrue("getMyBoolean(false)"));
059: }
060:
061: public void testFindStringMethod() throws Exception {
062: stack.push(new Object() {
063: public String getMyString() {
064: return "myString";
065: }
066:
067: public boolean getMyBoolean(boolean bool) {
068: return bool;
069: }
070: });
071:
072: assertEquals(webWorkUtil.findString("myString"), "myString");
073: assertNull(webWorkUtil.findString("myOtherString"));
074: assertEquals(webWorkUtil.findString("getMyBoolean(true)"),
075: "true");
076: }
077:
078: public void testIncludeMethod() throws Exception {
079: webWorkUtil.include("/some/includedJspFile.jsp");
080:
081: // with include, this must have been created and should not be null
082: assertNotNull(request.getDispatcher());
083: // this must be true, indicating we actaully ask container to do an include
084: assertTrue(request.getDispatcher().included);
085: }
086:
087: public void testTextToHtmlMethod() throws Exception {
088: assertEquals(
089: webWorkUtil
090: .textToHtml("<html><head><title>some title</title><body>some content</body></html>"),
091: "<html><head><title>some title</title><body>some content</body></html>");
092: }
093:
094: public void testUrlEncodeMethod() throws Exception {
095: assertEquals(
096: webWorkUtil
097: .urlEncode("http://www.opensymphony.com/webwork/index.jsp?param1=value1"),
098: "http%3A%2F%2Fwww.opensymphony.com%2Fwebwork%2Findex.jsp%3Fparam1%3Dvalue1");
099: }
100:
101: public void testBuildUrlMethod() throws Exception {
102: request.setContextPath("/myContextPath");
103: assertEquals(webWorkUtil.buildUrl("/someUrl?param1=value1"),
104: "/myContextPath/someUrl?param1=value1");
105: }
106:
107: public void testFindValueMethod() throws Exception {
108: stack.push(new Object() {
109: public String getMyString() {
110: return "myString";
111: }
112:
113: public boolean getMyBoolean(boolean bool) {
114: return bool;
115: }
116: });
117: Object obj1 = webWorkUtil.findValue("myString",
118: "java.lang.String");
119: Object obj2 = webWorkUtil.findValue("getMyBoolean(true)",
120: "java.lang.Boolean");
121:
122: assertNotNull(obj1);
123: assertNotNull(obj2);
124: assertTrue(obj1 instanceof String);
125: assertTrue(obj2 instanceof Boolean);
126: assertEquals(obj1, "myString");
127: assertEquals(obj2, Boolean.TRUE);
128: }
129:
130: public void testGetTextMethod() throws Exception {
131: // this should be in xwork-messages.properties (included by default
132: // by LocalizedTextUtil
133: assertNotNull(webWorkUtil
134: .getText("xwork.error.action.execution"));
135: assertEquals(webWorkUtil
136: .getText("xwork.error.action.execution"),
137: "Error during Action invocation");
138: }
139:
140: public void testGetContextMethod() throws Exception {
141: request.setContextPath("/myContext");
142: assertEquals(webWorkUtil.getContext(), "/myContext");
143: }
144:
145: public void testMakeSelectListMethod() throws Exception {
146: String[] selectedList = new String[] { "Car", "Airplane", "Bus" };
147: List list = new ArrayList();
148: list.add("Lorry");
149: list.add("Car");
150: list.add("Helicopter");
151:
152: stack.getContext().put("mySelectedList", selectedList);
153: stack.getContext().put("myList", list);
154:
155: List listMade = webWorkUtil.makeSelectList("#mySelectedList",
156: "#myList", null, null);
157:
158: assertEquals(listMade.size(), 3);
159: assertEquals(((ListEntry) listMade.get(0)).getKey(), "Lorry");
160: assertEquals(((ListEntry) listMade.get(0)).getValue(), "Lorry");
161: assertEquals(((ListEntry) listMade.get(0)).getIsSelected(),
162: false);
163: assertEquals(((ListEntry) listMade.get(1)).getKey(), "Car");
164: assertEquals(((ListEntry) listMade.get(1)).getValue(), "Car");
165: assertEquals(((ListEntry) listMade.get(1)).getIsSelected(),
166: true);
167: assertEquals(((ListEntry) listMade.get(2)).getKey(),
168: "Helicopter");
169: assertEquals(((ListEntry) listMade.get(2)).getValue(),
170: "Helicopter");
171: assertEquals(((ListEntry) listMade.get(2)).getIsSelected(),
172: false);
173: }
174:
175: public void testHtmlEncode() throws Exception {
176: assertEquals(
177: webWorkUtil
178: .htmlEncode("<html><head><title>some title</title><body>some content</body></html>"),
179: "<html><head><title>some title</title><body>some content</body></html>");
180: }
181:
182: public void testToInt() throws Exception {
183: assertEquals(webWorkUtil.toInt(11l), 11);
184: }
185:
186: public void testToLong() throws Exception {
187: assertEquals(webWorkUtil.toLong(11), 11l);
188: }
189:
190: public void testToString() throws Exception {
191: assertEquals(webWorkUtil.toString(1), "1");
192: assertEquals(webWorkUtil.toString(11l), "11");
193: }
194:
195: // === Junit Hook
196:
197: protected void setUp() throws Exception {
198: super .setUp();
199: stack = new OgnlValueStack();
200: request = new InternalMockHttpServletRequest();
201: response = new MockHttpServletResponse();
202: webWorkUtil = new WebWorkUtil(stack, request, response);
203: }
204:
205: protected void tearDown() throws Exception {
206: stack = null;
207: request = null;
208: response = null;
209: webWorkUtil = null;
210: super .tearDown();
211: }
212:
213: // === internal class to assist in testing
214:
215: class InternalMockHttpServletRequest extends MockHttpServletRequest {
216: InternalMockRequestDispatcher dispatcher = null;
217:
218: public RequestDispatcher getRequestDispatcher(String path) {
219: dispatcher = new InternalMockRequestDispatcher(path);
220: return dispatcher;
221: }
222:
223: public InternalMockRequestDispatcher getDispatcher() {
224: return dispatcher;
225: }
226: }
227:
228: class InternalMockRequestDispatcher extends MockRequestDispatcher {
229: private String url;
230: boolean included = false;
231:
232: public InternalMockRequestDispatcher(String url) {
233: super (url);
234: this .url = url;
235: }
236:
237: public void include(ServletRequest servletRequest,
238: ServletResponse servletResponse) {
239: if (servletResponse instanceof MockHttpServletResponse) {
240: ((MockHttpServletResponse) servletResponse)
241: .setIncludedUrl(this .url);
242: }
243: included = true;
244: }
245: }
246:
247: }
|