View Javadoc

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