View Javadoc

1   /*
2    * $Id: ComboBoxTest.java 560260 2007-07-27 14:57:26Z 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.LinkedHashMap;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.apache.struts2.TestAction;
29  import org.apache.struts2.views.jsp.AbstractUITagTest;
30  
31  
32  /***
33   * Test case for ComboBox component.
34   */
35  public class ComboBoxTest extends AbstractUITagTest {
36  
37      public void testGenericSimple() throws Exception {
38          ComboBoxTag tag = new ComboBoxTag();
39          prepareTagGeneric(tag);
40          verifyGenericProperties(tag, "simple", null);
41      }
42  
43      public void testGenericXhtml() throws Exception {
44          ComboBoxTag tag = new ComboBoxTag();
45          prepareTagGeneric(tag);
46          verifyGenericProperties(tag, "xhtml", null);
47      }
48  
49      public void testGenericAjax() throws Exception {
50          ComboBoxTag tag = new ComboBoxTag();
51          prepareTagGeneric(tag);
52          verifyGenericProperties(tag, "ajax", null);
53      }
54  
55      private void prepareTagGeneric(ComboBoxTag tag) {
56          TestAction testAction = (TestAction) action;
57          ArrayList collection = new ArrayList();
58          collection.add("foo");
59          collection.add("bar");
60          collection.add("baz");
61  
62          testAction.setCollection(collection);
63  
64          tag.setList("collection");
65      }
66  
67      public void testSimple() throws Exception {
68          TestAction testAction = (TestAction) action;
69          testAction.setFoo("hello");
70  
71          ArrayList collection = new ArrayList();
72          collection.add("foo");
73          collection.add("bar");
74          collection.add("baz");
75          testAction.setCollection(collection);
76  
77          ComboBoxTag tag = new ComboBoxTag();
78          tag.setPageContext(pageContext);
79          tag.setLabel("mylabel");
80          tag.setName("foo");
81          tag.setId("cb");
82          tag.setList("collection");
83  
84          tag.doStartTag();
85          tag.doEndTag();
86  
87          verify(ComboBoxTag.class.getResource("ComboBox-1.txt"));
88      }
89  
90      public void testWithEmptyOptionAndHeader() throws Exception {
91          TestAction testAction = (TestAction) action;
92          testAction.setFoo("banana");
93  
94          List l = new ArrayList();
95          l.add("apple");
96          l.add("banana");
97          l.add("pineaple");
98          l.add("grapes");
99          testAction.setCollection(l);
100 
101         ComboBoxTag tag = new ComboBoxTag();
102         tag.setPageContext(pageContext);
103         tag.setLabel("My Favourite Fruit");
104         tag.setName("myFavouriteFruit");
105         tag.setEmptyOption("true");
106         tag.setHeaderKey("-1");
107         tag.setHeaderValue("--- Please Select ---");
108         tag.setList("collection");
109         tag.setValue("%{foo}");
110 
111         tag.doStartTag();
112         tag.doEndTag();
113 
114         verify(ComboBoxTag.class.getResource("ComboBox-2.txt"));
115     }
116 
117     public void testWithMap() throws Exception {
118         TestAction testAction = (TestAction) action;
119         testAction.setFoo("banana");
120 
121         Map m = new LinkedHashMap();
122         m.put("apple", "apple");
123         m.put("banana", "banana");
124         m.put("pineaple", "pineaple");
125         m.put("grapes", "grapes");
126         testAction.setMap(m);
127 
128         ComboBoxTag tag = new ComboBoxTag();
129         tag.setPageContext(pageContext);
130         tag.setLabel("My Favourite Fruit");
131         tag.setName("myFavouriteFruit");
132         tag.setHeaderKey("-1");
133         tag.setHeaderValue("--- Please Select ---");
134         tag.setEmptyOption("true");
135         tag.setList("map");
136         tag.setValue("%{foo}");
137 
138         tag.doStartTag();
139         tag.doEndTag();
140 
141         verify(ComboBoxTag.class.getResource("ComboBox-3.txt"));
142     }
143 
144     public void testJsCallNamingUsesEscapedId() throws Exception {
145         TestAction testAction = (TestAction) action;
146         testAction.setFoo("hello");
147 
148         ArrayList collection = new ArrayList();
149         collection.add("foo");
150         testAction.setCollection(collection);
151 
152         ComboBoxTag tag = new ComboBoxTag();
153         tag.setPageContext(pageContext);
154         tag.setLabel("mylabel");
155         tag.setName("foo");
156         tag.setId("cb.bc");
157         tag.setList("collection");
158 
159         tag.doStartTag();
160         tag.doEndTag();
161 
162         verify(ComboBoxTag.class.getResource("ComboBox-4.txt"));
163     }
164 
165 }