View Javadoc

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