1 21 22 package nu.xom.tests; 23 24 import nu.xom.Element; 25 import nu.xom.IllegalDataException; 26 import nu.xom.IllegalTargetException; 27 import nu.xom.ProcessingInstruction; 28 29 38 public class ProcessingInstructionTest extends XOMTestCase { 39 40 41 public ProcessingInstructionTest(String name) { 42 super(name); 43 } 44 45 46 private ProcessingInstruction pi; 47 48 49 protected void setUp() { 50 pi = new ProcessingInstruction("test", "test"); 51 } 52 53 54 public void testToXML() { 55 assertEquals("<?test test?>", pi.toXML()); 56 } 57 58 59 public void testToString() { 60 assertEquals( 61 "[nu.xom.ProcessingInstruction: target=\"test\"; data=\"test\"]", 62 pi.toString()); 63 } 64 65 66 public void testToStringWithLineFeed() { 67 68 ProcessingInstruction p 69 = new ProcessingInstruction("test", "content\ncontent"); 70 assertEquals( 71 "[nu.xom.ProcessingInstruction: target=\"test\"; data=\"content\\ncontent\"]", 72 p.toString() 73 ); 74 75 } 76 77 78 public void testToStringWithLotsOfData() { 79 80 ProcessingInstruction p 81 = new ProcessingInstruction("target", 82 "content content 012345678901234567890123456789012345678901234567890123456789"); 83 String s = p.toString(); 84 assertTrue(s.endsWith("...\"]")); 85 86 } 87 88 89 public void testConstructor() { 90 91 assertEquals("test", pi.getValue()); 92 assertEquals("test", pi.getTarget()); 93 94 try { 95 new ProcessingInstruction("test:test", "test"); 96 fail("Processing instruction targets cannot contain colons"); 97 } 98 catch (IllegalTargetException success) { 99 assertNotNull(success.getMessage()); 100 assertEquals("test:test", success.getData()); 101 } 102 103 try { 104 new ProcessingInstruction("", "test"); 105 fail("Processing instruction targets cannot be empty"); 106 } 107 catch (IllegalTargetException success) { 108 assertNotNull(success.getMessage()); 109 assertEquals("", success.getData()); 110 } 111 112 try { 113 new ProcessingInstruction(null, "test"); 114 fail("Processing instruction targets cannot be empty"); 115 } 116 catch (IllegalTargetException success) { 117 assertNotNull(success.getMessage()); 118 assertNull(success.getData()); 119 } 120 121 try { 122 new ProcessingInstruction("12345", "test"); 123 fail("Processing instruction targets must be NCNames"); 124 } 125 catch (IllegalTargetException success) { 126 assertEquals("12345", success.getData()); 127 } 128 129 pi = new ProcessingInstruction("test", ""); 131 assertEquals("", pi.getValue()); 132 assertEquals("<?test?>", pi.toXML()); 133 134 } 135 136 137 public void testSetTarget() { 138 139 try { 140 pi.setTarget("test:test"); 141 fail("Processing instruction targets cannot contain colons"); 142 } 143 catch (IllegalTargetException success) { 144 assertNotNull(success.getMessage()); 145 assertEquals("test:test", success.getData()); 146 } 147 148 try { 149 pi.setTarget(""); 150 fail("Processing instruction targets cannot be empty"); 151 } 152 catch (IllegalTargetException success) { 153 assertNotNull(success.getMessage()); 154 assertEquals("", success.getData()); 155 } 156 157 try { 158 pi.setTarget(null); 159 fail("Processing instruction targets cannot be empty"); 160 } 161 catch (IllegalTargetException success) { 162 assertNotNull(success.getMessage()); 163 assertNull(success.getData()); 164 } 165 166 try { 167 pi.setTarget("12345"); 168 fail("Processing instruction targets must be NCNames"); 169 } 170 catch (IllegalTargetException success) { 171 assertEquals("12345", success.getData()); 172 } 173 174 pi.setTarget("testing123"); 175 assertEquals("testing123", pi.getTarget()); 176 177 } 178 179 180 public void testCopyConstructor() { 181 182 ProcessingInstruction instruction1 = new ProcessingInstruction("target", "data"); 183 ProcessingInstruction instruction2 = new ProcessingInstruction(instruction1); 184 185 assertEquals(instruction1.getTarget(), instruction2.getTarget()); 186 assertEquals(instruction1.getValue(), instruction2.getValue()); 187 assertEquals(instruction1.toXML(), instruction2.toXML()); 188 189 } 190 191 192 public void testSetValue() { 193 194 try { 195 pi.setValue("kjsahdj ?>"); 196 fail("Should raise an IllegalDataException"); 197 } 198 catch (IllegalDataException success) { 199 assertEquals("kjsahdj ?>", success.getData()); 200 assertNotNull(success.getMessage()); 201 } 202 203 try { 204 pi.setValue("?>"); 205 fail("Should raise an IllegalDataException"); 206 } 207 catch (IllegalDataException success) { 208 assertEquals("?>", success.getData()); 209 assertNotNull(success.getMessage()); 210 } 211 212 try { 213 pi.setValue("kjsahdj ?> skhskjlhd"); 214 fail("Should raise an IllegalDataException"); 215 } 216 catch (IllegalDataException success) { 217 assertEquals("kjsahdj ?> skhskjlhd", success.getData()); 218 assertNotNull(success.getMessage()); 219 } 220 221 try { 222 pi.setValue(null); 223 fail("Allowed null data"); 224 } 225 catch (IllegalDataException success) { 226 assertNull(success.getData()); 227 assertNotNull(success.getMessage()); 228 } 229 230 231 String [] testData = {"<html></html>", 233 "name=value", 234 "name='value'", 235 "name=\"value\"", 236 "salkdhsalkjhdkjsadhkj sadhsajkdh", 237 "<?", "? >", "--" 238 }; 239 for (int i = 0; i < testData.length; i++) { 240 pi.setValue(testData[i]); 241 assertEquals(testData[i], pi.getValue()); 242 } 243 244 } 245 246 247 public void testNames() { 248 assertEquals("test", pi.getTarget()); 249 } 250 251 252 public void testEquals() { 253 254 ProcessingInstruction pi1 255 = new ProcessingInstruction("test", "afaf"); 256 ProcessingInstruction pi2 257 = new ProcessingInstruction("test", "afaf"); 258 ProcessingInstruction pi3 259 = new ProcessingInstruction("tegggst", "afaf"); 260 ProcessingInstruction pi4 261 = new ProcessingInstruction("test", "1234"); 262 263 assertEquals(pi1, pi1); 264 assertEquals(pi1.hashCode(), pi1.hashCode()); 265 assertTrue(!pi1.equals(pi2)); 266 assertTrue(!pi1.equals(pi3)); 267 assertTrue(!pi3.equals(pi4)); 268 assertTrue(!pi2.equals(pi4)); 269 assertTrue(!pi2.equals(pi3)); 270 271 } 272 273 274 public void testCopy() { 275 276 Element test = new Element("test"); 277 test.appendChild(pi); 278 ProcessingInstruction c2 = (ProcessingInstruction) pi.copy(); 279 280 assertEquals(pi, c2); 281 assertEquals(pi.getValue(), c2.getValue()); 282 assertTrue(!pi.equals(c2)); 283 assertNull(c2.getParent()); 284 285 } 286 287 288 public void testCorrectSurrogates() { 290 291 String goodString = "test: \uD8F5\uDF80 "; 292 pi.setValue(goodString); 293 assertEquals(goodString, pi.getValue()); 294 295 } 296 297 298 public void testSurrogates() { 300 301 try { 302 pi.setValue("test \uD8F5\uD8F5 test"); 303 fail("Allowed two high halves"); 304 } 305 catch (IllegalDataException success) { 306 assertEquals("test \uD8F5\uD8F5 test", success.getData()); 307 assertNotNull(success.getMessage()); 308 } 309 310 try { 311 pi.setValue("test \uDF80\uDF80 test"); 312 fail("Allowed two low halves"); 313 } 314 catch (IllegalDataException success) { 315 assertEquals("test \uDF80\uDF80 test", success.getData()); 316 assertNotNull(success.getMessage()); 317 } 318 319 try { 320 pi.setValue("test \uD8F5 \uDF80 test"); 321 fail("Allowed two halves split by space"); 322 } 323 catch (IllegalDataException success) { 324 assertEquals("test \uD8F5 \uDF80 test", success.getData()); 325 assertNotNull(success.getMessage()); 326 } 327 328 try { 329 pi.setValue("test \uDF80\uD8F5 test"); 330 fail("Allowed reversed pair"); 331 } 332 catch (IllegalDataException success) { 333 assertEquals("test \uDF80\uD8F5 test", success.getData()); 334 assertNotNull(success.getMessage()); 335 } 336 337 } 338 339 340 public void testLeafNode() { 341 342 assertEquals(0, pi.getChildCount()); 343 try { 344 pi.getChild(0); 345 fail("Didn't throw IndexOutofBoundsException"); 346 } 347 catch (IndexOutOfBoundsException success) { 348 assertNotNull(success.getMessage()); 349 } 350 351 assertNull(pi.getParent()); 352 353 Element element = new Element("test"); 354 element.appendChild(pi); 355 assertEquals(element, pi.getParent()); 356 assertEquals(pi, element.getChild(0)); 357 358 element.removeChild(pi); 359 assertEquals(0, element.getChildCount()); 360 361 } 362 363 364 public void testCarriageReturnInProcessingInstructionData() { 368 369 try { 370 new ProcessingInstruction("target", "data\rdata"); 371 fail("Allowed carriage return in processing instruction data"); 372 } 373 catch (IllegalDataException success) { 374 assertEquals("data\rdata", success.getData()); 375 assertNotNull(success.getMessage()); 376 } 377 378 } 379 380 381 public void testAllowReservedCharactersInData() { 382 ProcessingInstruction pi = new ProcessingInstruction("target", "<test>&&greater;"); 383 String xml = pi.toXML(); 384 assertEquals("<?target <test>&&greater;?>", xml); 385 } 386 387 388 public void testNoInitialWhiteSpace() { 390 391 try { 392 new ProcessingInstruction("target", " initial spaces"); 393 fail("allowed processing instruction data with leading space"); 394 } 395 catch (IllegalDataException success) { 396 assertEquals(" initial spaces", success.getData()); 397 assertNotNull(success.getMessage()); 398 } 399 400 try { 401 new ProcessingInstruction("target", "\tinitial tab"); 402 fail("allowed processing instruction data with leading space"); 403 } 404 catch (IllegalDataException success) { 405 assertEquals("\tinitial tab", success.getData()); 406 assertNotNull(success.getMessage()); 407 } 408 409 try { 410 new ProcessingInstruction("target", "\ninitial linefeed"); 411 fail("allowed processing instruction data with leading space"); 412 } 413 catch (IllegalDataException success) { 414 assertEquals("\ninitial linefeed", success.getData()); 415 assertNotNull(success.getMessage()); 416 } 417 418 try { 419 new ProcessingInstruction("target", "\r initial carriage return"); 420 fail("allowed processing instruction data with leading space"); 421 } 422 catch (IllegalDataException success) { 423 assertEquals("\r initial carriage return", success.getData()); 424 assertNotNull(success.getMessage()); 425 } 426 427 } 428 429 430 public void testNoXMLTargets() { 431 432 try { 433 new ProcessingInstruction("xml", "data"); 434 fail("allowed processing instruction with target xml"); 435 } 436 catch (IllegalTargetException success) { 437 assertEquals("xml", success.getData()); 438 assertNotNull(success.getMessage()); 439 } 440 441 try { 442 new ProcessingInstruction("XML", "data"); 443 fail("allowed processing instruction with target XML"); 444 } 445 catch (IllegalTargetException success) { 446 assertEquals("XML", success.getData()); 447 assertNotNull(success.getMessage()); 448 } 449 450 try { 451 new ProcessingInstruction("Xml", "data"); 452 fail("allowed processing instruction with target Xml"); 453 } 454 catch (IllegalTargetException success) { 455 assertEquals("Xml", success.getData()); 456 assertNotNull(success.getMessage()); 457 } 458 459 } 460 461 462 public void testColonsNotAllowedInTargets() { 463 464 try { 465 new ProcessingInstruction("pre:target", "data"); 466 fail("allowed processing instruction with target that uses a prefixed name"); 467 } 468 catch (IllegalTargetException success) { 469 assertEquals("pre:target", success.getData()); 470 assertNotNull(success.getMessage()); 471 } 472 473 try { 474 new ProcessingInstruction("pre:", "data"); 475 fail("allowed processing instruction with trailing colon in target"); 476 } 477 catch (IllegalTargetException success) { 478 assertEquals("pre:", success.getData()); 479 assertNotNull(success.getMessage()); 480 } 481 482 try { 483 new ProcessingInstruction(":target", "data"); 484 fail("allowed processing instruction with initial colon in target"); 485 } 486 catch (IllegalTargetException success) { 487 assertEquals(":target", success.getData()); 488 assertNotNull(success.getMessage()); 489 } 490 491 } 492 493 494 } 495 | Popular Tags |