01: /*
02: * Copyright (C) 2004, 2005 Joe Walnes.
03: * Copyright (C) 2006, 2007 XStream Committers.
04: * All rights reserved.
05: *
06: * The software in this package is published under the terms of the BSD
07: * style license a copy of which has been included with this distribution in
08: * the LICENSE.txt file.
09: *
10: * Created on 07. March 2004 by Joe Walnes
11: */
12: package com.thoughtworks.xstream.io.xml;
13:
14: import com.thoughtworks.xstream.core.util.QuickWriter;
15:
16: import java.io.StringWriter;
17: import java.io.Writer;
18:
19: public class CompactWriterTest extends AbstractXMLWriterTest {
20: private Writer buffer;
21:
22: protected void setUp() throws Exception {
23: super .setUp();
24: buffer = new StringWriter();
25: writer = new CompactWriter(buffer);
26: }
27:
28: protected void assertXmlProducedIs(String expected) {
29: assertEquals(expected, buffer.toString());
30: }
31:
32: public void testXmlIsIndented() {
33: writer.startNode("hello");
34: writer.startNode("world");
35:
36: writer.startNode("one");
37: writer.setValue("potato");
38: writer.endNode();
39:
40: writer.startNode("two");
41: writer.setValue("potatae");
42: writer.endNode();
43:
44: writer.endNode();
45: writer.endNode();
46:
47: String expected = "<hello><world><one>potato</one><two>potatae</two></world></hello>";
48: assertXmlProducedIs(expected);
49: }
50:
51: public void testEncodesFunnyXmlChars() {
52: writer.startNode("tag");
53: writer.setValue("hello & this isn't \"really\" <good>");
54: writer.endNode();
55:
56: String expected = "<tag>hello & this isn't "really" <good></tag>";
57:
58: assertXmlProducedIs(expected);
59: }
60:
61: public void testWriteTextAsCDATA() {
62: writer = new CompactWriter(buffer) {
63: protected void writeText(QuickWriter writer, String text) {
64: writer.write("<[CDATA[");
65: writer.write(text);
66: writer.write("]]>");
67: }
68: };
69:
70: writer.startNode("tag");
71: writer.setValue("hello & this isn't \"really\" <good>");
72: writer.endNode();
73:
74: String expected = "<tag><[CDATA[hello & this isn't \"really\" <good>]]></tag>";
75:
76: assertXmlProducedIs(expected);
77: }
78:
79: public void testAttributesCanBeWritten() {
80: writer.startNode("tag");
81: writer.addAttribute("hello", "world");
82: writer.startNode("inner");
83: writer.addAttribute("foo", "bar");
84: writer.addAttribute("poo", "par");
85: writer.setValue("hi");
86: writer.endNode();
87: writer.endNode();
88:
89: String expected = "" + "<tag hello=\"world\">"
90: + "<inner foo=\"bar\" poo=\"par\">hi</inner>"
91: + "</tag>";
92:
93: assertXmlProducedIs(expected);
94: }
95: }
|