View Javadoc

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