KickJava   Java API By Example, From Geeks To Geeks.

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


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

18 public class EditCtrlConverterTest extends ConverterTestBase {
19
20   Document JavaDoc doc;
21
22   /**
23    * Constructor for EditCtrlConverterTest.
24    * @param arg0
25    */

26   public EditCtrlConverterTest(String JavaDoc arg0) {
27     super(arg0);
28   }
29
30   public static void main(String JavaDoc[] args) {
31     junit.textui.TestRunner.run(EditCtrlConverterTest.class);
32   }
33
34   /*
35    * bean was not initialized, should not crash
36    */

37   public void testNull() throws Exception JavaDoc {
38     conv.revert(bean, doc);
39     Map JavaDoc map = new HashMap JavaDoc();
40     conv.validate(map, null, doc, null);
41   }
42
43   public void testSetNode() throws Exception JavaDoc {
44     bean.setStringValue("hello");
45     bean.setIntValue(124);
46     Date JavaDoc 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 JavaDoc {
57     Map JavaDoc map = new HashMap JavaDoc();
58     map.put("stringId", new String JavaDoc[] { "hello" });
59     map.put("intId", new String JavaDoc[] { "124" });
60     map.put("dateId", new String JavaDoc[] { "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 JavaDoc {
70     Map JavaDoc map = new HashMap JavaDoc();
71     map.put("intId", new String JavaDoc[] { "124x" });
72     Element JavaDoc 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     // non empty error message
79
assertTrue(elem.getAttribute("error").trim().length() > 0);
80
81     conv.revert(bean, doc);
82     assertEquals(0, elem.getAttribute("error").length());
83
84   }
85
86   /*
87    * set double property from int field
88    */

89   public void testConvert() throws Exception JavaDoc {
90     Element JavaDoc elm = getElement(doc, "//textField[@id='intId']");
91     TextField.setModelReference(elm, "doubleValue");
92     Map JavaDoc map = new HashMap JavaDoc();
93     map.put("intId", new String JavaDoc[] { "129" });
94     conv.validate(map, null, doc, bean);
95     assertTrue(Math.abs(bean.getDoubleValue() - 129.0) < 0.001);
96   }
97
98   /*
99    * password does not have a type attribute!
100    */

101   public void testPassword() throws Exception JavaDoc {
102     Element JavaDoc elem = getElement(doc, "//password");
103     TextField.setModelReference(elem, "stringValue");
104     Map JavaDoc map = new HashMap JavaDoc();
105     map.put("passwordId", new String JavaDoc[] { "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 JavaDoc {
115     Element JavaDoc elem = getElement(doc, "//textArea");
116     TextField.setModelReference(elem, "stringValue");
117
118     // set value to bean
119
Map JavaDoc map = new HashMap JavaDoc();
120     map.put("textAreaId", new String JavaDoc[] { "userinput" });
121     conv.validate(map, null, doc, bean);
122     assertEquals("userinput", bean.getStringValue());
123
124     // bean value to dom
125
TextField.setValue(elem, "ops");
126     conv.revert(bean, doc);
127     assertEquals("userinput", TextField.getValue(elem));
128
129     // no modelReference -> ignored in both directions
130
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   /**
142    * @see junit.framework.TestCase#setUp()
143    */

144   protected void setUp() throws Exception JavaDoc {
145     super.setUp();
146     doc = load("EditCtrlConverter.xml");
147
148   }
149
150 }
151
Popular Tags