KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.thoughtworks.xstream.io.xml;
2
3 import com.megginson.sax.DataWriter;
4 import com.thoughtworks.acceptance.someobjects.X;
5 import com.thoughtworks.acceptance.someobjects.Y;
6 import com.thoughtworks.xstream.XStream;
7 import junit.framework.TestCase;
8
9 import javax.xml.transform.ErrorListener JavaDoc;
10 import javax.xml.transform.Templates JavaDoc;
11 import javax.xml.transform.Transformer JavaDoc;
12 import javax.xml.transform.TransformerException JavaDoc;
13 import javax.xml.transform.TransformerFactory JavaDoc;
14 import javax.xml.transform.stream.StreamResult JavaDoc;
15 import javax.xml.transform.stream.StreamSource JavaDoc;
16 import java.io.StringReader JavaDoc;
17 import java.io.StringWriter JavaDoc;
18 import java.io.Writer JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Arrays JavaDoc;
21 import java.util.Collections JavaDoc;
22 import java.util.List JavaDoc;
23
24 /*
25  * @author Laurent Bihanic
26  */

27 public class SaxWriterTest extends TestCase {
28
29     private final static String JavaDoc IDENTITY_STYLESHEET =
30             "<xsl:stylesheet version=\"1.0\"" +
31             " xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">\n" +
32             "\n" +
33             " <xsl:output method=\"xml\"" +
34             " omit-xml-declaration=\"yes\" indent=\"no\"/>\n" +
35             "\n" +
36             " <xsl:template" +
37             " match=\"*|@*|comment()|processing-instruction()|text()\">\n" +
38             " <xsl:copy>\n" +
39             " <xsl:apply-templates" +
40             " select=\"*|@*|comment()|processing-instruction()|text()\"/>\n" +
41             " </xsl:copy>\n" +
42             " </xsl:template>\n" +
43             "</xsl:stylesheet>";
44
45     private XStream xstream;
46     private X testInput;
47     private Templates JavaDoc identityStylesheet;
48
49     protected void setUp() throws Exception JavaDoc {
50         super.setUp();
51         xstream = new XStream();
52         xstream.alias("x", X.class);
53         xstream.alias("y", Y.class);
54
55         testInput = new X();
56         testInput.anInt = 9;
57         testInput.aStr = "zzz";
58         testInput.innerObj = new Y();
59         testInput.innerObj.yField = "ooo";
60
61         identityStylesheet = TransformerFactory.newInstance().newTemplates(new StreamSource JavaDoc(new StringReader JavaDoc(IDENTITY_STYLESHEET)));
62     }
63
64     public void testMarshalsObjectToSAX() {
65         String JavaDoc expected =
66                 "<?xml version=\"1.0\" standalone=\"yes\"?>\n\n" +
67                 "<x>\n" +
68                 " <aStr>zzz</aStr>\n" +
69                 " <anInt>9</anInt>\n" +
70                 " <innerObj>\n" +
71                 " <yField>ooo</yField>\n" +
72                 " </innerObj>\n" +
73                 "</x>\n\n";
74
75         Writer buffer = new StringWriter JavaDoc();
76         SaxWriter writer = new SaxWriter();
77         DataWriter outputter = new DataWriter(writer, buffer);
78         outputter.setIndentStep(2);
79
80         writer.setContentHandler(outputter);
81
82         xstream.marshal(testInput, writer);
83
84         assertEquals(expected, buffer.toString());
85     }
86
87     public void testAllowsStartAndEndDocCallbacksToBeSkipped() {
88         String JavaDoc expected =
89                 "<int>1</int>\n" +
90                 "<int>2</int>\n" +
91                 "<int>3</int>\n";
92
93         Writer buffer = new StringWriter JavaDoc();
94         SaxWriter writer = new SaxWriter(false);
95         DataWriter outputter = new DataWriter(writer, buffer);
96         outputter.setIndentStep(2);
97
98         writer.setContentHandler(outputter);
99
100         xstream.marshal(new Integer JavaDoc(1), writer);
101         xstream.marshal(new Integer JavaDoc(2), writer);
102         xstream.marshal(new Integer JavaDoc(3), writer);
103
104         assertEquals(expected, buffer.toString());
105     }
106
107     public void testMarshalsObjectToTrAX() throws Exception JavaDoc {
108         String JavaDoc expected =
109                 "<x><aStr>zzz</aStr><anInt>9</anInt>" +
110                 "<innerObj><yField>ooo</yField></innerObj>" +
111                 "</x>" +
112                 "<y><yField>ooo</yField></y>";
113
114         TraxSource traxSource = new TraxSource();
115         traxSource.setXStream(xstream);
116         traxSource.setSourceAsList(Arrays.asList(new Object JavaDoc[]{testInput, testInput.innerObj}));
117
118         Writer buffer = new StringWriter JavaDoc();
119         Transformer JavaDoc transformer = identityStylesheet.newTransformer();
120
121         transformer.transform(traxSource, new StreamResult JavaDoc(buffer));
122
123         assertEquals(expected, buffer.toString());
124     }
125
126     public void testNullSourceObject() {
127         TraxSource traxSource = new TraxSource();
128
129         try {
130             traxSource.setSource(null);
131             fail("Null source object not rejected");
132         } catch (IllegalArgumentException JavaDoc e) { /* good! */
133         }
134     }
135
136     public void testNullSourceList() {
137         TraxSource traxSource = new TraxSource();
138
139         try {
140             traxSource.setSourceAsList(null);
141             fail("Null source list not rejected");
142         } catch (IllegalArgumentException JavaDoc e) { /* good! */
143         }
144     }
145
146     public void testEmptySourceList() {
147         TraxSource traxSource = new TraxSource();
148
149         try {
150             traxSource.setSourceAsList(Collections.EMPTY_LIST);
151             fail("Empty source list not rejected");
152         } catch (IllegalArgumentException JavaDoc e) { /* good! */
153         }
154     }
155
156     /**
157      * This method tests a quite insidious side-effect of
158      * XStreamSource delaying the allocation and configuration of
159      * the SAXWriter until the XSLT processor requests it.
160      * <p/>
161      * SAXWriter performs a copy of the source list contents upon
162      * property setting to avoid objects being added or removed from
163      * the list during the parse.</p>
164      * <p/>
165      * To avoid just another list copy, XStreamSource does not
166      * protect itself against list changes. Hence, it is possible
167      * for an application to configure the XStreamSource and then
168      * empty the list prior triggering the XSL transformation.</p>.
169      * <p/>
170      * This method ensures SAXWriter indeed checks the list content
171      * prior starting the parse.</p>
172      */

173     public void testEmptySourceListAtParse() throws Exception JavaDoc {
174         TraxSource traxSource = new TraxSource();
175         Writer buffer = new StringWriter JavaDoc();
176
177         List JavaDoc list = new ArrayList JavaDoc();
178         list.add(testInput);
179
180         traxSource.setSourceAsList(list);
181         list.clear();
182
183         Transformer JavaDoc transformer = identityStylesheet.newTransformer();
184         transformer.setErrorListener(new TrAXErrorListener());
185
186         try {
187             transformer.transform(traxSource, new StreamResult JavaDoc(buffer));
188
189             fail("Empty source list not rejected");
190         } catch (Exception JavaDoc expectedException) {
191             if (expectedException.getMessage().endsWith("shall not be an empty list")) {
192                 // Good!
193
} else {
194                 throw expectedException;
195             }
196         }
197     }
198
199     private static class TrAXErrorListener implements ErrorListener JavaDoc {
200         public TrAXErrorListener() {
201             super();
202         }
203
204         public void warning(TransformerException JavaDoc e)
205                 throws TransformerException JavaDoc {
206             /* Ignore... */
207         }
208
209         public void error(TransformerException JavaDoc e)
210                 throws TransformerException JavaDoc {
211             /* Ignore... */
212         }
213
214         public void fatalError(TransformerException JavaDoc e)
215                 throws TransformerException JavaDoc {
216             throw e;
217         }
218     }
219 }
220
221
Popular Tags