View Javadoc

1   /*
2    * $Id: StrutsUtilTest.java 454565 2006-10-10 00:02:56Z jmitchell $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.portlet.util;
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import javax.servlet.RequestDispatcher;
24  import javax.servlet.ServletRequest;
25  import javax.servlet.ServletResponse;
26  
27  import org.apache.struts2.StrutsTestCase;
28  import org.apache.struts2.TestAction;
29  import org.apache.struts2.util.ListEntry;
30  import org.apache.struts2.util.StrutsUtil;
31  import org.springframework.mock.web.MockHttpServletRequest;
32  import org.springframework.mock.web.MockHttpServletResponse;
33  import org.springframework.mock.web.MockRequestDispatcher;
34  
35  import com.opensymphony.xwork2.util.ValueStack;
36  import com.opensymphony.xwork2.util.ValueStackFactory;
37  
38  /***
39   * Test case for StrutsUtil.
40   * 
41   */
42  public class StrutsUtilTest extends StrutsTestCase {
43  	
44  	protected ValueStack stack = null;
45  	protected InternalMockHttpServletRequest request = null;
46  	protected MockHttpServletResponse response = null;
47  	protected StrutsUtil strutsUtil = null;
48  	
49  	public void testBeanMethod() throws Exception {
50  		Object o = strutsUtil.bean("org.apache.struts2.TestAction");
51  		assertNotNull(o);
52  		assertTrue(o instanceof TestAction);
53  	}
54  	
55  	public void testIsTrueMethod() throws Exception {
56  		stack.push(new Object() {
57  			public String getMyString() {
58  				return "myString";
59  			}
60  			public boolean getMyBoolean(boolean bool) {
61  				return bool;
62  			}
63  		});
64  		assertTrue(strutsUtil.isTrue("myString == 'myString'"));
65  		assertFalse(strutsUtil.isTrue("myString == 'myOtherString'"));
66  		assertTrue(strutsUtil.isTrue("getMyBoolean(true)"));
67  		assertFalse(strutsUtil.isTrue("getMyBoolean(false)"));
68  	}
69  	
70  	public void testFindStringMethod() throws Exception {
71  		stack.push(new Object() {
72  			public String getMyString() {
73  				return "myString";
74  			}
75  			public boolean getMyBoolean(boolean bool) {
76  				return bool;
77  			}
78  		});
79  		
80  		assertEquals(strutsUtil.findString("myString"), "myString");
81  		assertNull(strutsUtil.findString("myOtherString"));
82  		assertEquals(strutsUtil.findString("getMyBoolean(true)"), "true");
83  	}
84  	
85  	public void testIncludeMethod() throws Exception {
86  		strutsUtil.include("/some/includedJspFile.jsp");
87  		
88  		// with include, this must have been created and should not be null
89  		assertNotNull(request.getDispatcher()); 
90  		// this must be true, indicating we actaully ask container to do an include
91  		assertTrue(request.getDispatcher().included);
92  	}
93  	
94  	
95  	public void testUrlEncodeMethod() throws Exception {
96  		assertEquals(
97  				strutsUtil.urlEncode("http://www.opensymphony.com/action2/index.jsp?param1=value1"), 
98  				"http%3A%2F%2Fwww.opensymphony.com%2Faction2%2Findex.jsp%3Fparam1%3Dvalue1");
99  	}
100 	
101 	public void testBuildUrlMethod() throws Exception {
102 		request.setContextPath("/myContextPath");
103 		assertEquals(strutsUtil.buildUrl("/someUrl?param1=value1"), "/myContextPath/someUrl?param1=value1");
104 	}
105 	
106 	
107 	public void testFindValueMethod() throws Exception {
108 		stack.push(new Object() {
109 			public String getMyString() {
110 				return "myString";
111 			}
112 			public boolean getMyBoolean(boolean bool) {
113 				return bool;
114 			}
115 		});
116 		Object obj1 = strutsUtil.findValue("myString", "java.lang.String");
117 		Object obj2 = strutsUtil.findValue("getMyBoolean(true)", "java.lang.Boolean");
118 		
119 		assertNotNull(obj1);
120 		assertNotNull(obj2);
121 		assertTrue(obj1 instanceof String);
122 		assertTrue(obj2 instanceof Boolean);
123 		assertEquals(obj1, "myString");
124 		assertEquals(obj2, Boolean.TRUE);
125 	}
126 	
127 	
128 
129 	public void testGetTextMethod() throws Exception {
130 		// this should be in xwork-messages.properties (included by default 
131 		// by LocalizedTextUtil
132 		assertNotNull(strutsUtil.getText("xwork.error.action.execution"));
133 		assertEquals(strutsUtil.getText("xwork.error.action.execution"), "Error during Action invocation");
134 	}
135 	
136 	
137 	public void testGetContextMethod() throws Exception {
138 		request.setContextPath("/myContext");
139 		assertEquals(strutsUtil.getContext(), "/myContext");
140 	}
141 	
142 	
143 	public void testMakeSelectListMethod() throws Exception {
144 		String[] selectedList = new String[] { "Car", "Airplane", "Bus" };
145 		List list = new ArrayList();
146 		list.add("Lorry");
147 		list.add("Car");
148 		list.add("Helicopter");
149 		
150 		stack.getContext().put("mySelectedList", selectedList);
151 		stack.getContext().put("myList", list);
152 		
153 		List listMade = strutsUtil.makeSelectList("#mySelectedList", "#myList", null, null);
154 		
155 		assertEquals(listMade.size(), 3);
156 		assertEquals(((ListEntry)listMade.get(0)).getKey(), "Lorry");
157 		assertEquals(((ListEntry)listMade.get(0)).getValue(), "Lorry");
158 		assertEquals(((ListEntry)listMade.get(0)).getIsSelected(), false);
159 		assertEquals(((ListEntry)listMade.get(1)).getKey(), "Car");
160 		assertEquals(((ListEntry)listMade.get(1)).getValue(), "Car");
161 		assertEquals(((ListEntry)listMade.get(1)).getIsSelected(), true);
162 		assertEquals(((ListEntry)listMade.get(2)).getKey(), "Helicopter");
163 		assertEquals(((ListEntry)listMade.get(2)).getValue(), "Helicopter");
164 		assertEquals(((ListEntry)listMade.get(2)).getIsSelected(), false);
165 	}
166 	
167 	
168 	public void testHtmlEncode() throws Exception {
169 		assertEquals(
170 				strutsUtil.htmlEncode("<html><head><title>some title</title><body>some content</body></html>"), 
171 				"&lt;html&gt;&lt;head&gt;&lt;title&gt;some title&lt;/title&gt;&lt;body&gt;some content&lt;/body&gt;&lt;/html&gt;");
172 	}
173 	
174 
175 	public void testToInt() throws Exception {
176 		assertEquals(strutsUtil.toInt(11l), 11);
177 	}
178 	
179 	
180 	public void testToLong() throws Exception {
181 		assertEquals(strutsUtil.toLong(11), 11l);
182 	}
183 	
184 	
185 	public void testToString() throws Exception {
186 		assertEquals(strutsUtil.toString(1), "1");
187 		assertEquals(strutsUtil.toString(11l), "11");
188 	}
189 	
190 	
191 	// === Junit Hook
192 	
193 	protected void setUp() throws Exception {
194 		super.setUp();
195 		stack = ValueStackFactory.getFactory().createValueStack();
196 		request = new InternalMockHttpServletRequest();
197 		response = new MockHttpServletResponse();
198 		strutsUtil = new StrutsUtil(stack, request, response);
199 	}
200 	
201 	protected void tearDown() throws Exception {
202 		stack = null;
203 		request = null;
204 		response = null;
205 		strutsUtil = null;
206 		super.tearDown();
207 	}
208 	
209 	
210 	
211 	// === internal class to assist in testing
212 	
213 	class InternalMockHttpServletRequest extends MockHttpServletRequest {
214 		InternalMockRequestDispatcher dispatcher = null;
215 		public RequestDispatcher getRequestDispatcher(String path) {
216 			dispatcher = new InternalMockRequestDispatcher(path);
217 			return dispatcher;
218 		}
219 		
220 		public InternalMockRequestDispatcher getDispatcher() { 
221 			return dispatcher;
222 		}
223 	}
224 	
225 	class InternalMockRequestDispatcher extends MockRequestDispatcher {
226 		private String url;
227 		boolean included = false;
228 		public InternalMockRequestDispatcher(String url) {
229 			super(url);
230 			this.url = url;
231 		}
232 		public void include(ServletRequest servletRequest, ServletResponse servletResponse) {
233 			if (servletResponse instanceof MockHttpServletResponse) {
234 				((MockHttpServletResponse) servletResponse).setIncludedUrl(this.url);
235 			}
236 			included = true;
237 		}
238 	}
239 	
240 }