1 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 40 public class CommentTest extends XOMTestCase { 41 42 43 public CommentTest(String name) { 44 super(name); 45 } 46 47 48 public void testConstructor() { 49 Comment c1 = new Comment("test"); 50 assertEquals("test", c1.getValue()); 51 Comment c2 = new Comment(""); 52 assertEquals("", c2.getValue()); 53 } 54 55 56 public void testCopyConstructor() { 57 Comment c1 = new Comment("test"); 58 Comment c2 = new Comment(c1); 59 assertEquals("test", c2.getValue()); 60 assertEquals(c1.getValue(), c2.getValue()); 61 assertTrue(c1 != c2); 62 } 63 64 65 public void testToString() { 66 67 Comment c1 = new Comment("content"); 68 assertEquals("[nu.xom.Comment: content]", c1.toString()); 69 70 c1.setValue("012345678901234567890123456789012345678901234567890123456789"); 71 assertEquals( 72 "[nu.xom.Comment: 01234567890123456789012345678901234...]", 73 c1.toString() 74 ); 75 76 } 77 78 79 public void testToStringWithLineFeed() { 80 81 Comment c = new Comment("content\ncontent"); 82 assertEquals("[nu.xom.Comment: content\\ncontent]", c.toString()); 83 84 } 85 86 87 public void testToStringWithLotsOfData() { 88 89 Comment c 90 = new Comment("content content 012345678901234567890123456789012345678901234567890123456789"); 91 String s = c.toString(); 92 assertTrue(s.length() < 72); 93 94 } 95 96 97 public void testToXML() { 98 99 Comment c1 = new Comment("content"); 100 assertEquals("<!--content-->", c1.toXML()); 101 102 c1.setValue(" 012345678901234567890123456789012345678901234567890123456789 "); 103 assertEquals( 104 "<!-- 012345678901234567890123456789012345678901234567890123456789 -->", 105 c1.toXML() 106 ); 107 108 } 109 110 111 public void testCarriageReturnInCommentData() { 115 try { 116 new Comment("data\rdata"); 117 fail("Allowed carriage return in comment"); 118 } 119 catch (IllegalDataException success) { 120 assertEquals("data\rdata", success.getData()); 121 assertNotNull(success.getMessage()); 122 } 123 } 124 125 126 public void testSetter() { 127 128 Comment c1 = new Comment("test"); 129 c1.setValue("legal"); 130 assertEquals("legal", c1.getValue()); 131 132 try { 133 c1.setValue("test -- test"); 134 fail("Should raise an IllegalDataException"); 135 } 136 catch (IllegalDataException success) { 137 assertEquals("test -- test", success.getData()); 138 assertNotNull(success.getMessage()); 139 } 140 141 try { 142 c1.setValue("test-"); 143 fail("Should raise an IllegalDataException"); 144 } 145 catch (IllegalDataException success) { 146 assertEquals("test-", success.getData()); 147 assertNotNull(success.getMessage()); 148 } 149 150 c1.setValue(null); 151 assertEquals("", c1.getValue()); 152 153 } 154 155 156 public void testEquals() { 157 158 Comment c1 = new Comment("test"); 159 Comment c2 = new Comment("test"); 160 Comment c3 = new Comment("skjlchsakdjh"); 161 162 assertEquals(c1, c1); 163 assertEquals(c1.hashCode(), c1.hashCode()); 164 assertTrue(!c1.equals(c2)); 165 assertTrue(!c1.equals(c3)); 166 167 } 168 169 170 public void testCopy() { 171 172 Comment c1 = new Comment("test"); 173 Comment c2 = (Comment) c1.copy(); 174 175 assertEquals(c1.getValue(), c2.getValue()); 176 assertTrue(!c1.equals(c2)); 177 assertNull(c2.getParent()); 178 179 } 180 181 182 public void testSurrogates() { 185 186 String goodString = "test: \uD8F5\uDF80 "; 187 Comment c = new Comment(goodString); 188 assertEquals(goodString, c.getValue()); 189 190 try { 192 new Comment("test: \uD8F5\uDBF0 "); 193 fail("Should raise an IllegalCharacterDataException"); 194 } 195 catch (IllegalCharacterDataException success) { 196 assertEquals("test: \uD8F5\uDBF0 ", success.getData()); 197 assertNotNull(success.getMessage()); 198 } 199 200 try { 202 new Comment("test: \uD8F5\uD8F5 "); 203 fail("Should raise an IllegalCharacterDataException"); 204 } 205 catch (IllegalCharacterDataException success) { 206 assertEquals("test: \uD8F5\uD8F5 ", success.getData()); 207 assertNotNull(success.getMessage()); 208 } 209 210 try { 212 new Comment("test: \uD8F5 "); 213 fail("Should raise an IllegalCharacterDataException"); 214 } 215 catch (IllegalCharacterDataException success) { 216 assertEquals("test: \uD8F5 ", success.getData()); 217 assertNotNull(success.getMessage()); 218 } 219 220 try { 222 new Comment("test: \uDF80 "); 223 fail("Should raise an IllegalCharacterDataException"); 224 } 225 catch (IllegalCharacterDataException success) { 226 assertEquals("test: \uDF80 ", success.getData()); 227 assertNotNull(success.getMessage()); 228 } 229 230 try { 232 new Comment("test: \uDCF5\uD8F5 "); 233 fail("Should raise an IllegalCharacterDataException"); 234 } 235 catch (IllegalCharacterDataException success) { 236 assertEquals("test: \uDCF5\uD8F5 ", success.getData()); 237 assertNotNull(success.getMessage()); 238 } 239 240 241 } 242 243 244 public void testLeafNode() { 245 246 Comment c1 = new Comment("data"); 247 assertEquals(0, c1.getChildCount()); 248 try { 249 c1.getChild(0); 250 fail("Didn't throw IndexOutofBoundsException"); 251 } 252 catch (IndexOutOfBoundsException success) { 253 assertNotNull(success.getMessage()); 254 } 255 256 assertNull(c1.getParent()); 257 258 Element element = new Element("test"); 259 element.appendChild(c1); 260 assertEquals(element, c1.getParent()); 261 assertEquals(element.getChild(0), c1); 262 263 element.removeChild(c1); 264 assertTrue(element.getChildCount() == 0); 265 266 } 267 268 269 public void testGetDocument() { 270 271 Comment c1 = new Comment("data"); 272 assertNull(c1.getDocument()); 273 Element root = new Element("root"); 274 root.appendChild(c1); 275 assertNull(c1.getDocument()); 276 Document doc = new Document(root); 277 assertEquals(doc, c1.getDocument()); 278 279 } 280 281 282 public void testAllowReservedCharactersInData() { 283 Comment comment = new Comment("<test>&&greater;"); 284 String xml = comment.toXML(); 285 assertEquals("<!--<test>&&greater;-->", xml); 286 } 287 288 289 public void testForbidIllegalCharactersInComments() { 290 291 try { 292 new Comment(" \u0001 "); 293 fail("Allowed C0 control in comment"); 294 } 295 catch (IllegalCharacterDataException success) { 296 assertEquals(" \u0001 ", success.getData()); 297 assertNotNull(success.getMessage()); 298 } 299 300 } 301 302 303 public void testForbidUnmatchedSurrogatesInComments() { 304 305 try { 306 new Comment(" \uD800 "); 307 fail("Allowed unmatched high surrogate in comment"); 308 } 309 catch (IllegalCharacterDataException success) { 310 assertEquals(" \uD800 ", success.getData()); 311 assertNotNull(success.getMessage()); 312 } 313 314 } 315 316 317 public void testCommentDataCanStartWithAHyphen() { 318 Comment c = new Comment("- - "); 319 assertEquals("- - ", c.getValue()); 320 } 321 322 } 323 | Popular Tags |