View Javadoc

1   /*
2    * $Id: TextfieldTest.java 439747 2006-09-03 09:22:46Z mrdon $
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.views.jsp.ui;
19  
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import org.apache.struts2.TestAction;
24  import org.apache.struts2.views.freemarker.tags.TextFieldModel;
25  import org.apache.struts2.views.jsp.AbstractUITagTest;
26  
27  import freemarker.template.TransformControl;
28  
29  
30  /***
31   */
32  public class TextfieldTest extends AbstractUITagTest {
33  
34      /***
35       * Initialize a map of {@link org.apache.struts2.views.jsp.AbstractUITagTest.PropertyHolder} for generic tag
36       * property testing. Will be used when calling {@link #verifyGenericProperties(org.apache.struts2.views.jsp.ui.AbstractUITag,
37       * String, String[])} as properties to verify.<p/> This implementation extends testdata from AbstractUITag.
38       *
39       * @return A Map of PropertyHolders values bound to {@link org.apache.struts2.views.jsp.AbstractUITagTest.PropertyHolder#getName()}
40       *         as key.
41       */
42      protected Map initializedGenericTagTestProperties() {
43          Map result = super.initializedGenericTagTestProperties();
44          new PropertyHolder("maxlength", "10").addToMap(result);
45          new PropertyHolder("readonly", "true", "readonly=\"readonly\"").addToMap(result);
46          new PropertyHolder("size", "12").addToMap(result);
47          return result;
48      }
49  
50      public void testGenericSimple() throws Exception {
51          TextFieldTag tag = new TextFieldTag();
52          verifyGenericProperties(tag, "simple", null);
53      }
54  
55      public void testGenericXhtml() throws Exception {
56          TextFieldTag tag = new TextFieldTag();
57          verifyGenericProperties(tag, "xhtml", null);
58      }
59  
60      public void testGenericAjax() throws Exception {
61          TextFieldTag tag = new TextFieldTag();
62          verifyGenericProperties(tag, "ajax", null);
63      }
64  
65      public void testErrors() throws Exception {
66          TestAction testAction = (TestAction) action;
67          testAction.setFoo("bar");
68  
69          TextFieldTag tag = new TextFieldTag();
70          tag.setPageContext(pageContext);
71          tag.setId("myId");
72          tag.setLabel("mylabel");
73          tag.setName("foo");
74          tag.setValue("bar");
75          tag.setTitle("mytitle");
76  
77          testAction.addFieldError("foo", "bar error message");
78          tag.doStartTag();
79          tag.doEndTag();
80  
81          verify(TextFieldTag.class.getResource("Textfield-2.txt"));
82      }
83  
84      public void testNoLabelJsp() throws Exception {
85          TestAction testAction = (TestAction) action;
86          testAction.setFoo("bar");
87  
88          TextFieldTag tag = new TextFieldTag();
89          tag.setPageContext(pageContext);
90          tag.setName("myname");
91          tag.setValue("%{foo}");
92          tag.setSize("10");
93          tag.setOnblur("blahescape('somevalue');");
94  
95          tag.doStartTag();
96          tag.doEndTag();
97  
98          verify(TextFieldTag.class.getResource("Textfield-3.txt"));
99      }
100 
101     public void testNoLabelFtl() throws Exception {
102         TestAction testAction = (TestAction) action;
103         testAction.setFoo("bar");
104 
105         TextFieldModel model = new TextFieldModel(stack, request, response);
106         HashMap params = new HashMap();
107         params.put("name", "myname");
108         params.put("value", "%{foo}");
109         params.put("size", "10");
110         params.put("onblur", "blahescape('somevalue');");
111         TransformControl control = (TransformControl) model.getWriter(writer, params);
112         control.onStart();
113         control.afterBody();
114 
115         verify(TextFieldTag.class.getResource("Textfield-3.txt"));
116     }
117 
118     public void testSimple() throws Exception {
119         TestAction testAction = (TestAction) action;
120         testAction.setFoo("bar");
121 
122         TextFieldTag tag = new TextFieldTag();
123         tag.setPageContext(pageContext);
124         tag.setLabel("mylabel");
125         tag.setName("myname");
126         tag.setValue("%{foo}");
127         tag.setSize("10");
128 
129         tag.doStartTag();
130         tag.doEndTag();
131 
132         verify(TextFieldTag.class.getResource("Textfield-1.txt"));
133     }
134 }