View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.io.output;
18  
19  import org.junit.Test;
20  
21  import java.io.ByteArrayOutputStream;
22  import java.io.IOException;
23  import java.util.Arrays;
24  
25  import static org.junit.Assert.assertEquals;
26  import static org.junit.Assert.assertTrue;
27  
28  /**
29   * @version $Id: XmlStreamWriterTest.java 1718944 2015-12-09 19:50:30Z krosenvold $
30   */
31  public class XmlStreamWriterTest {
32      /** french */
33      private static final String TEXT_LATIN1 = "eacute: \u00E9";
34      /** greek */
35      private static final String TEXT_LATIN7 = "alpha: \u03B1";
36      /** euro support */
37      private static final String TEXT_LATIN15 = "euro: \u20AC";
38      /** japanese */
39      private static final String TEXT_EUC_JP = "hiragana A: \u3042";
40      /** Unicode: support everything */
41      private static final String TEXT_UNICODE = TEXT_LATIN1 + ", " + TEXT_LATIN7
42              + ", " + TEXT_LATIN15 + ", " + TEXT_EUC_JP;
43  
44      private static String createXmlContent(final String text, final String encoding) {
45          String xmlDecl = "<?xml version=\"1.0\"?>";
46          if (encoding != null) {
47              xmlDecl = "<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>";
48          }
49          return xmlDecl + "\n<text>" + text + "</text>";
50      }
51  
52      private static void checkXmlContent(final String xml, final String encoding, final String defaultEncoding)
53              throws IOException {
54          final ByteArrayOutputStream out = new ByteArrayOutputStream();
55          final XmlStreamWriter writer = new XmlStreamWriter(out, defaultEncoding);
56          writer.write(xml);
57          writer.close();
58          final byte[] xmlContent = out.toByteArray();
59          assertEquals(encoding, writer.getEncoding());
60          assertTrue(Arrays.equals(xml.getBytes(encoding), xmlContent));
61  
62      }
63  
64      private static void checkXmlWriter(final String text, final String encoding)
65              throws IOException {
66          checkXmlWriter(text, encoding, null);
67      }
68  
69      private static void checkXmlWriter(final String text, final String encoding, final String defaultEncoding)
70              throws IOException {
71          final String xml = createXmlContent(text, encoding);
72          String effectiveEncoding = encoding;
73          if (effectiveEncoding == null) {
74              effectiveEncoding = defaultEncoding == null ? "UTF-8" : defaultEncoding;
75          }
76          checkXmlContent(xml, effectiveEncoding, defaultEncoding);
77      }
78  
79      @Test
80      public void testNoXmlHeader() throws IOException {
81          final String xml = "<text>text with no XML header</text>";
82          checkXmlContent(xml, "UTF-8", null);
83      }
84  
85      @Test
86      public void testEmpty() throws IOException {
87          final ByteArrayOutputStream out = new ByteArrayOutputStream();
88          final XmlStreamWriter writer = new XmlStreamWriter(out);
89          writer.flush();
90          writer.write("");
91          writer.flush();
92          writer.write(".");
93          writer.flush();
94          writer.close();
95      }
96  
97      @Test
98      public void testDefaultEncoding() throws IOException {
99          checkXmlWriter(TEXT_UNICODE, null, null);
100         checkXmlWriter(TEXT_UNICODE, null, "UTF-8");
101         checkXmlWriter(TEXT_UNICODE, null, "UTF-16");
102         checkXmlWriter(TEXT_UNICODE, null, "UTF-16BE");
103         checkXmlWriter(TEXT_UNICODE, null, "ISO-8859-1");
104     }
105 
106     @Test
107     public void testUTF8Encoding() throws IOException {
108         checkXmlWriter(TEXT_UNICODE, "UTF-8");
109     }
110 
111     @Test
112     public void testUTF16Encoding() throws IOException {
113         checkXmlWriter(TEXT_UNICODE, "UTF-16");
114     }
115 
116     @Test
117     public void testUTF16BEEncoding() throws IOException {
118         checkXmlWriter(TEXT_UNICODE, "UTF-16BE");
119     }
120 
121     @Test
122     public void testUTF16LEEncoding() throws IOException {
123         checkXmlWriter(TEXT_UNICODE, "UTF-16LE");
124     }
125 
126     @Test
127     public void testLatin1Encoding() throws IOException {
128         checkXmlWriter(TEXT_LATIN1, "ISO-8859-1");
129     }
130 
131     @Test
132     public void testLatin7Encoding() throws IOException {
133         checkXmlWriter(TEXT_LATIN7, "ISO-8859-7");
134     }
135 
136     @Test
137     public void testLatin15Encoding() throws IOException {
138         checkXmlWriter(TEXT_LATIN15, "ISO-8859-15");
139     }
140 
141     @Test
142     public void testEUC_JPEncoding() throws IOException {
143         checkXmlWriter(TEXT_EUC_JP, "EUC-JP");
144     }
145 
146     @Test
147     public void testEBCDICEncoding() throws IOException {
148         checkXmlWriter("simple text in EBCDIC", "CP1047");
149     }
150 }