1 package com.tonbeller.wcf.convert; 2 3 import java.util.ArrayList ; 4 import java.util.HashMap ; 5 import java.util.List ; 6 import java.util.Map ; 7 8 import org.w3c.dom.Document ; 9 import org.w3c.dom.Element ; 10 11 import com.tonbeller.wcf.ui.ListBoxN; 12 import com.tonbeller.wcf.ui.ListItem; 13 14 19 public class SelectMultipleConverterTest extends ConverterTestBase { 20 Document doc; 21 Element lb; 22 Element i1; 23 Element i2; 24 Element i3; 25 Map map; 26 27 public void testSelection() throws Exception { 28 List selection = new ArrayList (); 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 [] { "x" }); 43 map.put("lb", new String [] { "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 sel = ListBoxN.getSelectedItems(lb); 59 assertEquals(1, sel.size()); 60 Element item = (Element ) 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 [] { "x" }); 70 map.put("lb", new String [] { "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 90 public SelectMultipleConverterTest(String arg0) { 91 super(arg0); 92 } 93 94 public static void main(String [] args) { 95 junit.textui.TestRunner.run(SelectMultipleConverterTest.class); 96 } 97 98 101 protected void setUp() throws Exception { 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 (); 109 } 110 } 111 | Popular Tags |