KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > convert > SelectMultipleConverterTest


1 package com.tonbeller.wcf.convert;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.Map JavaDoc;
7
8 import org.w3c.dom.Document JavaDoc;
9 import org.w3c.dom.Element JavaDoc;
10
11 import com.tonbeller.wcf.ui.ListBoxN;
12 import com.tonbeller.wcf.ui.ListItem;
13
14 /**
15  * Created on 12.11.2002
16  *
17  * @author av
18  */

19 public class SelectMultipleConverterTest extends ConverterTestBase {
20   Document JavaDoc doc;
21   Element JavaDoc lb;
22   Element JavaDoc i1;
23   Element JavaDoc i2;
24   Element JavaDoc i3;
25   Map JavaDoc map;
26
27   public void testSelection() throws Exception JavaDoc {
28     List JavaDoc selection = new ArrayList JavaDoc();
29     selection.add(i2);
30     selection.add(i3);
31     ListBoxN.setSelectedItems(lb, selection);
32     selection = ListBoxN.getSelectedItems(lb);
33     assertEquals(2, selection.size());
34     assertTrue(selection.get(0) == i2);
35     assertTrue(selection.get(1) == i3);
36     assertTrue(!ListItem.isSelected(i1));
37     assertTrue(ListItem.isSelected(i2));
38     assertTrue(ListItem.isSelected(i3));
39   }
40
41   public void testUpdateBean() throws ConvertException {
42     map.put("lb.valid", new String JavaDoc[] { "x" });
43     map.put("lb", new String JavaDoc[] { "i1", "i3" });
44     conv.validate(map, null, doc, bean);
45     int[] arr = bean.getIntArray();
46     assertEquals(2, arr.length);
47     assertEquals(1, arr[0]);
48     assertEquals(3, arr[1]);
49     assertTrue(ListItem.isSelected(i1));
50     assertTrue(!ListItem.isSelected(i2));
51     assertTrue(ListItem.isSelected(i3));
52
53   }
54
55   public void testUpdateDom() throws ConvertException {
56     bean.setIntArray(new int[] { 2 });
57     conv.revert(bean, doc);
58     List JavaDoc sel = ListBoxN.getSelectedItems(lb);
59     assertEquals(1, sel.size());
60     Element JavaDoc item = (Element JavaDoc) sel.get(0);
61     assertTrue(item == i2);
62     assertTrue(!ListItem.isSelected(i1));
63     assertTrue(ListItem.isSelected(i2));
64     assertTrue(!ListItem.isSelected(i3));
65   }
66
67   public void testInvalidItemId() {
68     try {
69       map.put("lb.valid", new String JavaDoc[] { "x" });
70       map.put("lb", new String JavaDoc[] { "invalid-id" });
71       conv.validate(map, null, doc, bean);
72       assertTrue("Exception expected", false);
73     } catch (ConvertException e) {
74     }
75   }
76
77   public void testInvalidBeanValue() {
78     try {
79       bean.setIntArray(new int[] { 99 });
80       conv.revert(bean, doc);
81       assertTrue("Exception expected", false);
82     } catch (ConvertException e) {
83     }
84   }
85
86   /**
87    * Constructor for SelectMultipleConverterTest.
88    * @param arg0
89    */

90   public SelectMultipleConverterTest(String JavaDoc arg0) {
91     super(arg0);
92   }
93
94   public static void main(String JavaDoc[] args) {
95     junit.textui.TestRunner.run(SelectMultipleConverterTest.class);
96   }
97
98   /**
99    * @see junit.framework.TestCase#setUp()
100    */

101   protected void setUp() throws Exception JavaDoc {
102     super.setUp();
103     doc = load("ListBoxN.xml");
104     lb = getElement(doc, "//listBoxN");
105     i1 = getElement(doc, "//listItem[@id='i1']");
106     i2 = getElement(doc, "//listItem[@id='i2']");
107     i3 = getElement(doc, "//listItem[@id='i3']");
108     map = new HashMap JavaDoc();
109   }
110 }
111
Popular Tags