View Javadoc

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