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

« back to all changes in this revision

Viewing changes to src/nu/xom/tests/TextTest.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-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
 
 
22
package nu.xom.tests;
 
23
 
 
24
import nu.xom.Element;
 
25
import nu.xom.IllegalCharacterDataException;
 
26
import nu.xom.Node;
 
27
import nu.xom.Text;
 
28
 
 
29
/**
 
30
 * 
 
31
 * <p>
 
32
 *  Basic tests for the <code>Text</code> class.
 
33
 * </p>
 
34
 * 
 
35
 * @author Elliotte Rusty Harold
 
36
 * @version 1.1d7
 
37
 *
 
38
 */
 
39
public class TextTest extends XOMTestCase {
 
40
 
 
41
    
 
42
    public TextTest(String name) {
 
43
        super(name);
 
44
    }
 
45
 
 
46
    
 
47
    public void testConstructor() {       
 
48
        Text a1 = new Text("test");
 
49
        assertEquals("test", a1.getValue());
 
50
    }
 
51
 
 
52
    
 
53
    public void testSetter() {
 
54
        
 
55
        String[] legal = {
 
56
          "Hello",
 
57
          "hello there",
 
58
          "  spaces on both ends  ",
 
59
          " quotes \" \" quotes",
 
60
          " single \'\' quotes",
 
61
          " both double and single \"\'\"\' quotes",  
 
62
          " angle brackets <  > <<<",  
 
63
          " carriage returns \r\r\r",  
 
64
          " CDATA end: ]]>",  
 
65
          " <![CDATA[ CDATA end: ]]>",  
 
66
          " &amp; ",  
 
67
          " ampersands & &&& &name; "  
 
68
        };
 
69
 
 
70
        Text t = new Text("name");
 
71
        
 
72
        // Things that shouldn't cause an exception
 
73
        for (int i = 0; i < legal.length; i++) {
 
74
            t.setValue(legal[i]);   
 
75
            assertEquals(legal[i], t.getValue());
 
76
        }
 
77
        
 
78
        t.setValue(null);
 
79
        assertEquals("", t.getValue());
 
80
        
 
81
        try {
 
82
            t.setValue("test \u0000 test ");
 
83
            fail("Should raise an IllegalCharacterDataException");
 
84
        }
 
85
        catch (IllegalCharacterDataException success) {
 
86
            assertEquals("test \u0000 test ", success.getData());
 
87
            assertNotNull(success.getMessage());
 
88
        }
 
89
 
 
90
    }
 
91
 
 
92
    
 
93
    public void testToXML() {
 
94
        
 
95
        String[] easyCases = {
 
96
          "Hello",
 
97
          "hello there",
 
98
          "  spaces on both ends  ",
 
99
          " quotes \" \" quotes",
 
100
          " single \'\' quotes",
 
101
          " both double and single \"\'\"\' quotes"  
 
102
        };
 
103
 
 
104
        Text t = new Text("name");
 
105
        
 
106
        // Things that shouldn't cause an exception
 
107
        for (int i = 0; i < easyCases.length; i++) {
 
108
            t.setValue(easyCases[i]);   
 
109
            assertEquals(easyCases[i], t.toXML());
 
110
        }
 
111
        
 
112
        t.setValue("<>");
 
113
        assertEquals("&lt;&gt;", t.toXML());
 
114
        t.setValue("&amp;");
 
115
        assertEquals("&amp;amp;", t.toXML());
 
116
        t.setValue("]]>");
 
117
        assertEquals("]]&gt;", t.toXML());
 
118
        t.setValue("\r");
 
119
        assertEquals("&#x0D;", t.toXML());
 
120
        
 
121
    }
 
122
 
 
123
    
 
124
    public void testPunctuationCharactersInToXML() {
 
125
        
 
126
        String data = "=,.!@#$%^*()_-\"'[]{}+/?;:`|\\";
 
127
        Text t = new Text(data);
 
128
        assertEquals(data, t.toXML());
 
129
        
 
130
    }
 
131
 
 
132
    
 
133
    public void testEquals() {
 
134
        
 
135
        Text c1 = new Text("test");
 
136
        Text c2 = new Text("test");
 
137
        Text c3 = new Text("skjlchsakdjh");
 
138
 
 
139
        assertEquals(c1, c1);
 
140
        assertEquals(c1.hashCode(), c1.hashCode());
 
141
        assertTrue(!c1.equals(c2));
 
142
        assertTrue(!c1.equals(c3));
 
143
        
 
144
    }
 
145
 
 
146
    
 
147
    public void testCopy() {
 
148
        
 
149
        Text c1 = new Text("test");
 
150
        Text c2 = (Text) c1.copy();
 
151
 
 
152
        assertEquals(c1.getValue(), c2.getValue());
 
153
        assertEquals(c1, c2);
 
154
        assertTrue(!c1.equals(c2));
 
155
        assertNull(c2.getParent());
 
156
 
 
157
    }
 
158
 
 
159
 
 
160
    public void testCopyisNotACDATASection() {
 
161
        
 
162
        Text c1 = new Text("test");
 
163
        Node c2 = c1.copy();
 
164
        assertEquals(Text.class, c2.getClass());
 
165
 
 
166
    }
 
167
 
 
168
 
 
169
    // Check passing in a string with broken surrogate pairs
 
170
    // and with correct surrogate pairs
 
171
    public void testSurrogates() {
 
172
 
 
173
        String goodString = "test: \uD8F5\uDF80  ";
 
174
        Text c = new Text(goodString);
 
175
        assertEquals(goodString, c.getValue());
 
176
 
 
177
        // Two high-halves
 
178
        try {
 
179
          new Text("test: \uD8F5\uDBF0  ");
 
180
          fail("Should raise an IllegalCharacterDataException");
 
181
        }
 
182
        catch (IllegalCharacterDataException success) {
 
183
            assertNotNull(success.getMessage());
 
184
            assertEquals("test: \uD8F5\uDBF0  ", success.getData());
 
185
        }
 
186
 
 
187
        // Two high-halves
 
188
        try {
 
189
            new Text("test: \uD8F5\uD8F5  ");
 
190
            fail("Should raise an IllegalCharacterDataException");
 
191
        }
 
192
        catch (IllegalCharacterDataException success) {
 
193
            assertEquals("test: \uD8F5\uD8F5  ", success.getData());
 
194
            assertNotNull(success.getMessage());
 
195
        }
 
196
 
 
197
        // One high-half
 
198
        try {
 
199
            new Text("test: \uD8F5  ");
 
200
            fail("Should raise an IllegalCharacterDataException");
 
201
        }
 
202
        catch (IllegalCharacterDataException success) {
 
203
            assertNotNull(success.getMessage());
 
204
            assertEquals("test: \uD8F5  ", success.getData());
 
205
        }
 
206
 
 
207
        // One low half
 
208
        try {
 
209
            new Text("test: \uDF80  ");
 
210
            fail("Should raise an IllegalCharacterDataException");
 
211
        }
 
212
        catch (IllegalCharacterDataException success) {
 
213
            assertNotNull(success.getMessage());
 
214
            assertEquals("test: \uDF80  ", success.getData());
 
215
        }
 
216
 
 
217
        // Low half before high half
 
218
        try {
 
219
            new Text("test: \uDCF5\uD8F5  ");
 
220
            fail("Should raise an IllegalCharacterDataException");
 
221
        }
 
222
        catch (IllegalCharacterDataException success) {
 
223
            assertEquals("test: \uDCF5\uD8F5  ", success.getData());
 
224
            assertNotNull(success.getMessage());
 
225
        }
 
226
 
 
227
    }
 
228
 
 
229
    
 
230
    public void testNonBMPText() {
 
231
        
 
232
        StringBuffer sb = new StringBuffer(2);
 
233
        for (char high = '\uD800'; high <= '\uDB7F'; high++) {
 
234
            for (char low = '\uDC00'; low <= '\uDFFF'; low++) {
 
235
                sb.setLength(0);
 
236
                sb.append(high);
 
237
                sb.append(low);
 
238
                String s = sb.toString();
 
239
                Text t = new Text(s);
 
240
                assertEquals(s, t.getValue());
 
241
            }
 
242
        }
 
243
        
 
244
    }
 
245
    
 
246
    
 
247
    public void testEndOfBMP() {
 
248
        
 
249
        try {
 
250
            new Text("\uFFFE");
 
251
            fail("allowed FFFE");
 
252
        }
 
253
        catch (IllegalCharacterDataException success) {
 
254
            assertEquals("\uFFFE", success.getData());
 
255
            assertNotNull(success.getMessage());
 
256
        }
 
257
        
 
258
        try {
 
259
            new Text("\uFFFF");
 
260
            fail("allowed FFFF");
 
261
        }
 
262
        catch (IllegalCharacterDataException success) {
 
263
            assertEquals("\uFFFF", success.getData());
 
264
            assertNotNull(success.getMessage());
 
265
        }
 
266
        
 
267
    }
 
268
 
 
269
    
 
270
    public void testLeafNode() {
 
271
 
 
272
        Text c1 = new Text("data");
 
273
        assertEquals(0, c1.getChildCount());
 
274
        try {
 
275
            c1.getChild(0);
 
276
            fail("Didn't throw IndexOutofBoundsException");
 
277
        }
 
278
        catch (IndexOutOfBoundsException success) {
 
279
            // success   
 
280
        }
 
281
        
 
282
        assertNull(c1.getParent());
 
283
 
 
284
        Element element = new Element("test");
 
285
        element.appendChild(c1);
 
286
        assertEquals(element, c1.getParent());
 
287
        assertEquals(c1, element.getChild(0));
 
288
 
 
289
        element.removeChild(c1);
 
290
        assertEquals(0, element.getChildCount());
 
291
 
 
292
    }
 
293
 
 
294
    
 
295
    public void testToStringWithLineFeed() {
 
296
        
 
297
        Text t = new Text("content\ncontent");
 
298
        assertEquals("[nu.xom.Text: content\\ncontent]", t.toString());          
 
299
        
 
300
    }
 
301
 
 
302
 
 
303
    public void testToStringWithCarriageReturn() {
 
304
        
 
305
        Text t = new Text("content\rcontent");
 
306
        assertEquals("[nu.xom.Text: content\\rcontent]", t.toString());          
 
307
        
 
308
    }
 
309
 
 
310
 
 
311
    public void testToStringWithCarriageReturnLinefeed() {
 
312
        
 
313
        Text t = new Text("content\r\ncontent");
 
314
        assertEquals("[nu.xom.Text: content\\r\\ncontent]", t.toString());          
 
315
        
 
316
    }
 
317
 
 
318
 
 
319
    public void testToStringWithTab() {
 
320
        
 
321
        Text t = new Text("content\tcontent");
 
322
        assertEquals("[nu.xom.Text: content\\tcontent]", t.toString());          
 
323
        
 
324
    }
 
325
 
 
326
 
 
327
    public void testToString() {
 
328
        
 
329
        Text t = new Text("content");
 
330
        assertEquals("[nu.xom.Text: content]", t.toString());          
 
331
        
 
332
        t.setValue("012345678901234567890123456789012345678901234567890123456789");
 
333
        assertEquals(
 
334
          "[nu.xom.Text: 01234567890123456789012345678901234...]", 
 
335
          t.toString()
 
336
        );          
 
337
        
 
338
    }
 
339
 
 
340
    
 
341
    // Make sure carriage returns are escaped properly by toXML()
 
342
    public void testCarriageReturnInText() {
 
343
        Text text = new Text("data\rdata");
 
344
        String xml = text.toXML();
 
345
        assertEquals("data&#x0D;data", xml);   
 
346
    }
 
347
    
 
348
    
 
349
    public void testHighSurrogateWithNoLowSurrogate() {
 
350
        
 
351
        String data = String.valueOf((char) 0xD800);
 
352
        try {
 
353
            new Text(data);
 
354
            fail("Allowed single high surrogate in text node");
 
355
        }
 
356
        catch (IllegalCharacterDataException success) {
 
357
            assertEquals(data, success.getData());
 
358
            assertNotNull(success.getMessage());
 
359
        }
 
360
        
 
361
    }
 
362
 
 
363
 
 
364
}