View Javadoc

1   /*
2    * $Id: TextfieldTest.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.views.jsp.ui;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import org.apache.struts2.TestAction;
28  import org.apache.struts2.views.freemarker.tags.TextFieldModel;
29  import org.apache.struts2.views.jsp.AbstractUITagTest;
30  
31  import freemarker.template.TransformControl;
32  
33  
34  /***
35   */
36  public class TextfieldTest extends AbstractUITagTest {
37  
38      /***
39       * Initialize a map of {@link org.apache.struts2.views.jsp.AbstractUITagTest.PropertyHolder} for generic tag
40       * property testing. Will be used when calling {@link #verifyGenericProperties(org.apache.struts2.views.jsp.ui.AbstractUITag,
41       * String, String[])} as properties to verify.<p/> This implementation extends testdata from AbstractUITag.
42       *
43       * @return A Map of PropertyHolders values bound to {@link org.apache.struts2.views.jsp.AbstractUITagTest.PropertyHolder#getName()}
44       *         as key.
45       */
46      protected Map initializedGenericTagTestProperties() {
47          Map result = super.initializedGenericTagTestProperties();
48          new PropertyHolder("maxlength", "10").addToMap(result);
49          new PropertyHolder("readonly", "true", "readonly=\"readonly\"").addToMap(result);
50          new PropertyHolder("size", "12").addToMap(result);
51          return result;
52      }
53  
54      public void testGenericSimple() throws Exception {
55          TextFieldTag tag = new TextFieldTag();
56          verifyGenericProperties(tag, "simple", null);
57      }
58  
59      public void testGenericXhtml() throws Exception {
60          TextFieldTag tag = new TextFieldTag();
61          verifyGenericProperties(tag, "xhtml", null);
62      }
63  
64      public void testErrors() throws Exception {
65          TestAction testAction = (TestAction) action;
66          testAction.setFoo("bar");
67  
68          TextFieldTag tag = new TextFieldTag();
69          tag.setPageContext(pageContext);
70          tag.setId("myId");
71          tag.setLabel("mylabel");
72          tag.setName("foo");
73          tag.setValue("bar");
74          tag.setTitle("mytitle");
75  
76          testAction.addFieldError("foo", "bar error message");
77          tag.doStartTag();
78          tag.doEndTag();
79  
80          verify(TextFieldTag.class.getResource("Textfield-2.txt"));
81      }
82  
83      public void testNoLabelJsp() throws Exception {
84          TestAction testAction = (TestAction) action;
85          testAction.setFoo("bar");
86  
87          TextFieldTag tag = new TextFieldTag();
88          tag.setPageContext(pageContext);
89          tag.setName("myname");
90          tag.setValue("%{foo}");
91          tag.setSize("10");
92          tag.setOnblur("blahescape('somevalue');");
93  
94          tag.doStartTag();
95          tag.doEndTag();
96  
97          verify(TextFieldTag.class.getResource("Textfield-3.txt"));
98      }
99      
100     public void testLabelSeparatorJsp() throws Exception {
101         TestAction testAction = (TestAction) action;
102         testAction.setFoo("bar");
103 
104         TextFieldTag tag = new TextFieldTag();
105         tag.setPageContext(pageContext);
106         tag.setName("myname");
107         tag.setValue("%{foo}");
108         tag.setSize("10");
109         tag.setOnblur("blahescape('somevalue');");
110         tag.setLabelSeparator("??");
111         tag.setLabel("label");
112 
113         tag.doStartTag();
114         tag.doEndTag();
115 
116         verify(TextFieldTag.class.getResource("Textfield-4.txt"));
117     }
118 
119     public void testNoLabelFtl() throws Exception {
120         TestAction testAction = (TestAction) action;
121         testAction.setFoo("bar");
122 
123         TextFieldModel model = new TextFieldModel(stack, request, response);
124         HashMap params = new HashMap();
125         params.put("name", "myname");
126         params.put("value", "%{foo}");
127         params.put("size", "10");
128         params.put("onblur", "blahescape('somevalue');");
129         TransformControl control = (TransformControl) model.getWriter(writer, params);
130         control.onStart();
131         control.afterBody();
132 
133         verify(TextFieldTag.class.getResource("Textfield-3.txt"));
134     }
135 
136     public void testSimple() throws Exception {
137         TestAction testAction = (TestAction) action;
138         testAction.setFoo("bar");
139 
140         TextFieldTag tag = new TextFieldTag();
141         tag.setPageContext(pageContext);
142         tag.setLabel("mylabel");
143         tag.setName("myname");
144         tag.setValue("%{foo}");
145         tag.setSize("10");
146 
147         tag.doStartTag();
148         tag.doEndTag();
149 
150         verify(TextFieldTag.class.getResource("Textfield-1.txt"));
151     }
152     
153     public void testSimple_recursionTest() throws Exception {
154         TestAction testAction = (TestAction) action;
155         testAction.setFoo("%{1+1}");
156 
157         TextFieldTag tag = new TextFieldTag();
158         tag.setPageContext(pageContext);
159         tag.setLabel("mylabel");
160         tag.setName("myname");
161         tag.setValue("%{foo}");
162         tag.setSize("10");
163 
164         tag.doStartTag();
165         tag.doEndTag();
166 
167         verify(TextFieldTag.class.getResource("Textfield-5.txt"));
168     }
169     
170     public void testSimple_recursionTestNoValue() throws Exception {
171         TestAction testAction = (TestAction) action;
172         testAction.setFoo("%{1+1}");
173 
174         TextFieldTag tag = new TextFieldTag();
175         tag.setPageContext(pageContext);
176         tag.setLabel("mylabel");
177         tag.setName("foo");
178         tag.setSize("10");
179 
180         tag.doStartTag();
181         tag.doEndTag();
182 
183         verify(TextFieldTag.class.getResource("Textfield-6.txt"));
184     }
185 }