KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > io > xml > CompactWriterTest


1 package com.thoughtworks.xstream.io.xml;
2
3 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
4 import junit.framework.TestCase;
5
6 import java.io.StringWriter JavaDoc;
7
8 public class CompactWriterTest extends TestCase {
9     private StringWriter JavaDoc stringWriter;
10     private HierarchicalStreamWriter xmlWriter;
11
12     protected void setUp() throws Exception JavaDoc {
13         super.setUp();
14         stringWriter = new StringWriter JavaDoc();
15         xmlWriter = new CompactWriter(stringWriter);
16     }
17
18     public void testXmlIsIndented() {
19         xmlWriter.startNode("hello");
20         xmlWriter.startNode("world");
21
22         xmlWriter.startNode("one");
23         xmlWriter.setValue("potato");
24         xmlWriter.endNode();
25
26         xmlWriter.startNode("two");
27         xmlWriter.setValue("potatae");
28         xmlWriter.endNode();
29
30         xmlWriter.endNode();
31         xmlWriter.endNode();
32
33         String JavaDoc expected = "<hello><world><one>potato</one><two>potatae</two></world></hello>";
34
35         assertEquals(expected, stringWriter.toString());
36     }
37
38     public void testEncodesFunnyXmlChars() {
39         xmlWriter.startNode("tag");
40         xmlWriter.setValue("hello & this isn't \"really\" <good>");
41         xmlWriter.endNode();
42
43         String JavaDoc expected = "<tag>hello &amp; this isn't \"really\" &lt;good&gt;</tag>";
44
45         assertEquals(expected, stringWriter.toString());
46     }
47
48     public void testAttributesCanBeWritten() {
49         xmlWriter.startNode("tag");
50         xmlWriter.addAttribute("hello", "world");
51         xmlWriter.startNode("inner");
52         xmlWriter.addAttribute("foo", "bar");
53         xmlWriter.addAttribute("poo", "par");
54         xmlWriter.setValue("hi");
55         xmlWriter.endNode();
56         xmlWriter.endNode();
57
58         String JavaDoc expected = "" +
59                 "<tag hello=\"world\">" +
60                 "<inner foo=\"bar\" poo=\"par\">hi</inner>" +
61                 "</tag>";
62
63         assertEquals(expected, stringWriter.toString());
64     }
65
66 }
67
Popular Tags