View Javadoc

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