001: /*
002: * Copyright (C) 2004, 2005 Joe Walnes.
003: * Copyright (C) 2006, 2007, 2008 XStream Committers.
004: * All rights reserved.
005: *
006: * The software in this package is published under the terms of the BSD
007: * style license a copy of which has been included with this distribution in
008: * the LICENSE.txt file.
009: *
010: * Created on 05. September 2004 by Joe Walnes
011: */
012: package com.thoughtworks.xstream.io.xml;
013:
014: import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
015:
016: import junit.framework.TestCase;
017:
018: public abstract class AbstractXMLWriterTest extends TestCase {
019:
020: protected HierarchicalStreamWriter writer;
021:
022: protected abstract void assertXmlProducedIs(String expected);
023:
024: // String.replaceAll is JDK 1.4
025: protected String replaceAll(String s, final String occurance,
026: final String replacement) {
027: final int len = occurance.length();
028: final int inc = len - replacement.length();
029: int i = -inc;
030: final StringBuffer buff = new StringBuffer(s);
031: // StringBuffer has no indexOf in JDK 1.3
032: while ((i = buff.toString().indexOf(occurance, i + inc)) >= 0) {
033: buff.replace(i, i + len, replacement);
034: }
035: return buff.toString();
036: }
037:
038: public void testProducesXmlElements() {
039: writer.startNode("hello");
040: writer.setValue("world");
041: writer.endNode();
042:
043: assertXmlProducedIs("<hello>world</hello>");
044: }
045:
046: public void testSupportsNestedElements() {
047:
048: writer.startNode("a");
049:
050: writer.startNode("b");
051: writer.setValue("one");
052: writer.endNode();
053:
054: writer.startNode("b");
055: writer.setValue("two");
056: writer.endNode();
057:
058: writer.startNode("c");
059: writer.startNode("d");
060: writer.setValue("three");
061: writer.endNode();
062: writer.endNode();
063:
064: writer.endNode();
065:
066: assertXmlProducedIs("<a><b>one</b><b>two</b><c><d>three</d></c></a>");
067: }
068:
069: public void testSupportsEmptyTags() {
070: writer.startNode("empty");
071: writer.endNode();
072:
073: assertXmlProducedIs("<empty/>");
074: }
075:
076: public void testSupportsAttributes() {
077: writer.startNode("person");
078: writer.addAttribute("firstname", "Joe");
079: writer.addAttribute("lastname", "Walnes");
080: writer.endNode();
081:
082: assertXmlProducedIs("<person firstname=\"Joe\" lastname=\"Walnes\"/>");
083: }
084:
085: public void testAttributesAreResettedForNewNode() {
086: writer.startNode("work");
087: writer.startNode("person");
088: writer.addAttribute("firstname", "Joe");
089: writer.addAttribute("lastname", "Walnes");
090: writer.endNode();
091: writer.startNode("project");
092: writer.addAttribute("XStream", "Codehaus");
093: writer.endNode();
094: writer.endNode();
095:
096: assertXmlProducedIs("<work><person firstname=\"Joe\" lastname=\"Walnes\"/><project XStream=\"Codehaus\"/></work>");
097: }
098:
099: public void testEscapesXmlUnfriendlyCharacters() {
100: writer.startNode("evil");
101: writer.addAttribute("attr", "w0000 $ <x\"x> &!;");
102: writer.setValue("w0000 $ <xx> &!;");
103: writer.endNode();
104:
105: assertXmlProducedIs("<evil attr=\"w0000 $ <x"x> &!;\">w0000 $ <xx> &!;</evil>");
106: }
107:
108: public void testEscapesWhitespaceCharacters() {
109: writer.startNode("evil");
110: writer.setValue("one\ntwo\rthree\r\nfour\n\rfive\tsix");
111: writer.endNode();
112:
113: assertXmlProducedIs("<evil>one\n" + "two
three
\n"
114: + "four\n" + "
five\tsix</evil>");
115: }
116:
117: public void testSupportsEmptyNestedTags() {
118: writer.startNode("parent");
119: writer.startNode("child");
120: writer.endNode();
121: writer.endNode();
122:
123: assertXmlProducedIs("<parent><child/></parent>");
124: }
125: }
|