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 java.io.File;
025: import java.io.IOException;
026:
027: import nu.xom.Builder;
028: import nu.xom.Comment;
029: import nu.xom.DocType;
030: import nu.xom.Document;
031: import nu.xom.Element;
032: import nu.xom.IllegalAddException;
033: import nu.xom.MultipleParentException;
034: import nu.xom.NoSuchChildException;
035: import nu.xom.ParsingException;
036: import nu.xom.ProcessingInstruction;
037: import nu.xom.Text;
038: import nu.xom.WellformednessException;
039:
040: /**
041: * <p>
042: * Various basic tests for the <code>Document</code> class.
043: * </p>
044: *
045: * @author Elliotte Rusty Harold
046: * @version 1.0
047: *
048: */
049: public class DocumentTest extends XOMTestCase {
050:
051: public DocumentTest(String name) {
052: super (name);
053: }
054:
055: private Element root;
056: private Document doc;
057:
058: protected void setUp() {
059: root = new Element("root");
060: doc = new Document(root);
061: }
062:
063: public void testToString() {
064: assertEquals("[nu.xom.Document: root]", doc.toString());
065: }
066:
067: public void testDocTypeInsertion() {
068:
069: DocType type1 = new DocType("root");
070:
071: try {
072: doc.insertChild(type1, 1);
073: fail("inserted doctype after root element");
074: } catch (IllegalAddException success) {
075: assertNotNull(success.getMessage());
076: }
077:
078: doc.insertChild(type1, 0);
079: assertEquals(type1, doc.getDocType());
080:
081: DocType type2 = new DocType("test");
082: try {
083: doc.insertChild(type2, 1);
084: fail("Inserted 2nd DocType");
085: } catch (IllegalAddException success) {
086: assertNotNull(success.getMessage());
087: }
088: assertEquals(type1, doc.getDocType());
089: assertNull(type2.getParent());
090: assertEquals(type1, doc.getChild(0));
091:
092: doc.setDocType(type2);
093: assertEquals(doc.getDocType(), type2);
094: assertNull(type1.getParent());
095: assertEquals(type2, doc.getChild(0));
096:
097: }
098:
099: public void testSetDocType() {
100:
101: DocType type1 = new DocType("root");
102: doc.setDocType(type1);
103: assertEquals(type1, doc.getDocType());
104:
105: doc.insertChild(new Comment("test"), 0);
106:
107: DocType type2 = new DocType("root", "http://www.example.com/");
108: doc.setDocType(type2);
109: assertEquals(type2, doc.getDocType());
110: assertEquals(1, doc.indexOf(type2));
111: assertNull(type1.getParent());
112: assertEquals(doc, type2.getParent());
113:
114: // set same doctype
115: doc.setDocType(type2);
116: assertEquals(type2, doc.getDocType());
117: assertEquals(1, doc.indexOf(type2));
118: assertEquals(doc, type2.getParent());
119:
120: try {
121: doc.setDocType(null);
122: fail("Allowed null doctype");
123: } catch (NullPointerException success) {
124: assertNotNull(success.getMessage());
125: assertEquals(type2, doc.getDocType());
126: }
127:
128: try {
129: Document doc2 = new Document(new Element("root"));
130: doc2.setDocType(type2);
131: fail("Allowed multiple parents for doctype");
132: } catch (MultipleParentException success) {
133: assertNotNull(success.getMessage());
134: }
135:
136: }
137:
138: public void testBaseURI() {
139:
140: assertEquals("", doc.getBaseURI());
141: doc.setBaseURI("http://www.example.com/index.xml");
142: assertEquals("http://www.example.com/index.xml", doc
143: .getBaseURI());
144: doc.setBaseURI("file:///home/elharo/XOM/data/test.xml");
145: assertEquals("file:///home/elharo/XOM/data/test.xml", doc
146: .getBaseURI());
147: doc.setBaseURI("file:///home/elharo/XO%4D/data/test.xml");
148: assertEquals("file:///home/elharo/XO%4D/data/test.xml", doc
149: .getBaseURI());
150:
151: }
152:
153: public void testSecondRoot() {
154:
155: try {
156: doc.insertChild(new Element("test"), 0);
157: fail("Added second root element");
158: } catch (IllegalAddException success) {
159: assertNotNull(success.getMessage());
160: }
161:
162: }
163:
164: public void testSetRoot() {
165:
166: Element newRoot = new Element("newroot");
167: doc.setRootElement(newRoot);
168:
169: assertEquals(newRoot, doc.getRootElement());
170: assertNull(root.getParent());
171:
172: try {
173: doc.setRootElement(null);
174: } catch (NullPointerException success) {
175: assertNotNull(success.getMessage());
176: }
177:
178: Element top = new Element("top");
179: Element child = new Element("child");
180: top.appendChild(child);
181: try {
182: doc.setRootElement(child);
183: fail("Allowed element with two parents");
184: } catch (MultipleParentException success) {
185: assertNotNull(success.getMessage());
186: }
187:
188: }
189:
190: public void testReplaceRootWithItself() {
191: Element root = doc.getRootElement();
192: doc.setRootElement(root);
193: assertEquals(root, doc.getRootElement());
194: }
195:
196: public void testReplaceRootElementWithDifferentElementUsingReplaceChild() {
197:
198: Element newRoot = new Element("newRoot");
199: Element oldRoot = doc.getRootElement();
200: doc.replaceChild(oldRoot, newRoot);
201: assertEquals(newRoot, doc.getRootElement());
202: assertNull(oldRoot.getParent());
203: assertEquals(doc, newRoot.getParent());
204:
205: }
206:
207: public void testInsertionAllowed() {
208:
209: Document doc = new Document(new Element("root"));
210: Comment original = new Comment("original");
211: doc.insertChild(original, 0);
212:
213: Element temp = new Element("temp");
214: Comment c2 = new Comment("new comment");
215: temp.appendChild(c2);
216:
217: try {
218: doc.replaceChild(original, c2);
219: fail("Missed multiple parent exception");
220: } catch (MultipleParentException success) {
221: assertEquals(2, doc.getChildCount());
222: }
223:
224: }
225:
226: public void testReplaceDocTypeWithDifferentDocTypeUsingReplaceChild() {
227:
228: DocType newDocType = new DocType("new");
229: DocType oldDocType = new DocType("old");
230: doc.setDocType(oldDocType);
231: doc.replaceChild(oldDocType, newDocType);
232: assertEquals(newDocType, doc.getDocType());
233: assertNull(oldDocType.getParent());
234: assertEquals(doc, newDocType.getParent());
235:
236: }
237:
238: public void testReplaceDocTypeWithParentedDocTypeUsingReplaceChild() {
239:
240: DocType newDocType = new DocType("new");
241: DocType oldDocType = new DocType("old");
242: Document temp = new Document(new Element("root"));
243: temp.setDocType(newDocType);
244:
245: doc.setDocType(oldDocType);
246: try {
247: doc.replaceChild(oldDocType, newDocType);
248: fail("Missed MultipleParentException");
249: } catch (MultipleParentException success) {
250: assertEquals(2, doc.getChildCount());
251: assertEquals(2, temp.getChildCount());
252: }
253:
254: assertEquals(oldDocType, doc.getDocType());
255: assertNotNull(oldDocType.getParent());
256: assertEquals(doc, oldDocType.getParent());
257: assertEquals(newDocType, temp.getDocType());
258: assertNotNull(oldDocType.getParent());
259: assertEquals(temp, newDocType.getParent());
260:
261: }
262:
263: public void testReplaceRootElementWithParentedElementUsingReplaceChild() {
264:
265: Element oldRoot = new Element("oldRoot");
266: Element newRoot = new Element("newRoot");
267: Document doc = new Document(oldRoot);
268: Element temp = new Element("temp");
269: temp.appendChild(newRoot);
270:
271: try {
272: doc.replaceChild(oldRoot, newRoot);
273: fail("Missed MultipleParentException");
274: } catch (MultipleParentException success) {
275: assertEquals(1, doc.getChildCount());
276: assertEquals(1, temp.getChildCount());
277: }
278:
279: assertEquals(oldRoot, doc.getRootElement());
280: assertEquals(newRoot, temp.getChild(0));
281: assertNotNull(oldRoot.getParent());
282: assertEquals(temp, newRoot.getParent());
283:
284: }
285:
286: public void testReplaceRootElementWithComment() {
287:
288: Element oldRoot = new Element("oldRoot");
289: Document doc = new Document(oldRoot);
290:
291: try {
292: doc.replaceChild(oldRoot, new Comment("c"));
293: fail("Replaced root with comment");
294: } catch (WellformednessException success) {
295: assertNotNull(success.getMessage());
296: }
297:
298: assertEquals(oldRoot, doc.getRootElement());
299: assertEquals(doc, oldRoot.getParent());
300:
301: }
302:
303: public void testReplaceNonDocTypeWithDocTypeUsingReplaceChild() {
304:
305: Comment c = new Comment("Not a doctype");
306: DocType newDocType = new DocType("new");
307: doc.insertChild(c, 0);
308: doc.replaceChild(c, newDocType);
309: assertEquals(newDocType, doc.getDocType());
310: assertNull(c.getParent());
311: assertEquals(doc, newDocType.getParent());
312:
313: }
314:
315: public void testDetach() {
316: Comment comment = new Comment(
317: "This will be attached then detached");
318: doc.appendChild(comment);
319: assertEquals(doc, comment.getParent());
320: comment.detach();
321: assertNull(comment.getParent());
322: }
323:
324: public void testGetDocument() {
325: assertEquals(doc, doc.getDocument());
326: }
327:
328: public void testConstructor() {
329:
330: assertEquals(root, doc.getRootElement());
331: assertEquals(1, doc.getChildCount());
332:
333: Element newRoot = new Element("newRoot");
334: doc.setRootElement(newRoot);
335: assertEquals(newRoot, doc.getRootElement());
336: assertEquals(1, doc.getChildCount());
337:
338: doc.appendChild(new Comment("test"));
339: assertEquals(2, doc.getChildCount());
340:
341: doc.insertChild(new Comment("prolog comment"), 0);
342: assertEquals(3, doc.getChildCount());
343: assertTrue(doc.getChild(0) instanceof Comment);
344: assertTrue(doc.getChild(1) instanceof Element);
345: assertTrue(doc.getChild(2) instanceof Comment);
346:
347: doc.insertChild(new ProcessingInstruction("target", "data"), 1);
348: assertTrue(doc.getChild(0) instanceof Comment);
349: assertTrue(doc.getChild(1) instanceof ProcessingInstruction);
350: assertTrue(doc.getChild(2) instanceof Element);
351: assertTrue(doc.getChild(3) instanceof Comment);
352:
353: doc.insertChild(new ProcessingInstruction("epilog", "data"), 3);
354: assertTrue(doc.getChild(0) instanceof Comment);
355: assertTrue(doc.getChild(1) instanceof ProcessingInstruction);
356: assertTrue(doc.getChild(2) instanceof Element);
357: assertTrue(doc.getChild(3) instanceof ProcessingInstruction);
358: assertTrue(doc.getChild(4) instanceof Comment);
359:
360: try {
361: Element nullRoot = null;
362: new Document(nullRoot);
363: fail("allowed null root!");
364: } catch (NullPointerException success) {
365: }
366:
367: try {
368: Document nullDoc = null;
369: new Document(nullDoc);
370: fail("allowed null doc!");
371: } catch (NullPointerException success) {
372: // success
373: }
374:
375: }
376:
377: public void testCopyConstructor() {
378:
379: doc.insertChild(new Comment("text"), 0);
380: doc.insertChild(new ProcessingInstruction("text", "data"), 1);
381: doc.insertChild(new DocType("text"), 2);
382: root.appendChild("some data");
383: doc.appendChild(new Comment("after"));
384: doc.appendChild(new ProcessingInstruction("text", "after"));
385:
386: Document doc2 = new Document(doc);
387: assertEquals(doc, doc2);
388:
389: }
390:
391: public void testCopyConstructorBaseURI() {
392:
393: doc.setBaseURI("http://www.example.com/");
394:
395: Document doc2 = new Document(doc);
396: assertEquals(doc.getBaseURI(), doc2.getBaseURI());
397: assertEquals("http://www.example.com/", doc2.getBaseURI());
398: assertEquals(doc.getRootElement().getBaseURI(), doc2
399: .getRootElement().getBaseURI());
400: assertEquals("http://www.example.com/", doc2.getRootElement()
401: .getBaseURI());
402:
403: }
404:
405: public void testCopy() {
406:
407: doc.insertChild(new Comment("text"), 0);
408: doc.insertChild(new ProcessingInstruction("text", "data"), 1);
409: doc.insertChild(new DocType("text"), 2);
410: root.appendChild("some data");
411: doc.appendChild(new Comment("after"));
412: doc.appendChild(new ProcessingInstruction("text", "after"));
413:
414: Document doc2 = (Document) doc.copy();
415: assertEquals(doc, doc2);
416:
417: }
418:
419: public void testAppend() {
420:
421: Element root = new Element("root");
422: Document doc = new Document(root);
423:
424: try {
425: doc.appendChild(new Text("test"));
426: fail("appended string");
427: } catch (IllegalAddException success) {
428: assertNotNull(success.getMessage());
429: }
430:
431: try {
432: doc.appendChild(new Text(" "));
433: fail("appended white space");
434: } catch (IllegalAddException success) {
435: assertNotNull(success.getMessage());
436: }
437:
438: try {
439: doc.appendChild(new Text("test"));
440: fail("appended Text");
441: } catch (IllegalAddException success) {
442: assertNotNull(success.getMessage());
443: }
444:
445: try {
446: doc.appendChild(new Comment("test"));
447: doc.appendChild(new Element("test"));
448: fail("appended element");
449: } catch (IllegalAddException success) {
450: assertNotNull(success.getMessage());
451: }
452:
453: try {
454: doc.insertChild(new Element("test"), 0);
455: fail("inserted element");
456: } catch (IllegalAddException success) {
457: assertNotNull(success.getMessage());
458: }
459:
460: }
461:
462: public void testRemoval() {
463:
464: Element root = new Element("root");
465: Document doc = new Document(root);
466:
467: try {
468: root.detach();
469: fail("detached root element");
470: } catch (WellformednessException success) {
471: assertNotNull(success.getMessage());
472: }
473:
474: try {
475: doc.removeChild(root);
476: fail("removed root element");
477: } catch (WellformednessException success) {
478: assertNotNull(success.getMessage());
479: }
480:
481: try {
482: doc.removeChild(0);
483: fail("removed root element");
484: } catch (WellformednessException success) {
485: assertNotNull(success.getMessage());
486: }
487:
488: doc.appendChild(new Comment("test"));
489: doc.removeChild(1);
490: assertEquals(1, doc.getChildCount());
491:
492: Comment test = new Comment("test");
493: doc.appendChild(test);
494: doc.removeChild(test);
495: assertEquals(1, doc.getChildCount());
496:
497: try {
498: Comment something = new Comment("sd");
499: doc.removeChild(something);
500: fail("Removed nonchild");
501: } catch (NoSuchChildException success) {
502: assertNotNull(success.getMessage());
503: }
504:
505: try {
506: doc.removeChild(20);
507: fail("removed overly sized element");
508: } catch (IndexOutOfBoundsException success) {
509: assertNotNull(success.getMessage());
510: }
511:
512: }
513:
514: public void testToXML() throws ParsingException, IOException {
515:
516: Builder builder = new Builder();
517: File f = new File("data");
518: f = new File(f, "test.xml");
519: Document input = builder.build(f);
520: String s = input.toXML();
521: Document output = builder.build(s, f.toURL().toExternalForm());
522: assertEquals(input, output);
523:
524: }
525:
526: }
|