1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.io.output;
18
19 import java.io.ByteArrayOutputStream;
20 import java.io.IOException;
21 import java.util.Arrays;
22
23 import junit.framework.TestCase;
24
25
26
27
28 public class XmlStreamWriterTest extends TestCase {
29
30 private static final String TEXT_LATIN1 = "eacute: \u00E9";
31
32 private static final String TEXT_LATIN7 = "alpha: \u03B1";
33
34 private static final String TEXT_LATIN15 = "euro: \u20AC";
35
36 private static final String TEXT_EUC_JP = "hiragana A: \u3042";
37
38 private static final String TEXT_UNICODE = TEXT_LATIN1 + ", " + TEXT_LATIN7
39 + ", " + TEXT_LATIN15 + ", " + TEXT_EUC_JP;
40
41 private static String createXmlContent(final String text, final String encoding) {
42 String xmlDecl = "<?xml version=\"1.0\"?>";
43 if (encoding != null) {
44 xmlDecl = "<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>";
45 }
46 final String xml = xmlDecl + "\n<text>" + text + "</text>";
47 return xml;
48 }
49
50 private static void checkXmlContent(final String xml, final String encoding, final String defaultEncoding)
51 throws IOException {
52 final ByteArrayOutputStream out = new ByteArrayOutputStream();
53 final XmlStreamWriter writer = new XmlStreamWriter(out, defaultEncoding);
54 writer.write(xml);
55 writer.close();
56 final byte[] xmlContent = out.toByteArray();
57 assertEquals(encoding, writer.getEncoding());
58 assertTrue(Arrays.equals(xml.getBytes(encoding), xmlContent));
59
60 }
61
62 private static void checkXmlWriter(final String text, final String encoding)
63 throws IOException {
64 checkXmlWriter(text, encoding, null);
65 }
66
67 private static void checkXmlWriter(final String text, final String encoding, final String defaultEncoding)
68 throws IOException {
69 final String xml = createXmlContent(text, encoding);
70 String effectiveEncoding = encoding;
71 if (effectiveEncoding == null) {
72 effectiveEncoding = defaultEncoding == null ? "UTF-8" : defaultEncoding;
73 }
74 checkXmlContent(xml, effectiveEncoding, defaultEncoding);
75 }
76
77 public void testNoXmlHeader() throws IOException {
78 final String xml = "<text>text with no XML header</text>";
79 checkXmlContent(xml, "UTF-8", null);
80 }
81
82 public void testEmpty() throws IOException {
83 final ByteArrayOutputStream out = new ByteArrayOutputStream();
84 final XmlStreamWriter writer = new XmlStreamWriter(out);
85 writer.flush();
86 writer.write("");
87 writer.flush();
88 writer.write(".");
89 writer.flush();
90 writer.close();
91 }
92
93 public void testDefaultEncoding() throws IOException {
94 checkXmlWriter(TEXT_UNICODE, null, null);
95 checkXmlWriter(TEXT_UNICODE, null, "UTF-8");
96 checkXmlWriter(TEXT_UNICODE, null, "UTF-16");
97 checkXmlWriter(TEXT_UNICODE, null, "UTF-16BE");
98 checkXmlWriter(TEXT_UNICODE, null, "ISO-8859-1");
99 }
100
101 public void testUTF8Encoding() throws IOException {
102 checkXmlWriter(TEXT_UNICODE, "UTF-8");
103 }
104
105 public void testUTF16Encoding() throws IOException {
106 checkXmlWriter(TEXT_UNICODE, "UTF-16");
107 }
108
109 public void testUTF16BEEncoding() throws IOException {
110 checkXmlWriter(TEXT_UNICODE, "UTF-16BE");
111 }
112
113 public void testUTF16LEEncoding() throws IOException {
114 checkXmlWriter(TEXT_UNICODE, "UTF-16LE");
115 }
116
117 public void testLatin1Encoding() throws IOException {
118 checkXmlWriter(TEXT_LATIN1, "ISO-8859-1");
119 }
120
121 public void testLatin7Encoding() throws IOException {
122 checkXmlWriter(TEXT_LATIN7, "ISO-8859-7");
123 }
124
125 public void testLatin15Encoding() throws IOException {
126 checkXmlWriter(TEXT_LATIN15, "ISO-8859-15");
127 }
128
129 public void testEUC_JPEncoding() throws IOException {
130 checkXmlWriter(TEXT_EUC_JP, "EUC-JP");
131 }
132
133 public void testEBCDICEncoding() throws IOException {
134 checkXmlWriter("simple text in EBCDIC", "CP1047");
135 }
136 }