1 package com.tonbeller.wcf.convert; 2 3 import java.util.Date ; 4 import java.util.HashMap ; 5 import java.util.Map ; 6 7 import org.w3c.dom.Document ; 8 import org.w3c.dom.Element ; 9 10 import com.tonbeller.wcf.format.FormatException; 11 import com.tonbeller.wcf.ui.TextField; 12 13 18 public class EditCtrlConverterTest extends ConverterTestBase { 19 20 Document doc; 21 22 26 public EditCtrlConverterTest(String arg0) { 27 super(arg0); 28 } 29 30 public static void main(String [] args) { 31 junit.textui.TestRunner.run(EditCtrlConverterTest.class); 32 } 33 34 37 public void testNull() throws Exception { 38 conv.revert(bean, doc); 39 Map map = new HashMap (); 40 conv.validate(map, null, doc, null); 41 } 42 43 public void testSetNode() throws Exception { 44 bean.setStringValue("hello"); 45 bean.setIntValue(124); 46 Date date = getDate(12, 5, 2000); 47 bean.setDateValue(date); 48 49 conv.revert(bean, doc); 50 51 assertEquals("hello", TextField.getValue(getElement(doc, "//textField[@id='stringId']"))); 52 assertEquals("124", TextField.getValue(getElement(doc, "//textField[@id='intId']"))); 53 assertEquals("12.05.2000", TextField.getValue(getElement(doc, "//textField[@id='dateId']"))); 54 } 55 56 public void testSetBean() throws Exception { 57 Map map = new HashMap (); 58 map.put("stringId", new String [] { "hello" }); 59 map.put("intId", new String [] { "124" }); 60 map.put("dateId", new String [] { "12.05.2000" }); 61 62 conv.validate(map, null, doc, bean); 63 64 assertEquals("hello", bean.getStringValue()); 65 assertEquals(124, bean.getIntValue()); 66 assertEquals(getDate(12, 5, 2000), bean.getDateValue()); 67 } 68 69 public void testError() throws Exception { 70 Map map = new HashMap (); 71 map.put("intId", new String [] { "124x" }); 72 Element elem = getElement(doc, "//textField[@id='intId']"); 73 try { 74 conv.validate(map, null, doc, bean); 75 assertTrue("Exception expected", false); 76 } catch (FormatException e) { 77 } 78 assertTrue(elem.getAttribute("error").trim().length() > 0); 80 81 conv.revert(bean, doc); 82 assertEquals(0, elem.getAttribute("error").length()); 83 84 } 85 86 89 public void testConvert() throws Exception { 90 Element elm = getElement(doc, "//textField[@id='intId']"); 91 TextField.setModelReference(elm, "doubleValue"); 92 Map map = new HashMap (); 93 map.put("intId", new String [] { "129" }); 94 conv.validate(map, null, doc, bean); 95 assertTrue(Math.abs(bean.getDoubleValue() - 129.0) < 0.001); 96 } 97 98 101 public void testPassword() throws Exception { 102 Element elem = getElement(doc, "//password"); 103 TextField.setModelReference(elem, "stringValue"); 104 Map map = new HashMap (); 105 map.put("passwordId", new String [] { "secret" }); 106 conv.validate(map, null, doc, bean); 107 assertEquals("secret", bean.getStringValue()); 108 109 TextField.setValue(elem, "ops"); 110 conv.revert(bean, doc); 111 assertEquals("secret", TextField.getValue(elem)); 112 } 113 114 public void testTextArea() throws Exception { 115 Element elem = getElement(doc, "//textArea"); 116 TextField.setModelReference(elem, "stringValue"); 117 118 Map map = new HashMap (); 120 map.put("textAreaId", new String [] { "userinput" }); 121 conv.validate(map, null, doc, bean); 122 assertEquals("userinput", bean.getStringValue()); 123 124 TextField.setValue(elem, "ops"); 126 conv.revert(bean, doc); 127 assertEquals("userinput", TextField.getValue(elem)); 128 129 TextField.setModelReference(elem, ""); 131 TextField.setValue(elem, "ops1"); 132 bean.setStringValue("ops2"); 133 conv.revert(bean, doc); 134 assertEquals("ops1", TextField.getValue(elem)); 135 assertEquals("ops2", bean.getStringValue()); 136 conv.validate(map, null, doc, bean); 137 assertEquals("userinput", TextField.getValue(elem)); 138 assertEquals("ops2", bean.getStringValue()); 139 } 140 141 144 protected void setUp() throws Exception { 145 super.setUp(); 146 doc = load("EditCtrlConverter.xml"); 147 148 } 149 150 } 151 | Popular Tags |