1 /*
2 * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
3 *
4 * This software is open source.
5 * See the bottom of this file for the licence.
6 *
7 * $Id: TestNullAttributes.java,v 1.6 2004/06/25 08:03:47 maartenc Exp $
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 // Test case(s)
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 * Redistribution and use of this software and associated documentation
102 * ("Software"), with or without modification, are permitted provided
103 * that the following conditions are met:
104 *
105 * 1. Redistributions of source code must retain copyright
106 * statements and notices. Redistributions must also contain a
107 * copy of this document.
108 *
109 * 2. Redistributions in binary form must reproduce the
110 * above copyright notice, this list of conditions and the
111 * following disclaimer in the documentation and/or other
112 * materials provided with the distribution.
113 *
114 * 3. The name "DOM4J" must not be used to endorse or promote
115 * products derived from this Software without prior written
116 * permission of MetaStuff, Ltd. For written permission,
117 * please contact dom4j-info@metastuff.com.
118 *
119 * 4. Products derived from this Software may not be called "DOM4J"
120 * nor may "DOM4J" appear in their names without prior written
121 * permission of MetaStuff, Ltd. DOM4J is a registered
122 * trademark of MetaStuff, Ltd.
123 *
124 * 5. Due credit should be given to the DOM4J Project -
125 * http://www.dom4j.org
126 *
127 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
128 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
129 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
130 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
131 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
132 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
133 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
134 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
135 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
136 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
137 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
138 * OF THE POSSIBILITY OF SUCH DAMAGE.
139 *
140 * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
141 *
142 * $Id: TestNullAttributes.java,v 1.6 2004/06/25 08:03:47 maartenc Exp $
143 */