~ubuntu-branches/ubuntu/precise/xom/precise

« back to all changes in this revision

Viewing changes to src/nu/xom/tests/AttributeTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2007-11-25 15:50:40 UTC
  • Revision ID: james.westby@ubuntu.com-20071125155040-r75ikcqf1vu0cei7
Tags: upstream-1.1
ImportĀ upstreamĀ versionĀ 1.1

Show diffs side-by-side

added added

removed removed

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