1
2
3
4
5
6
7
8
9
10 package org.dom4j;
11
12 import junit.framework.Test;
13 import junit.framework.TestSuite;
14 import junit.textui.TestRunner;
15
16 /*** Tests the use of null attribute values
17 *
18 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
19 * @version $Revision: 1.6 $
20 */
21 public class TestNullAttributes extends AbstractTestCase {
22
23 protected DocumentFactory factory = DocumentFactory.getInstance();
24 protected Document document = factory.createDocument();
25 protected Element element = document.addElement( "root" );
26
27 public static void main( String[] args ) {
28 TestRunner.run( suite() );
29 }
30
31 public static Test suite() {
32 return new TestSuite( TestNullAttributes.class );
33 }
34
35 public TestNullAttributes(String name) {
36 super(name);
37 }
38
39
40
41 public void testStringNames() throws Exception {
42
43 element.addAttribute( "foo", null );
44 Attribute attribute = element.attribute( "foo" );
45 assertTrue( attribute == null );
46
47 element.addAttribute( "foo", "123" );
48 attribute = element.attribute( "foo" );
49 assertTrue( attribute != null );
50
51 element.addAttribute( "foo", null );
52 attribute = element.attribute( "foo" );
53 assertTrue( attribute == null );
54 }
55
56 public void testQNames() throws Exception {
57
58 QName bar = QName.get( "bar" );
59
60 element.addAttribute( bar, null );
61 Attribute attribute = element.attribute( bar );
62 assertTrue( attribute == null );
63
64 element.addAttribute( bar, "123" );
65 attribute = element.attribute( bar );
66 assertTrue( attribute != null );
67
68 element.addAttribute( bar, null );
69 attribute = element.attribute( bar );
70 assertTrue( attribute == null );
71 }
72
73 public void testAttributes() throws Exception {
74
75 Attribute attribute = factory.createAttribute( element, "v", null );
76
77 assertTrue( attribute.getText() == null );
78 assertTrue( attribute.getValue() == null );
79
80 element.add( attribute );
81 attribute = element.attribute( "v" );
82 assertTrue( attribute == null );
83
84 attribute = factory.createAttribute( element, "v", "123" );
85 element.add( attribute );
86 attribute = element.attribute( "v" );
87 assertTrue( attribute != null );
88
89 attribute = factory.createAttribute( element, "v", null );
90 element.add( attribute );
91 attribute = element.attribute( "v" );
92 assertTrue( attribute == null );
93 }
94
95 }
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143