KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xmlrpc > XmlWriterTest


1 /*
2  * Copyright 1999,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package org.apache.xmlrpc;
19
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.util.Hashtable JavaDoc;
23
24 import junit.framework.Test;
25 import junit.framework.TestCase;
26 import junit.framework.TestSuite;
27
28 /**
29  * Tests XmlWriter.
30  *
31  * @author Daniel L. Rall
32  * @version $Id: XmlWriterTest.java,v 1.12 2005/05/16 22:39:27 dlr Exp $
33  */

34 public class XmlWriterTest
35     extends TestCase
36 {
37     private ByteArrayOutputStream JavaDoc buffer;
38     private XmlWriter writer;
39
40     /**
41      * Constructor
42      */

43     public XmlWriterTest(String JavaDoc testName)
44     {
45         super(testName);
46     }
47
48     /**
49      * Return the Test
50      */

51     public static Test suite()
52     {
53         return new TestSuite(XmlWriterTest.class);
54     }
55
56     /**
57      * Setup the test.
58      */

59     public void setUp()
60     {
61         XmlRpc.setDebug(true);
62         buffer = new ByteArrayOutputStream JavaDoc();
63     }
64    
65     /**
66      * Tear down the test.
67      */

68     public void tearDown()
69     {
70         XmlRpc.setDebug(false);
71     }
72
73     public void testForceAlternateEncoding()
74         throws Exception JavaDoc
75     {
76         writer = new XmlWriter(buffer, null);
77         assertEquals("null should be forced to UTF-8",
78                      XmlWriter.UTF8, writer.getEncoding());
79
80         writer = new XmlWriter(buffer, XmlWriter.ISO8859_1);
81         assertEquals(XmlWriter.ISO8859_1 + " should be forced to " +
82                      XmlWriter.UTF8, XmlWriter.UTF8, writer.getEncoding());
83
84         writer = new XmlWriter(buffer, "ISO8859_15");
85         assertEquals("ISO8859_15 should be forced to " + XmlWriter.UTF8,
86                      XmlWriter.UTF8, writer.getEncoding());
87
88         writer = new XmlWriter(buffer, "EUC_JP");
89         assertEquals("EUC_JP should be forced to " + XmlWriter.UTF8,
90                      XmlWriter.UTF8, writer.getEncoding());
91
92         writer = new XmlWriter(buffer, XmlWriter.UTF16);
93         assertEquals(XmlWriter.UTF16 + " should remain " + XmlWriter.UTF16,
94                      XmlWriter.UTF16, writer.getEncoding());
95     }
96
97     public void testProlog()
98         throws IOException JavaDoc
99     {
100         final String JavaDoc EXPECTED_PROLOG =
101             XmlWriter.PROLOG_START + XmlWriter.PROLOG_END;
102
103         writer = new XmlWriter(buffer, XmlWriter.UTF8);
104         writer.write(new char[0], 0, 0);
105         writer.flush();
106         assertEquals("Unexpected or missing XML prolog when writing char[]",
107                      EXPECTED_PROLOG, buffer.toString());
108         // Append a space using an overload, and assure non-duplication.
109
writer.write(' ');
110         writer.flush();
111         assertEquals("Unexpected or missing XML prolog when writing char",
112                      EXPECTED_PROLOG + ' ', buffer.toString());
113
114         buffer = new ByteArrayOutputStream JavaDoc();
115         writer = new XmlWriter(buffer, XmlWriter.UTF8);
116         writer.write("");
117         writer.flush();
118         assertEquals("Unexpected or missing XML prolog when writing String",
119                      EXPECTED_PROLOG, buffer.toString());
120         // Try again to assure it's not duplicated in the output.
121
writer.write("");
122         writer.flush();
123         assertEquals("Unexpected or missing XML prolog when writing String",
124                      EXPECTED_PROLOG, buffer.toString());
125         
126     }
127
128     public void testBasicResults()
129         throws Exception JavaDoc
130     {
131         try
132         {
133             writer = new XmlWriter(buffer, XmlWriter.UTF8);
134
135             String JavaDoc foobar = "foobar";
136             writer.writeObject(foobar);
137             writer.flush();
138             String JavaDoc postProlog = "<value>" + foobar + "</value>";
139             assertTrue("Unexpected results from writing of String",
140                        buffer.toString().endsWith(postProlog));
141
142             Integer JavaDoc thirtySeven = new Integer JavaDoc(37);
143             writer.writeObject(thirtySeven);
144             writer.flush();
145             postProlog += "<value><int>" + thirtySeven + "</int></value>";
146             assertTrue("Unexpected results from writing of Integer",
147                        buffer.toString().endsWith(postProlog));
148
149             Boolean JavaDoc flag = Boolean.TRUE;
150             writer.writeObject(flag);
151             writer.flush();
152             postProlog += "<value><boolean>1</boolean></value>";
153             assertTrue("Unexpected results from writing of Boolean",
154                        buffer.toString().endsWith(postProlog));
155
156             Object JavaDoc[] array = { foobar, thirtySeven };
157             writer.writeObject(array);
158             writer.flush();
159             postProlog += "<value><array><data>";
160             postProlog += "<value>" + foobar + "</value>";
161             postProlog += "<value><int>" + thirtySeven + "</int></value>";
162             postProlog += "</data></array></value>";
163             assertTrue("Unexpected results from writing of Object[]",
164                        buffer.toString().endsWith(postProlog));
165
166             Hashtable JavaDoc map = new Hashtable JavaDoc();
167             map.put(foobar, thirtySeven);
168             writer.writeObject(map);
169             writer.flush();
170             postProlog += "<value><struct><member>";
171             postProlog += "<name>" + foobar + "</name>";
172             postProlog += "<value><int>" + thirtySeven + "</int></value>";
173             postProlog += "</member></struct></value>";
174             assertTrue("Unexpected results from writing of Hashtable",
175                        buffer.toString().endsWith(postProlog));
176         }
177         catch (Exception JavaDoc e)
178         {
179             e.printStackTrace();
180             fail(e.getMessage());
181         }
182     }
183
184     public void testWriteCharacterReference()
185         throws Exception JavaDoc
186     {
187         writer = new XmlWriter(buffer, null);
188         writer.hasWrittenProlog = true;
189         writer.writeObject(String.valueOf((char) 0x80));
190         writer.flush();
191         String JavaDoc postProlog = "<value>&#128;</value>";
192         assertTrue("Character reference not created as expected",
193                    buffer.toString().endsWith(postProlog));
194     }
195 }
196
Popular Tags