001: /* Copyright 2002-2004 Elliotte Rusty Harold
002:
003: This library is free software; you can redistribute it and/or modify
004: it under the terms of version 2.1 of the GNU Lesser General Public
005: License as published by the Free Software Foundation.
006:
007: This library is distributed in the hope that it will be useful,
008: but WITHOUT ANY WARRANTY; without even the implied warranty of
009: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: GNU Lesser General Public License for more details.
011:
012: You should have received a copy of the GNU Lesser General Public
013: License along with this library; if not, write to the
014: Free Software Foundation, Inc., 59 Temple Place, Suite 330,
015: Boston, MA 02111-1307 USA
016:
017: You can contact Elliotte Rusty Harold by sending e-mail to
018: elharo@metalab.unc.edu. Please include the word "XOM" in the
019: subject line. The XOM home page is located at http://www.xom.nu/
020: */
021:
022: package nu.xom.tests;
023:
024: import nu.xom.Comment;
025: import nu.xom.Document;
026: import nu.xom.Element;
027: import nu.xom.IllegalDataException;
028: import nu.xom.IllegalCharacterDataException;
029:
030: /**
031: * <p>
032: * Various basic unit tests for the <code>Comment</code> class.
033: * </p>
034: *
035: * @author Elliotte Rusty Harold
036: * @version 1.0
037: *
038: */
039: public class CommentTest extends XOMTestCase {
040:
041: public CommentTest(String name) {
042: super (name);
043: }
044:
045: public void testConstructor() {
046: Comment c1 = new Comment("test");
047: assertEquals("test", c1.getValue());
048: Comment c2 = new Comment("");
049: assertEquals("", c2.getValue());
050: }
051:
052: public void testCopyConstructor() {
053: Comment c1 = new Comment("test");
054: Comment c2 = new Comment(c1);
055: assertEquals("test", c2.getValue());
056: assertEquals(c1.getValue(), c2.getValue());
057: assertTrue(c1 != c2);
058: }
059:
060: public void testToString() {
061:
062: Comment c1 = new Comment("content");
063: assertEquals("[nu.xom.Comment: content]", c1.toString());
064:
065: c1
066: .setValue("012345678901234567890123456789012345678901234567890123456789");
067: assertEquals(
068: "[nu.xom.Comment: 01234567890123456789012345678901234...]",
069: c1.toString());
070:
071: }
072:
073: public void testToStringWithLineFeed() {
074:
075: Comment c = new Comment("content\ncontent");
076: assertEquals("[nu.xom.Comment: content\\ncontent]", c
077: .toString());
078:
079: }
080:
081: public void testToStringWithLotsOfData() {
082:
083: Comment c = new Comment(
084: "content content 012345678901234567890123456789012345678901234567890123456789");
085: String s = c.toString();
086: assertTrue(s.length() < 72);
087:
088: }
089:
090: public void testToXML() {
091:
092: Comment c1 = new Comment("content");
093: assertEquals("<!--content-->", c1.toXML());
094:
095: c1
096: .setValue(" 012345678901234567890123456789012345678901234567890123456789 ");
097: assertEquals(
098: "<!-- 012345678901234567890123456789012345678901234567890123456789 -->",
099: c1.toXML());
100:
101: }
102:
103: // This is a problem because it cannot be serialized
104: // since character and entity references aren't
105: // recognized in comment data
106: public void testCarriageReturnInCommentData() {
107: try {
108: new Comment("data\rdata");
109: fail("Allowed carriage return in comment");
110: } catch (IllegalDataException success) {
111: assertEquals("data\rdata", success.getData());
112: assertNotNull(success.getMessage());
113: }
114: }
115:
116: public void testSetter() {
117:
118: Comment c1 = new Comment("test");
119: c1.setValue("legal");
120: assertEquals("legal", c1.getValue());
121:
122: try {
123: c1.setValue("test -- test");
124: fail("Should raise an IllegalDataException");
125: } catch (IllegalDataException success) {
126: assertEquals("test -- test", success.getData());
127: assertNotNull(success.getMessage());
128: }
129:
130: try {
131: c1.setValue("test-");
132: fail("Should raise an IllegalDataException");
133: } catch (IllegalDataException success) {
134: assertEquals("test-", success.getData());
135: assertNotNull(success.getMessage());
136: }
137:
138: c1.setValue(null);
139: assertEquals("", c1.getValue());
140:
141: }
142:
143: public void testEquals() {
144:
145: Comment c1 = new Comment("test");
146: Comment c2 = new Comment("test");
147: Comment c3 = new Comment("skjlchsakdjh");
148:
149: assertEquals(c1, c1);
150: assertEquals(c1.hashCode(), c1.hashCode());
151: assertTrue(!c1.equals(c2));
152: assertTrue(!c1.equals(c3));
153:
154: }
155:
156: public void testCopy() {
157:
158: Comment c1 = new Comment("test");
159: Comment c2 = (Comment) c1.copy();
160:
161: assertEquals(c1.getValue(), c2.getValue());
162: assertTrue(!c1.equals(c2));
163: assertNull(c2.getParent());
164:
165: }
166:
167: // Check passing in a string with broken surrogate pairs
168: // and with correct surrogate pairs
169: public void testSurrogates() {
170:
171: String goodString = "test: \uD8F5\uDF80 ";
172: Comment c = new Comment(goodString);
173: assertEquals(goodString, c.getValue());
174:
175: // Two high-halves
176: try {
177: new Comment("test: \uD8F5\uDBF0 ");
178: fail("Should raise an IllegalCharacterDataException");
179: } catch (IllegalCharacterDataException success) {
180: assertEquals("test: \uD8F5\uDBF0 ", success.getData());
181: assertNotNull(success.getMessage());
182: }
183:
184: // Two high-halves
185: try {
186: new Comment("test: \uD8F5\uD8F5 ");
187: fail("Should raise an IllegalCharacterDataException");
188: } catch (IllegalCharacterDataException success) {
189: assertEquals("test: \uD8F5\uD8F5 ", success.getData());
190: assertNotNull(success.getMessage());
191: }
192:
193: // One high-half
194: try {
195: new Comment("test: \uD8F5 ");
196: fail("Should raise an IllegalCharacterDataException");
197: } catch (IllegalCharacterDataException success) {
198: assertEquals("test: \uD8F5 ", success.getData());
199: assertNotNull(success.getMessage());
200: }
201:
202: // One low half
203: try {
204: new Comment("test: \uDF80 ");
205: fail("Should raise an IllegalCharacterDataException");
206: } catch (IllegalCharacterDataException success) {
207: assertEquals("test: \uDF80 ", success.getData());
208: assertNotNull(success.getMessage());
209: }
210:
211: // Low half before high half
212: try {
213: new Comment("test: \uDCF5\uD8F5 ");
214: fail("Should raise an IllegalCharacterDataException");
215: } catch (IllegalCharacterDataException success) {
216: assertEquals("test: \uDCF5\uD8F5 ", success.getData());
217: assertNotNull(success.getMessage());
218: }
219:
220: }
221:
222: public void testLeafNode() {
223:
224: Comment c1 = new Comment("data");
225: assertEquals(0, c1.getChildCount());
226: try {
227: c1.getChild(0);
228: fail("Didn't throw IndexOutofBoundsException");
229: } catch (IndexOutOfBoundsException success) {
230: assertNotNull(success.getMessage());
231: }
232:
233: assertNull(c1.getParent());
234:
235: Element element = new Element("test");
236: element.appendChild(c1);
237: assertEquals(element, c1.getParent());
238: assertEquals(element.getChild(0), c1);
239:
240: element.removeChild(c1);
241: assertTrue(element.getChildCount() == 0);
242:
243: }
244:
245: public void testGetDocument() {
246:
247: Comment c1 = new Comment("data");
248: assertNull(c1.getDocument());
249: Element root = new Element("root");
250: root.appendChild(c1);
251: assertNull(c1.getDocument());
252: Document doc = new Document(root);
253: assertEquals(doc, c1.getDocument());
254:
255: }
256:
257: public void testAllowReservedCharactersInData() {
258: Comment comment = new Comment("<test>&&greater;");
259: String xml = comment.toXML();
260: assertEquals("<!--<test>&&greater;-->", xml);
261: }
262:
263: public void testForbidIllegalCharactersInComments() {
264:
265: try {
266: new Comment(" \u0001 ");
267: fail("Allowed C0 control in comment");
268: } catch (IllegalCharacterDataException success) {
269: assertEquals(" \u0001 ", success.getData());
270: assertNotNull(success.getMessage());
271: }
272:
273: }
274:
275: public void testForbidUnmatchedSurrogatesInComments() {
276:
277: try {
278: new Comment(" \uD800 ");
279: fail("Allowed unmatched high surrogate in comment");
280: } catch (IllegalCharacterDataException success) {
281: assertEquals(" \uD800 ", success.getData());
282: assertNotNull(success.getMessage());
283: }
284:
285: }
286:
287: public void testCommentDataCanStartWithAHyphen() {
288: Comment c = new Comment("- - ");
289: assertEquals("- - ", c.getValue());
290: }
291:
292: }
|