KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nu > xom > tests > AttributeTest


1 /* Copyright 2002-2004 Elliotte Rusty Harold
2    
3    This library is free software; you can redistribute it and/or modify
4    it under the terms of version 2.1 of the GNU Lesser General Public
5    License as published by the Free Software Foundation.
6    
7    This library is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10    GNU Lesser General Public License for more details.
11    
12    You should have received a copy of the GNU Lesser General Public
13    License along with this library; if not, write to the
14    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15    Boston, MA 02111-1307 USA
16    
17    You can contact Elliotte Rusty Harold by sending e-mail to
18    elharo@metalab.unc.edu. Please include the word "XOM" in the
19    subject line. The XOM home page is located at http://www.xom.nu/
20 */

21
22 package nu.xom.tests;
23
24 import nu.xom.Attribute;
25 import nu.xom.Element;
26 import nu.xom.IllegalDataException;
27 import nu.xom.IllegalNameException;
28 import nu.xom.MalformedURIException;
29 import nu.xom.NamespaceConflictException;
30
31 /**
32  * <p>
33  * Basic tests for the <code>Attribute</code> class.
34  * </p>
35  *
36  * @author Elliotte Rusty Harold
37  * @version 1.0
38  *
39  */

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

670         Attribute a = new Attribute("xml:lang", "http://www.w3.org/XML/1998/namespace", "");
671         assertEquals("", a.getValue());
672         
673     }
674
675     
676     public void testPunctuationCharactersInToXML() {
677         
678         String JavaDoc data = "=,.!@#$%^*()_-'[]{}+/?;:`|\\";
679         Attribute a = new Attribute("a", data);
680         assertEquals("a=\"" + data + "\"", a.toXML());
681         
682     }
683
684     
685 }
Popular Tags