KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > dom4j > TestNullAttributes


1 /*
2  * Copyright 2001 (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.3.2.2 2004/01/15 22:02:37 per_nyfelt Exp $
8  */

9
10 package test.dom4j;
11
12 import junit.framework.Test;
13 import junit.framework.TestSuite;
14 import junit.textui.TestRunner;
15 import org.dom4j.Attribute;
16 import org.dom4j.Document;
17 import org.dom4j.Element;
18 import org.dom4j.QName;
19
20 /** Tests the use of null attribute values
21  *
22  * @author <a HREF="mailto:jstrachan@apache.org">James Strachan</a>
23  * @version $Revision$
24  */

25 public class TestNullAttributes extends AbstractTestCase {
26
27     protected Document document;
28     protected Element element;
29
30     public static void main(String JavaDoc[] args) {
31         TestRunner.run(suite());
32     }
33
34     public static Test suite() {
35         return new TestSuite(TestNullAttributes.class);
36     }
37
38     public TestNullAttributes(String JavaDoc name) {
39         super(name);
40     }
41
42     // Implementation methods
43
//-------------------------------------------------------------------------
44
protected void setUp() throws Exception JavaDoc {
45         super.setUp();
46         document = nodeFactory.createDocument();
47         element = document.addElement("root");
48     }
49
50     // Test case(s)
51
//-------------------------------------------------------------------------
52
public void testStringNames() throws Exception JavaDoc {
53
54         element.addAttribute("foo", null);
55         Attribute attribute = element.attribute("foo");
56         assertTrue(attribute == null);
57
58         element.addAttribute("foo", "123");
59         attribute = element.attribute("foo");
60         assertTrue(attribute != null);
61
62         element.addAttribute("foo", null);
63         attribute = element.attribute("foo");
64         assertTrue(attribute == null);
65     }
66
67     public void testQNames() throws Exception JavaDoc {
68
69         QName bar = nodeFactory.createQName("bar");
70
71         element.addAttribute(bar, null);
72         Attribute attribute = element.attribute(bar);
73         assertTrue(attribute == null);
74
75         element.addAttribute(bar, "123");
76         attribute = element.attribute(bar);
77         assertTrue(attribute != null);
78
79         element.addAttribute(bar, null);
80         attribute = element.attribute(bar);
81         assertTrue(attribute == null);
82     }
83
84     public void testAttributes() throws Exception JavaDoc {
85
86         Attribute attribute = nodeFactory.createAttribute(element, "v", null);
87
88         assertTrue(attribute.getText() == null);
89         assertTrue(attribute.getValue() == null);
90
91         element.add(attribute);
92         attribute = element.attribute("v");
93         assertTrue(attribute == null);
94
95         attribute = nodeFactory.createAttribute(element, "v", "123");
96         element.add(attribute);
97         attribute = element.attribute("v");
98         assertTrue(attribute != null);
99
100         attribute = nodeFactory.createAttribute(element, "v", null);
101         element.add(attribute);
102         attribute = element.attribute("v");
103         assertTrue(attribute == null);
104     }
105
106 }
107
108
109 /*
110  * Redistribution and use of this software and associated documentation
111  * ("Software"), with or without modification, are permitted provided
112  * that the following conditions are met:
113  *
114  * 1. Redistributions of source code must retain copyright
115  * statements and notices. Redistributions must also contain a
116  * copy of this document.
117  *
118  * 2. Redistributions in binary form must reproduce the
119  * above copyright notice, this list of conditions and the
120  * following disclaimer in the documentation and/or other
121  * materials provided with the distribution.
122  *
123  * 3. The name "DOM4J" must not be used to endorse or promote
124  * products derived from this Software without prior written
125  * permission of MetaStuff, Ltd. For written permission,
126  * please contact dom4j-info@metastuff.com.
127  *
128  * 4. Products derived from this Software may not be called "DOM4J"
129  * nor may "DOM4J" appear in their names without prior written
130  * permission of MetaStuff, Ltd. DOM4J is a registered
131  * trademark of MetaStuff, Ltd.
132  *
133  * 5. Due credit should be given to the DOM4J Project
134  * (http://dom4j.org/).
135  *
136  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
137  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
138  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
139  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
140  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
141  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
142  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
143  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
144  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
145  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
146  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
147  * OF THE POSSIBILITY OF SUCH DAMAGE.
148  *
149  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
150  *
151  * $Id: TestNullAttributes.java,v 1.3.2.2 2004/01/15 22:02:37 per_nyfelt Exp $
152  */

153
Popular Tags