View Javadoc

1   /*
2    * $Id: UIBeanTest.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.components;
23  
24  import java.util.Collections;
25  import java.util.Map;
26  
27  import org.apache.struts2.StrutsTestCase;
28  import org.springframework.mock.web.MockHttpServletRequest;
29  import org.springframework.mock.web.MockHttpServletResponse;
30  
31  import com.opensymphony.xwork2.ActionContext;
32  import com.opensymphony.xwork2.util.ValueStack;
33  
34  /***
35   *
36   * @version $Date: 2008-04-27 13:41:38 +0000 (Sun, 27 Apr 2008) $ $Id: UIBeanTest.java 651946 2008-04-27 13:41:38Z apetrelli $
37   */
38  public class UIBeanTest extends StrutsTestCase {
39  
40      public void testPopulateComponentHtmlId1() throws Exception {
41          ValueStack stack = ActionContext.getContext().getValueStack();
42          MockHttpServletRequest req = new MockHttpServletRequest();
43          MockHttpServletResponse res = new MockHttpServletResponse();
44  
45          Form form = new Form(stack, req, res);
46          form.getParameters().put("id", "formId");
47  
48          TextField txtFld = new TextField(stack, req, res);
49          txtFld.setId("txtFldId");
50  
51          txtFld.populateComponentHtmlId(form);
52  
53          assertEquals("txtFldId", txtFld.getParameters().get("id"));
54      }
55  
56      public void testPopulateComponentHtmlIdWithOgnl() throws Exception {
57          ValueStack stack = ActionContext.getContext().getValueStack();
58          MockHttpServletRequest req = new MockHttpServletRequest();
59          MockHttpServletResponse res = new MockHttpServletResponse();
60  
61          Form form = new Form(stack, req, res);
62          form.getParameters().put("id", "formId");
63  
64          TextField txtFld = new TextField(stack, req, res);
65          txtFld.setName("txtFldName%{'1'}");
66  
67          txtFld.populateComponentHtmlId(form);
68  
69          assertEquals("formId_txtFldName1", txtFld.getParameters().get("id"));
70      }
71  
72      public void testPopulateComponentHtmlId2() throws Exception {
73          ValueStack stack = ActionContext.getContext().getValueStack();
74          MockHttpServletRequest req = new MockHttpServletRequest();
75          MockHttpServletResponse res = new MockHttpServletResponse();
76  
77          Form form = new Form(stack, req, res);
78          form.getParameters().put("id", "formId");
79  
80          TextField txtFld = new TextField(stack, req, res);
81          txtFld.setName("txtFldName");
82  
83          txtFld.populateComponentHtmlId(form);
84  
85          assertEquals("formId_txtFldName", txtFld.getParameters().get("id"));
86      }
87  
88      public void testEscape() throws Exception {
89          ValueStack stack = ActionContext.getContext().getValueStack();
90          MockHttpServletRequest req = new MockHttpServletRequest();
91          MockHttpServletResponse res = new MockHttpServletResponse();
92          UIBean bean = new UIBean(stack, req, res) {
93              protected String getDefaultTemplate() {
94                  return null;
95              }
96          };
97  
98          assertEquals(bean.escape("hello[world"), "hello_world");
99          assertEquals(bean.escape("hello.world"), "hello_world");
100         assertEquals(bean.escape("hello]world"), "hello_world");
101         assertEquals(bean.escape("hello!world"), "hello_world");
102         assertEquals(bean.escape("hello!@#$%^&*()world"), "hello__________world");
103     }
104 
105     public void testEscapeId() throws Exception {
106         ValueStack stack = ActionContext.getContext().getValueStack();
107         MockHttpServletRequest req = new MockHttpServletRequest();
108         MockHttpServletResponse res = new MockHttpServletResponse();
109 
110         Form form = new Form(stack, req, res);
111         form.getParameters().put("id", "formId");
112 
113         TextField txtFld = new TextField(stack, req, res);
114         txtFld.setName("foo/bar");
115         txtFld.populateComponentHtmlId(form);
116         assertEquals("formId_foo_bar", txtFld.getParameters().get("id"));
117     }
118 
119     public void testGetThemeFromForm() throws Exception {
120         ValueStack stack = ActionContext.getContext().getValueStack();
121         MockHttpServletRequest req = new MockHttpServletRequest();
122         MockHttpServletResponse res = new MockHttpServletResponse();
123 
124         Form form = new Form(stack, req, res);
125         form.setTheme("foo");
126 
127         TextField txtFld = new TextField(stack, req, res);
128         assertEquals("foo", txtFld.getTheme());
129     }
130 
131     public void testGetThemeFromContext() throws Exception {
132         ValueStack stack = ActionContext.getContext().getValueStack();
133         MockHttpServletRequest req = new MockHttpServletRequest();
134         MockHttpServletResponse res = new MockHttpServletResponse();
135         Map context = Collections.singletonMap("theme", "bar");
136         ActionContext.getContext().put("attr", context);
137 
138         TextField txtFld = new TextField(stack, req, res);
139         assertEquals("bar", txtFld.getTheme());
140     }
141 
142     public void testGetThemeFromContextNonString() throws Exception {
143         ValueStack stack = ActionContext.getContext().getValueStack();
144         MockHttpServletRequest req = new MockHttpServletRequest();
145         MockHttpServletResponse res = new MockHttpServletResponse();
146         Map context = Collections.singletonMap("theme", new Integer(12));
147         ActionContext.getContext().put("attr", context);
148 
149         TextField txtFld = new TextField(stack, req, res);
150         assertEquals("12", txtFld.getTheme());
151     }
152 
153 //    I couldn't figure out how to make this test work. Bailing for now.
154 //    public void testEscapeLabel() throws Exception {
155 //        ValueStack stack = ActionContext.getContext().getValueStack();
156 //        MockHttpServletRequest req = new MockHttpServletRequest();
157 //        MockHttpServletResponse res = new MockHttpServletResponse();
158 //        stack.push(this);
159 //
160 //        TextField txtFld = new TextField(stack, req, res);
161 //        txtFld.setKey("test['foo']");
162 //        txtFld.evaluateParams();
163 //        assertEquals("test_label", txtFld.getParameters().get("label"));
164 //    }
165 //
166 //    public String getText(String key) {
167 //        assertEquals("test[//'foo//']", key);
168 //        return "test_label";
169 //    }
170 }