1 21 22 package nu.xom.tests; 23 24 import java.io.BufferedInputStream ; 25 import java.io.ByteArrayInputStream ; 26 import java.io.ByteArrayOutputStream ; 27 import java.io.DataInputStream ; 28 import java.io.File ; 29 import java.io.FileInputStream ; 30 import java.io.FilenameFilter ; 31 import java.io.IOException ; 32 import java.io.InputStream ; 33 34 import com.ibm.icu.text.Normalizer; 35 36 import nu.xom.Attribute; 37 import nu.xom.Builder; 38 import nu.xom.Document; 39 import nu.xom.Element; 40 import nu.xom.Elements; 41 import nu.xom.ParsingException; 42 import nu.xom.canonical.Canonicalizer; 43 44 53 public class CanonicalizerTest extends XOMTestCase { 54 55 private final static double version = Double.parseDouble( 56 System.getProperty("java.version").substring(0,3) 57 ); 58 59 private File canonical; 60 private File input; 61 private File output; 62 63 public CanonicalizerTest(String name) { 64 super(name); 65 } 66 67 68 private Builder builder = new Builder(); 69 70 71 protected void setUp() { 72 File data = new File ("data"); 73 canonical = new File (data, "canonical"); 74 input = new File (canonical, "input"); 75 output = new File (canonical, "output"); 76 } 77 78 79 public void testWithComments() throws ParsingException, IOException { 80 81 File tests = input; 82 String [] inputs = tests.list(new XMLFilter()); 83 for (int i = 0; i < inputs.length; i++) { 84 File input = new File (tests, inputs[i]); 85 Document doc = builder.build(input); 86 ByteArrayOutputStream out = new ByteArrayOutputStream (); 87 try { 88 Canonicalizer serializer = new Canonicalizer(out); 89 serializer.write(doc); 90 } 91 finally { 92 out.close(); 93 } 94 byte[] actual = out.toByteArray(); 95 96 File expected = new File (output, input.getName() + ".out"); 97 assertEquals( 98 input.getName(), expected.length(), actual.length); 99 byte[] expectedBytes = new byte[actual.length]; 100 InputStream fin = new FileInputStream (expected); 101 DataInputStream in = new DataInputStream (fin); 102 try { 103 in.readFully(expectedBytes); 104 } 105 finally { 106 in.close(); 107 } 108 for (int j = 0; j < expectedBytes.length; j++) { 109 assertEquals(expectedBytes[i], actual[i]); 110 } 111 112 } 113 114 } 115 116 117 public void testWithoutComments() 118 throws ParsingException, IOException { 119 120 File tests = input; 121 String [] inputs = tests.list(new XMLFilter()); 122 for (int i = 0; i < inputs.length; i++) { 123 File input = new File (tests, inputs[i]); 124 Document doc = builder.build(input); 125 126 ByteArrayOutputStream out = new ByteArrayOutputStream (); 127 try { 128 Canonicalizer serializer 129 = new Canonicalizer(out, false); 130 serializer.write(doc); 131 } 132 finally { 133 out.close(); 134 } 135 136 byte[] actual = out.toByteArray(); 137 138 144 145 File expected = new File (canonical, "wocommentsoutput/"); 146 expected = new File (expected, input.getName() + ".out"); 147 byte[] expectedBytes = new byte[actual.length]; 148 InputStream fin = new FileInputStream (expected); 149 DataInputStream in = new DataInputStream (fin); 150 try { 151 in.readFully(expectedBytes); 152 } 153 finally { 154 in.close(); 155 } 156 for (int j = 0; j < expectedBytes.length; j++) { 157 assertEquals(expectedBytes[i], actual[i]); 158 } 159 out.close(); 160 161 } 162 163 } 164 165 166 public void testRelativeNamespaceURIsForbidden() 167 throws ParsingException, IOException { 168 169 try { 170 String data = "<test xmlns=\"relative\">data</test>"; 171 Document doc = builder.build(data, "http://www.ex.org/"); 172 ByteArrayOutputStream out = new ByteArrayOutputStream (); 173 Canonicalizer serializer 174 = new Canonicalizer(out, false); 175 serializer.write(doc); 176 fail("Canonicalized document with relative namespace URI"); 177 } 178 catch (ParsingException success) { 179 assertNotNull(success.getMessage()); 180 } 181 182 } 183 184 185 256 257 private static class XMLFilter implements FilenameFilter { 258 259 public boolean accept(File directory, String name) { 260 if (name.endsWith(".xml")) return true; 261 return false; 262 } 263 264 } 265 266 267 public void testNFCFromISO88591() 268 throws ParsingException, IOException { 269 isoNormalizationTest("ISO-8859-1"); 270 } 271 272 273 public void testNFCFromISO88592() 274 throws ParsingException, IOException { 275 isoNormalizationTest("ISO-8859-2"); 276 } 277 278 279 public void testNFCFromISO88593() 280 throws ParsingException, IOException { 281 isoNormalizationTest("ISO-8859-3"); 282 } 283 284 285 public void testNFCFromISO88594() 286 throws ParsingException, IOException { 287 isoNormalizationTest("ISO-8859-4"); 288 } 289 290 291 public void testNFCFromISO88595() 292 throws ParsingException, IOException { 293 isoNormalizationTest("ISO-8859-5"); 294 } 295 296 297 public void testNFCFromISO88596() 298 throws ParsingException, IOException { 299 300 isoNormalizationTest("ISO-8859-6"); 304 305 } 306 307 308 public void testNFCFromISO88597() 309 throws ParsingException, IOException { 310 isoNormalizationTest("ISO-8859-7"); 311 } 312 313 314 public void testNFCFromISO88598() 315 throws ParsingException, IOException { 316 isoNormalizationTest("ISO-8859-8"); 317 } 318 319 320 public void testNFCFromISO88599() 321 throws ParsingException, IOException { 322 isoNormalizationTest("ISO-8859-9"); 323 } 324 325 326 public void testNFCFromISO885913() 327 throws ParsingException, IOException { 328 329 if (version >= 1.3) { 330 isoNormalizationTest("ISO-8859-13"); 332 } 333 334 } 335 336 337 public void testNFCFromISO885915() 338 throws ParsingException, IOException { 339 isoNormalizationTest("ISO-8859-15"); 340 } 341 342 343 private void isoNormalizationTest(String encoding) 345 throws ParsingException, IOException { 346 347 String prolog = "<?xml version='1.0' encoding='" 348 + encoding + "'?>\r\n<root>"; 349 350 byte[] prologData = prolog.getBytes(encoding); 351 352 String epilog = "</root>"; 353 byte[] epilogData = epilog.getBytes(encoding); 354 byte[] data = new byte[prologData.length + epilogData.length + 255 - 160 + 1]; 355 System.arraycopy(prologData, 0, data, 0, prologData.length); 356 System.arraycopy(epilogData, 0, data, 357 data.length - epilogData.length, epilogData.length); 358 for (int i = 160; i <= 255; i++) { 359 data[prologData.length + (i-160)] = (byte) i; 360 } 361 InputStream in = new ByteArrayInputStream (data); 362 Document doc = builder.build(in); 363 String rawResult = doc.getValue(); 364 String normalizedResult = Normalizer.normalize(rawResult, Normalizer.NFC); 365 assertEquals("Parser doesn't use NFC when converting from " + encoding, 366 normalizedResult, rawResult); 367 368 } 369 370 371 public void testEBCDIC() 372 throws ParsingException, IOException { 373 374 String encoding = "IBM037"; 375 String prolog = "<?xml version='1.0' encoding='" 376 + encoding + "'?>\r\n<root>"; 377 byte[] prologData = prolog.getBytes(encoding); 378 String epilog = "</root>"; 379 byte[] epilogData = epilog.getBytes(encoding); 380 byte[] data = new byte[prologData.length + epilogData.length + 255 - 160 + 1]; 381 System.arraycopy(prologData, 0, data, 0, prologData.length); 382 System.arraycopy(epilogData, 0, data, 383 data.length - epilogData.length, epilogData.length); 384 StringBuffer buffer = new StringBuffer (255 - 160 + 1); 385 for (int i = 160; i <= 255; i++) { 386 buffer.append((char) i); 387 } 388 byte[] temp = buffer.toString().getBytes(encoding); 389 System.arraycopy(temp, 0, data, prologData.length, temp.length); 390 InputStream in = new ByteArrayInputStream (data); 391 Document doc = builder.build(in); 392 String rawResult = doc.getValue(); 393 String normalizedResult = Normalizer.normalize(rawResult, Normalizer.NFC); 394 assertEquals("Parser doesn't use NFC when converting from " + encoding, 395 normalizedResult, rawResult); 396 397 } 398 399 400 public void testNullDocument() 402 throws IOException { 403 404 ByteArrayOutputStream out = new ByteArrayOutputStream (); 405 Canonicalizer canonicalizer = new Canonicalizer(out); 406 try { 407 canonicalizer.write(null); 408 fail("Wrote null document"); 409 } 410 catch (NullPointerException success) { 411 } 413 byte[] result = out.toByteArray(); 414 assertEquals(0, result.length); 415 416 } 417 418 419 public void testWhiteSpaceTrimmingInNonCDATAAttribute() 420 throws IOException { 421 Attribute attribute = new Attribute("name", " value1 value2 "); 422 attribute.setType(Attribute.Type.NMTOKENS); 423 Element root = new Element("root"); 424 root.addAttribute(attribute); 425 Document doc = new Document(root); 426 ByteArrayOutputStream out = new ByteArrayOutputStream (); 427 Canonicalizer canonicalizer = new Canonicalizer(out); 428 canonicalizer.write(doc); 429 out.close(); 430 String result = new String (out.toByteArray(), "UTF8"); 431 assertEquals("<root name=\"value1 value2\"></root>", result); 432 } 433 434 435 public void testXMLConformanceTestSuiteDocuments() 437 throws ParsingException, IOException { 438 439 File masterList = new File (canonical, "xmlconf"); 440 masterList = new File (masterList, "xmlconf.xml"); 441 if (masterList.exists()) { 442 Document xmlconf = builder.build(masterList); 443 Elements testcases = xmlconf.getRootElement().getChildElements("TESTCASES"); 444 processTestCases(testcases); 445 } 446 447 } 448 449 450 private void processTestCases(Elements testcases) 453 throws ParsingException, IOException { 454 455 for (int i = 0; i < testcases.size(); i++) { 456 Element testcase = testcases.get(i); 457 Elements tests = testcase.getChildElements("TEST"); 458 processTests(tests); 459 Elements level2 = testcase.getChildElements("TESTCASES"); 460 processTestCases(level2); 462 } 463 464 } 465 466 467 private void processTests(Elements tests) 468 throws ParsingException, IOException { 469 470 for (int i = 0; i < tests.size(); i++) { 471 Element test = tests.get(i); 472 String namespace = test.getAttributeValue("NAMESPACE"); 473 if ("no".equals(namespace)) continue; 474 String type = test.getAttributeValue("TYPE"); 475 if ("not-wf".equals(type)) continue; 476 String uri = test.getAttributeValue("URI"); 477 String base = test.getBaseURI(); 478 Element parent = new Element("e"); 481 parent.setBaseURI(base); 482 Element child = new Element("a"); 483 child.addAttribute(new Attribute("xml:base", 484 "http://www.w3.org/XML/1998/namespace", uri)); 485 parent.appendChild(child); 486 String resolvedURI = child.getBaseURI(); 487 488 Document doc = builder.build(resolvedURI); 489 ByteArrayOutputStream out = new ByteArrayOutputStream (); 490 try { 491 Canonicalizer serializer = new Canonicalizer(out); 492 serializer.write(doc); 493 } 494 finally { 495 out.close(); 496 } 497 byte[] actual = out.toByteArray(); 498 499 File input = new File (resolvedURI.substring(5) + ".can"); 500 assertEquals(resolvedURI, input.length(), actual.length); 501 byte[] expected = new byte[actual.length]; 502 DataInputStream in = new DataInputStream ( 503 new BufferedInputStream (new FileInputStream (input))); 504 try { 505 in.readFully(expected); 506 } 507 finally { 508 in.close(); 509 } 510 for (int j = 0; j < expected.length; j++) { 511 assertEquals(resolvedURI + " at byte " + j, 512 expected[j], actual[j]); 513 } 514 515 } 516 517 } 518 519 520 } 521 | Popular Tags |