|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| package nu.xom.tests; |
|
23 |
| |
|
24 |
| import java.io.File; |
|
25 |
| import java.io.IOException; |
|
26 |
| |
|
27 |
| import nu.xom.Attribute; |
|
28 |
| import nu.xom.Builder; |
|
29 |
| import nu.xom.Document; |
|
30 |
| import nu.xom.Element; |
|
31 |
| import nu.xom.IllegalDataException; |
|
32 |
| import nu.xom.IllegalNameException; |
|
33 |
| import nu.xom.MalformedURIException; |
|
34 |
| import nu.xom.NamespaceConflictException; |
|
35 |
| import nu.xom.ParsingException; |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| public class AttributeTest extends XOMTestCase { |
|
47 |
| |
|
48 |
37
| public AttributeTest(String name) {
|
|
49 |
37
| super(name);
|
|
50 |
| } |
|
51 |
| |
|
52 |
| |
|
53 |
| private Attribute a1; |
|
54 |
| private Attribute a2; |
|
55 |
| |
|
56 |
| |
|
57 |
37
| protected void setUp() {
|
|
58 |
37
| a1 = new Attribute("test", "value");
|
|
59 |
37
| a2 = new Attribute("test", " value ");
|
|
60 |
| } |
|
61 |
| |
|
62 |
| |
|
63 |
1
| public void testGetChildCount() {
|
|
64 |
1
| assertEquals(0, a1.getChildCount());
|
|
65 |
| } |
|
66 |
| |
|
67 |
| |
|
68 |
1
| public void testNullType() {
|
|
69 |
1
| try {
|
|
70 |
1
| a1.setType(null);
|
|
71 |
0
| fail("Didn't throw NullPointerException");
|
|
72 |
| } |
|
73 |
| catch (NullPointerException success) { |
|
74 |
1
| assertNotNull(success.getMessage());
|
|
75 |
| } |
|
76 |
| } |
|
77 |
| |
|
78 |
| |
|
79 |
1
| public void testGetChild() {
|
|
80 |
1
| try {
|
|
81 |
1
| a1.getChild(0);
|
|
82 |
0
| fail("Didn't throw IndexOutofBoundsException");
|
|
83 |
| } |
|
84 |
| catch (IndexOutOfBoundsException success) { |
|
85 |
| |
|
86 |
| } |
|
87 |
| } |
|
88 |
| |
|
89 |
| |
|
90 |
1
| public void testConstructor() {
|
|
91 |
1
| assertEquals("test", a1.getLocalName());
|
|
92 |
1
| assertEquals("test", a1.getQualifiedName());
|
|
93 |
1
| assertEquals("", a1.getNamespacePrefix());
|
|
94 |
1
| assertEquals("", a1.getNamespaceURI());
|
|
95 |
1
| assertEquals("value", a1.getValue());
|
|
96 |
1
| assertEquals(" value ", a2.getValue());
|
|
97 |
| } |
|
98 |
| |
|
99 |
| |
|
100 |
1
| public void testConstructor2() {
|
|
101 |
| |
|
102 |
1
| Attribute a1 = new Attribute("name", "value", Attribute.Type.CDATA);
|
|
103 |
1
| assertEquals("name", a1.getLocalName());
|
|
104 |
1
| assertEquals("name", a1.getQualifiedName());
|
|
105 |
1
| assertEquals("", a1.getNamespacePrefix());
|
|
106 |
1
| assertEquals("", a1.getNamespaceURI());
|
|
107 |
1
| assertEquals("value", a1.getValue());
|
|
108 |
1
| assertEquals(Attribute.Type.CDATA, a1.getType());
|
|
109 |
| } |
|
110 |
| |
|
111 |
| |
|
112 |
1
| public void testGetExternalForm() {
|
|
113 |
| |
|
114 |
1
| Attribute a1 = new Attribute("test", "value contains a \"");
|
|
115 |
1
| assertEquals("test=\"value contains a "\"", a1.toXML());
|
|
116 |
| |
|
117 |
1
| Attribute a2 = new Attribute("test", "value contains a '");
|
|
118 |
1
| assertEquals("test=\"value contains a '\"", a2.toXML());
|
|
119 |
| |
|
120 |
| } |
|
121 |
| |
|
122 |
| |
|
123 |
1
| public void testSetLocalName() {
|
|
124 |
| |
|
125 |
1
| Attribute a = new Attribute("name", "value");
|
|
126 |
1
| a.setLocalName("newname");
|
|
127 |
1
| assertEquals("newname", a.getLocalName());
|
|
128 |
| |
|
129 |
1
| try {
|
|
130 |
1
| a.setLocalName("pre:a");
|
|
131 |
0
| fail("Allowed local attribute name containing colon");
|
|
132 |
| } |
|
133 |
| catch (IllegalNameException success) { |
|
134 |
1
| assertNotNull(success.getMessage());
|
|
135 |
| } |
|
136 |
| |
|
137 |
| } |
|
138 |
| |
|
139 |
| |
|
140 |
1
| public void testSetLocalNameInNamespaceQualifiedAttribute() {
|
|
141 |
| |
|
142 |
1
| Attribute a = new Attribute("pre:name", "http://www.example.org", "value");
|
|
143 |
1
| a.setLocalName("newname");
|
|
144 |
1
| assertEquals("newname", a.getLocalName());
|
|
145 |
1
| assertEquals("pre:newname", a.getQualifiedName());
|
|
146 |
| |
|
147 |
| } |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
1
| public void testXmlns() {
|
|
152 |
| |
|
153 |
1
| try {
|
|
154 |
1
| new Attribute("xmlns", "http://www.w3.org/TR");
|
|
155 |
0
| fail("Created attribute with name xmlns");
|
|
156 |
| } |
|
157 |
| catch (IllegalNameException success) { |
|
158 |
1
| assertNotNull(success.getMessage());
|
|
159 |
| } |
|
160 |
| |
|
161 |
1
| try {
|
|
162 |
1
| new Attribute("xmlns:prefix", "http://www.w3.org/TR");
|
|
163 |
0
| fail("Created attribute with name xmlns:prefix");
|
|
164 |
| } |
|
165 |
| catch (IllegalNameException success) { |
|
166 |
1
| assertNotNull(success.getMessage());
|
|
167 |
| } |
|
168 |
| |
|
169 |
| |
|
170 |
1
| try {
|
|
171 |
1
| new Attribute("xmlns", "http://www.w3.org/2000/xmlns/", "http://www.w3.org/");
|
|
172 |
0
| fail("created xmlns attribute");
|
|
173 |
| } |
|
174 |
| catch (IllegalNameException success) { |
|
175 |
1
| assertNotNull(success.getMessage());
|
|
176 |
| } |
|
177 |
| |
|
178 |
| |
|
179 |
1
| try {
|
|
180 |
1
| new Attribute("xmlns:pre", "http://www.w3.org/2000/xmlns/", "http://www.w3.org/");
|
|
181 |
0
| fail("created xmlns:pre attribute");
|
|
182 |
| } |
|
183 |
| catch (IllegalNameException success) { |
|
184 |
1
| assertNotNull(success.getMessage());
|
|
185 |
| } |
|
186 |
| |
|
187 |
| } |
|
188 |
| |
|
189 |
| |
|
190 |
| |
|
191 |
| |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
| |
|
196 |
| |
|
197 |
| |
|
198 |
| |
|
199 |
| |
|
200 |
| |
|
201 |
| |
|
202 |
1
| public void testXMLBase() {
|
|
203 |
| |
|
204 |
1
| String xmlNamespace = "http://www.w3.org/XML/1998/namespace";
|
|
205 |
1
| Attribute a1 = new Attribute("xml:base", xmlNamespace, "http://www.w3.org/");
|
|
206 |
1
| assertEquals( "base", a1.getLocalName());
|
|
207 |
1
| assertEquals("xml:base", a1.getQualifiedName());
|
|
208 |
1
| assertEquals(xmlNamespace, a1.getNamespaceURI());
|
|
209 |
| |
|
210 |
1
| a1.setValue("http://www.example.com/>");
|
|
211 |
1
| assertEquals("http://www.example.com/>", a1.getValue());
|
|
212 |
| |
|
213 |
1
| a1.setValue("http://www.example.com/<");
|
|
214 |
1
| assertEquals("http://www.example.com/<", a1.getValue());
|
|
215 |
| |
|
216 |
1
| a1.setValue("http://www.example.com/\u00FE");
|
|
217 |
1
| assertEquals(a1.getValue(), "http://www.example.com/\u00FE");
|
|
218 |
| |
|
219 |
| } |
|
220 |
| |
|
221 |
| |
|
222 |
1
| public void testXmlPrefix() {
|
|
223 |
| |
|
224 |
1
| try {
|
|
225 |
1
| new Attribute("xml:base", "http://www.w3.org/TR");
|
|
226 |
0
| fail("Created attribute with name xml:base");
|
|
227 |
| } |
|
228 |
| catch (NamespaceConflictException success) { |
|
229 |
1
| assertNotNull(success.getMessage());
|
|
230 |
| } |
|
231 |
| |
|
232 |
1
| try {
|
|
233 |
1
| new Attribute("xml:space", "preserve");
|
|
234 |
0
| fail("Created attribute with local name xml:space");
|
|
235 |
| } |
|
236 |
| catch (NamespaceConflictException success) { |
|
237 |
1
| assertNotNull(success.getMessage());
|
|
238 |
| } |
|
239 |
| |
|
240 |
1
| try {
|
|
241 |
1
| new Attribute("xml:lang", "fr-FR");
|
|
242 |
0
| fail("Created attribute with name xml:lang");
|
|
243 |
| } |
|
244 |
| catch (NamespaceConflictException success) { |
|
245 |
1
| assertNotNull(success.getMessage());
|
|
246 |
| } |
|
247 |
| |
|
248 |
1
| String xmlNamespace = "http://www.w3.org/XML/1998/namespace";
|
|
249 |
1
| Attribute a1 = new Attribute(
|
|
250 |
| "xml:base", xmlNamespace, "http://www.w3.org/"); |
|
251 |
1
| assertEquals("base", a1.getLocalName());
|
|
252 |
1
| assertEquals("xml:base", a1.getQualifiedName());
|
|
253 |
1
| assertEquals(xmlNamespace, a1.getNamespaceURI());
|
|
254 |
| |
|
255 |
1
| Attribute a2 = new Attribute("xml:space", xmlNamespace, "preserve");
|
|
256 |
1
| assertEquals(a2.getLocalName(), "space");
|
|
257 |
1
| assertEquals("xml:space", a2.getQualifiedName());
|
|
258 |
1
| assertEquals(xmlNamespace, a2.getNamespaceURI());
|
|
259 |
| |
|
260 |
1
| Attribute a3
|
|
261 |
| = new Attribute("xml:lang", xmlNamespace, "en-UK"); |
|
262 |
1
| assertEquals("lang", a3.getLocalName());
|
|
263 |
1
| assertEquals("xml:lang", a3.getQualifiedName());
|
|
264 |
1
| assertEquals(xmlNamespace, a3.getNamespaceURI());
|
|
265 |
| |
|
266 |
1
| try {
|
|
267 |
1
| new Attribute("xml:base", "http://www.notTheXMLNamespace",
|
|
268 |
| "http://www.w3.org/"); |
|
269 |
0
| fail("remapped xml prefix");
|
|
270 |
| } |
|
271 |
| catch (NamespaceConflictException success) { |
|
272 |
1
| assertNotNull(success.getMessage());
|
|
273 |
| } |
|
274 |
| |
|
275 |
| } |
|
276 |
| |
|
277 |
| |
|
278 |
1
| public void testXMLLangAttributeCanBeEmpty() {
|
|
279 |
| |
|
280 |
1
| String xmlNamespace = "http://www.w3.org/XML/1998/namespace";
|
|
281 |
1
| Attribute a = new Attribute("xml:lang", xmlNamespace, "");
|
|
282 |
1
| assertEquals("", a.getValue());
|
|
283 |
| |
|
284 |
| } |
|
285 |
| |
|
286 |
| |
|
287 |
1
| public void testWrongPrefixNotAllowedWithXMLURI() {
|
|
288 |
| |
|
289 |
1
| try {
|
|
290 |
1
| new Attribute("test:base", "http://www.w3.org/XML/1998/namespace", "value");
|
|
291 |
0
| fail("Allowed XML namespace to be associated with non-xml prefix");
|
|
292 |
| } |
|
293 |
| catch (NamespaceConflictException success) { |
|
294 |
1
| assertNotNull(success.getMessage());
|
|
295 |
| } |
|
296 |
| |
|
297 |
| } |
|
298 |
| |
|
299 |
| |
|
300 |
1
| public void testToString() {
|
|
301 |
1
| assertEquals(
|
|
302 |
| "[nu.xom.Attribute: test=\"value\"]", a1.toString()); |
|
303 |
1
| assertEquals(
|
|
304 |
| "[nu.xom.Attribute: test=\" value \"]", a2.toString()); |
|
305 |
| } |
|
306 |
| |
|
307 |
| |
|
308 |
1
| public void testToStringWithLineFeed() {
|
|
309 |
| |
|
310 |
1
| Attribute a = new Attribute("name", "content\ncontent");
|
|
311 |
1
| assertEquals("[nu.xom.Attribute: name=\"content\\ncontent\"]", a.toString());
|
|
312 |
| |
|
313 |
| } |
|
314 |
| |
|
315 |
| |
|
316 |
1
| public void testToStringWithCarriageReturnLineFeed() {
|
|
317 |
| |
|
318 |
1
| Attribute a = new Attribute("name", "content\r\ncontent");
|
|
319 |
1
| assertEquals("[nu.xom.Attribute: name=\"content\\r\\ncontent\"]", a.toString());
|
|
320 |
| |
|
321 |
| } |
|
322 |
| |
|
323 |
| |
|
324 |
1
| public void testToStringWithCarriageReturn() {
|
|
325 |
| |
|
326 |
1
| Attribute a = new Attribute("name", "content\rcontent");
|
|
327 |
1
| assertEquals("[nu.xom.Attribute: name=\"content\\rcontent\"]", a.toString());
|
|
328 |
| |
|
329 |
| } |
|
330 |
| |
|
331 |
| |
|
332 |
1
| public void testToStringWithLotsOfData() {
|
|
333 |
| |
|
334 |
1
| Attribute a = new Attribute("name",
|
|
335 |
| "012345678901234567890123456789012345678901234567890123456789"); |
|
336 |
1
| String s = a.toString();
|
|
337 |
1
| assertEquals(
|
|
338 |
| "[nu.xom.Attribute: name=\"01234567890123456789012345678901234...\"]", |
|
339 |
| s); |
|
340 |
| |
|
341 |
| } |
|
342 |
| |
|
343 |
| |
|
344 |
1
| public void testToXML() {
|
|
345 |
1
| assertEquals("test=\"value\"", a1.toXML());
|
|
346 |
1
| assertEquals("test=\" value \"", a2.toXML());
|
|
347 |
| } |
|
348 |
| |
|
349 |
| |
|
350 |
1
| public void testEscapingWithToXML() {
|
|
351 |
| |
|
352 |
1
| a1.setValue("<");
|
|
353 |
1
| assertEquals("test=\"<\"", a1.toXML());
|
|
354 |
1
| a1.setValue(">");
|
|
355 |
1
| assertEquals("test=\">\"", a1.toXML());
|
|
356 |
1
| a1.setValue("\"");
|
|
357 |
1
| assertEquals("test=\""\"", a1.toXML());
|
|
358 |
1
| a1.setValue("\'");
|
|
359 |
1
| assertEquals("test=\"'\"", a1.toXML());
|
|
360 |
1
| a1.setValue("&");
|
|
361 |
1
| assertEquals("test=\"&\"", a1.toXML());
|
|
362 |
| |
|
363 |
| } |
|
364 |
| |
|
365 |
| |
|
366 |
1
| public void testWhiteSpaceEscapingWithToXML() {
|
|
367 |
| |
|
368 |
1
| a1.setValue(" ");
|
|
369 |
1
| assertEquals("test=\" \"", a1.toXML());
|
|
370 |
1
| a1.setValue("\n");
|
|
371 |
1
| assertEquals("test=\"
\"", a1.toXML());
|
|
372 |
1
| a1.setValue("\r");
|
|
373 |
1
| assertEquals("test=\"
\"", a1.toXML());
|
|
374 |
1
| a1.setValue("\t");
|
|
375 |
1
| assertEquals("test=\"	\"", a1.toXML());
|
|
376 |
| |
|
377 |
| } |
|
378 |
| |
|
379 |
| |
|
380 |
1
| public void testSetValue() {
|
|
381 |
| |
|
382 |
1
| String[] legal = {
|
|
383 |
| "Hello", |
|
384 |
| "hello there", |
|
385 |
| " spaces on both ends ", |
|
386 |
| " quotes \" \" quotes", |
|
387 |
| " single \'\' quotes", |
|
388 |
| " both double and single \"\'\"\' quotes", |
|
389 |
| " angle brackets < > <<<", |
|
390 |
| " carriage returns \r\r\r", |
|
391 |
| " ampersands & &&& &name; " |
|
392 |
| }; |
|
393 |
| |
|
394 |
| |
|
395 |
1
| for (int i = 0; i < legal.length; i++) {
|
|
396 |
9
| a1.setValue(legal[i]);
|
|
397 |
9
| assertEquals(legal[i], a1.getValue());
|
|
398 |
| } |
|
399 |
| |
|
400 |
1
| try {
|
|
401 |
1
| a1.setValue("test \u0000 test ");
|
|
402 |
0
| fail("Should raise an IllegalDataException");
|
|
403 |
| } |
|
404 |
| catch (IllegalDataException ex) { |
|
405 |
| |
|
406 |
1
| assertNotNull(ex.getMessage());
|
|
407 |
| } |
|
408 |
| |
|
409 |
| } |
|
410 |
| |
|
411 |
| |
|
412 |
1
| public void testNames() {
|
|
413 |
| |
|
414 |
1
| String prefix = "testPrefix";
|
|
415 |
1
| String name = "testName";
|
|
416 |
1
| String URI = "http://www.elharo.com/";
|
|
417 |
1
| String value = " here's some data";
|
|
418 |
| |
|
419 |
| |
|
420 |
1
| Attribute a1 = new Attribute(prefix + ":" + name, URI, value);
|
|
421 |
1
| assertEquals(name, a1.getLocalName());
|
|
422 |
1
| assertEquals(prefix + ":" + name, a1.getQualifiedName());
|
|
423 |
1
| assertEquals(URI, a1.getNamespaceURI());
|
|
424 |
| } |
|
425 |
| |
|
426 |
| |
|
427 |
1
| public void testEquals() {
|
|
428 |
1
| Attribute c1 = new Attribute("test", "limit");
|
|
429 |
1
| Attribute c2 = new Attribute("test", "limit");
|
|
430 |
1
| Attribute c3 = new Attribute("retina", "retina test");
|
|
431 |
| |
|
432 |
1
| assertEquals(c1, c1);
|
|
433 |
1
| assertEquals(c1.hashCode(), c1.hashCode());
|
|
434 |
1
| assertTrue(!c1.equals(c2));
|
|
435 |
1
| assertTrue(!c1.equals(c3));
|
|
436 |
1
| assertTrue(!c1.equals(null));
|
|
437 |
1
| assertFalse(c1.equals("limit"));
|
|
438 |
1
| assertFalse(c1.equals(new Element("test")));
|
|
439 |
| } |
|
440 |
| |
|
441 |
| |
|
442 |
1
| public void testTypeEquals() {
|
|
443 |
1
| assertEquals(Attribute.Type.CDATA, Attribute.Type.CDATA);
|
|
444 |
1
| assertTrue(!Attribute.Type.CDATA.equals(Attribute.Type.NMTOKEN));
|
|
445 |
1
| assertTrue(!Attribute.Type.CDATA.equals(null));
|
|
446 |
1
| assertFalse(Attribute.Type.CDATA.equals("CDATA"));
|
|
447 |
1
| assertFalse(Attribute.Type.CDATA.equals(new Element("CDATA")));
|
|
448 |
| } |
|
449 |
| |
|
450 |
| |
|
451 |
1
| public void testCopyConstructor() {
|
|
452 |
1
| Attribute c1 = new Attribute("test", "data");
|
|
453 |
1
| Attribute c2 = new Attribute(c1);
|
|
454 |
| |
|
455 |
1
| assertEquals(c1.getValue(), c2.getValue());
|
|
456 |
1
| assertEquals(c1.getLocalName(), c2.getLocalName());
|
|
457 |
1
| assertEquals(c1.getQualifiedName(), c2.getQualifiedName());
|
|
458 |
1
| assertEquals(c1.getValue(), c2.getValue());
|
|
459 |
1
| assertTrue(!c1.equals(c2));
|
|
460 |
1
| assertNull(c2.getParent());
|
|
461 |
| |
|
462 |
| } |
|
463 |
| |
|
464 |
| |
|
465 |
| |
|
466 |
| |
|
467 |
1
| public void testSurrogates() {
|
|
468 |
| |
|
469 |
1
| String goodString = "test: \uD8F5\uDF80 ";
|
|
470 |
1
| Attribute c = new Attribute("surrogate", goodString);
|
|
471 |
1
| assertEquals(goodString, c.getValue());
|
|
472 |
| |
|
473 |
| |
|
474 |
1
| try {
|
|
475 |
1
| new Attribute("surrogate", "test: \uD8F5\uDBF0 ");
|
|
476 |
0
| fail("Should raise an IllegalDataException");
|
|
477 |
| } |
|
478 |
| catch (IllegalDataException success) { |
|
479 |
1
| assertEquals("test: \uD8F5\uDBF0 ", success.getData());
|
|
480 |
1
| assertNotNull(success.getMessage());
|
|
481 |
| } |
|
482 |
| |
|
483 |
| |
|
484 |
1
| try {
|
|
485 |
1
| new Attribute("surrogate", "test: \uD8F5\uD8F5 ");
|
|
486 |
0
| fail("Should raise an IllegalDataException");
|
|
487 |
| } |
|
488 |
| catch (IllegalDataException success) { |
|
489 |
1
| assertEquals("test: \uD8F5\uD8F5 ", success.getData());
|
|
490 |
1
| assertNotNull(success.getMessage());
|
|
491 |
| } |
|
492 |
| |
|
493 |
| |
|
494 |
1
| try {
|
|
495 |
1
| new Attribute("surrogate", "test: \uD8F5 ");
|
|
496 |
0
| fail("Should raise an IllegalDataException");
|
|
497 |
| } |
|
498 |
| catch (IllegalDataException success) { |
|
499 |
1
| assertEquals("test: \uD8F5 ", success.getData());
|
|
500 |
1
| assertNotNull(success.getMessage());
|
|
501 |
| } |
|
502 |
| |
|
503 |
| |
|
504 |
1
| try {
|
|
505 |
1
| new Attribute("surrogate", "test: \uDF80 ");
|
|
506 |
0
| fail("One low half");
|
|
507 |
| } |
|
508 |
| catch (IllegalDataException success) { |
|
509 |
1
| assertEquals("test: \uDF80 ", success.getData());
|
|
510 |
1
| assertNotNull(success.getMessage());
|
|
511 |
| } |
|
512 |
| |
|
513 |
| |
|
514 |
1
| try {
|
|
515 |
1
| new Attribute("surrogate", "test: \uDCF5\uD8F5 ");
|
|
516 |
0
| fail("Low half before high half");
|
|
517 |
| } |
|
518 |
| catch (IllegalDataException success) { |
|
519 |
1
| assertEquals("test: \uDCF5\uD8F5 ", success.getData());
|
|
520 |
1
| assertNotNull(success.getMessage());
|
|
521 |
| } |
|
522 |
| |
|
523 |
| |
|
524 |
| } |
|
525 |
| |
|
526 |
| |
|
527 |
1
| public void testNullNamespace() {
|
|
528 |
1
| Attribute a = new Attribute("red:prefix",
|
|
529 |
| "http://www.example.com", "data"); |
|
530 |
1
| a.setNamespace(null, null);
|
|
531 |
1
| assertEquals("", a.getNamespaceURI());
|
|
532 |
1
| assertEquals("", a.getNamespacePrefix());
|
|
533 |
| } |
|
534 |
| |
|
535 |
| |
|
536 |
1
| public void testChangeNamespaceToSameNamespaceAsElement() {
|
|
537 |
1
| Attribute a = new Attribute("red:prefix",
|
|
538 |
| "http://www.example.com", "data"); |
|
539 |
1
| Element e = new Element("pre:test", "http://www.example.org/");
|
|
540 |
1
| e.addAttribute(a);
|
|
541 |
1
| a.setNamespace("pre", "http://www.example.org/");
|
|
542 |
1
| assertEquals("http://www.example.org/", a.getNamespaceURI());
|
|
543 |
1
| assertEquals("pre", a.getNamespacePrefix());
|
|
544 |
1
| assertEquals("http://www.example.org/", e.getNamespaceURI());
|
|
545 |
1
| assertEquals("pre", e.getNamespacePrefix());
|
|
546 |
| } |
|
547 |
| |
|
548 |
| |
|
549 |
1
| public void testSetNamespaceURI() {
|
|
550 |
| |
|
551 |
1
| String name = "red:sakjdhjhd";
|
|
552 |
1
| String uri = "http://www.red.com/";
|
|
553 |
1
| String prefix = "red";
|
|
554 |
1
| Attribute a = new Attribute(name, uri, "");
|
|
555 |
| |
|
556 |
1
| assertEquals(uri, a.getNamespaceURI());
|
|
557 |
| |
|
558 |
1
| String[] legal = {"http://www.is.edu/sakdsk#sjadh",
|
|
559 |
| "http://www.is.edu/sakdsk?name=value&name=head", |
|
560 |
| "uri:isbn:0832473864", |
|
561 |
| "http://www.examples.com:80", |
|
562 |
| "http://www.examples.com:80/", |
|
563 |
| "http://www.is.edu/%20sakdsk#sjadh"}; |
|
564 |
| |
|
565 |
1
| String[] illegal = {
|
|
566 |
| "http://www.is.edu/%sakdsk#sjadh", |
|
567 |
| "http://www.is.edu/k\u0245kakdsk#sjadh", |
|
568 |
| "!@#$%^&*()", |
|
569 |
| "fred", |
|
570 |
| "#fred", |
|
571 |
| "/fred" |
|
572 |
| }; |
|
573 |
| |
|
574 |
1
| for (int i = 0; i < legal.length; i++) {
|
|
575 |
6
| a.setNamespace(prefix, legal[i]);
|
|
576 |
6
| assertEquals(legal[i], a.getNamespaceURI());
|
|
577 |
| } |
|
578 |
| |
|
579 |
1
| for (int i = 0; i < illegal.length; i++) {
|
|
580 |
6
| try {
|
|
581 |
6
| a.setNamespace(prefix, illegal[i]);
|
|
582 |
0
| fail("Illegal namespace URI allowed");
|
|
583 |
| } |
|
584 |
| catch (MalformedURIException success) { |
|
585 |
6
| assertEquals(illegal[i], success.getData());
|
|
586 |
| } |
|
587 |
| catch (IllegalNameException success) { |
|
588 |
0
| assertNotNull(success.getMessage());
|
|
589 |
| } |
|
590 |
| } |
|
591 |
| |
|
592 |
| } |
|
593 |
| |
|
594 |
| |
|
595 |
1
| public void testSetNamespace() {
|
|
596 |
| |
|
597 |
1
| Attribute a = new Attribute("name", "value");
|
|
598 |
1
| try {
|
|
599 |
1
| a.setNamespace("pre", "");
|
|
600 |
0
| fail("Allowed prefix with empty URI");
|
|
601 |
| } |
|
602 |
| catch (NamespaceConflictException success) { |
|
603 |
1
| assertNotNull(success.getMessage());
|
|
604 |
| } |
|
605 |
| |
|
606 |
1
| try {
|
|
607 |
1
| a.setNamespace("", "http://www.example.com");
|
|
608 |
0
| fail("Allowed empty prefix with non-empty URI");
|
|
609 |
| } |
|
610 |
| catch (NamespaceConflictException success) { |
|
611 |
1
| assertNotNull(success.getMessage());
|
|
612 |
| } |
|
613 |
| |
|
614 |
| } |
|
615 |
| |
|
616 |
| |
|
617 |
1
| public void testNodeProperties() {
|
|
618 |
| |
|
619 |
1
| Attribute a1 = new Attribute("test", "data");
|
|
620 |
| |
|
621 |
1
| assertNull(a1.getParent());
|
|
622 |
| |
|
623 |
1
| Element element = new Element("test");
|
|
624 |
1
| element.addAttribute(a1);
|
|
625 |
1
| assertEquals(element, a1.getParent());
|
|
626 |
1
| assertEquals(a1, element.getAttribute("test"));
|
|
627 |
| |
|
628 |
1
| element.removeAttribute(a1);
|
|
629 |
1
| assertNull(element.getAttribute("test"));
|
|
630 |
| |
|
631 |
| } |
|
632 |
| |
|
633 |
| |
|
634 |
1
| public void testDistinctTypes() {
|
|
635 |
| |
|
636 |
1
| assertTrue(!(Attribute.Type.CDATA.equals(Attribute.Type.UNDECLARED)));
|
|
637 |
| |
|
638 |
1
| assertTrue(!(Attribute.Type.ID.equals(Attribute.Type.CDATA)));
|
|
639 |
1
| assertTrue(!(Attribute.Type.IDREF.equals(Attribute.Type.ID)));
|
|
640 |
1
| assertTrue(!(Attribute.Type.IDREFS.equals(Attribute.Type.IDREF)));
|
|
641 |
1
| assertTrue(!(Attribute.Type.NMTOKEN.equals(Attribute.Type.IDREFS)));
|
|
642 |
1
| assertTrue(!(Attribute.Type.NMTOKENS.equals(Attribute.Type.NMTOKEN)));
|
|
643 |
1
| assertTrue(!(Attribute.Type.NOTATION.equals(Attribute.Type.NMTOKENS)));
|
|
644 |
1
| assertTrue(!(Attribute.Type.ENTITY.equals(Attribute.Type.NOTATION)));
|
|
645 |
1
| assertTrue(!(Attribute.Type.ENTITIES.equals(Attribute.Type.ENTITY)));
|
|
646 |
1
| assertTrue(!(Attribute.Type.ENUMERATION.equals(Attribute.Type.ENTITIES)));
|
|
647 |
1
| assertTrue(!(Attribute.Type.CDATA.equals(Attribute.Type.ENUMERATION)));
|
|
648 |
| } |
|
649 |
| |
|
650 |
| |
|
651 |
1
| public void testAdditionConstraints() {
|
|
652 |
| |
|
653 |
1
| Element element = new Element("test");
|
|
654 |
1
| Attribute a1 = new Attribute(
|
|
655 |
| "foo:data", "http://www.example.com", "valueFoo"); |
|
656 |
1
| Attribute a2 = new Attribute(
|
|
657 |
| "bar:data", "http://www.example.com", "valueBar"); |
|
658 |
1
| Attribute a3 = new Attribute("data", "valueFoo");
|
|
659 |
1
| Attribute a4 = new Attribute("data", "valueBar");
|
|
660 |
| |
|
661 |
1
| element.addAttribute(a1);
|
|
662 |
1
| assertEquals("valueFoo",
|
|
663 |
| element.getAttributeValue("data", "http://www.example.com")); |
|
664 |
1
| assertEquals(1, element.getAttributeCount());
|
|
665 |
1
| element.addAttribute(a2);
|
|
666 |
1
| assertEquals(
|
|
667 |
| element.getAttributeValue("data", "http://www.example.com"), |
|
668 |
| "valueBar" |
|
669 |
| ); |
|
670 |
1
| assertEquals(1, element.getAttributeCount());
|
|
671 |
1
| element.addAttribute(a3);
|
|
672 |
1
| assertEquals(element.getAttributeValue("data"), "valueFoo");
|
|
673 |
1
| assertEquals("valueBar",
|
|
674 |
| element.getAttributeValue("data", "http://www.example.com")); |
|
675 |
1
| assertEquals(2, element.getAttributeCount());
|
|
676 |
1
| element.addAttribute(a4);
|
|
677 |
1
| assertEquals("valueBar", element.getAttributeValue("data"));
|
|
678 |
1
| assertEquals(2, element.getAttributeCount());
|
|
679 |
| |
|
680 |
| |
|
681 |
| |
|
682 |
1
| Attribute a5 = new Attribute(
|
|
683 |
| "red:ab", "http://www.example.org", "valueRed"); |
|
684 |
1
| Attribute a6 = new Attribute(
|
|
685 |
| "green:cd", "http://www.example.org", "valueGreen"); |
|
686 |
1
| element.addAttribute(a5);
|
|
687 |
1
| element.addAttribute(a6);
|
|
688 |
1
| assertEquals("valueRed",
|
|
689 |
| element.getAttributeValue("ab", "http://www.example.org")); |
|
690 |
1
| assertEquals("valueGreen",
|
|
691 |
| element.getAttributeValue("cd", "http://www.example.org")); |
|
692 |
| |
|
693 |
| } |
|
694 |
| |
|
695 |
| |
|
696 |
1
| public void testXMLLangCanBeEmptyString() {
|
|
697 |
| |
|
698 |
| |
|
699 |
1
| Attribute a = new Attribute("xml:lang", "http://www.w3.org/XML/1998/namespace", "");
|
|
700 |
1
| assertEquals("", a.getValue());
|
|
701 |
| |
|
702 |
| } |
|
703 |
| |
|
704 |
| |
|
705 |
1
| public void testPunctuationCharactersInToXML() {
|
|
706 |
| |
|
707 |
1
| String data = "=,.!@#$%^*()_-'[]{}+/?;:`|\\";
|
|
708 |
1
| Attribute a = new Attribute("a", data);
|
|
709 |
1
| assertEquals("a=\"" + data + "\"", a.toXML());
|
|
710 |
| |
|
711 |
| } |
|
712 |
| |
|
713 |
| |
|
714 |
| |
|
715 |
1
| public void testPrefixedAttributeBug() throws ParsingException, IOException {
|
|
716 |
| |
|
717 |
1
| Builder builder = new Builder();
|
|
718 |
1
| File f = new File("data");
|
|
719 |
1
| f = new File(f, "xtest.xml");
|
|
720 |
1
| Document input = builder.build(f);
|
|
721 |
1
| String s = input.toXML();
|
|
722 |
1
| Document output = builder.build(s, f.toURL().toExternalForm());
|
|
723 |
1
| assertEquals(input, output);
|
|
724 |
| |
|
725 |
| } |
|
726 |
| } |