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

« back to all changes in this revision

Viewing changes to src/nu/xom/tests/CommentTest.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-2004 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 nu.xom.Comment;
 
25
import nu.xom.Document;
 
26
import nu.xom.Element;
 
27
import nu.xom.IllegalDataException;
 
28
import nu.xom.IllegalCharacterDataException;
 
29
 
 
30
 
 
31
/**
 
32
 * <p>
 
33
 *  Various basic unit tests for the <code>Comment</code> class.
 
34
 * </p>
 
35
 * 
 
36
 * @author Elliotte Rusty Harold
 
37
 * @version 1.0
 
38
 *
 
39
 */
 
40
public class CommentTest extends XOMTestCase {
 
41
 
 
42
    
 
43
    public CommentTest(String name) {
 
44
        super(name);
 
45
    }
 
46
 
 
47
    
 
48
    public void testConstructor() {
 
49
        Comment c1 = new Comment("test");
 
50
        assertEquals("test", c1.getValue());
 
51
        Comment c2 = new Comment("");
 
52
        assertEquals("", c2.getValue());
 
53
    }
 
54
 
 
55
    
 
56
    public void testCopyConstructor() {
 
57
        Comment c1 = new Comment("test");
 
58
        Comment c2 = new Comment(c1);
 
59
        assertEquals("test", c2.getValue());
 
60
        assertEquals(c1.getValue(), c2.getValue());
 
61
        assertTrue(c1 != c2);     
 
62
    }
 
63
 
 
64
    
 
65
    public void testToString() {
 
66
        
 
67
        Comment c1 = new Comment("content");
 
68
        assertEquals("[nu.xom.Comment: content]", c1.toString());          
 
69
        
 
70
        c1.setValue("012345678901234567890123456789012345678901234567890123456789");
 
71
        assertEquals(
 
72
          "[nu.xom.Comment: 01234567890123456789012345678901234...]", 
 
73
          c1.toString()
 
74
        ); 
 
75
           
 
76
    }
 
77
    
 
78
    
 
79
    public void testToStringWithLineFeed() {
 
80
        
 
81
        Comment c = new Comment("content\ncontent");
 
82
        assertEquals("[nu.xom.Comment: content\\ncontent]", c.toString());         
 
83
        
 
84
    }
 
85
    
 
86
    
 
87
    public void testToStringWithLotsOfData() {
 
88
        
 
89
        Comment c 
 
90
          = new Comment("content content 012345678901234567890123456789012345678901234567890123456789");
 
91
        String s = c.toString();
 
92
        assertTrue(s.length() < 72);
 
93
        
 
94
    }
 
95
 
 
96
 
 
97
    public void testToXML() {
 
98
        
 
99
        Comment c1 = new Comment("content");
 
100
        assertEquals("<!--content-->", c1.toXML());          
 
101
        
 
102
        c1.setValue(" 012345678901234567890123456789012345678901234567890123456789 ");
 
103
        assertEquals(
 
104
          "<!-- 012345678901234567890123456789012345678901234567890123456789 -->", 
 
105
          c1.toXML()
 
106
        ); 
 
107
           
 
108
    }
 
109
    
 
110
    
 
111
    // This is a problem because it cannot be serialized
 
112
    // since character and entity references aren't
 
113
    // recognized in comment data
 
114
    public void testCarriageReturnInCommentData() {
 
115
        try {
 
116
            new Comment("data\rdata");
 
117
            fail("Allowed carriage return in comment");
 
118
        }
 
119
        catch (IllegalDataException success) {
 
120
            assertEquals("data\rdata", success.getData());
 
121
            assertNotNull(success.getMessage());   
 
122
        }   
 
123
    }
 
124
    
 
125
    
 
126
    public void testSetter() {
 
127
 
 
128
        Comment c1 = new Comment("test");
 
129
        c1.setValue("legal");
 
130
        assertEquals("legal", c1.getValue());
 
131
        
 
132
        try {
 
133
          c1.setValue("test -- test");
 
134
          fail("Should raise an IllegalDataException");
 
135
        }
 
136
        catch (IllegalDataException success) {
 
137
            assertEquals("test -- test", success.getData());
 
138
            assertNotNull(success.getMessage());   
 
139
        }
 
140
        
 
141
        try {
 
142
          c1.setValue("test-");
 
143
          fail("Should raise an IllegalDataException");
 
144
        }
 
145
        catch (IllegalDataException success) {
 
146
            assertEquals("test-", success.getData());
 
147
            assertNotNull(success.getMessage());   
 
148
        }   
 
149
 
 
150
        c1.setValue(null);
 
151
        assertEquals("", c1.getValue());
 
152
 
 
153
     }
 
154
 
 
155
 
 
156
    public void testEquals() {
 
157
        
 
158
        Comment c1 = new Comment("test");
 
159
        Comment c2 = new Comment("test");
 
160
        Comment c3 = new Comment("skjlchsakdjh");
 
161
 
 
162
        assertEquals(c1, c1);
 
163
        assertEquals(c1.hashCode(), c1.hashCode());
 
164
        assertTrue(!c1.equals(c2));
 
165
        assertTrue(!c1.equals(c3));
 
166
        
 
167
    }
 
168
 
 
169
    
 
170
    public void testCopy() {
 
171
        
 
172
        Comment c1 = new Comment("test");
 
173
        Comment c2 = (Comment) c1.copy();
 
174
 
 
175
        assertEquals(c1.getValue(), c2.getValue());
 
176
        assertTrue(!c1.equals(c2));
 
177
        assertNull(c2.getParent());
 
178
 
 
179
    }
 
180
 
 
181
    
 
182
    // Check passing in a string with broken surrogate pairs
 
183
    // and with correct surrogate pairs
 
184
    public void testSurrogates() {
 
185
 
 
186
        String goodString = "test: \uD8F5\uDF80  ";
 
187
        Comment c = new Comment(goodString);
 
188
        assertEquals(goodString, c.getValue());
 
189
 
 
190
        // Two high-halves
 
191
        try {
 
192
          new Comment("test: \uD8F5\uDBF0  ");
 
193
          fail("Should raise an IllegalCharacterDataException");
 
194
        }
 
195
        catch (IllegalCharacterDataException success) {
 
196
            assertEquals("test: \uD8F5\uDBF0  ", success.getData());
 
197
            assertNotNull(success.getMessage());   
 
198
        }   
 
199
 
 
200
        // Two high-halves
 
201
        try {
 
202
          new Comment("test: \uD8F5\uD8F5  ");
 
203
          fail("Should raise an IllegalCharacterDataException");
 
204
        }
 
205
        catch (IllegalCharacterDataException success) {
 
206
            assertEquals("test: \uD8F5\uD8F5  ", success.getData());
 
207
            assertNotNull(success.getMessage());   
 
208
        }   
 
209
 
 
210
        // One high-half
 
211
        try {
 
212
           new Comment("test: \uD8F5  ");
 
213
           fail("Should raise an IllegalCharacterDataException");
 
214
        }
 
215
        catch (IllegalCharacterDataException success) {
 
216
            assertEquals("test: \uD8F5  ", success.getData());
 
217
            assertNotNull(success.getMessage());   
 
218
        }   
 
219
 
 
220
        // One low half
 
221
        try {
 
222
            new Comment("test: \uDF80  ");
 
223
            fail("Should raise an IllegalCharacterDataException");
 
224
         }
 
225
        catch (IllegalCharacterDataException success) {
 
226
            assertEquals("test: \uDF80  ", success.getData());
 
227
            assertNotNull(success.getMessage());   
 
228
        }   
 
229
 
 
230
        // Low half before high half
 
231
        try {
 
232
            new Comment("test: \uDCF5\uD8F5  ");
 
233
            fail("Should raise an IllegalCharacterDataException");
 
234
        }
 
235
        catch (IllegalCharacterDataException success) {
 
236
            assertEquals("test: \uDCF5\uD8F5  ", success.getData());
 
237
            assertNotNull(success.getMessage());   
 
238
        }   
 
239
 
 
240
 
 
241
    }
 
242
 
 
243
    
 
244
    public void testLeafNode() {
 
245
 
 
246
        Comment c1 = new Comment("data");
 
247
        assertEquals(0, c1.getChildCount());
 
248
        try {
 
249
            c1.getChild(0);
 
250
            fail("Didn't throw IndexOutofBoundsException");
 
251
        }
 
252
        catch (IndexOutOfBoundsException success) {
 
253
            assertNotNull(success.getMessage()); 
 
254
        }
 
255
        
 
256
        assertNull(c1.getParent());
 
257
 
 
258
        Element element = new Element("test");
 
259
        element.appendChild(c1);
 
260
        assertEquals(element, c1.getParent());
 
261
        assertEquals(element.getChild(0), c1);
 
262
 
 
263
        element.removeChild(c1);
 
264
        assertTrue(element.getChildCount() == 0);
 
265
 
 
266
    }
 
267
 
 
268
    
 
269
    public void testGetDocument() {
 
270
 
 
271
        Comment c1 = new Comment("data");
 
272
        assertNull(c1.getDocument());
 
273
        Element root = new Element("root");
 
274
        root.appendChild(c1);
 
275
        assertNull(c1.getDocument());
 
276
        Document doc = new Document(root);
 
277
        assertEquals(doc, c1.getDocument());
 
278
 
 
279
    }
 
280
 
 
281
    
 
282
    public void testAllowReservedCharactersInData() {
 
283
        Comment comment = new Comment("<test>&amp;&greater;");
 
284
        String xml = comment.toXML();
 
285
        assertEquals("<!--<test>&amp;&greater;-->", xml);  
 
286
    }
 
287
 
 
288
 
 
289
    public void testForbidIllegalCharactersInComments() {
 
290
        
 
291
        try {
 
292
            new Comment(" \u0001 ");
 
293
            fail("Allowed C0 control in comment");
 
294
        }
 
295
        catch (IllegalCharacterDataException success) {
 
296
            assertEquals(" \u0001 ", success.getData());
 
297
            assertNotNull(success.getMessage());
 
298
        }
 
299
          
 
300
    }
 
301
 
 
302
 
 
303
    public void testForbidUnmatchedSurrogatesInComments() {
 
304
        
 
305
        try {
 
306
            new Comment(" \uD800 ");
 
307
            fail("Allowed unmatched high surrogate in comment");
 
308
        }
 
309
        catch (IllegalCharacterDataException success) {
 
310
            assertEquals(" \uD800 ", success.getData());
 
311
            assertNotNull(success.getMessage());
 
312
        }
 
313
          
 
314
    }
 
315
 
 
316
    
 
317
    public void testCommentDataCanStartWithAHyphen() {
 
318
        Comment c = new Comment("- - ");
 
319
        assertEquals("- - ", c.getValue());
 
320
    }
 
321
 
 
322
}