View Javadoc

1   /*
2    * $Id: CheckboxListTest.java 632856 2008-03-02 21:54:46Z rgielen $
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.ArrayList;
24  import java.util.Collection;
25  import java.util.Map;
26  
27  import org.apache.struts2.TestAction;
28  import org.apache.struts2.views.jsp.AbstractUITagTest;
29  
30  
31  /***
32   * Test case for CheckboxList.
33   *
34   */
35  public class CheckboxListTest extends AbstractUITagTest {
36  
37      /***
38       * Initialize a map of {@link org.apache.struts2.views.jsp.AbstractUITagTest.PropertyHolder} for generic tag
39       * property testing. Will be used when calling {@link #verifyGenericProperties(org.apache.struts2.views.jsp.ui.AbstractUITag,
40       * String, String[])} as properties to verify.<p/> This implementation extends testdata from AbstractUITag.
41       *
42       * @return A Map of PropertyHolders values bound to {@link org.apache.struts2.views.jsp.AbstractUITagTest.PropertyHolder#getName()}
43       *         as key.
44       */
45      protected Map initializedGenericTagTestProperties() {
46          Map result = super.initializedGenericTagTestProperties();
47          new PropertyHolder("value", "hello").addToMap(result);
48          return result;
49      }
50  
51      public void testGenericSimple() throws Exception {
52          CheckboxListTag tag = new CheckboxListTag();
53          prepareTagGeneric(tag);
54          verifyGenericProperties(tag, "simple", new String[]{"tabindex","cssClass","cssStyle","id"});
55      }
56  
57      public void testGenericXhtml() throws Exception {
58          CheckboxListTag tag = new CheckboxListTag();
59          prepareTagGeneric(tag);
60          verifyGenericProperties(tag, "xhtml", new String[]{"tabindex","cssClass","cssStyle","id"});
61      }
62  
63      public void testGenericAjax() throws Exception {
64          CheckboxListTag tag = new CheckboxListTag();
65          prepareTagGeneric(tag);
66          verifyGenericProperties(tag, "ajax", new String[]{"tabindex","cssClass","cssStyle","id"});
67      }
68  
69      private void prepareTagGeneric(CheckboxListTag tag) {
70          TestAction testAction = (TestAction) action;
71          Collection collection = new ArrayList(2);
72          collection.add("hello");
73          collection.add("foo");
74          testAction.setCollection(collection);
75          testAction.setList(new String[][]{
76                  {"hello", "world"},
77                  {"foo", "bar"},
78          });
79          tag.setName("collection");
80          tag.setList("list");
81          tag.setListKey("top[0]");
82          tag.setListValue("top[1]");
83      }
84  
85      public void testMultiple() throws Exception {
86          TestAction testAction = (TestAction) action;
87          Collection collection = new ArrayList(2);
88          collection.add("hello");
89          collection.add("foo");
90          testAction.setCollection(collection);
91          testAction.setList(new String[][]{
92                  {"hello", "world"},
93                  {"foo", "bar"},
94                  {"cat", "dog"}
95          });
96  
97          CheckboxListTag tag = new CheckboxListTag();
98          tag.setPageContext(pageContext);
99          tag.setLabel("mylabel");
100         tag.setName("collection");
101         tag.setList("list");
102         tag.setListKey("top[0]");
103         tag.setListValue("top[1]");
104 
105         tag.doStartTag();
106         tag.doEndTag();
107 
108         verify(CheckboxListTag.class.getResource("CheckboxList-2.txt"));
109     }
110 
111     public void testMultipleWithDisabledOn() throws Exception {
112         TestAction testAction = (TestAction) action;
113         Collection collection = new ArrayList(2);
114         collection.add("hello");
115         collection.add("foo");
116         testAction.setCollection(collection);
117         testAction.setList(new String[][]{
118                 {"hello", "world"},
119                 {"foo", "bar"},
120                 {"cat", "dog"}
121         });
122 
123         CheckboxListTag tag = new CheckboxListTag();
124         tag.setPageContext(pageContext);
125         tag.setLabel("mylabel");
126         tag.setName("collection");
127         tag.setList("list");
128         tag.setListKey("top[0]");
129         tag.setListValue("top[1]");
130         tag.setDisabled("true");
131 
132         tag.doStartTag();
133         tag.doEndTag();
134 
135         verify(CheckboxListTag.class.getResource("CheckboxList-3.txt"));
136     }
137 
138     public void testSimple() throws Exception {
139         TestAction testAction = (TestAction) action;
140         testAction.setFoo("hello");
141         testAction.setList(new String[][]{
142                 {"hello", "world"},
143                 {"foo", "bar"},
144                 {"baz", null}
145         });
146 
147         CheckboxListTag tag = new CheckboxListTag();
148         tag.setPageContext(pageContext);
149         tag.setLabel("mylabel");
150         tag.setName("foo");
151         tag.setList("list");
152         tag.setListKey("top[0]");
153         tag.setListValue("top[1]");
154         tag.setOnchange("alert('foo');");
155         tag.setTitle("mytitle");
156 
157         tag.doStartTag();
158         tag.doEndTag();
159 
160         verify(CheckboxListTag.class.getResource("CheckboxList-1.txt"));
161     }
162 
163     public void testSimpleWithDisableOn() throws Exception {
164         TestAction testAction = (TestAction) action;
165         testAction.setFoo("hello");
166         testAction.setList(new String[][]{
167                 {"hello", "world"},
168                 {"foo", "bar"}
169         });
170 
171         CheckboxListTag tag = new CheckboxListTag();
172         tag.setPageContext(pageContext);
173         tag.setLabel("mylabel");
174         tag.setName("foo");
175         tag.setList("list");
176         tag.setListKey("top[0]");
177         tag.setListValue("top[1]");
178         tag.setOnchange("alert('foo');");
179         tag.setDisabled("true");
180 
181         tag.doStartTag();
182         tag.doEndTag();
183 
184         verify(CheckboxListTag.class.getResource("CheckboxList-4.txt"));
185     }
186 }