1   /*
2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons-sandbox//xmlio/src/test/org/apache/commons/xmlio/out/XMLWriterTest.java,v 1.1 2004/10/08 11:56:20 ozeigermann Exp $
3    * $Revision: 155476 $
4    * $Date: 2005-02-26 13:31:24 +0000 (Sat, 26 Feb 2005) $
5    *
6    * ====================================================================
7    *
8    * Copyright 2004 The Apache Software Foundation 
9    *
10   * Licensed under the Apache License, Version 2.0 (the "License");
11   * you may not use this file except in compliance with the License.
12   * You may obtain a copy of the License at
13   *
14   *     http://www.apache.org/licenses/LICENSE-2.0
15   *
16   * Unless required by applicable law or agreed to in writing, software
17   * distributed under the License is distributed on an "AS IS" BASIS,
18   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   * See the License for the specific language governing permissions and
20   * limitations under the License.
21   *
22   */
23  
24  package org.apache.commons.xmlio.out;
25  
26  import java.io.*;
27  
28  import junit.framework.*;
29  
30  import org.apache.commons.xmlio.out.XMLEncode;
31  import org.apache.commons.xmlio.out.XMLOutputStreamWriter;
32  import org.apache.commons.xmlio.out.XMLWriter;
33  import org.xml.sax.helpers.AttributesImpl;
34  
35  /**
36   * Test cases for {@link XMLWriter}, {@link XMLOutputStreamWriter},
37   * and {@link XMLEncode}.
38   *
39   */
40  public class XMLWriterTest extends TestCase {
41  
42      private XMLWriter xmlWriter;
43      private StringWriter stringWriter;
44  
45      public XMLWriterTest(java.lang.String testName) {
46          super(testName);
47      }
48  
49      public static void main(java.lang.String[] args) {
50          junit.textui.TestRunner.run(suite());
51      }
52  
53      public static Test suite() {
54          TestSuite suite = new TestSuite(XMLWriterTest.class);
55  
56          return suite;
57      }
58  
59      /** Test of createEndTag method, of class XMLOutputStreamWriter. */
60      public void testCreateEndTag() {
61          System.out.println("testCreateEndTag");
62  
63          String xml = XMLWriter.createEndTag("root");
64          String expected = "</root>";
65          assertEquals(expected, xml);
66      }
67  
68      /** Test of createStartTag method, of class XMLOutputStreamWriter. */
69      public void testCreateStartTag() {
70          System.out.println("testCreateStartTag");
71  
72          String xml =
73              XMLWriter.createStartTag("root", new String[] { "at1", "at2" }, new String[] { "v1", "v2" }, false);
74          String expected = "<root at1=\"v1\" at2=\"v2\">";
75          assertEquals(expected, xml);
76  
77      }
78  
79      /** Test of createStartTag method, of class XMLOutputStreamWriter. */
80      public void testCreateStartTag2() {
81          System.out.println("testCreateStartTag2");
82  
83          String xml = XMLWriter.createStartTag("root", new String[][] { { "at1", "v1" }, {
84                  "at2", "v2\"'" }, {
85                  "at3", null }
86          }, false, true, '\'');
87          String expected = "<root at1='v1' at2='v2&quot;&apos;'>";
88          assertEquals(expected, xml);
89  
90      }
91  
92      /** Test of createStartTag method, of class XMLOutputStreamWriter. */
93      public void testCreateEmptyStartTag() {
94          System.out.println("testCreateEmptyStartTag");
95  
96          String xml = XMLWriter.createStartTag("root", "at1", "v1\"'<no-tag>", true);
97          String expected = "<root at1=\"v1&quot;&apos;&lt;no-tag&gt;\"/>";
98          assertEquals(expected, xml);
99      }
100 
101     /** Test of writeProlog method, of class XMLOutputStreamWriter. */
102     public void testWriteProlog() {
103         System.out.println("testWriteProlog");
104 
105         try {
106             stringWriter = new StringWriter();
107             xmlWriter = new XMLWriter(stringWriter);
108             xmlWriter.writeXMLDeclaration();
109             String xml = stringWriter.toString();
110             String expected = "<?xml version=\"1.0\"?>\n";
111             assertEquals(expected, xml);
112         } catch (IOException ioe) {
113             System.out.println("EXCEPTION");
114             fail("Exception: " + ioe);
115         }
116     }
117 
118     /** Test of writeComment method, of class XMLOutputStreamWriter. */
119     public void testWriteComment() {
120         System.out.println("testWriteComment");
121 
122         try {
123             stringWriter = new StringWriter();
124             xmlWriter = new XMLWriter(stringWriter);
125             xmlWriter.writeComment("comment");
126             String xml = stringWriter.toString();
127             String expected = "<!-- comment -->";
128             assertEquals(expected, xml);
129         } catch (IOException ioe) {
130             System.out.println("EXCEPTION");
131             fail("Exception: " + ioe);
132         }
133     }
134 
135     /** Test of writePI method, of class XMLOutputStreamWriter. */
136     public void testWritePI() {
137         System.out.println("testWritePI");
138 
139         try {
140             stringWriter = new StringWriter();
141             xmlWriter = new XMLWriter(stringWriter);
142             xmlWriter.writePI("target", "data");
143             String xml = stringWriter.toString();
144             String expected = "<?target data?>";
145             assertEquals(expected, xml);
146         } catch (IOException ioe) {
147             System.out.println("EXCEPTION");
148             fail("Exception: " + ioe);
149         }
150     }
151 
152     // as start/end tag writing can hardly be checked isolated do this here 
153     // generating a whole file
154     public void testMain() {
155         System.out.println("MAIN TEST");
156         try {
157             stringWriter = new StringWriter();
158             xmlWriter = new XMLWriter(stringWriter);
159             xmlWriter.writeProlog("<?xml version='1.0' encoding='UTF-8' ?>");
160             xmlWriter.writeProlog("<!DOCTYPE log SYSTEM '../share/log.dtd'>");
161             xmlWriter.writeStartTag("<next>");
162             String startTag =
163                 XMLOutputStreamWriter.createStartTag(
164                     "root",
165                     new String[] { "at1", "at2" },
166                     new String[] { "v1", "v2" },
167                     false);
168             String endTag = XMLOutputStreamWriter.createEndTag("root");
169             String cData = "<kein-tag>";
170             xmlWriter.writeElementWithCData(startTag, cData, endTag);
171             String emptyStartTag =
172                 XMLOutputStreamWriter.createStartTag(
173                     "root",
174                     new String[] { "at1", "at2" },
175                     new String[] { "v1", "v2" },
176                     true);
177             xmlWriter.writeEmptyElement(emptyStartTag);
178             xmlWriter.writeStartTag("<next1>");
179             xmlWriter.writeStartTag("<next2>");
180             xmlWriter.writeStartTag("<next3>", false);
181             xmlWriter.writeCData("This is long <![CDATA[CDATA that can be encoded as CDATA block");
182             xmlWriter.writeEndTag("</next3>");
183             xmlWriter.writeStartTag("<next3>", false);
184             xmlWriter.writeCData("This is long <![CDATA[CDATA]]> that can not be encoded as CDATA block");
185             xmlWriter.writeEmptyElement(emptyStartTag);
186             xmlWriter.writeEndTag("</next3>");
187             xmlWriter.writeEndTag("</next2>");
188             xmlWriter.writeEndTag("</next1>");
189             xmlWriter.writeEndTag("</next>");
190 
191             String xml = stringWriter.toString();
192             String expected =
193                 "<?xml version='1.0' encoding='UTF-8' ?>\n<!DOCTYPE log SYSTEM '../share/log.dtd'>\n<next>\n  <root at1=\"v1\" at2=\"v2\">&lt;kein-tag&gt;</root>\n  <root at1=\"v1\" at2=\"v2\"/>\n  <next1>\n    <next2>\n      <next3><![CDATA[This is long <![CDATA[CDATA that can be encoded as CDATA block]]></next3>\n      <next3>This is long &lt;![CDATA[CDATA]]&gt; that can not be encoded as CDATA block<root at1=\"v1\" at2=\"v2\"/>\n      </next3>\n    </next2>\n  </next1>\n</next>\n";
194             System.out.println(xml);
195             if (!xml.equals(expected)) {
196                 fail("\nWrong output:\n" + xml + "\nShould be:\n" + expected + "\n");
197             }
198         } catch (IOException ioe) {
199             System.out.println("EXCEPTION");
200             fail("Exception: " + ioe);
201         }
202     }
203 
204     // test XMLEncode
205     public void testDecode() {
206         System.out.println("XMLEncode.xmlDecodeTextToCDATA");
207 
208         String xml = XMLEncode.xmlDecodeTextToCDATA("<root at1=\"v1&quot;&apos;&lt;&amp;&gt;no-tag>\"/>");
209         String expected = "<root at1=\"v1\"'<&>no-tag>\"/>";
210         assertEquals(expected, xml);
211     }
212 
213     public void testDetail() {
214         System.out.println("DETAIL TEST");
215         try {
216             stringWriter = new StringWriter();
217             xmlWriter = new XMLWriter(stringWriter);
218             xmlWriter.setPrettyPrintMode(false);
219             xmlWriter.writeStartTag("<next>");
220             String emptyStartTag =
221                 XMLOutputStreamWriter.createStartTag(
222                     "root",
223                     new String[] { "at1", "at2" },
224                     new String[] { "v1", "v2" },
225                     true);
226             xmlWriter.writeEmptyElement(emptyStartTag);
227             xmlWriter.writeStartTag("<next1>");
228             xmlWriter.writeEndTag("</next1>");
229             xmlWriter.writeEndTag("</next>");
230 
231             String xml = stringWriter.toString();
232             String expected = "<next\n><root at1=\"v1\" at2=\"v2\"\n/><next1\n></next1\n></next\n>";
233             System.out.println(xml);
234             if (!xml.equals(expected)) {
235                 fail("\nWrong output:\n" + xml + "\nShould be:\n" + expected + "\n");
236             }
237         } catch (IOException ioe) {
238             System.out.println("EXCEPTION");
239             fail("Exception: " + ioe);
240         }
241     }
242 
243     public void testExtendedConvenience() {
244         System.out.println("EXTENDED CONVENIENCE TEST");
245         try {
246             stringWriter = new StringWriter();
247             xmlWriter = new XMLWriter(stringWriter);
248             XMLOutputStreamWriter
249                 .generateAndWriteElementWithCData(xmlWriter, "root", new String[][] { { "at1", "v1" }, {
250                     "at2", "v2" }
251             }, "<cdata>");
252             String xml = stringWriter.toString();
253             String expected = "<root at1=\"v1\" at2=\"v2\">&lt;cdata&gt;</root>\n";
254             System.out.println(xml);
255             if (!xml.equals(expected)) {
256                 fail("\nWrong output:\n" + xml + "\nShould be:\n" + expected + "\n");
257             }
258         } catch (IOException ioe) {
259             System.out.println("EXCEPTION");
260             fail("Exception: " + ioe);
261         }
262     }
263 
264     public void testUmlaute() {
265         System.out.println("CHECKING ISO-8859-1 german umlaute");
266         try {
267             stringWriter = new StringWriter();
268             xmlWriter = new XMLWriter(stringWriter);
269             xmlWriter.writeXMLDeclaration();
270             xmlWriter.writeStartTag("<root>", XMLOutputStreamWriter.NO_NEWLINE);
271             xmlWriter.writePCData("text öäü ÄÖÜ ß");
272             xmlWriter.writeEndTag("</root>");
273             String xml = stringWriter.toString();
274             String expected = "<?xml version=\"1.0\"?>\n<root>text öäü ÄÖÜ ß</root>\n";
275             System.out.println(xml);
276             if (!xml.equals(expected)) {
277                 fail("\nWrong output:\n'" + xml + "'\nShould be:\n'" + expected + "'\n");
278             }
279         } catch (IOException ioe) {
280             System.out.println("EXCEPTION");
281             fail("Exception: " + ioe);
282         }
283 
284     }
285 
286     public void testEncoding() {
287         System.out.println("CHECKING TWO BYTE ENCODING");
288         try {
289             ByteArrayOutputStream out = new ByteArrayOutputStream();
290             XMLOutputStreamWriter xmlWriter = new XMLOutputStreamWriter(out, XMLOutputStreamWriter.ENCODING_UTF_16);
291             // this must not be encoded to UTF-16 - CHECK!
292             xmlWriter.writeXMLDeclaration();
293             xmlWriter.writeStartTag("<r>", XMLOutputStreamWriter.NO_NEWLINE);
294             xmlWriter.writePCData("t öäü ß");
295             xmlWriter.writeEndTag("</r>");
296             xmlWriter.flush();
297             out.flush();
298             // this actually can not be encoded into string as there is a mixture
299             // of two encodings...
300             //            String xml = out.toString(XMLOutputStreamWriter.ENCODING_UTF_16);
301             //            System.out.println("XML: "+xml);
302             byte[] outBytes = out.toByteArray();
303             // the following is simple to understand: 
304             // - first there is the literal, not encoded plain ascii (as required by XML spec) XML declaration followed by a newline (10 or 0xa)
305             // - then follows a sequence specifying big endian order for UTF-16 (-1 or 0xFF), -2 or 0xFE)
306             // - the rest are just normal ascii bytes preceeded by 0x0 as this is the UTF-16 encoding for ascii
307             // - finally there is a newline encoded as UTF-16 which is just 0 followed by 10 or 0xa
308             byte[] expectedBytes = {
309                 // xml delcaration in plain ascii
310                 '<',
311                     '?',
312                     'x',
313                     'm',
314                     'l',
315                     ' ',
316                     'v',
317                     'e',
318                     'r',
319                     's',
320                     'i',
321                     'o',
322                     'n',
323                     '=',
324                     '"',
325                     '1',
326                     '.',
327                     '0',
328                     '"',
329                     ' ',
330                     'e',
331                     'n',
332                     'c',
333                     'o',
334                     'd',
335                     'i',
336                     'n',
337                     'g',
338                     '=',
339                     '"',
340                     'U',
341                     'T',
342                     'F',
343                     '-',
344                     '1',
345                     '6',
346                     '"',
347                     '?',
348                     '>',
349                 // plain ascii newline
350                 10,
351                 // UTF-16 start big endian order
352                 -2, -1,
353                 // <r>t in UTF-16 
354                 0, '<', 0, 'r', 0, '>', 0, 't', 0, ' ',
355                 // german umlaute encoded as UTF-16
356                 //                0, 'ö' (F6=246,246-256=-10), 0, 'ä' (E4=228, 228-256=-28), 0, 'ü' (FC=252, 252-256=-4), 0, ' ', 0, 'ß' (DF=223, 223-256=33),
357                 0, -10, 0, -28, 0, -4, 0, ' ', 0, -33,
358                 // <r> in UTF-16
359                 0, '<', 0, '/', 0, 'r', 0, '>',
360                 // UTF-16 newline
361                 0, 10 };
362             assertEquals(outBytes.length, expectedBytes.length);
363 
364             // check all bytes
365             for (int i = 0; i < outBytes.length; i++) {
366                 System.out.print(outBytes[i] + ", ");
367                 if (outBytes[i] != expectedBytes[i]) {
368                     fail("\nWrong encoding:\n" + outBytes[i] + "\nShould be:\n" + expectedBytes[i] + "\n");
369                 }
370             }
371             System.out.println("DONE");
372         } catch (IOException ioe) {
373             System.out.println("EXCEPTION");
374             fail("Exception: " + ioe);
375         }
376     }
377 
378     public void testSAXAttributes() {
379         System.out.println("CHECKING SAX Attributes");
380         AttributesImpl attributes = new AttributesImpl();
381         attributes.addAttribute("", "", "at1", "CDATA", "v1\"'<no-tag>");
382         attributes.addAttribute("", "", "at2", "CDATA", "v2");
383 
384         String xml = XMLWriter.createStartTag("root", attributes, true);
385         String expected = "<root at1=\"v1&quot;&apos;&lt;no-tag&gt;\" at2=\"v2\"/>";
386         assertEquals(expected, xml);
387     }
388 }