View Javadoc

1   /*
2    * $Id: StrutsUtilTest.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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          // with include, this must have been created and should not be null
94          assertNotNull(request.getDispatcher());
95          // this must be true, indicating we actaully ask container to do an include
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         // this should be in xwork-messages.properties (included by default
136         // by LocalizedTextUtil
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 
173     public void testHtmlEncode() throws Exception {
174         assertEquals(
175                 strutsUtil.htmlEncode("<html><head><title>some title</title><body>some content</body></html>"),
176                 "&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;");
177     }
178 
179 
180     public void testToInt() throws Exception {
181         assertEquals(strutsUtil.toInt(11l), 11);
182     }
183 
184 
185     public void testToLong() throws Exception {
186         assertEquals(strutsUtil.toLong(11), 11l);
187     }
188 
189 
190     public void testToString() throws Exception {
191         assertEquals(strutsUtil.toString(1), "1");
192         assertEquals(strutsUtil.toString(11l), "11");
193     }
194 
195 
196     // === Junit Hook
197 
198     protected void setUp() throws Exception {
199         super.setUp();
200         stack = ActionContext.getContext().getValueStack();
201         request = new InternalMockHttpServletRequest();
202         response = new MockHttpServletResponse();
203         strutsUtil = new StrutsUtil(stack, request, response);
204     }
205 
206     protected void tearDown() throws Exception {
207         stack = null;
208         request = null;
209         response = null;
210         strutsUtil = null;
211         super.tearDown();
212     }
213 
214 
215 
216     // === internal class to assist in testing
217 
218     class InternalMockHttpServletRequest extends MockHttpServletRequest {
219         InternalMockRequestDispatcher dispatcher = null;
220         public RequestDispatcher getRequestDispatcher(String path) {
221             dispatcher = new InternalMockRequestDispatcher(path);
222             return dispatcher;
223         }
224 
225         public InternalMockRequestDispatcher getDispatcher() {
226             return dispatcher;
227         }
228     }
229 
230     class InternalMockRequestDispatcher extends MockRequestDispatcher {
231         private String url;
232         boolean included = false;
233         public InternalMockRequestDispatcher(String url) {
234             super(url);
235             this.url = url;
236         }
237         public void include(ServletRequest servletRequest, ServletResponse servletResponse) {
238             if (servletResponse instanceof MockHttpServletResponse) {
239                 ((MockHttpServletResponse) servletResponse).setIncludedUrl(this.url);
240             }
241             included = true;
242         }
243     }
244 
245 }