1 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 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 " & ", 67 " ampersands & &&& &name; " 68 }; 69 70 Text t = new Text("name"); 71 72 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 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("<>", t.toXML()); 114 t.setValue("&"); 115 assertEquals("&amp;", t.toXML()); 116 t.setValue("]]>"); 117 assertEquals("]]>", t.toXML()); 118 t.setValue("\r"); 119 assertEquals("
", 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 public void testSurrogates() { 172 173 String goodString = "test: \uD8F5\uDF80 "; 174 Text c = new Text(goodString); 175 assertEquals(goodString, c.getValue()); 176 177 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 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 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 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 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 for (char high = '\uD800'; high <= '\uDB7F'; high++) { 233 for (char low = '\uDC00'; low <= '\uDFFF'; low++) { 234 String s = high + "" + low; 235 Text t = new Text(s); 236 assertEquals(s, t.getValue()); 237 } 238 } 239 240 } 241 242 243 public void testEndOfBMP() { 244 245 try { 246 new Text("\uFFFE"); 247 fail("allowed FFFE"); 248 } 249 catch (IllegalCharacterDataException success) { 250 assertEquals("\uFFFE", success.getData()); 251 assertNotNull(success.getMessage()); 252 } 253 254 try { 255 new Text("\uFFFF"); 256 fail("allowed FFFF"); 257 } 258 catch (IllegalCharacterDataException success) { 259 assertEquals("\uFFFF", success.getData()); 260 assertNotNull(success.getMessage()); 261 } 262 263 } 264 265 266 public void testLeafNode() { 267 268 Text c1 = new Text("data"); 269 assertEquals(0, c1.getChildCount()); 270 try { 271 c1.getChild(0); 272 fail("Didn't throw IndexOutofBoundsException"); 273 } 274 catch (IndexOutOfBoundsException success) { 275 } 277 278 assertNull(c1.getParent()); 279 280 Element element = new Element("test"); 281 element.appendChild(c1); 282 assertEquals(element, c1.getParent()); 283 assertEquals(c1, element.getChild(0)); 284 285 element.removeChild(c1); 286 assertEquals(0, element.getChildCount()); 287 288 } 289 290 291 public void testToStringWithLineFeed() { 292 293 Text t = new Text("content\ncontent"); 294 assertEquals("[nu.xom.Text: content\\ncontent]", t.toString()); 295 296 } 297 298 299 public void testToStringWithCarriageReturn() { 300 301 Text t = new Text("content\rcontent"); 302 assertEquals("[nu.xom.Text: content\\rcontent]", t.toString()); 303 304 } 305 306 307 public void testToStringWithCarriageReturnLinefeed() { 308 309 Text t = new Text("content\r\ncontent"); 310 assertEquals("[nu.xom.Text: content\\r\\ncontent]", t.toString()); 311 312 } 313 314 315 public void testToStringWithTab() { 316 317 Text t = new Text("content\tcontent"); 318 assertEquals("[nu.xom.Text: content\\tcontent]", t.toString()); 319 320 } 321 322 323 public void testToString() { 324 325 Text t = new Text("content"); 326 assertEquals("[nu.xom.Text: content]", t.toString()); 327 328 t.setValue("012345678901234567890123456789012345678901234567890123456789"); 329 assertEquals( 330 "[nu.xom.Text: 01234567890123456789012345678901234...]", 331 t.toString() 332 ); 333 334 } 335 336 337 public void testCarriageReturnInText() { 339 Text text = new Text("data\rdata"); 340 String xml = text.toXML(); 341 assertEquals("data
data", xml); 342 } 343 344 345 public void testHighSurrogateWithNoLowSurrogate() { 346 347 String data = String.valueOf((char) 0xD800); 348 try { 349 new Text(data); 350 fail("Allowed single high surrogate in text node"); 351 } 352 catch (IllegalCharacterDataException success) { 353 assertEquals(data, success.getData()); 354 assertNotNull(success.getMessage()); 355 } 356 357 } 358 359 360 } 361 | Popular Tags |