001: /*
002: * $Id: SelectTest.java 539825 2007-05-20 04:28:35Z mrdon $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.views.jsp.ui;
022:
023: import java.math.BigDecimal;
024: import java.util.ArrayList;
025: import java.util.Collection;
026: import java.util.List;
027:
028: import org.apache.struts2.TestAction;
029: import org.apache.struts2.views.jsp.AbstractUITagTest;
030:
031: /**
032: */
033: public class SelectTest extends AbstractUITagTest {
034:
035: public void testHeaderCanBePreselected() throws Exception {
036: SelectTag tag = new SelectTag();
037: tag.setPageContext(pageContext);
038: tag.setLabel("myLabel");
039: tag.setList("#{1:'Cat',2:'Dog'}");
040: tag.setName("myPet");
041: tag.setHeaderKey("-1");
042: tag.setHeaderValue("--- Please Select ---");
043: tag.setValue("%{'-1'}");
044:
045: tag.doStartTag();
046: tag.doEndTag();
047:
048: verify(SelectTag.class.getResource("Select-8.txt"));
049: }
050:
051: /**
052: * Tests WW-1601: Select tag template does not work properly for Object like Byte.
053: */
054: public void testByte() throws Exception {
055: ByteObject hello = new ByteObject(new Byte((byte) 1), "hello");
056: ByteObject foo = new ByteObject(new Byte((byte) 2), "foo");
057:
058: TestAction testAction = (TestAction) action;
059:
060: Collection collection = new ArrayList(2);
061: // expect strings to be returned, we're still dealing with HTTP here!
062: collection.add("1");
063: collection.add("2");
064: testAction.setCollection(collection);
065:
066: List list2 = new ArrayList();
067: list2.add(hello);
068: list2.add(foo);
069: testAction.setList2(list2);
070:
071: SelectTag tag = new SelectTag();
072: tag.setPageContext(pageContext);
073: tag.setLabel("mylabel");
074: tag.setName("collection");
075: tag.setList("list2");
076: tag.setListKey("byte");
077: tag.setListValue("name");
078: tag.setMultiple("true");
079: tag.setTitle("mytitle");
080: tag.setOnmousedown("alert('onmousedown');");
081: tag.setOnmousemove("alert('onmousemove');");
082: tag.setOnmouseout("alert('onmouseout');");
083: tag.setOnmouseover("alert('onmouseover');");
084: tag.setOnmouseup("alert('onmouseup');");
085:
086: tag.doStartTag();
087: tag.doEndTag();
088:
089: verify(SelectTag.class.getResource("Select-10.txt"));
090: }
091:
092: /**
093: * Tests WW-455: Select tag template does not work properly for Object like BigDecimal.
094: */
095: public void testBigDecimal() throws Exception {
096: BigDecimalObject hello = new BigDecimalObject("hello",
097: new BigDecimal(1));
098: BigDecimalObject foo = new BigDecimalObject("foo",
099: new BigDecimal(2));
100:
101: TestAction testAction = (TestAction) action;
102:
103: Collection collection = new ArrayList(2);
104: // expect strings to be returned, we're still dealing with HTTP here!
105: collection.add("hello");
106: collection.add("foo");
107: testAction.setCollection(collection);
108:
109: List list2 = new ArrayList();
110: list2.add(hello);
111: list2.add(foo);
112: list2.add(new BigDecimalObject("<cat>", new BigDecimal(1.500)));
113: testAction.setList2(list2);
114:
115: SelectTag tag = new SelectTag();
116: tag.setPageContext(pageContext);
117: tag.setLabel("mylabel");
118: tag.setName("collection");
119: tag.setList("list2");
120: tag.setListKey("name");
121: tag.setListValue("bigDecimal");
122: tag.setMultiple("true");
123: tag.setTitle("mytitle");
124: tag.setOnmousedown("alert('onmousedown');");
125: tag.setOnmousemove("alert('onmousemove');");
126: tag.setOnmouseout("alert('onmouseout');");
127: tag.setOnmouseover("alert('onmouseover');");
128: tag.setOnmouseup("alert('onmouseup');");
129:
130: tag.doStartTag();
131: tag.doEndTag();
132:
133: verify(SelectTag.class.getResource("Select-3.txt"));
134: }
135:
136: public class BigDecimalObject {
137: private String name;
138: private BigDecimal bigDecimal;
139:
140: public BigDecimalObject(String name, BigDecimal bigDecimal) {
141: this .name = name;
142: this .bigDecimal = bigDecimal;
143: }
144:
145: public String getName() {
146: return name;
147: }
148:
149: public BigDecimal getBigDecimal() {
150: return bigDecimal;
151: }
152: }
153:
154: public class ByteObject {
155: private String name;
156: private Byte byteValue;
157:
158: public ByteObject(Byte byteValue, String name) {
159: this .name = name;
160: this .byteValue = byteValue;
161: }
162:
163: public String getName() {
164: return name;
165: }
166:
167: public Byte getByte() {
168: return byteValue;
169: }
170: }
171:
172: public class LongObject {
173: private Long id;
174: private String value;
175:
176: public LongObject(Long id, String value) {
177: this .id = id;
178: this .value = value;
179: }
180:
181: public Long getId() {
182: return id;
183: }
184:
185: public void setId(Long id) {
186: this .id = id;
187: }
188:
189: public String getValue() {
190: return value;
191: }
192:
193: public void setValue(String value) {
194: this .value = value;
195: }
196: }
197:
198: public void testNullList() throws Exception {
199: TestAction testAction = (TestAction) action;
200: testAction.setList2(null);
201:
202: SelectTag tag = new SelectTag();
203: tag.setName("collection");
204: tag.setList("list2");
205: tag.setLabel("tmjee_name");
206:
207: tag.setPageContext(pageContext);
208: try {
209: tag.doStartTag();
210: tag.doEndTag();
211: fail("exception should have been thrown value of select tag is null");
212: } catch (Exception e) {
213: assertTrue(true);
214: }
215: }
216:
217: public void testEmptyList() throws Exception {
218: TestAction testAction = (TestAction) action;
219: testAction.setList2(new ArrayList());
220:
221: SelectTag tag = new SelectTag();
222: tag.setName("collection");
223: tag.setList("list2");
224: tag.setLabel("tmjee_name");
225:
226: tag.setPageContext(pageContext);
227: tag.doStartTag();
228: tag.doEndTag();
229:
230: verify(SelectTag.class.getResource("Select-4.txt"));
231: }
232:
233: public void testMultiple() throws Exception {
234: TestAction testAction = (TestAction) action;
235: Collection collection = new ArrayList(2);
236: collection.add("hello");
237: collection.add("foo");
238: testAction.setCollection(collection);
239: testAction.setList(new String[][] { { "hello", "world" },
240: { "foo", "bar" }, { "cat", "dog" } });
241:
242: SelectTag tag = new SelectTag();
243: tag.setPageContext(pageContext);
244: tag.setLabel("mylabel");
245: tag.setName("collection");
246: tag.setList("list");
247: tag.setListKey("top[0]");
248: tag.setListValue("top[1]");
249: tag.setMultiple("true");
250: tag.setOnmousedown("alert('onmousedown');");
251: tag.setOnmousemove("alert('onmousemove');");
252: tag.setOnmouseout("alert('onmouseout');");
253: tag.setOnmouseover("alert('onmouseover');");
254: tag.setOnmouseup("alert('onmouseup');");
255:
256: tag.doStartTag();
257: tag.doEndTag();
258:
259: verify(SelectTag.class.getResource("Select-2.txt"));
260: }
261:
262: /**
263: * WW-1747 - should be a valid test case for the described issue
264: * @throws Exception
265: */
266: public void testMultipleWithLists() throws Exception {
267: TestAction testAction = (TestAction) action;
268: Collection collection = new ArrayList(2);
269:
270: collection.add(1l);
271: collection.add(300000000l);
272: testAction.setCollection(collection);
273:
274: List selectList = new ArrayList();
275: selectList.add(new LongObject(1l, "foo"));
276: selectList.add(new LongObject(2l, "bar"));
277: selectList.add(new LongObject(300000000l, "foobar"));
278: testAction.setList2(selectList);
279:
280: SelectTag tag = new SelectTag();
281: tag.setPageContext(pageContext);
282: tag.setLabel("mylabel");
283: tag.setName("collection");
284: tag.setList("list2");
285: tag.setListKey("id");
286: tag.setListValue("value");
287: tag.setMultiple("true");
288: tag.setOnmousedown("alert('onmousedown');");
289: tag.setOnmousemove("alert('onmousemove');");
290: tag.setOnmouseout("alert('onmouseout');");
291: tag.setOnmouseover("alert('onmouseover');");
292: tag.setOnmouseup("alert('onmouseup');");
293:
294: tag.doStartTag();
295: tag.doEndTag();
296:
297: verify(SelectTag.class.getResource("Select-12.txt"));
298: }
299:
300: public void testSimple() throws Exception {
301: TestAction testAction = (TestAction) action;
302: testAction.setFoo("hello");
303: testAction.setList(new String[][] { { "hello", "world" },
304: { "foo", "bar" } });
305:
306: SelectTag tag = new SelectTag();
307: tag.setPageContext(pageContext);
308: tag.setEmptyOption("true");
309: tag.setLabel("mylabel");
310: tag.setName("foo");
311: tag.setList("list");
312: tag.setListKey("top[0]");
313: tag.setListValue("top[1]");
314:
315: // header stuff
316: tag.setHeaderKey("headerKey");
317: tag.setHeaderValue("headerValue");
318:
319: // empty option
320: tag.setEmptyOption("true");
321:
322: tag.doStartTag();
323: tag.doEndTag();
324:
325: verify(SelectTag.class.getResource("Select-1.txt"));
326: }
327:
328: public void testSimpleWithNulls() throws Exception {
329: TestAction testAction = (TestAction) action;
330: testAction.setFoo("hello");
331: testAction.setList(new String[][] { { "hello", null },
332: { null, "bar" } });
333:
334: SelectTag tag = new SelectTag();
335: tag.setPageContext(pageContext);
336: tag.setEmptyOption("true");
337: tag.setLabel("mylabel");
338: tag.setName("foo");
339: tag.setList("list");
340: tag.setListKey("top[0]");
341: tag.setListValue("top[1]");
342:
343: // header stuff
344: tag.setHeaderKey("headerKey");
345: tag.setHeaderValue("headerValue");
346:
347: // empty option
348: tag.setEmptyOption("true");
349:
350: tag.doStartTag();
351: tag.doEndTag();
352:
353: verify(SelectTag.class.getResource("Select-9.txt"));
354: }
355:
356: public void testExtended() throws Exception {
357: TestAction testAction = (TestAction) action;
358: testAction.setFoo("hello");
359: testAction.setList(new String[][] { { "hello", "world" },
360: { "foo", "bar" } });
361:
362: SelectTag tag = new SelectTag();
363: tag.setPageContext(pageContext);
364: tag.setEmptyOption("true");
365: tag.setLabel("mylabel");
366: tag.setName("foo");
367: tag.setList("list");
368: tag.setListKey("top[0]");
369: tag.setListValue("%{top[0] + ' - ' + top[1]}");
370:
371: // header stuff
372: tag.setHeaderKey("headerKey");
373: tag.setHeaderValue("%{foo + ': headerValue'}");
374:
375: // empty option
376: tag.setEmptyOption("true");
377:
378: tag.doStartTag();
379: tag.doEndTag();
380:
381: verify(SelectTag.class.getResource("Select-7.txt"));
382: }
383:
384: public void testGenericSimple() throws Exception {
385: SelectTag tag = new SelectTag();
386: prepareTagGeneric(tag);
387: verifyGenericProperties(tag, "simple", new String[] { "value" });
388: }
389:
390: public void testGenericXhtml() throws Exception {
391: SelectTag tag = new SelectTag();
392: prepareTagGeneric(tag);
393: verifyGenericProperties(tag, "xhtml", new String[] { "value" });
394: }
395:
396: public void testGenericAjax() throws Exception {
397: SelectTag tag = new SelectTag();
398: prepareTagGeneric(tag);
399: verifyGenericProperties(tag, "ajax", new String[] { "value" });
400: }
401:
402: public void testMultipleOn() throws Exception {
403: SelectTag tag = new SelectTag();
404: tag.setPageContext(pageContext);
405: tag.setLabel("media1");
406: tag.setId("myId");
407: tag.setEmptyOption("true");
408: tag.setName("myName");
409: tag.setMultiple("true");
410: tag.setList("{'aaa','bbb'}");
411:
412: tag.doStartTag();
413: tag.doEndTag();
414:
415: verify(SelectTag.class.getResource("Select-5.txt"));
416: }
417:
418: public void testMultipleOff() throws Exception {
419: SelectTag tag = new SelectTag();
420: tag.setPageContext(pageContext);
421: tag.setLabel("media2");
422: tag.setId("myId");
423: tag.setEmptyOption("true");
424: tag.setName("myName");
425: tag.setMultiple("false");
426: tag.setList("{'aaa','bbb'}");
427:
428: tag.doStartTag();
429: tag.doEndTag();
430:
431: verify(SelectTag.class.getResource("Select-6.txt"));
432: }
433:
434: public void testSimpleInteger() throws Exception {
435: TestAction testAction = (TestAction) action;
436:
437: IdName hello = new IdName(new Integer(1), "hello");
438: IdName world = new IdName(new Integer(2), "world");
439: List list2 = new ArrayList();
440: list2.add(hello);
441: list2.add(world);
442: testAction.setList2(list2);
443:
444: testAction.setFooInt(new Integer(1));
445:
446: SelectTag tag = new SelectTag();
447: tag.setPageContext(pageContext);
448: tag.setEmptyOption("true");
449: tag.setLabel("mylabel");
450: tag.setName("fooInt");
451: tag.setList("list2");
452: tag.setListKey("id");
453: tag.setListValue("name");
454:
455: // header stuff
456: tag.setHeaderKey("headerKey");
457: tag.setHeaderValue("headerValue");
458:
459: // empty option
460: tag.setEmptyOption("true");
461:
462: tag.doStartTag();
463: tag.doEndTag();
464:
465: verify(SelectTag.class.getResource("Select-11.txt"));
466: }
467:
468: public void testSimpleIntegerWithValueWorkaround() throws Exception {
469: TestAction testAction = (TestAction) action;
470:
471: IdName hello = new IdName(new Integer(1), "hello");
472: IdName world = new IdName(new Integer(2), "world");
473: List list2 = new ArrayList();
474: list2.add(hello);
475: list2.add(world);
476: testAction.setList2(list2);
477:
478: testAction.setFooInt(new Integer(1));
479:
480: SelectTag tag = new SelectTag();
481: tag.setPageContext(pageContext);
482: tag.setEmptyOption("true");
483: tag.setLabel("mylabel");
484: tag.setName("fooInt");
485: tag.setList("list2");
486: tag.setListKey("id");
487: tag.setListValue("name");
488: tag.setValue("fooInt.toString()");
489:
490: // header stuff
491: tag.setHeaderKey("headerKey");
492: tag.setHeaderValue("headerValue");
493:
494: // empty option
495: tag.setEmptyOption("true");
496:
497: tag.doStartTag();
498: tag.doEndTag();
499:
500: verify(SelectTag.class.getResource("Select-11.txt"));
501: }
502:
503: public void testEnumList() throws Exception {
504:
505: SelectTag tag = new SelectTag();
506: tag.setPageContext(pageContext);
507: tag.setLabel("mylabel");
508: tag.setName("status");
509: tag.setList("statusList");
510: tag.setListKey("name");
511: tag.setListValue("displayName");
512:
513: tag.doStartTag();
514: tag.doEndTag();
515:
516: verify(SelectTag.class.getResource("Select-13.txt"));
517: }
518:
519: public class IdName {
520: private String name;
521: private Integer id;
522:
523: public IdName(Integer id, String name) {
524: this .name = name;
525: this .id = id;
526: }
527:
528: public String getName() {
529: return name;
530: }
531:
532: public Integer getId() {
533: return id;
534: }
535: }
536:
537: private void prepareTagGeneric(SelectTag tag) {
538: TestAction testAction = (TestAction) action;
539: ArrayList collection = new ArrayList();
540: collection.add("foo");
541: collection.add("bar");
542: collection.add("baz");
543:
544: testAction.setCollection(collection);
545:
546: tag.setList("collection");
547: }
548:
549: }
|