1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 }