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