View Javadoc

1   /*
2    * $Id: SubmitTest.java 501762 2007-01-31 08:25:24Z mrdon $
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 javax.servlet.jsp.tagext.BodyContent;
27  
28  import org.apache.struts2.TestAction;
29  import org.apache.struts2.views.jsp.AbstractUITagTest;
30  
31  import com.mockobjects.servlet.MockBodyContent;
32  
33  
34  /***
35   * Unit test for {@link SubmitTag}.
36   *
37   */
38  public class SubmitTest extends AbstractUITagTest {
39  
40      public void testDefaultValues() throws Exception {
41          TestAction testAction = (TestAction) action;
42          testAction.setFoo("bar");
43  
44          SubmitTag tag = new SubmitTag();
45          tag.setPageContext(pageContext);
46          tag.setLabel("mylabel");
47          tag.setName("myname");
48          tag.setTitle("mytitle");
49  
50          tag.doStartTag();
51          tag.doEndTag();
52  
53          verify(TextFieldTag.class.getResource("Submit-2.txt"));
54      }
55  
56      public void testSimple() throws Exception {
57          TestAction testAction = (TestAction) action;
58          testAction.setFoo("bar");
59  
60          SubmitTag tag = new SubmitTag();
61          tag.setPageContext(pageContext);
62          tag.setLabel("mylabel");
63          tag.setAlign("left");
64          tag.setName("myname");
65          tag.setValue("%{foo}");
66  
67          tag.doStartTag();
68          tag.doEndTag();
69  
70          verify(TextFieldTag.class.getResource("Submit-1.txt"));
71      }
72  
73      public void testButtonSimple() throws Exception {
74          TestAction testAction = (TestAction) action;
75          testAction.setFoo("bar");
76  
77          SubmitTag tag = new SubmitTag();
78          tag.setPageContext(pageContext);
79          tag.setType("button");
80          tag.setName("myname");
81          tag.setValue("%{foo}");
82  
83          tag.doStartTag();
84          tag.doEndTag();
85  
86          verify(TextFieldTag.class.getResource("Submit-3.txt"));
87      }
88      
89      public void testButtonSimpleWithBody() throws Exception {
90          TestAction testAction = (TestAction) action;
91          testAction.setFoo("bar");
92  
93          SubmitTag tag = new SubmitTag();
94          tag.setPageContext(pageContext);
95          tag.setType("button");
96          tag.setName("myname");
97          tag.setValue("%{foo}");
98  
99          tag.doStartTag();
100         StrutsBodyContent body = new StrutsBodyContent(null);
101         body.append("foo");
102         tag.setBodyContent(body);
103         tag.doEndTag();
104 
105         verify(TextFieldTag.class.getResource("Submit-7.txt"));
106     }
107 
108     public void testButtonWithLabel() throws Exception {
109         TestAction testAction = (TestAction) action;
110         testAction.setFoo("bar");
111 
112         SubmitTag tag = new SubmitTag();
113         tag.setPageContext(pageContext);
114         tag.setLabel("mylabel");
115         tag.setType("button");
116         tag.setAlign("left");
117         tag.setName("myname");
118         tag.setValue("%{foo}");
119 
120         tag.doStartTag();
121         tag.doEndTag();
122 
123         verify(TextFieldTag.class.getResource("Submit-4.txt"));
124     }
125 
126     public void testImageSimple() throws Exception {
127         TestAction testAction = (TestAction) action;
128         testAction.setFoo("bar");
129 
130         SubmitTag tag = new SubmitTag();
131         tag.setPageContext(pageContext);
132         tag.setType("image");
133         tag.setName("myname");
134         tag.setValue("%{foo}");
135 
136         tag.doStartTag();
137         tag.doEndTag();
138 
139         verify(TextFieldTag.class.getResource("Submit-5.txt"));
140     }
141 
142     public void testImageWithSrc() throws Exception {
143         TestAction testAction = (TestAction) action;
144         testAction.setFoo("bar");
145 
146         SubmitTag tag = new SubmitTag();
147         tag.setPageContext(pageContext);
148         tag.setType("image");
149         tag.setName("myname");
150         tag.setLabel("mylabel");
151         tag.setValue("%{foo}");
152         tag.setSrc("some.gif");
153 
154         tag.doStartTag();
155         tag.doEndTag();
156 
157         verify(TextFieldTag.class.getResource("Submit-6.txt"));
158     }
159 
160     public void testSimpleThemeImageUsingActionAndMethod() throws Exception {
161         TestAction testAction = (TestAction) action;
162         testAction.setFoo("bar");
163 
164         SubmitTag tag = new SubmitTag();
165         tag.setPageContext(pageContext);
166         tag.setTheme("simple");
167         tag.setType("button");
168         tag.setName("myname");
169         tag.setLabel("mylabel");
170         tag.setAction("manager");
171         tag.setMethod("update");
172         tag.setAlign("left");
173 
174         tag.doStartTag();
175         tag.doEndTag();
176 
177         assertEquals("<button type=\"submit\" id=\"myname\" name=\"action:manager!update\" value=\"Submit\">mylabel</button>", writer.toString().trim());
178     }
179 
180     public void testSimpleThemeImageUsingActionOnly() throws Exception {
181         TestAction testAction = (TestAction) action;
182         testAction.setFoo("bar");
183 
184         SubmitTag tag = new SubmitTag();
185         tag.setPageContext(pageContext);
186         tag.setTheme("simple");
187         tag.setType("button");
188         tag.setName("myname");
189         tag.setLabel("mylabel");
190         tag.setAction("manager");
191         tag.setMethod(null); // no method
192         tag.setAlign("left");
193 
194         tag.doStartTag();
195         tag.doEndTag();
196 
197         assertEquals("<button type=\"submit\" id=\"myname\" name=\"action:manager\" value=\"Submit\">mylabel</button>", writer.toString().trim());
198     }
199 
200     public void testSimpleThemeImageUsingMethodOnly() throws Exception {
201         TestAction testAction = (TestAction) action;
202         testAction.setFoo("bar");
203 
204         SubmitTag tag = new SubmitTag();
205         tag.setPageContext(pageContext);
206         tag.setTheme("simple");
207         tag.setType("button");
208         tag.setName("myname");
209         tag.setLabel("mylabel");
210         tag.setAction(null); // no action
211         tag.setMethod("update");
212         tag.setAlign("left");
213 
214         tag.doStartTag();
215         tag.doEndTag();
216 
217         assertEquals("<button type=\"submit\" id=\"myname\" name=\"method:update\" value=\"Submit\">mylabel</button>", writer.toString().trim());
218     }
219 
220     public void testSimpleThemeInput() throws Exception {
221         TestAction testAction = (TestAction) action;
222         testAction.setFoo("bar");
223 
224         SubmitTag tag = new SubmitTag();
225         tag.setPageContext(pageContext);
226         tag.setTheme("simple");
227         tag.setType("input");
228         tag.setName("myname");
229         tag.setLabel("mylabel");
230         tag.setAction(null);
231         tag.setMethod(null);
232 
233         tag.doStartTag();
234         tag.doEndTag();
235 
236         assertEquals("<input type=\"submit\" id=\"myname\" name=\"myname\" value=\"Submit\"/>", writer.toString().trim());
237     }
238 
239     /***
240      * Initialize a map of {@link org.apache.struts2.views.jsp.AbstractUITagTest.PropertyHolder} for generic tag
241      * property testing. Will be used when calling {@link #verifyGenericProperties(org.apache.struts2.views.jsp.ui.AbstractUITag,
242      * String, String[])} as properties to verify.<p/> This implementation extends testdata from AbstractUITag.
243      *
244      * @return A Map of PropertyHolders values bound to {@link org.apache.struts2.views.jsp.AbstractUITagTest.PropertyHolder#getName()}
245      *         as key.
246      */
247     protected Map initializedGenericTagTestProperties() {
248         Map result = new HashMap();
249         new PropertyHolder("title", "someTitle").addToMap(result);
250         new PropertyHolder("cssClass", "cssClass1", "class=\"cssClass1\"").addToMap(result);
251         new PropertyHolder("cssStyle", "cssStyle1", "style=\"cssStyle1\"").addToMap(result);
252         new PropertyHolder("name", "someName").addToMap(result);
253         new PropertyHolder("value", "someValue").addToMap(result);
254         return result;
255     }
256 
257     public void testGenericSimple() throws Exception {
258         SubmitTag tag = new SubmitTag();
259         verifyGenericProperties(tag, "simple", null);
260     }
261 
262     public void testGenericXhtml() throws Exception {
263         SubmitTag tag = new SubmitTag();
264         verifyGenericProperties(tag, "xhtml", null);
265     }
266 
267     public void testGenericAjax() throws Exception {
268         SubmitTag tag = new SubmitTag();
269         verifyGenericProperties(tag, "ajax", null);
270     }
271 
272 }