View Javadoc

1   /*
2    * $Id: StrutsUtilTest.java 471756 2006-11-06 15:01:43Z husted $
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  package org.apache.struts2.portlet.util;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import javax.servlet.RequestDispatcher;
27  import javax.servlet.ServletRequest;
28  import javax.servlet.ServletResponse;
29  
30  import org.apache.struts2.StrutsTestCase;
31  import org.apache.struts2.TestAction;
32  import org.apache.struts2.util.ListEntry;
33  import org.apache.struts2.util.StrutsUtil;
34  import org.springframework.mock.web.MockHttpServletRequest;
35  import org.springframework.mock.web.MockHttpServletResponse;
36  import org.springframework.mock.web.MockRequestDispatcher;
37  
38  import com.opensymphony.xwork2.util.ValueStack;
39  import com.opensymphony.xwork2.util.ValueStackFactory;
40  
41  /***
42   * Test case for StrutsUtil.
43   *
44   */
45  public class StrutsUtilTest extends StrutsTestCase {
46  
47      protected ValueStack stack = null;
48      protected InternalMockHttpServletRequest request = null;
49      protected MockHttpServletResponse response = null;
50      protected StrutsUtil strutsUtil = null;
51  
52      public void testBeanMethod() throws Exception {
53          Object o = strutsUtil.bean("org.apache.struts2.TestAction");
54          assertNotNull(o);
55          assertTrue(o instanceof TestAction);
56      }
57  
58      public void testIsTrueMethod() throws Exception {
59          stack.push(new Object() {
60              public String getMyString() {
61                  return "myString";
62              }
63              public boolean getMyBoolean(boolean bool) {
64                  return bool;
65              }
66          });
67          assertTrue(strutsUtil.isTrue("myString == 'myString'"));
68          assertFalse(strutsUtil.isTrue("myString == 'myOtherString'"));
69          assertTrue(strutsUtil.isTrue("getMyBoolean(true)"));
70          assertFalse(strutsUtil.isTrue("getMyBoolean(false)"));
71      }
72  
73      public void testFindStringMethod() throws Exception {
74          stack.push(new Object() {
75              public String getMyString() {
76                  return "myString";
77              }
78              public boolean getMyBoolean(boolean bool) {
79                  return bool;
80              }
81          });
82  
83          assertEquals(strutsUtil.findString("myString"), "myString");
84          assertNull(strutsUtil.findString("myOtherString"));
85          assertEquals(strutsUtil.findString("getMyBoolean(true)"), "true");
86      }
87  
88      public void testIncludeMethod() throws Exception {
89          strutsUtil.include("/some/includedJspFile.jsp");
90  
91          // with include, this must have been created and should not be null
92          assertNotNull(request.getDispatcher());
93          // this must be true, indicating we actaully ask container to do an include
94          assertTrue(request.getDispatcher().included);
95      }
96  
97  
98      public void testUrlEncodeMethod() throws Exception {
99          assertEquals(
100                 strutsUtil.urlEncode("http://www.opensymphony.com/action2/index.jsp?param1=value1"),
101                 "http%3A%2F%2Fwww.opensymphony.com%2Faction2%2Findex.jsp%3Fparam1%3Dvalue1");
102     }
103 
104     public void testBuildUrlMethod() throws Exception {
105         request.setContextPath("/myContextPath");
106         assertEquals(strutsUtil.buildUrl("/someUrl?param1=value1"), "/myContextPath/someUrl?param1=value1");
107     }
108 
109 
110     public void testFindValueMethod() throws Exception {
111         stack.push(new Object() {
112             public String getMyString() {
113                 return "myString";
114             }
115             public boolean getMyBoolean(boolean bool) {
116                 return bool;
117             }
118         });
119         Object obj1 = strutsUtil.findValue("myString", "java.lang.String");
120         Object obj2 = strutsUtil.findValue("getMyBoolean(true)", "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 
131 
132     public void testGetTextMethod() throws Exception {
133         // this should be in xwork-messages.properties (included by default
134         // by LocalizedTextUtil
135         assertNotNull(strutsUtil.getText("xwork.error.action.execution"));
136         assertEquals(strutsUtil.getText("xwork.error.action.execution"), "Error during Action invocation");
137     }
138 
139 
140     public void testGetContextMethod() throws Exception {
141         request.setContextPath("/myContext");
142         assertEquals(strutsUtil.getContext(), "/myContext");
143     }
144 
145 
146     public void testMakeSelectListMethod() throws Exception {
147         String[] selectedList = new String[] { "Car", "Airplane", "Bus" };
148         List list = new ArrayList();
149         list.add("Lorry");
150         list.add("Car");
151         list.add("Helicopter");
152 
153         stack.getContext().put("mySelectedList", selectedList);
154         stack.getContext().put("myList", list);
155 
156         List listMade = strutsUtil.makeSelectList("#mySelectedList", "#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(), false);
162         assertEquals(((ListEntry)listMade.get(1)).getKey(), "Car");
163         assertEquals(((ListEntry)listMade.get(1)).getValue(), "Car");
164         assertEquals(((ListEntry)listMade.get(1)).getIsSelected(), true);
165         assertEquals(((ListEntry)listMade.get(2)).getKey(), "Helicopter");
166         assertEquals(((ListEntry)listMade.get(2)).getValue(), "Helicopter");
167         assertEquals(((ListEntry)listMade.get(2)).getIsSelected(), false);
168     }
169 
170 
171     public void testHtmlEncode() throws Exception {
172         assertEquals(
173                 strutsUtil.htmlEncode("<html><head><title>some title</title><body>some content</body></html>"),
174                 "&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;");
175     }
176 
177 
178     public void testToInt() throws Exception {
179         assertEquals(strutsUtil.toInt(11l), 11);
180     }
181 
182 
183     public void testToLong() throws Exception {
184         assertEquals(strutsUtil.toLong(11), 11l);
185     }
186 
187 
188     public void testToString() throws Exception {
189         assertEquals(strutsUtil.toString(1), "1");
190         assertEquals(strutsUtil.toString(11l), "11");
191     }
192 
193 
194     // === Junit Hook
195 
196     protected void setUp() throws Exception {
197         super.setUp();
198         stack = ValueStackFactory.getFactory().createValueStack();
199         request = new InternalMockHttpServletRequest();
200         response = new MockHttpServletResponse();
201         strutsUtil = new StrutsUtil(stack, request, response);
202     }
203 
204     protected void tearDown() throws Exception {
205         stack = null;
206         request = null;
207         response = null;
208         strutsUtil = null;
209         super.tearDown();
210     }
211 
212 
213 
214     // === internal class to assist in testing
215 
216     class InternalMockHttpServletRequest extends MockHttpServletRequest {
217         InternalMockRequestDispatcher dispatcher = null;
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         public InternalMockRequestDispatcher(String url) {
232             super(url);
233             this.url = url;
234         }
235         public void include(ServletRequest servletRequest, ServletResponse servletResponse) {
236             if (servletResponse instanceof MockHttpServletResponse) {
237                 ((MockHttpServletResponse) servletResponse).setIncludedUrl(this.url);
238             }
239             included = true;
240         }
241     }
242 
243 }