001: /*
002: * Copyright (C) 2006, 2007 XStream Committers.
003: * All rights reserved.
004: *
005: * The software in this package is published under the terms of the BSD
006: * style license a copy of which has been included with this distribution in
007: * the LICENSE.txt file.
008: *
009: * Created on 19. October 2006 by Joerg Schaible
010: */
011: package com.thoughtworks.xstream.io.xml;
012:
013: import com.thoughtworks.xstream.io.copy.HierarchicalStreamCopier;
014: import com.thoughtworks.xstream.io.xml.xppdom.Xpp3Dom;
015:
016: import junit.framework.TestCase;
017:
018: import java.util.Arrays;
019: import java.util.HashSet;
020: import java.util.Set;
021:
022: public abstract class AbstractDocumentWriterTest extends TestCase {
023:
024: private final HierarchicalStreamCopier copier = new HierarchicalStreamCopier();
025: protected DocumentWriter writer;
026:
027: protected abstract DocumentReader createDocumentReaderFor(
028: Object node);
029:
030: protected void assertDocumentProducedIs(final Xpp3Dom expected) {
031: assertDocumentProducedIs(new Xpp3Dom[] { expected });
032: }
033:
034: protected boolean equals(final Xpp3Dom node1, final Xpp3Dom node2) {
035: if (node1.getName().equals(node2.getName())) {
036: final String value1 = node1.getValue();
037: final String value2 = node2.getValue();
038: if ((value1 == null && value2 == null)
039: || value1.equals(value2)) {
040: final Set set1 = new HashSet(Arrays.asList(node1
041: .getAttributeNames()));
042: final Set set2 = new HashSet(Arrays.asList(node2
043: .getAttributeNames()));
044: if (set1.equals(set2)) {
045: final Xpp3Dom[] children1 = node1.getChildren();
046: final Xpp3Dom[] children2 = node2.getChildren();
047: if (children1.length == children2.length) {
048: for (int i = 0; i < children1.length; i++) {
049: if (!equals(children1[i], children2[i])) {
050: return false;
051: }
052: }
053: return true;
054: }
055: }
056: }
057: }
058: return false;
059: }
060:
061: protected void assertDocumentProducedIs(final Xpp3Dom[] expected) {
062: for (int i = 0; i < expected.length; i++) {
063: copier.copy(new XppDomReader(expected[i]), writer);
064: }
065: final Object[] nodes = writer.getTopLevelNodes().toArray(
066: new Object[0]);
067: assertEquals(expected.length, nodes.length);
068: for (int i = 0; i < nodes.length; i++) {
069: final XppDomWriter xpp3 = new XppDomWriter();
070: copier.copy(createDocumentReaderFor(nodes[i]), xpp3);
071: assertTrue(equals(expected[i], xpp3.getConfiguration()));
072: }
073: }
074:
075: public void testProducesDomElements() {
076: final Xpp3Dom root = new Xpp3Dom("hello");
077: root.setValue("world");
078: assertDocumentProducedIs(root);
079: }
080:
081: public void testSupportsNestedElements() {
082: final Xpp3Dom a = new Xpp3Dom("a");
083:
084: Xpp3Dom b = new Xpp3Dom("b");
085: b.setValue("one");
086: a.addChild(b);
087:
088: b = new Xpp3Dom("b");
089: b.setValue("two");
090: a.addChild(b);
091:
092: final Xpp3Dom c = new Xpp3Dom("c");
093: a.addChild(c);
094: final Xpp3Dom d = new Xpp3Dom("d");
095: d.setValue("three");
096: c.addChild(d);
097:
098: assertDocumentProducedIs(a);
099: }
100:
101: public void testSupportsAttributes() {
102: final Xpp3Dom person = new Xpp3Dom("person");
103: person.setAttribute("firstname", "Joe");
104: person.setAttribute("lastname", "Walnes");
105: assertDocumentProducedIs(person);
106: }
107:
108: public void testAttributesAreResettedForNewNode() {
109: final Xpp3Dom[] roots = new Xpp3Dom[2];
110: final Xpp3Dom person = roots[0] = new Xpp3Dom("person");
111: person.setAttribute("firstname", "Joe");
112: person.setAttribute("lastname", "Walnes");
113: final Xpp3Dom project = roots[1] = new Xpp3Dom("project");
114: project.setAttribute("XStream", "Codehaus");
115:
116: assertDocumentProducedIs(roots);
117: }
118:
119: public void testSupportsEmptyNestedTags() {
120: final Xpp3Dom parent = new Xpp3Dom("parent");
121: parent.addChild(new Xpp3Dom("child"));
122:
123: assertDocumentProducedIs(parent);
124: }
125: }
|