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

« back to all changes in this revision

Viewing changes to src/nu/xom/tests/XOMTestCaseTest.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 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
package nu.xom.tests;
 
22
 
 
23
import junit.framework.ComparisonFailure;
 
24
import nu.xom.Attribute;
 
25
import nu.xom.Comment;
 
26
import nu.xom.Element;
 
27
import nu.xom.Namespace;
 
28
import nu.xom.Node;
 
29
import nu.xom.Text;
 
30
 
 
31
/**
 
32
 * <p>
 
33
 * Unit tests for XOMTestCase. Added after the first bug discovered in
 
34
 * XOM 1.0 showed up in XOMTestCase. 
 
35
 * </p>
 
36
 * 
 
37
 * @author Elliotte Rusty Harold
 
38
 * @version 1.1a2
 
39
 *
 
40
 */
 
41
public class XOMTestCaseTest extends XOMTestCase {
 
42
 
 
43
    
 
44
    public XOMTestCaseTest(String name) {
 
45
        super(name);
 
46
    }
 
47
 
 
48
    
 
49
    public void testNullCheck() {
 
50
     
 
51
        Text t = new Text("");
 
52
        try {
 
53
            assertEquals(t, null);
 
54
            fail("Allowed comparison with null");
 
55
        }
 
56
        catch (ComparisonFailure ex) {
 
57
            assertNotNull(ex.getMessage());
 
58
        }
 
59
        
 
60
        try {
 
61
            assertEquals(null, t);
 
62
            fail("Allowed comparison with null");
 
63
        }
 
64
        catch (ComparisonFailure ex) {
 
65
            assertNotNull(ex.getMessage());
 
66
        }
 
67
        
 
68
    }
 
69
    
 
70
    
 
71
    public void testNamespaceEqualsItself() {
 
72
        Namespace ns = new Namespace("pre", "http://www.example.org", null);
 
73
        assertEquals(ns, ns);
 
74
    }
 
75
    
 
76
    
 
77
    public void testCompareMismatchedTypes() {
 
78
     
 
79
        Node n1 = new Text("");
 
80
        Node n2 = new Attribute("name", "value");
 
81
        
 
82
        try {
 
83
            assertEquals(n1, n2);
 
84
            fail("Text equals Attribute?!");
 
85
        }
 
86
        catch (ComparisonFailure ex) {
 
87
            assertNotNull(ex.getMessage());
 
88
        }
 
89
        
 
90
        try {
 
91
            assertEquals(n2, n1);
 
92
            fail("Text equals Attribute?!");
 
93
        }
 
94
        catch (ComparisonFailure ex) {
 
95
            assertNotNull(ex.getMessage());
 
96
        }
 
97
        
 
98
    }
 
99
    
 
100
    
 
101
    public void testCompareMismatchedNullNodeTypes() {
 
102
     
 
103
        Node n1 = new Text("");
 
104
        Node n2 = null;
 
105
        
 
106
        try {
 
107
            assertEquals(n1, n2);
 
108
            fail("Text equals null?!");
 
109
        }
 
110
        catch (ComparisonFailure ex) {
 
111
            assertNotNull(ex.getMessage());
 
112
        }
 
113
        
 
114
    }
 
115
    
 
116
    
 
117
    public void testCompareAttributesAsNodes() {
 
118
     
 
119
        Node a1 = new Attribute("test", "value");
 
120
        Node a2 = a1.copy();
 
121
        assertEquals(a1, a2);
 
122
        
 
123
    }
 
124
    
 
125
    
 
126
    public void testCombineTextNodes() {
 
127
     
 
128
        Element e1 = new Element("test");
 
129
        e1.appendChild("1");
 
130
        e1.appendChild("2");
 
131
        Element e2 = new Element("test");
 
132
        e2.appendChild("12");
 
133
        assertEquals(e1, e2);
 
134
        assertEquals(2, e1.getChildCount());
 
135
        
 
136
    }
 
137
    
 
138
    
 
139
    public void testTrickyCombineTextNodes() {
 
140
     
 
141
        Element e1 = new Element("test");
 
142
        e1.appendChild("12");
 
143
        e1.appendChild("3");
 
144
        Element e2 = new Element("test");
 
145
        e2.appendChild("1");
 
146
        e2.appendChild("23");
 
147
        assertEquals(e1, e2);
 
148
        assertEquals(2, e1.getChildCount());
 
149
        
 
150
    }
 
151
    
 
152
    
 
153
    public void testCombineThreeTextNodes() {
 
154
     
 
155
        Element e1 = new Element("test");
 
156
        e1.appendChild("1");
 
157
        e1.appendChild("2");
 
158
        e1.appendChild("3");
 
159
        Element e2 = new Element("test");
 
160
        e2.appendChild("123");
 
161
        assertEquals(e1, e2);
 
162
        
 
163
    }
 
164
    
 
165
    
 
166
    public void testCombineThreeTextNodes2() {
 
167
     
 
168
        Element e1 = new Element("test");
 
169
        e1.appendChild("\n");
 
170
        e1.appendChild(new Element("p"));
 
171
        e1.appendChild("1");
 
172
        e1.appendChild("2");
 
173
        e1.appendChild("3");
 
174
        Element e2 = new Element("test");
 
175
        e2.appendChild("\n");
 
176
        e2.appendChild(new Element("p"));
 
177
        e2.appendChild("123");
 
178
        assertEquals(e2, e1);
 
179
        
 
180
    }
 
181
    
 
182
    
 
183
    public void testUnequalElements() {
 
184
     
 
185
        Element e1 = new Element("test");
 
186
        e1.appendChild("1");
 
187
        e1.appendChild(new Element("b"));
 
188
        e1.appendChild("3");
 
189
        Element e2 = new Element("test");
 
190
        e2.appendChild("1");
 
191
        e2.appendChild(new Element("c"));
 
192
        e2.appendChild("3");
 
193
        try {
 
194
            assertEquals(e1, e2);
 
195
            fail("Unequal elements compared equal");
 
196
        }
 
197
        catch (ComparisonFailure success) {
 
198
            assertNotNull(success.getMessage());
 
199
        }
 
200
        
 
201
    }
 
202
    
 
203
    
 
204
    public void testCompareXMLBaseAttributes() {
 
205
     
 
206
        Node a1 = new Attribute("xml:base", Namespace.XML_NAMESPACE, "value.xml");
 
207
        Node a2 = new Attribute("xml:base", Namespace.XML_NAMESPACE, "./value.xml");
 
208
        assertEquals(a1, a2);
 
209
        
 
210
    }
 
211
    
 
212
    
 
213
    public void testCompareChildren() {
 
214
     
 
215
        Element e1 = new Element("e");
 
216
        Element e2 = new Element("e");
 
217
        e1.appendChild(new Comment("a"));
 
218
        e2.appendChild(new Comment("b"));
 
219
        try {
 
220
            assertEquals("BOO!", e1, e2);
 
221
            fail("didn't check children");
 
222
        }
 
223
        catch (ComparisonFailure ex) {
 
224
            assertTrue(ex.getMessage().indexOf("BOO!") >= 0 );
 
225
        }
 
226
        
 
227
    }
 
228
    
 
229
    
 
230
}