View Javadoc

1   /*
2    * $Id: RadioTest.java 670174 2008-06-21 10:16:19Z 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  
22  package org.apache.struts2.views.jsp.ui;
23  
24  import java.util.HashMap;
25  import java.util.LinkedHashMap;
26  import java.util.Map;
27  import java.util.TreeMap;
28  
29  import org.apache.struts2.TestAction;
30  import org.apache.struts2.views.jsp.AbstractUITagTest;
31  
32  
33  /***
34   */
35  public class RadioTest extends AbstractUITagTest {
36  	
37  	public void testMapWithBooleanAsKey() throws Exception {
38  		TestAction testAction = (TestAction) action;
39  		
40  		HashMap map = new LinkedHashMap();
41  		map.put(Boolean.TRUE, "male");
42  		map.put(Boolean.FALSE, "female");
43  		testAction.setMap(map);
44  		
45  		RadioTag tag = new RadioTag();
46  		tag.setPageContext(pageContext);
47  		tag.setLabel("mylabel");
48  		tag.setName("myname");
49  		tag.setValue("%{true}");
50  		tag.setList("map");
51  		
52  		tag.doStartTag();
53  		tag.doEndTag();
54  		
55  		verify(RadioTag.class.getResource("Radio-3.txt"));
56  	}
57  	
58      public void testMapChecked() throws Exception {
59          TestAction testAction = (TestAction) action;
60          testAction.setFoo("bar");
61  
62          HashMap map = new HashMap();
63          map.put("1", "One");
64          map.put("2", "Two");
65          testAction.setMap(map);
66  
67          RadioTag tag = new RadioTag();
68          tag.setPageContext(pageContext);
69          tag.setLabel("mylabel");
70          tag.setName("myname");
71          tag.setValue("\"1\"");
72          tag.setList("map");
73          tag.setListKey("key");
74          tag.setListValue("value");
75  
76          tag.doStartTag();
77          tag.doEndTag();
78  
79          verify(RadioTag.class.getResource("Radio-2.txt"));
80      }
81      
82      public void testMapCheckedNull() throws Exception {
83          TestAction testAction = (TestAction) action;
84          testAction.setFoo("bar");
85  
86          HashMap map = new HashMap();
87          map.put("1", "One");
88          map.put("2", "Two");
89          testAction.setMap(map);
90  
91          RadioTag tag = new RadioTag();
92          tag.setPageContext(pageContext);
93          tag.setLabel("mylabel");
94          tag.setName("myname");
95          tag.setValue("%{map['3']}");
96          tag.setList("#@java.util.TreeMap@{\"1\":\"One\", \"2\":\"Two\", \"\":\"N/A\"}");
97  
98          tag.doStartTag();
99          tag.doEndTag();
100 
101         verify(RadioTag.class.getResource("Radio-4.txt"));
102     }
103 
104     public void testSimple() throws Exception {
105         TestAction testAction = (TestAction) action;
106         testAction.setFoo("bar");
107         testAction.setList(new String[][]{
108                 {"hello", "world"},
109                 {"foo", "bar"}
110         });
111 
112         RadioTag tag = new RadioTag();
113         tag.setPageContext(pageContext);
114         tag.setLabel("mylabel");
115         tag.setName("myname");
116         tag.setValue("");
117         tag.setList("list");
118         tag.setListKey("top[0]");
119         tag.setListValue("top[1]");
120 
121         tag.doStartTag();
122         tag.doEndTag();
123 
124         verify(RadioTag.class.getResource("Radio-1.txt"));
125     }
126 
127     public void testSimpleWithStringMap() throws Exception {
128         final Map<String, String> myMap = new TreeMap<String, String>();
129         myMap.put("name", "Std.");
130         stack.push(new HashMap() {{ put ("myMap", myMap); }});
131 
132         RadioTag tag = new RadioTag();
133         tag.setPageContext(pageContext);
134         tag.setName("myMap['name']");
135         tag.setList("#@java.util.TreeMap@{\"Opt.\":\"Opt.\", \"Std.\":\"Std.\", \"\":\"N/A\"}");
136         tag.doStartTag();
137         tag.doEndTag();
138 
139         verify(RadioTag.class.getResource("Radio-6.txt"));
140     }
141 
142     public void testSimpleWithLabelSeparator() throws Exception {
143         TestAction testAction = (TestAction) action;
144         testAction.setFoo("bar");
145         testAction.setList(new String[][]{
146                 {"hello", "world"},
147                 {"foo", "bar"}
148         });
149 
150         RadioTag tag = new RadioTag();
151         tag.setPageContext(pageContext);
152         tag.setLabel("mylabel");
153         tag.setName("myname");
154         tag.setValue("");
155         tag.setList("list");
156         tag.setListKey("top[0]");
157         tag.setListValue("top[1]");
158         tag.setLabelSeparator("--");
159 
160         tag.doStartTag();
161         tag.doEndTag();
162 
163         verify(RadioTag.class.getResource("Radio-5.txt"));
164     }
165 
166     public void testGenericSimple() throws Exception {
167         RadioTag tag = new RadioTag();
168         prepareTagGeneric(tag);
169         verifyGenericProperties(tag, "simple", new String[]{"id","value"});
170     }
171 
172     public void testGenericXhtml() throws Exception {
173         RadioTag tag = new RadioTag();
174         prepareTagGeneric(tag);
175         verifyGenericProperties(tag, "xhtml", new String[]{"id","value"});
176     }
177 
178     private void prepareTagGeneric(RadioTag tag) {
179         TestAction testAction = (TestAction) action;
180         testAction.setFoo("bar");
181         testAction.setList(new String[][]{
182                 {"hello", "world"},
183                 {"foo", "bar"}
184         });
185         tag.setList("list");
186         tag.setListKey("top[0]");
187         tag.setListValue("top[1]");
188     }
189 
190 }