1 21 22 package nu.xom.tests; 23 24 import java.io.ByteArrayOutputStream ; 25 import java.io.IOException ; 26 27 import nu.xom.*; 28 29 39 public class CDATASectionTest extends XOMTestCase { 40 41 42 public CDATASectionTest(String name) { 43 super(name); 44 } 45 46 47 private String data = "<test><child1><![CDATA[<&>]]></child1>" 48 + "<child2> <![CDATA[<&>]]> </child2> " 49 + "<child3><![CDATA[<&>]]> </child3> " 50 + "<child4><![CDATA[<&>]]> <![CDATA[<&>]]></child4> " 51 + "</test>"; 52 private Document doc; 53 private Builder builder; 54 55 56 protected void setUp() 57 throws ValidityException, ParsingException, IOException { 58 builder = new Builder(); 59 doc = builder.build(data, "http://www.base.com"); 60 } 61 62 63 public void testCopy() { 64 Element child1 = doc.getRootElement().getFirstChildElement("child1"); 65 Node cdata = child1.getChild(0); 66 Node copy = cdata.copy(); 67 assertTrue(cdata instanceof Text); 68 assertEquals("nu.xom.CDATASection", copy.getClass().getName()); 69 assertEquals("<&>", copy.getValue()); 70 } 71 72 73 public void testUseCDATAWherePossible() { 74 Element child1 = doc.getRootElement().getFirstChildElement("child1"); 75 Node cdata = child1.getChild(0); 76 assertTrue(cdata instanceof Text); 77 assertEquals("nu.xom.CDATASection", cdata.getClass().getName()); 78 assertEquals("<&>", cdata.getValue()); 79 } 80 81 82 public void testDontAllowCDATASectionToSplitTextNode() { 83 Element child2 = doc.getRootElement().getFirstChildElement("child2"); 84 assertEquals(1, child2.getChildCount()); 85 Node data = child2.getChild(0); 86 assertTrue(data instanceof Text); 87 assertEquals("nu.xom.Text", data.getClass().getName()); 88 assertEquals(" <&> ", data.getValue()); 89 } 90 91 92 public void testAccumulateTextNodeAfterCDATASection() { 93 Element child3 = doc.getRootElement().getFirstChildElement("child3"); 94 assertEquals(1, child3.getChildCount()); 95 Node data = child3.getChild(0); 96 assertTrue(data instanceof Text); 97 assertEquals("nu.xom.Text", data.getClass().getName()); 98 assertEquals("<&> ", data.getValue()); 99 } 100 101 102 public void testAccumulateTextNodeAcrossMultipleCDATASections() { 103 Element child4 = doc.getRootElement().getFirstChildElement("child4"); 104 assertEquals(1, child4.getChildCount()); 105 Node data = child4.getChild(0); 106 assertTrue(data instanceof Text); 107 assertEquals("nu.xom.Text", data.getClass().getName()); 108 assertEquals("<&> <&>", data.getValue()); 109 } 110 111 112 public void testSerializeCDATASection() throws IOException { 113 ByteArrayOutputStream out = new ByteArrayOutputStream (); 114 Serializer serializer = new Serializer(out); 115 serializer.write(doc); 116 byte[] data = out.toByteArray(); 117 String result = new String (data, "UTF8"); 118 assertTrue(result.indexOf("<![CDATA[<&>]]>") > 0); 119 120 } 121 122 123 public void testSerializeCDATASectionWithOutOfRangeCharacter() 124 throws ValidityException, ParsingException, IOException { 125 126 String data = "<test><![CDATA[\u0298]]></test>"; 127 doc = builder.build(data, "http://www.example.com"); 128 ByteArrayOutputStream out = new ByteArrayOutputStream (); 129 Serializer serializer = new Serializer(out, "ISO-8859-1"); 130 serializer.write(doc); 131 byte[] output = out.toByteArray(); 132 String result = new String (output, "8859_1"); 133 assertEquals(-1, result.indexOf("<![CDATA[<&>]]>")); 134 assertTrue(result.indexOf("ʘ") > 1); 135 136 } 137 138 139 public void testSerializeCDATASectionWithInRangeCharactersAndANonUnicodeEncoding() 140 throws ValidityException, ParsingException, IOException { 141 142 String data = "<test><![CDATA[abcd]]></test>"; 143 doc = builder.build(data, "http://www.example.com"); 144 ByteArrayOutputStream out = new ByteArrayOutputStream (); 145 Serializer serializer = new Serializer(out, "ISO-8859-1"); 146 serializer.write(doc); 147 byte[] output = out.toByteArray(); 148 String result = new String (output, "8859_1"); 149 assertTrue(result.indexOf("<![CDATA[abcd]]>") > 1); 150 151 } 152 153 154 public void testSerializeCDATASectionWithCDATASectionEndDelimiter() 155 throws ValidityException, ParsingException, IOException { 156 157 String data = "<test><![CDATA[original data]]></test>"; 158 doc = builder.build(data, "http://www.example.com"); 159 Text content = (Text) (doc.getRootElement().getChild(0)); 160 content.setValue("]]>"); 161 ByteArrayOutputStream out = new ByteArrayOutputStream (); 162 Serializer serializer = new Serializer(out); 163 serializer.write(doc); 164 byte[] output = out.toByteArray(); 165 String result = new String (output, "UTF8"); 166 assertEquals(-1, result.indexOf("<![CDATA[]]>]]>")); 167 assertTrue(result.indexOf("]]>") > 1); 168 } 169 170 171 } | Popular Tags |