forked from codehaus-plexus/plexus-xml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXmlStreamWriterTest.java
More file actions
201 lines (177 loc) · 5.13 KB
/
XmlStreamWriterTest.java
File metadata and controls
201 lines (177 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package org.codehaus.plexus.util.xml;
/*
* Copyright The Codehaus Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* <p>XmlStreamWriterTest class.</p>
*
* @author herve
* @version $Id: $Id
* @since 3.4.0
*/
class XmlStreamWriterTest {
/** french */
private static final String TEXT_LATIN1 = "eacute: \u00E9";
/** greek */
private static final String TEXT_LATIN7 = "alpha: \u03B1";
/** euro support */
private static final String TEXT_LATIN15 = "euro: \u20AC";
/** japanese */
private static final String TEXT_EUC_JP = "hiragana A: \u3042";
/** Unicode: support everything */
private static final String TEXT_UNICODE =
TEXT_LATIN1 + ", " + TEXT_LATIN7 + ", " + TEXT_LATIN15 + ", " + TEXT_EUC_JP;
private static String createXmlContent(String text, String encoding) {
String xmlDecl = "<?xml version=\"1.0\"?>";
if (encoding != null) {
xmlDecl = "<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>";
}
return xmlDecl + "\n<text>" + text + "</text>";
}
private static void checkXmlContent(String xml, String encoding) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
XmlStreamWriter writer = new XmlStreamWriter(out);
writer.write(xml);
writer.close();
byte[] xmlContent = out.toByteArray();
String result = new String(xmlContent, encoding);
assertEquals(xml, result);
}
private static void checkXmlWriter(String text, String encoding) throws IOException {
String xml = createXmlContent(text, encoding);
String effectiveEncoding = (encoding == null) ? "UTF-8" : encoding;
checkXmlContent(xml, effectiveEncoding);
}
/**
* <p>testNoXmlHeader.</p>
*
* @throws java.io.IOException if any.
*/
@Test
void noXmlHeader() throws Exception {
String xml = "<text>text with no XML header</text>";
checkXmlContent(xml, "UTF-8");
}
/**
* <p>testEmpty.</p>
*
* @throws java.io.IOException if any.
*/
@Test
void empty() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
XmlStreamWriter writer = new XmlStreamWriter(out);
writer.flush();
writer.write("");
writer.flush();
writer.write(".");
writer.flush();
writer.close();
}
/**
* <p>testDefaultEncoding.</p>
*
* @throws java.io.IOException if any.
*/
@Test
void defaultEncoding() throws Exception {
checkXmlWriter(TEXT_UNICODE, null);
}
/**
* <p>testUTF8Encoding.</p>
*
* @throws java.io.IOException if any.
*/
@Test
void utf8Encoding() throws Exception {
checkXmlWriter(TEXT_UNICODE, "UTF-8");
}
/**
* <p>testUTF16Encoding.</p>
*
* @throws java.io.IOException if any.
*/
@Test
void utf16Encoding() throws Exception {
checkXmlWriter(TEXT_UNICODE, "UTF-16");
}
/**
* <p>testUTF16BEEncoding.</p>
*
* @throws java.io.IOException if any.
*/
@Test
void utf16beEncoding() throws Exception {
checkXmlWriter(TEXT_UNICODE, "UTF-16BE");
}
/**
* <p>testUTF16LEEncoding.</p>
*
* @throws java.io.IOException if any.
*/
@Test
void utf16leEncoding() throws Exception {
checkXmlWriter(TEXT_UNICODE, "UTF-16LE");
}
/**
* <p>testLatin1Encoding.</p>
*
* @throws java.io.IOException if any.
*/
@Test
void latin1Encoding() throws Exception {
checkXmlWriter(TEXT_LATIN1, "ISO-8859-1");
}
/**
* <p>testLatin7Encoding.</p>
*
* @throws java.io.IOException if any.
*/
@Test
void latin7Encoding() throws Exception {
checkXmlWriter(TEXT_LATIN7, "ISO-8859-7");
}
/**
* <p>testLatin15Encoding.</p>
*
* @throws java.io.IOException if any.
*/
@Test
void latin15Encoding() throws Exception {
checkXmlWriter(TEXT_LATIN15, "ISO-8859-15");
}
/**
* <p>testEUC_JPEncoding.</p>
*
* @throws java.io.IOException if any.
*/
@Test
void euc_jpEncoding() throws Exception {
checkXmlWriter(TEXT_EUC_JP, "EUC-JP");
}
/**
* <p>testEBCDICEncoding.</p>
*
* @throws java.io.IOException if any.
*/
@Test
void ebcdicEncoding() throws Exception {
checkXmlWriter("simple text in EBCDIC", "CP1047");
}
}