View Javadoc

1   /*
2    * $Id: CheckboxListTest.java 439747 2006-09-03 09:22:46Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.views.jsp.ui;
19  
20  import java.util.ArrayList;
21  import java.util.Collection;
22  import java.util.Map;
23  
24  import org.apache.struts2.TestAction;
25  import org.apache.struts2.views.jsp.AbstractUITagTest;
26  
27  
28  /***
29   * Test case for CheckboxList.
30   *
31   */
32  public class CheckboxListTest extends AbstractUITagTest {
33  
34      /***
35       * Initialize a map of {@link org.apache.struts2.views.jsp.AbstractUITagTest.PropertyHolder} for generic tag
36       * property testing. Will be used when calling {@link #verifyGenericProperties(org.apache.struts2.views.jsp.ui.AbstractUITag,
37       * String, String[])} as properties to verify.<p/> This implementation extends testdata from AbstractUITag.
38       *
39       * @return A Map of PropertyHolders values bound to {@link org.apache.struts2.views.jsp.AbstractUITagTest.PropertyHolder#getName()}
40       *         as key.
41       */
42      protected Map initializedGenericTagTestProperties() {
43          Map result = super.initializedGenericTagTestProperties();
44          new PropertyHolder("value", "hello").addToMap(result);
45          return result;
46      }
47  
48      public void testGenericSimple() throws Exception {
49          CheckboxListTag tag = new CheckboxListTag();
50          prepareTagGeneric(tag);
51          verifyGenericProperties(tag, "simple", new String[]{"tabindex","cssClass","cssStyle","id"});
52      }
53  
54      public void testGenericXhtml() throws Exception {
55          CheckboxListTag tag = new CheckboxListTag();
56          prepareTagGeneric(tag);
57          verifyGenericProperties(tag, "xhtml", new String[]{"tabindex","cssClass","cssStyle","id"});
58      }
59  
60      public void testGenericAjax() throws Exception {
61          CheckboxListTag tag = new CheckboxListTag();
62          prepareTagGeneric(tag);
63          verifyGenericProperties(tag, "ajax", new String[]{"tabindex","cssClass","cssStyle","id"});
64      }
65  
66      private void prepareTagGeneric(CheckboxListTag tag) {
67          TestAction testAction = (TestAction) action;
68          Collection collection = new ArrayList(2);
69          collection.add("hello");
70          collection.add("foo");
71          testAction.setCollection(collection);
72          testAction.setList(new String[][]{
73                  {"hello", "world"},
74                  {"foo", "bar"},
75          });
76          tag.setName("collection");
77          tag.setList("list");
78          tag.setListKey("top[0]");
79          tag.setListValue("top[1]");
80      }
81  
82      public void testMultiple() throws Exception {
83          TestAction testAction = (TestAction) action;
84          Collection collection = new ArrayList(2);
85          collection.add("hello");
86          collection.add("foo");
87          testAction.setCollection(collection);
88          testAction.setList(new String[][]{
89                  {"hello", "world"},
90                  {"foo", "bar"},
91                  {"cat", "dog"}
92          });
93  
94          CheckboxListTag tag = new CheckboxListTag();
95          tag.setPageContext(pageContext);
96          tag.setLabel("mylabel");
97          tag.setName("collection");
98          tag.setList("list");
99          tag.setListKey("top[0]");
100         tag.setListValue("top[1]");
101 
102         tag.doStartTag();
103         tag.doEndTag();
104 
105         verify(CheckboxListTag.class.getResource("CheckboxList-2.txt"));
106     }
107 
108     public void testMultipleWithDisabledOn() throws Exception {
109         TestAction testAction = (TestAction) action;
110         Collection collection = new ArrayList(2);
111         collection.add("hello");
112         collection.add("foo");
113         testAction.setCollection(collection);
114         testAction.setList(new String[][]{
115                 {"hello", "world"},
116                 {"foo", "bar"},
117                 {"cat", "dog"}
118         });
119 
120         CheckboxListTag tag = new CheckboxListTag();
121         tag.setPageContext(pageContext);
122         tag.setLabel("mylabel");
123         tag.setName("collection");
124         tag.setList("list");
125         tag.setListKey("top[0]");
126         tag.setListValue("top[1]");
127         tag.setDisabled("true");
128 
129         tag.doStartTag();
130         tag.doEndTag();
131 
132         verify(CheckboxListTag.class.getResource("CheckboxList-3.txt"));
133     }
134 
135     public void testSimple() throws Exception {
136         TestAction testAction = (TestAction) action;
137         testAction.setFoo("hello");
138         testAction.setList(new String[][]{
139                 {"hello", "world"},
140                 {"foo", "bar"}
141         });
142 
143         CheckboxListTag tag = new CheckboxListTag();
144         tag.setPageContext(pageContext);
145         tag.setLabel("mylabel");
146         tag.setName("foo");
147         tag.setList("list");
148         tag.setListKey("top[0]");
149         tag.setListValue("top[1]");
150         tag.setOnchange("alert('foo');");
151         tag.setTitle("mytitle");
152 
153         tag.doStartTag();
154         tag.doEndTag();
155 
156         verify(CheckboxListTag.class.getResource("CheckboxList-1.txt"));
157     }
158 
159     public void testSimpleWithDisableOn() throws Exception {
160         TestAction testAction = (TestAction) action;
161         testAction.setFoo("hello");
162         testAction.setList(new String[][]{
163                 {"hello", "world"},
164                 {"foo", "bar"}
165         });
166 
167         CheckboxListTag tag = new CheckboxListTag();
168         tag.setPageContext(pageContext);
169         tag.setLabel("mylabel");
170         tag.setName("foo");
171         tag.setList("list");
172         tag.setListKey("top[0]");
173         tag.setListValue("top[1]");
174         tag.setOnchange("alert('foo');");
175         tag.setDisabled("true");
176 
177         tag.doStartTag();
178         tag.doEndTag();
179 
180         verify(CheckboxListTag.class.getResource("CheckboxList-4.txt"));
181     }
182 }