1
/* Copyright 2002-2004 Elliotte Rusty Harold
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.
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.
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
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/
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;
33
* Various basic unit tests for the <code>Comment</code> class.
36
* @author Elliotte Rusty Harold
40
public class CommentTest extends XOMTestCase {
43
public CommentTest(String name) {
48
public void testConstructor() {
49
Comment c1 = new Comment("test");
50
assertEquals("test", c1.getValue());
51
Comment c2 = new Comment("");
52
assertEquals("", c2.getValue());
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());
65
public void testToString() {
67
Comment c1 = new Comment("content");
68
assertEquals("[nu.xom.Comment: content]", c1.toString());
70
c1.setValue("012345678901234567890123456789012345678901234567890123456789");
72
"[nu.xom.Comment: 01234567890123456789012345678901234...]",
79
public void testToStringWithLineFeed() {
81
Comment c = new Comment("content\ncontent");
82
assertEquals("[nu.xom.Comment: content\\ncontent]", c.toString());
87
public void testToStringWithLotsOfData() {
90
= new Comment("content content 012345678901234567890123456789012345678901234567890123456789");
91
String s = c.toString();
92
assertTrue(s.length() < 72);
97
public void testToXML() {
99
Comment c1 = new Comment("content");
100
assertEquals("<!--content-->", c1.toXML());
102
c1.setValue(" 012345678901234567890123456789012345678901234567890123456789 ");
104
"<!-- 012345678901234567890123456789012345678901234567890123456789 -->",
111
// This is a problem because it cannot be serialized
112
// since character and entity references aren't
113
// recognized in comment data
114
public void testCarriageReturnInCommentData() {
116
new Comment("data\rdata");
117
fail("Allowed carriage return in comment");
119
catch (IllegalDataException success) {
120
assertEquals("data\rdata", success.getData());
121
assertNotNull(success.getMessage());
126
public void testSetter() {
128
Comment c1 = new Comment("test");
129
c1.setValue("legal");
130
assertEquals("legal", c1.getValue());
133
c1.setValue("test -- test");
134
fail("Should raise an IllegalDataException");
136
catch (IllegalDataException success) {
137
assertEquals("test -- test", success.getData());
138
assertNotNull(success.getMessage());
142
c1.setValue("test-");
143
fail("Should raise an IllegalDataException");
145
catch (IllegalDataException success) {
146
assertEquals("test-", success.getData());
147
assertNotNull(success.getMessage());
151
assertEquals("", c1.getValue());
156
public void testEquals() {
158
Comment c1 = new Comment("test");
159
Comment c2 = new Comment("test");
160
Comment c3 = new Comment("skjlchsakdjh");
162
assertEquals(c1, c1);
163
assertEquals(c1.hashCode(), c1.hashCode());
164
assertTrue(!c1.equals(c2));
165
assertTrue(!c1.equals(c3));
170
public void testCopy() {
172
Comment c1 = new Comment("test");
173
Comment c2 = (Comment) c1.copy();
175
assertEquals(c1.getValue(), c2.getValue());
176
assertTrue(!c1.equals(c2));
177
assertNull(c2.getParent());
182
// Check passing in a string with broken surrogate pairs
183
// and with correct surrogate pairs
184
public void testSurrogates() {
186
String goodString = "test: \uD8F5\uDF80 ";
187
Comment c = new Comment(goodString);
188
assertEquals(goodString, c.getValue());
192
new Comment("test: \uD8F5\uDBF0 ");
193
fail("Should raise an IllegalCharacterDataException");
195
catch (IllegalCharacterDataException success) {
196
assertEquals("test: \uD8F5\uDBF0 ", success.getData());
197
assertNotNull(success.getMessage());
202
new Comment("test: \uD8F5\uD8F5 ");
203
fail("Should raise an IllegalCharacterDataException");
205
catch (IllegalCharacterDataException success) {
206
assertEquals("test: \uD8F5\uD8F5 ", success.getData());
207
assertNotNull(success.getMessage());
212
new Comment("test: \uD8F5 ");
213
fail("Should raise an IllegalCharacterDataException");
215
catch (IllegalCharacterDataException success) {
216
assertEquals("test: \uD8F5 ", success.getData());
217
assertNotNull(success.getMessage());
222
new Comment("test: \uDF80 ");
223
fail("Should raise an IllegalCharacterDataException");
225
catch (IllegalCharacterDataException success) {
226
assertEquals("test: \uDF80 ", success.getData());
227
assertNotNull(success.getMessage());
230
// Low half before high half
232
new Comment("test: \uDCF5\uD8F5 ");
233
fail("Should raise an IllegalCharacterDataException");
235
catch (IllegalCharacterDataException success) {
236
assertEquals("test: \uDCF5\uD8F5 ", success.getData());
237
assertNotNull(success.getMessage());
244
public void testLeafNode() {
246
Comment c1 = new Comment("data");
247
assertEquals(0, c1.getChildCount());
250
fail("Didn't throw IndexOutofBoundsException");
252
catch (IndexOutOfBoundsException success) {
253
assertNotNull(success.getMessage());
256
assertNull(c1.getParent());
258
Element element = new Element("test");
259
element.appendChild(c1);
260
assertEquals(element, c1.getParent());
261
assertEquals(element.getChild(0), c1);
263
element.removeChild(c1);
264
assertTrue(element.getChildCount() == 0);
269
public void testGetDocument() {
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());
282
public void testAllowReservedCharactersInData() {
283
Comment comment = new Comment("<test>&&greater;");
284
String xml = comment.toXML();
285
assertEquals("<!--<test>&&greater;-->", xml);
289
public void testForbidIllegalCharactersInComments() {
292
new Comment(" \u0001 ");
293
fail("Allowed C0 control in comment");
295
catch (IllegalCharacterDataException success) {
296
assertEquals(" \u0001 ", success.getData());
297
assertNotNull(success.getMessage());
303
public void testForbidUnmatchedSurrogatesInComments() {
306
new Comment(" \uD800 ");
307
fail("Allowed unmatched high surrogate in comment");
309
catch (IllegalCharacterDataException success) {
310
assertEquals(" \uD800 ", success.getData());
311
assertNotNull(success.getMessage());
317
public void testCommentDataCanStartWithAHyphen() {
318
Comment c = new Comment("- - ");
319
assertEquals("- - ", c.getValue());