Clover coverage report - Clover results for XOM 1.2d1
Coverage timestamp: Wed Feb 8 2006 08:31:33 EST
file stats: LOC: 214   Methods: 15
NCLOC: 139   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
CDATASectionTest.java - 100% 100% 100%
coverage
 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 java.io.ByteArrayOutputStream;
 25    import java.io.IOException;
 26   
 27    import nu.xom.*;
 28   
 29    /**
 30    * <p>
 31    * Test that CDATA sections are read and where possible
 32    * preserved upon serialization.
 33    * </p>
 34    *
 35    * @author Elliotte Rusty Harold
 36    * @version 1.1b4
 37    *
 38    */
 39    public class CDATASectionTest extends XOMTestCase {
 40   
 41   
 42  13 public CDATASectionTest(String name) {
 43  13 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    + "<child5><![CDATA[<&>]]>]]&gt;<![CDATA[<&>]]></child5> "
 52    + "</test>";
 53    private Document doc;
 54    private Builder builder;
 55   
 56   
 57  13 protected void setUp()
 58    throws ValidityException, ParsingException, IOException {
 59  13 builder = new Builder();
 60  13 doc = builder.build(data, "http://www.base.com");
 61    }
 62   
 63   
 64  1 public void testCopy() {
 65  1 Element child1 = doc.getRootElement().getFirstChildElement("child1");
 66  1 Node cdata = child1.getChild(0);
 67  1 Node copy = cdata.copy();
 68  1 assertTrue(cdata instanceof Text);
 69  1 assertEquals("nu.xom.CDATASection", copy.getClass().getName());
 70  1 assertEquals("<&>", copy.getValue());
 71    }
 72   
 73   
 74  1 public void testToXML() {
 75  1 Element child1 = doc.getRootElement().getFirstChildElement("child1");
 76  1 Node cdata = child1.getChild(0);
 77  1 String result = cdata.toXML();
 78  1 assertEquals("<![CDATA[<&>]]>", result);
 79    }
 80   
 81   
 82  1 public void testToXMLWhenCDATASectionContainsEndDelimiter() {
 83  1 Element child1 = doc.getRootElement().getFirstChildElement("child1");
 84  1 Text cdata = (Text) child1.getChild(0);
 85  1 cdata.setValue("A]]>A");
 86  1 assertEquals("A]]&gt;A", cdata.toXML());
 87    }
 88   
 89   
 90  1 public void testUseCDATAWherePossible() {
 91  1 Element child1 = doc.getRootElement().getFirstChildElement("child1");
 92  1 Node cdata = child1.getChild(0);
 93  1 assertTrue(cdata instanceof Text);
 94  1 assertEquals("nu.xom.CDATASection", cdata.getClass().getName());
 95  1 assertEquals("<&>", cdata.getValue());
 96    }
 97   
 98   
 99  1 public void testDontAllowCDATASectionToSplitTextNode() {
 100  1 Element child2 = doc.getRootElement().getFirstChildElement("child2");
 101  1 assertEquals(1, child2.getChildCount());
 102  1 Node data = child2.getChild(0);
 103  1 assertTrue(data instanceof Text);
 104  1 assertEquals("nu.xom.Text", data.getClass().getName());
 105  1 assertEquals(" <&> ", data.getValue());
 106    }
 107   
 108   
 109  1 public void testAccumulateTextNodeAfterCDATASection() {
 110  1 Element child3 = doc.getRootElement().getFirstChildElement("child3");
 111  1 assertEquals(1, child3.getChildCount());
 112  1 Node data = child3.getChild(0);
 113  1 assertTrue(data instanceof Text);
 114  1 assertEquals("nu.xom.Text", data.getClass().getName());
 115  1 assertEquals("<&> ", data.getValue());
 116    }
 117   
 118   
 119  1 public void testAccumulateTextNodeAcrossMultipleCDATASections() {
 120  1 Element child4 = doc.getRootElement().getFirstChildElement("child4");
 121  1 assertEquals(1, child4.getChildCount());
 122  1 Node data = child4.getChild(0);
 123  1 assertTrue(data instanceof Text);
 124  1 assertEquals("nu.xom.Text", data.getClass().getName());
 125  1 assertEquals("<&> <&>", data.getValue());
 126    }
 127   
 128   
 129  1 public void testDontAllowCDATASectionToContainCDATASectionEndDelimiter() {
 130  1 Element child5 = doc.getRootElement().getFirstChildElement("child5");
 131  1 assertEquals(1, child5.getChildCount());
 132  1 Node data = child5.getChild(0);
 133  1 assertTrue(data instanceof Text);
 134  1 assertEquals("<&>]]><&>", data.getValue());
 135  1 assertEquals("&lt;&amp;&gt;]]&gt;&lt;&amp;&gt;", data.toXML());
 136    }
 137   
 138   
 139  1 public void testDontAllowCDATASectionToContainCDATASectionEndDelimiter2()
 140    throws IOException {
 141  1 Element child5 = doc.getRootElement().getFirstChildElement("child5");
 142  1 assertEquals(1, child5.getChildCount());
 143  1 child5.detach();
 144  1 Document doc = new Document(child5);
 145  1 ByteArrayOutputStream out = new ByteArrayOutputStream();
 146  1 Serializer serializer = new Serializer(out);
 147  1 serializer.write(doc);
 148  1 serializer.flush();
 149  1 String result = new String(out.toByteArray(), "UTF-8");
 150  1 assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
 151    + "<child5>&lt;&amp;&gt;]]&gt;&lt;&amp;&gt;</child5>\r\n", result);
 152    }
 153   
 154   
 155  1 public void testSerializeCDATASection() throws IOException {
 156  1 ByteArrayOutputStream out = new ByteArrayOutputStream();
 157  1 Serializer serializer = new Serializer(out);
 158  1 serializer.write(doc);
 159  1 byte[] data = out.toByteArray();
 160  1 String result = new String(data, "UTF8");
 161  1 assertTrue(result.indexOf("<![CDATA[<&>]]>") > 0);
 162   
 163    }
 164   
 165   
 166  1 public void testSerializeCDATASectionWithOutOfRangeCharacter()
 167    throws ValidityException, ParsingException, IOException {
 168   
 169  1 String data = "<test><![CDATA[\u0298]]></test>";
 170  1 doc = builder.build(data, "http://www.example.com");
 171  1 ByteArrayOutputStream out = new ByteArrayOutputStream();
 172  1 Serializer serializer = new Serializer(out, "ISO-8859-1");
 173  1 serializer.write(doc);
 174  1 byte[] output = out.toByteArray();
 175  1 String result = new String(output, "8859_1");
 176  1 assertEquals(-1, result.indexOf("<![CDATA[<&>]]>"));
 177  1 assertTrue(result.indexOf("&#x298;") > 1);
 178   
 179    }
 180   
 181   
 182  1 public void testSerializeCDATASectionWithInRangeCharactersAndANonUnicodeEncoding()
 183    throws ValidityException, ParsingException, IOException {
 184   
 185  1 String data = "<test><![CDATA[abcd]]></test>";
 186  1 doc = builder.build(data, "http://www.example.com");
 187  1 ByteArrayOutputStream out = new ByteArrayOutputStream();
 188  1 Serializer serializer = new Serializer(out, "ISO-8859-1");
 189  1 serializer.write(doc);
 190  1 byte[] output = out.toByteArray();
 191  1 String result = new String(output, "8859_1");
 192  1 assertTrue(result.indexOf("<![CDATA[abcd]]>") > 1);
 193   
 194    }
 195   
 196   
 197  1 public void testSerializeCDATASectionWithCDATASectionEndDelimiter()
 198    throws ValidityException, ParsingException, IOException {
 199   
 200  1 String data = "<test><![CDATA[original data]]></test>";
 201  1 doc = builder.build(data, "http://www.example.com");
 202  1 Text content = (Text) (doc.getRootElement().getChild(0));
 203  1 content.setValue("]]>");
 204  1 ByteArrayOutputStream out = new ByteArrayOutputStream();
 205  1 Serializer serializer = new Serializer(out);
 206  1 serializer.write(doc);
 207  1 byte[] output = out.toByteArray();
 208  1 String result = new String(output, "UTF8");
 209  1 assertEquals(-1, result.indexOf("<![CDATA[]]>]]>"));
 210  1 assertTrue(result.indexOf("]]&gt;") > 1);
 211    }
 212   
 213   
 214    }