1
2
3
4
5
6
7
8 package org.dom4j.datatype;
9
10 import junit.textui.TestRunner;
11
12 import java.math.BigInteger;
13
14 import org.dom4j.AbstractTestCase;
15 import org.dom4j.Attribute;
16 import org.dom4j.Document;
17 import org.dom4j.Element;
18 import org.dom4j.Namespace;
19 import org.dom4j.QName;
20
21 /***
22 * Tests setting the value of datatype aware element or attribute value
23 *
24 * @author <a href="mailto:jstrachan@apache.org">James Strachan </a>
25 * @version $Revision: 1.4 $
26 */
27 public class SetDataTest extends AbstractTestCase {
28 private DatatypeDocumentFactory factory = new DatatypeDocumentFactory();
29
30 public static void main(String[] args) {
31 TestRunner.run(SetDataTest.class);
32 }
33
34
35
36 public void testAttribute() throws Exception {
37 QName personName = factory.createQName("person");
38 QName ageName = factory.createQName("age");
39
40 Element person = factory.createElement(personName);
41
42 person.addAttribute(ageName, "10");
43
44 Attribute age = person.attribute(ageName);
45
46 assertTrue("Created DatatypeAttribute not correct",
47 age instanceof DatatypeAttribute);
48
49 log("Found attribute: " + age);
50
51 Object data = age.getData();
52 Object expected = new BigInteger("10");
53
54 assertEquals("Data is correct type", BigInteger.class, data.getClass());
55
56 assertEquals("Set age correctly", expected, data);
57
58 age.setValue("32");
59 data = age.getData();
60 expected = new BigInteger("32");
61
62 assertEquals("Set age correctly", expected, data);
63
64 /***
65 * not sure if numeric types should be round tripped back to BigDecimal
66 * (say) age.setData( new Long( 21 ) ); data = age.getData(); expected =
67 * new BigInteger( "21" ); assertEquals( "Set age correctly", expected,
68 * data );
69 */
70
71
72 try {
73 age.setValue("abc");
74 fail("Appeared to set an invalid value");
75 } catch (IllegalArgumentException e) {
76 }
77 }
78
79 public void testAttributeWithNamespace() throws Exception {
80 QName personName = factory.createQName("person", "t", "urn://testing");
81 QName ageName = factory.createQName("age", "t", "urn://testing");
82
83 Element person = factory.createElement(personName);
84
85 person.addAttribute(ageName, "10");
86
87 Attribute age = person.attribute(ageName);
88
89 assertTrue("Created DatatypeAttribute not correct",
90 age instanceof DatatypeAttribute);
91
92 log("Found attribute: " + age);
93
94 Object data = age.getData();
95 Object expected = new BigInteger("10");
96
97 assertEquals("Data is correct type", BigInteger.class, data.getClass());
98
99 assertEquals("Set age correctly", expected, data);
100
101 age.setValue("32");
102 data = age.getData();
103 expected = new BigInteger("32");
104
105 assertEquals("Set age correctly", expected, data);
106
107 try {
108 age.setValue("abc");
109 fail("Appeared to set an invalid value");
110 } catch (IllegalArgumentException e) {
111 }
112 }
113
114 public void testElement() throws Exception {
115 QName personName = factory.createQName("person");
116 QName numberOfCarsName = factory.createQName("numberOfCars");
117
118 Element person = factory.createElement(personName);
119
120 Element cars = person.addElement(numberOfCarsName);
121
122 log("Found element: " + cars);
123
124 Object expected = new Short((short) 10);
125 cars.setData(expected);
126
127 Object data = cars.getData();
128
129 assertEquals("Data is correct type", Short.class, data.getClass());
130 assertEquals("Set cars correctly", expected, data);
131
132 cars.setData(new Short((short) 32));
133 data = cars.getData();
134 expected = new Short((short) 32);
135
136 assertEquals("Set cars correctly", expected, data);
137
138 cars.setText("34");
139 data = cars.getData();
140 expected = new Short((short) 34);
141
142 assertEquals("Set cars correctly", expected, data);
143
144
145 try {
146 cars.setText("abc");
147 fail("Appeared to set an invalid value");
148 } catch (IllegalArgumentException e) {
149 }
150 }
151
152
153
154 protected void setUp() throws Exception {
155 super.setUp();
156
157 Document schema = getDocument("/xml/test/schema/personal.xsd");
158 factory.loadSchema(schema);
159
160 Namespace ns = new Namespace("t", "urn://testing");
161 factory.loadSchema(schema, ns);
162 }
163 }
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200