KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xmlpull > v1 > tests > TestSerialize


1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- //------100-columns-wide------>|*/
2 // for license see accompanying LICENSE_TESTS.txt file (available also at http://www.xmlpull.org)
3

4 package org.xmlpull.v1.tests;
5
6 import junit.framework.TestCase;
7 import junit.framework.TestSuite;
8
9 import java.io.ByteArrayOutputStream JavaDoc;
10 import java.io.StringWriter JavaDoc;
11
12 import java.io.ByteArrayInputStream JavaDoc;
13 import java.io.StringReader JavaDoc;
14
15 import org.xmlpull.v1.XmlPullParser;
16 import org.xmlpull.v1.XmlPullParserFactory;
17 import org.xmlpull.v1.XmlPullParserException;
18 import org.xmlpull.v1.XmlSerializer;
19
20 /**
21  * Simple test to verify serializer (with no namespaces)
22  *
23  * @author <a HREF="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a>
24  */

25 public class TestSerialize extends UtilTestCase {
26     private XmlPullParserFactory factory;
27     private XmlPullParser xpp;
28
29
30     public static void main (String JavaDoc[] args) {
31         junit.textui.TestRunner.run (new TestSuite(TestSerialize.class));
32     }
33
34     public TestSerialize(String JavaDoc name) {
35         super(name);
36     }
37
38     protected void setUp() throws XmlPullParserException {
39         factory = factoryNewInstance();
40         xpp = factory.newPullParser();
41         assertEquals(false, xpp.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES));
42     }
43
44     protected void tearDown() {
45     }
46
47     public void testSimpleWriter() throws Exception JavaDoc {
48         XmlSerializer ser = factory.newSerializer();
49         PackageTests.addNote("* default serializer "+ser.getClass()+"\n");
50
51         //assert there is error if trying to write
52

53         //assert there is error if trying to write
54
try {
55             ser.startTag(null, "foo");
56             fail("exception was expected of serializer if no input was set on parser");
57         } catch(Exception JavaDoc ex) {}
58
59         ser.setOutput(null);
60
61         //assert there is error if trying to write
62
try {
63             ser.startTag(null, "foo");
64             fail("exception was expected of serializer if no input was set on parser");
65         } catch(Exception JavaDoc ex) {}
66
67         StringWriter JavaDoc sw = new StringWriter JavaDoc();
68
69         ser.setOutput(sw);
70
71         try {
72             ser.setOutput(null, null);
73             fail("exception was expected of setOutput() if output stream is null");
74         } catch(IllegalArgumentException JavaDoc ex) {}
75
76         //check get property
77

78         ser.setOutput(sw);
79
80         //assertEquals(null, ser.getOutputEncoding());
81

82         ser.startDocument("ISO-8859-1", Boolean.TRUE);
83         ser.startTag(null, "foo");
84
85         ser.endTag(null, "foo");
86         ser.endDocument();
87
88         // now validate that can be deserialzied
89

90         //xpp.setInput(new StringReader("<foo></foo>"));
91
String JavaDoc serialized = sw.toString();
92         xpp.setInput(new StringReader JavaDoc(serialized));
93
94         assertEquals(null, xpp.getInputEncoding());
95         checkParserState(xpp, 0, xpp.START_DOCUMENT, null, null, false, -1);
96         xpp.next();
97         checkParserState(xpp, 1, xpp.START_TAG, "foo", null, xpp.isEmptyElementTag() /*empty*/, 0);
98         xpp.next();
99         checkParserState(xpp, 1, xpp.END_TAG, "foo", null, false, -1);
100         xpp.next();
101         checkParserState(xpp, 0, xpp.END_DOCUMENT, null, null, false, -1);
102     }
103
104     public void testSimpleStream() throws Exception JavaDoc {
105         XmlSerializer ser = factory.newSerializer();
106
107         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
108         ser.setOutput(baos, "UTF-8");
109         ser.startDocument("UTF-8", null);
110         ser.startTag("", "foo");
111         ser.text("test");
112         ser.endTag("", "foo");
113         ser.endDocument();
114
115         //check taking input form input stream
116
//byte[] binput = "<foo>test</foo>".getBytes("UTF8");
117

118         byte[] binput = baos.toByteArray();
119
120         xpp.setInput(new ByteArrayInputStream JavaDoc( binput ), "UTF-8" );
121         assertEquals("UTF-8", xpp.getInputEncoding());
122
123         //xpp.setInput(new StringReader( "<foo/>" ) );
124
checkParserState(xpp, 0, xpp.START_DOCUMENT, null, null, false, -1);
125         xpp.next();
126         checkParserState(xpp, 1, xpp.START_TAG, "foo", null, false /*empty*/, 0);
127         xpp.next();
128         checkParserState(xpp, 1, xpp.TEXT, null, "test", false, -1);
129         assertEquals(false, xpp.isWhitespace());
130         xpp.next();
131         checkParserState(xpp, 1, xpp.END_TAG, "foo", null, false, -1);
132         xpp.next();
133         checkParserState(xpp, 0, xpp.END_DOCUMENT, null, null, false, -1);
134     }
135
136     public void testSimpleSerWithAttribute() throws Exception JavaDoc {
137         XmlSerializer ser = factory.newSerializer();
138         // one step further - it has an attribute and content ...
139

140         StringWriter JavaDoc sw = new StringWriter JavaDoc();
141
142         assertEquals(0, ser.getDepth());
143         ser.setOutput(sw);
144         assertEquals(0, ser.getDepth());
145
146         ser.startDocument(null, null);
147         assertEquals(0, ser.getDepth());
148         assertEquals(null, ser.getNamespace());
149         assertEquals(null, ser.getName());
150
151         ser.startTag(null, "foo");
152         assertEquals(1, ser.getDepth());
153         assertEquals(null, ser.getNamespace());
154         assertEquals("foo", ser.getName());
155
156         ser.attribute(null, "attrName", "attrVal");
157         assertEquals(1, ser.getDepth());
158         assertEquals(null, ser.getNamespace());
159         assertEquals("foo", ser.getName());
160
161         ser.text("bar");
162         assertEquals(1, ser.getDepth());
163         assertEquals(null, ser.getNamespace());
164         assertEquals("foo", ser.getName());
165
166         ser.startTag("", "p:t");
167         assertEquals(2, ser.getDepth());
168         assertEquals("", ser.getNamespace());
169         assertEquals("p:t", ser.getName());
170
171         ser.text("\n\t ");
172
173         ser.endTag("", "p:t");
174         assertEquals(1, ser.getDepth());
175         assertEquals(null, ser.getNamespace());
176         assertEquals("foo", ser.getName());
177
178         ser.endTag(null, "foo");
179         assertEquals(0, ser.getDepth());
180         assertEquals(null, ser.getNamespace());
181         assertEquals(null, ser.getName());
182
183         ser.endDocument();
184         assertEquals(0, ser.getDepth());
185         assertEquals(null, ser.getNamespace());
186         assertEquals(null, ser.getName());
187
188         //xpp.setInput(new StringReader("<foo attrName='attrVal'>bar<p:t>\r\n\t </p:t></foo>"));
189
String JavaDoc serialized = sw.toString();
190         xpp.setInput(new StringReader JavaDoc(serialized));
191         checkParserState(xpp, 0, xpp.START_DOCUMENT, null, null, false, -1);
192         xpp.next();
193         checkParserState(xpp, 1, xpp.START_TAG, "foo", null, false, 1);
194         checkAttrib(xpp, 0, "attrName", "attrVal");
195         xpp.next();
196         checkParserState(xpp, 1, xpp.TEXT, null, "bar", false, -1);
197         assertEquals(false, xpp.isWhitespace());
198         xpp.next();
199         checkParserState(xpp, 2, xpp.START_TAG, "p:t", null, false, 0);
200         xpp.next();
201         checkParserState(xpp, 2, xpp.TEXT, null, "\n\t ", false, -1);
202         assertTrue(xpp.isWhitespace());
203         xpp.next();
204         checkParserState(xpp, 2, xpp.END_TAG, "p:t", null, false, -1);
205         xpp.next();
206         checkParserState(xpp, 1, xpp.END_TAG, "foo", null, false, -1);
207         xpp.next();
208         checkParserState(xpp, 0, xpp.END_DOCUMENT, null, null, false, -1);
209
210
211     }
212
213 }
214
215
Popular Tags