Clover coverage report - Clover results for XOM 1.2d1
Coverage timestamp: Wed Feb 8 2006 08:31:33 EST
file stats: LOC: 322   Methods: 18
NCLOC: 202   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
CommentTest.java - 90.7% 100% 91.9%
coverage coverage
 1    /* Copyright 2002-2004 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 nu.xom.Comment;
 25    import nu.xom.Document;
 26    import nu.xom.Element;
 27    import nu.xom.IllegalDataException;
 28    import nu.xom.IllegalCharacterDataException;
 29   
 30   
 31    /**
 32    * <p>
 33    * Various basic unit tests for the <code>Comment</code> class.
 34    * </p>
 35    *
 36    * @author Elliotte Rusty Harold
 37    * @version 1.0
 38    *
 39    */
 40    public class CommentTest extends XOMTestCase {
 41   
 42   
 43  17 public CommentTest(String name) {
 44  17 super(name);
 45    }
 46   
 47   
 48  1 public void testConstructor() {
 49  1 Comment c1 = new Comment("test");
 50  1 assertEquals("test", c1.getValue());
 51  1 Comment c2 = new Comment("");
 52  1 assertEquals("", c2.getValue());
 53    }
 54   
 55   
 56  1 public void testCopyConstructor() {
 57  1 Comment c1 = new Comment("test");
 58  1 Comment c2 = new Comment(c1);
 59  1 assertEquals("test", c2.getValue());
 60  1 assertEquals(c1.getValue(), c2.getValue());
 61  1 assertTrue(c1 != c2);
 62    }
 63   
 64   
 65  1 public void testToString() {
 66   
 67  1 Comment c1 = new Comment("content");
 68  1 assertEquals("[nu.xom.Comment: content]", c1.toString());
 69   
 70  1 c1.setValue("012345678901234567890123456789012345678901234567890123456789");
 71  1 assertEquals(
 72    "[nu.xom.Comment: 01234567890123456789012345678901234...]",
 73    c1.toString()
 74    );
 75   
 76    }
 77   
 78   
 79  1 public void testToStringWithLineFeed() {
 80   
 81  1 Comment c = new Comment("content\ncontent");
 82  1 assertEquals("[nu.xom.Comment: content\\ncontent]", c.toString());
 83   
 84    }
 85   
 86   
 87  1 public void testToStringWithLotsOfData() {
 88   
 89  1 Comment c
 90    = new Comment("content content 012345678901234567890123456789012345678901234567890123456789");
 91  1 String s = c.toString();
 92  1 assertTrue(s.length() < 72);
 93   
 94    }
 95   
 96   
 97  1 public void testToXML() {
 98   
 99  1 Comment c1 = new Comment("content");
 100  1 assertEquals("<!--content-->", c1.toXML());
 101   
 102  1 c1.setValue(" 012345678901234567890123456789012345678901234567890123456789 ");
 103  1 assertEquals(
 104    "<!-- 012345678901234567890123456789012345678901234567890123456789 -->",
 105    c1.toXML()
 106    );
 107   
 108    }
 109   
 110   
 111    // This is a problem because it cannot be serialized
 112    // since character and entity references aren't
 113    // recognized in comment data
 114  1 public void testCarriageReturnInCommentData() {
 115  1 try {
 116  1 new Comment("data\rdata");
 117  0 fail("Allowed carriage return in comment");
 118    }
 119    catch (IllegalDataException success) {
 120  1 assertEquals("data\rdata", success.getData());
 121  1 assertNotNull(success.getMessage());
 122    }
 123    }
 124   
 125   
 126  1 public void testSetter() {
 127   
 128  1 Comment c1 = new Comment("test");
 129  1 c1.setValue("legal");
 130  1 assertEquals("legal", c1.getValue());
 131   
 132  1 try {
 133  1 c1.setValue("test -- test");
 134  0 fail("Should raise an IllegalDataException");
 135    }
 136    catch (IllegalDataException success) {
 137  1 assertEquals("test -- test", success.getData());
 138  1 assertNotNull(success.getMessage());
 139    }
 140   
 141  1 try {
 142  1 c1.setValue("test-");
 143  0 fail("Should raise an IllegalDataException");
 144    }
 145    catch (IllegalDataException success) {
 146  1 assertEquals("test-", success.getData());
 147  1 assertNotNull(success.getMessage());
 148    }
 149   
 150  1 c1.setValue(null);
 151  1 assertEquals("", c1.getValue());
 152   
 153    }
 154   
 155   
 156  1 public void testEquals() {
 157   
 158  1 Comment c1 = new Comment("test");
 159  1 Comment c2 = new Comment("test");
 160  1 Comment c3 = new Comment("skjlchsakdjh");
 161   
 162  1 assertEquals(c1, c1);
 163  1 assertEquals(c1.hashCode(), c1.hashCode());
 164  1 assertTrue(!c1.equals(c2));
 165  1 assertTrue(!c1.equals(c3));
 166   
 167    }
 168   
 169   
 170  1 public void testCopy() {
 171   
 172  1 Comment c1 = new Comment("test");
 173  1 Comment c2 = (Comment) c1.copy();
 174   
 175  1 assertEquals(c1.getValue(), c2.getValue());
 176  1 assertTrue(!c1.equals(c2));
 177  1 assertNull(c2.getParent());
 178   
 179    }
 180   
 181   
 182    // Check passing in a string with broken surrogate pairs
 183    // and with correct surrogate pairs
 184  1 public void testSurrogates() {
 185   
 186  1 String goodString = "test: \uD8F5\uDF80 ";
 187  1 Comment c = new Comment(goodString);
 188  1 assertEquals(goodString, c.getValue());
 189   
 190    // Two high-halves
 191  1 try {
 192  1 new Comment("test: \uD8F5\uDBF0 ");
 193  0 fail("Should raise an IllegalCharacterDataException");
 194    }
 195    catch (IllegalCharacterDataException success) {
 196  1 assertEquals("test: \uD8F5\uDBF0 ", success.getData());
 197  1 assertNotNull(success.getMessage());
 198    }
 199   
 200    // Two high-halves
 201  1 try {
 202  1 new Comment("test: \uD8F5\uD8F5 ");
 203  0 fail("Should raise an IllegalCharacterDataException");
 204    }
 205    catch (IllegalCharacterDataException success) {
 206  1 assertEquals("test: \uD8F5\uD8F5 ", success.getData());
 207  1 assertNotNull(success.getMessage());
 208    }
 209   
 210    // One high-half
 211  1 try {
 212  1 new Comment("test: \uD8F5 ");
 213  0 fail("Should raise an IllegalCharacterDataException");
 214    }
 215    catch (IllegalCharacterDataException success) {
 216  1 assertEquals("test: \uD8F5 ", success.getData());
 217  1 assertNotNull(success.getMessage());
 218    }
 219   
 220    // One low half
 221  1 try {
 222  1 new Comment("test: \uDF80 ");
 223  0 fail("Should raise an IllegalCharacterDataException");
 224    }
 225    catch (IllegalCharacterDataException success) {
 226  1 assertEquals("test: \uDF80 ", success.getData());
 227  1 assertNotNull(success.getMessage());
 228    }
 229   
 230    // Low half before high half
 231  1 try {
 232  1 new Comment("test: \uDCF5\uD8F5 ");
 233  0 fail("Should raise an IllegalCharacterDataException");
 234    }
 235    catch (IllegalCharacterDataException success) {
 236  1 assertEquals("test: \uDCF5\uD8F5 ", success.getData());
 237  1 assertNotNull(success.getMessage());
 238    }
 239   
 240   
 241    }
 242   
 243   
 244  1 public void testLeafNode() {
 245   
 246  1 Comment c1 = new Comment("data");
 247  1 assertEquals(0, c1.getChildCount());
 248  1 try {
 249  1 c1.getChild(0);
 250  0 fail("Didn't throw IndexOutofBoundsException");
 251    }
 252    catch (IndexOutOfBoundsException success) {
 253  1 assertNotNull(success.getMessage());
 254    }
 255   
 256  1 assertNull(c1.getParent());
 257   
 258  1 Element element = new Element("test");
 259  1 element.appendChild(c1);
 260  1 assertEquals(element, c1.getParent());
 261  1 assertEquals(element.getChild(0), c1);
 262   
 263  1 element.removeChild(c1);
 264  1 assertTrue(element.getChildCount() == 0);
 265   
 266    }
 267   
 268   
 269  1 public void testGetDocument() {
 270   
 271  1 Comment c1 = new Comment("data");
 272  1 assertNull(c1.getDocument());
 273  1 Element root = new Element("root");
 274  1 root.appendChild(c1);
 275  1 assertNull(c1.getDocument());
 276  1 Document doc = new Document(root);
 277  1 assertEquals(doc, c1.getDocument());
 278   
 279    }
 280   
 281   
 282  1 public void testAllowReservedCharactersInData() {
 283  1 Comment comment = new Comment("<test>&amp;&greater;");
 284  1 String xml = comment.toXML();
 285  1 assertEquals("<!--<test>&amp;&greater;-->", xml);
 286    }
 287   
 288   
 289  1 public void testForbidIllegalCharactersInComments() {
 290   
 291  1 try {
 292  1 new Comment(" \u0001 ");
 293  0 fail("Allowed C0 control in comment");
 294    }
 295    catch (IllegalCharacterDataException success) {
 296  1 assertEquals(" \u0001 ", success.getData());
 297  1 assertNotNull(success.getMessage());
 298    }
 299   
 300    }
 301   
 302   
 303  1 public void testForbidUnmatchedSurrogatesInComments() {
 304   
 305  1 try {
 306  1 new Comment(" \uD800 ");
 307  0 fail("Allowed unmatched high surrogate in comment");
 308    }
 309    catch (IllegalCharacterDataException success) {
 310  1 assertEquals(" \uD800 ", success.getData());
 311  1 assertNotNull(success.getMessage());
 312    }
 313   
 314    }
 315   
 316   
 317  1 public void testCommentDataCanStartWithAHyphen() {
 318  1 Comment c = new Comment("- - ");
 319  1 assertEquals("- - ", c.getValue());
 320    }
 321   
 322    }