001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.views.jsp.ui;
006:
007: import com.opensymphony.webwork.TestAction;
008: import com.opensymphony.webwork.views.jsp.AbstractUITagTest;
009:
010: import java.math.BigDecimal;
011: import java.util.ArrayList;
012: import java.util.Collection;
013: import java.util.List;
014:
015: /**
016: * @author Matt Ho <a href="mailto:matt@enginegreen.com"><matt@enginegreen.com></a>
017: * @author tm_jee
018: * @author Rene Gielen
019: * @version $Id: SelectTest.java 2724 2006-09-19 16:44:28Z tmjee $
020: */
021: public class SelectTest extends AbstractUITagTest {
022:
023: public void testHeaderCanBePreselected() throws Exception {
024: SelectTag tag = new SelectTag();
025: tag.setPageContext(pageContext);
026: tag.setLabel("myLabel");
027: tag.setList("#{1:'Cat',2:'Dog'}");
028: tag.setName("myPet");
029: tag.setHeaderKey("-1");
030: tag.setHeaderValue("--- Please Select ---");
031: tag.setValue("%{'-1'}");
032:
033: tag.doStartTag();
034: tag.doEndTag();
035:
036: verify(SelectTag.class.getResource("Select-8.txt"));
037: }
038:
039: /**
040: * Tests WW-455: Select tag template does not work properly for Object like BigDecimal.
041: */
042: public void testBigDecimal() throws Exception {
043: BigDecimalObject hello = new BigDecimalObject("hello",
044: new BigDecimal(1));
045: BigDecimalObject foo = new BigDecimalObject("foo",
046: new BigDecimal(2));
047:
048: TestAction testAction = (TestAction) action;
049:
050: Collection collection = new ArrayList(2);
051: // expect strings to be returned, we're still dealing with HTTP here!
052: collection.add("hello");
053: collection.add("foo");
054: testAction.setCollection(collection);
055:
056: List list2 = new ArrayList();
057: list2.add(hello);
058: list2.add(foo);
059: list2.add(new BigDecimalObject("<cat>", new BigDecimal(1.500)));
060: testAction.setList2(list2);
061:
062: SelectTag tag = new SelectTag();
063: tag.setPageContext(pageContext);
064: tag.setLabel("mylabel");
065: tag.setName("collection");
066: tag.setList("list2");
067: tag.setListKey("name");
068: tag.setListValue("bigDecimal");
069: tag.setMultiple("true");
070: tag.setTitle("mytitle");
071: tag.setOnmousedown("alert('onmousedown');");
072: tag.setOnmousemove("alert('onmousemove');");
073: tag.setOnmouseout("alert('onmouseout');");
074: tag.setOnmouseover("alert('onmouseover');");
075: tag.setOnmouseup("alert('onmouseup');");
076:
077: tag.doStartTag();
078: tag.doEndTag();
079:
080: verify(SelectTag.class.getResource("Select-3.txt"));
081: }
082:
083: public class BigDecimalObject {
084: private String name;
085: private BigDecimal bigDecimal;
086:
087: public BigDecimalObject(String name, BigDecimal bigDecimal) {
088: this .name = name;
089: this .bigDecimal = bigDecimal;
090: }
091:
092: public String getName() {
093: return name;
094: }
095:
096: public BigDecimal getBigDecimal() {
097: return bigDecimal;
098: }
099: }
100:
101: public void testNullList() throws Exception {
102: TestAction testAction = (TestAction) action;
103: testAction.setList2(null);
104:
105: SelectTag tag = new SelectTag();
106: tag.setName("collection");
107: tag.setList("list2");
108: tag.setLabel("tmjee_name");
109:
110: tag.setPageContext(pageContext);
111: try {
112: tag.doStartTag();
113: tag.doEndTag();
114: fail("exception should have been thrown value of select tag is null");
115: } catch (Exception e) {
116: assertTrue(true);
117: }
118: }
119:
120: public void testEmptyList() throws Exception {
121: TestAction testAction = (TestAction) action;
122: testAction.setList2(new ArrayList());
123:
124: SelectTag tag = new SelectTag();
125: tag.setName("collection");
126: tag.setList("list2");
127: tag.setLabel("tmjee_name");
128:
129: tag.setPageContext(pageContext);
130: tag.doStartTag();
131: tag.doEndTag();
132:
133: verify(SelectTag.class.getResource("Select-4.txt"));
134: }
135:
136: public void testMultiple() throws Exception {
137: TestAction testAction = (TestAction) action;
138: Collection collection = new ArrayList(2);
139: collection.add("hello");
140: collection.add("foo");
141: testAction.setCollection(collection);
142: testAction.setList(new String[][] { { "hello", "world" },
143: { "foo", "bar" }, { "cat", "dog" } });
144:
145: SelectTag tag = new SelectTag();
146: tag.setPageContext(pageContext);
147: tag.setLabel("mylabel");
148: tag.setName("collection");
149: tag.setList("list");
150: tag.setListKey("top[0]");
151: tag.setListValue("top[1]");
152: tag.setMultiple("true");
153: tag.setOnmousedown("alert('onmousedown');");
154: tag.setOnmousemove("alert('onmousemove');");
155: tag.setOnmouseout("alert('onmouseout');");
156: tag.setOnmouseover("alert('onmouseover');");
157: tag.setOnmouseup("alert('onmouseup');");
158:
159: tag.doStartTag();
160: tag.doEndTag();
161:
162: verify(SelectTag.class.getResource("Select-2.txt"));
163: }
164:
165: public void testSimple() throws Exception {
166: TestAction testAction = (TestAction) action;
167: testAction.setFoo("hello");
168: testAction.setList(new String[][] { { "hello", "world" },
169: { "foo", "bar" } });
170:
171: SelectTag tag = new SelectTag();
172: tag.setPageContext(pageContext);
173: tag.setEmptyOption("true");
174: tag.setLabel("mylabel");
175: tag.setName("foo");
176: tag.setList("list");
177: tag.setListKey("top[0]");
178: tag.setListValue("top[1]");
179:
180: // header stuff
181: tag.setHeaderKey("headerKey");
182: tag.setHeaderValue("headerValue");
183:
184: // empty option
185: tag.setEmptyOption("true");
186:
187: tag.doStartTag();
188: tag.doEndTag();
189:
190: verify(SelectTag.class.getResource("Select-1.txt"));
191: }
192:
193: public void testExtended() throws Exception {
194: TestAction testAction = (TestAction) action;
195: testAction.setFoo("hello");
196: testAction.setList(new String[][] { { "hello", "world" },
197: { "foo", "bar" } });
198:
199: SelectTag tag = new SelectTag();
200: tag.setPageContext(pageContext);
201: tag.setEmptyOption("true");
202: tag.setLabel("mylabel");
203: tag.setName("foo");
204: tag.setList("list");
205: tag.setListKey("top[0]");
206: tag.setListValue("%{top[0] + ' - ' + top[1]}");
207:
208: // header stuff
209: tag.setHeaderKey("headerKey");
210: tag.setHeaderValue("%{foo + ': headerValue'}");
211:
212: // empty option
213: tag.setEmptyOption("true");
214:
215: tag.doStartTag();
216: tag.doEndTag();
217:
218: verify(SelectTag.class.getResource("Select-7.txt"));
219: }
220:
221: public void testGenericSimple() throws Exception {
222: SelectTag tag = new SelectTag();
223: prepareTagGeneric(tag);
224: verifyGenericProperties(tag, "simple", new String[] { "value" });
225: }
226:
227: public void testGenericXhtml() throws Exception {
228: SelectTag tag = new SelectTag();
229: prepareTagGeneric(tag);
230: verifyGenericProperties(tag, "xhtml", new String[] { "value" });
231: }
232:
233: public void testGenericAjax() throws Exception {
234: SelectTag tag = new SelectTag();
235: prepareTagGeneric(tag);
236: verifyGenericProperties(tag, "ajax", new String[] { "value" });
237: }
238:
239: public void testMultipleOn() throws Exception {
240: SelectTag tag = new SelectTag();
241: tag.setPageContext(pageContext);
242: tag.setLabel("media1");
243: tag.setId("myId");
244: tag.setEmptyOption("true");
245: tag.setName("myName");
246: tag.setMultiple("true");
247: tag.setList("{'aaa','bbb'}");
248:
249: tag.doStartTag();
250: tag.doEndTag();
251:
252: verify(SelectTag.class.getResource("Select-5.txt"));
253: }
254:
255: public void testMultipleOff() throws Exception {
256: SelectTag tag = new SelectTag();
257: tag.setPageContext(pageContext);
258: tag.setLabel("media2");
259: tag.setId("myId");
260: tag.setEmptyOption("true");
261: tag.setName("myName");
262: tag.setMultiple("false");
263: tag.setList("{'aaa','bbb'}");
264:
265: tag.doStartTag();
266: tag.doEndTag();
267:
268: verify(SelectTag.class.getResource("Select-6.txt"));
269: }
270:
271: private void prepareTagGeneric(SelectTag tag) {
272: TestAction testAction = (TestAction) action;
273: ArrayList collection = new ArrayList();
274: collection.add("foo");
275: collection.add("bar");
276: collection.add("baz");
277:
278: testAction.setCollection(collection);
279:
280: tag.setList("collection");
281: }
282:
283: }
|