KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.thoughtworks.xstream.io.xml;
2
3 import junit.framework.TestCase;
4 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
5
6 public abstract class AbstractXMLWriterTest extends TestCase {
7
8     protected HierarchicalStreamWriter writer;
9
10     protected abstract void assertXmlProducedIs(String JavaDoc expected);
11
12     public void testProducesXmlElements() {
13         writer.startNode("hello");
14         writer.setValue("world");
15         writer.endNode();
16
17         assertXmlProducedIs("<hello>world</hello>");
18     }
19
20     public void testSupportsNestedElements() {
21
22         writer.startNode("a");
23
24         writer.startNode("b");
25         writer.setValue("one");
26         writer.endNode();
27
28         writer.startNode("b");
29         writer.setValue("two");
30         writer.endNode();
31
32         writer.startNode("c");
33         writer.startNode("d");
34         writer.setValue("three");
35         writer.endNode();
36         writer.endNode();
37
38         writer.endNode();
39
40         assertXmlProducedIs("<a><b>one</b><b>two</b><c><d>three</d></c></a>");
41     }
42
43     public void testSupportsEmptyTags() {
44         writer.startNode("empty");
45         writer.endNode();
46
47         assertXmlProducedIs("<empty/>");
48     }
49
50     public void testSupportsAttributes() {
51         writer.startNode("person");
52         writer.addAttribute("firstname", "Joe");
53         writer.addAttribute("lastname", "Walnes");
54         writer.endNode();
55
56         assertXmlProducedIs("<person firstname=\"Joe\" lastname=\"Walnes\"/>");
57     }
58
59     public void testEscapesXmlUnfriendlyCharacters() {
60         writer.startNode("evil");
61         writer.addAttribute("attr", "w0000 $ <x\"x> &!;");
62         writer.setValue("w0000 $ <xx> &!;");
63         writer.endNode();
64
65         assertXmlProducedIs("<evil attr=\"w0000 $ &lt;x&quot;x&gt; &amp;!;\">w0000 $ &lt;xx&gt; &amp;!;</evil>");
66     }
67
68     public void testEscapesWhitespaceCharacters() {
69         writer.startNode("evil");
70         writer.setValue("one\ntwo\rthree\r\nfour\n\rfive\tsix");
71         writer.endNode();
72
73         assertXmlProducedIs("<evil>one\n"
74                 + "two&#x0D;three&#x0D;\n"
75                 + "four\n"
76                 + "&#x0D;five\tsix</evil>");
77     }
78
79 }
80
Popular Tags