Skip to content

Commit b123f5b

Browse files
committed
[IO-320] Add XmlStreamReader support for UTF-32. Split out some @test methods for finer grain failure reporting. Port to JUnit 4. Fix a param name typo.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1310360 13f79535-47bb-0310-9956-ffa450edef68
1 parent 12e4b99 commit b123f5b

1 file changed

Lines changed: 57 additions & 9 deletions

File tree

src/test/java/org/apache/commons/io/input/XmlStreamReaderUtilitiesTest.java

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,21 @@
1616
*/
1717
package org.apache.commons.io.input;
1818

19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertNull;
21+
import static org.junit.Assert.assertTrue;
22+
import static org.junit.Assert.fail;
23+
1924
import java.io.ByteArrayInputStream;
2025
import java.io.IOException;
2126

22-
import junit.framework.TestCase;
27+
import org.junit.Ignore;
28+
import org.junit.Test;
2329

2430
/**
2531
* Test the Encoding Utilities part of {@link XmlStreamReader}.
2632
*/
27-
public class XmlStreamReaderUtilitiesTest extends TestCase {
33+
public class XmlStreamReaderUtilitiesTest {
2834

2935
private static String RAWMGS1 = "encoding mismatch";
3036
private static String RAWMGS2 = "unknown BOM";
@@ -35,15 +41,15 @@ public class XmlStreamReaderUtilitiesTest extends TestCase {
3541
private static String APPXML = "application/xml";
3642
private static String APPXML_UTF8 = "application/xml;charset=UTF-8";
3743
private static String APPXML_UTF16 = "application/xml;charset=UTF-16";
44+
private static String APPXML_UTF32 = "application/xml;charset=UTF-32";
3845
private static String APPXML_UTF16BE = "application/xml;charset=UTF-16BE";
3946
private static String APPXML_UTF16LE = "application/xml;charset=UTF-16LE";
47+
private static String APPXML_UTF32BE = "application/xml;charset=UTF-32BE";
48+
private static String APPXML_UTF32LE = "application/xml;charset=UTF-32LE";
4049
private static String TXTXML = "text/xml";
4150

42-
@Override
43-
protected void setUp() {
44-
}
45-
4651
/** Test for {@link XmlStreamReader#getContentTypeEncoding(String)}. */
52+
@Test
4753
public void testContentTypeEncoding() {
4854
checkContentTypeEncoding(null, null);
4955
checkContentTypeEncoding(null, "");
@@ -56,26 +62,33 @@ public void testContentTypeEncoding() {
5662
checkContentTypeEncoding("UTF-16", "application/xml;charset=UTF-16");
5763
checkContentTypeEncoding("UTF-16", "application/xml;charset='UTF-16'");
5864
checkContentTypeEncoding("UTF-16", "application/xml;charset=\"UTF-16\"");
65+
checkContentTypeEncoding("UTF-32", "application/xml;charset=utf-32");
66+
checkContentTypeEncoding("UTF-32", "application/xml;charset=UTF-32");
67+
checkContentTypeEncoding("UTF-32", "application/xml;charset='UTF-32'");
68+
checkContentTypeEncoding("UTF-32", "application/xml;charset=\"UTF-32\"");
5969
}
6070

6171
private void checkContentTypeEncoding(String expected, String httpContentType) {
6272
assertEquals("ContentTypeEncoding=[" + httpContentType + "]", expected, XmlStreamReader.getContentTypeEncoding(httpContentType));
6373
}
6474

6575
/** Test for {@link XmlStreamReader#getContentTypeEncoding(String)}. */
76+
@Test
6677
public void testContentTypeMime() {
6778
checkContentTypeMime(null, null);
6879
checkContentTypeMime("", "");
6980
checkContentTypeMime("application/xml", "application/xml");
7081
checkContentTypeMime("application/xml", "application/xml;");
7182
checkContentTypeMime("application/xml", "application/xml;charset=utf-16");
83+
checkContentTypeMime("application/xml", "application/xml;charset=utf-32");
7284
}
7385

7486
private void checkContentTypeMime(String expected, String httpContentType) {
7587
assertEquals("ContentTypeMime=[" + httpContentType + "]", expected, XmlStreamReader.getContentTypeMime(httpContentType));
7688
}
7789

7890
/** Test for {@link XmlStreamReader#isAppXml(String)}. */
91+
@Test
7992
public void testAppXml() {
8093
checkAppXml(false, null);
8194
checkAppXml(false, "");
@@ -96,6 +109,7 @@ private void checkAppXml(boolean expected, String mime) {
96109
}
97110

98111
/** Test for {@link XmlStreamReader#isTextXml(String)}. */
112+
@Test
99113
public void testTextXml() {
100114
checkTextXml(false, null);
101115
checkTextXml(false, "");
@@ -113,29 +127,42 @@ private void checkTextXml(boolean expected, String mime) {
113127
}
114128

115129
/** No BOM calculateRawEncoding() Test */
130+
@Test
116131
public void testCalculateRawEncodingNoBOM() throws IOException {
117132
// No BOM Expected BOM Guess XML Default
118133
checkRawError(RAWMGS2, "UTF-32", null, null, null);
134+
//
119135
checkRawEncoding("UTF-8", null, null, null, null);
120136
checkRawEncoding("UTF-8", null, "UTF-16BE", null, null); /* why default & not Guess? */
121137
checkRawEncoding("UTF-8", null, null, "UTF-16BE", null); /* why default & not XMLEnc? */
138+
checkRawEncoding("UTF-8", null, "UTF-8", "UTF-8", "UTF-16BE");
139+
//
122140
checkRawEncoding("UTF-16BE", null, "UTF-16BE", "UTF-16BE", null);
123141
checkRawEncoding("UTF-16BE", null, null, null, "UTF-16BE");
124142
checkRawEncoding("UTF-16BE", null, "UTF-8", null, "UTF-16BE"); /* why default & not Guess? */
125143
checkRawEncoding("UTF-16BE", null, null, "UTF-8", "UTF-16BE"); /* why default & not Guess? */
126-
checkRawEncoding("UTF-8", null, "UTF-8", "UTF-8", "UTF-16BE");
127144
checkRawEncoding("UTF-16BE", null, "UTF-16BE", "UTF-16", null);
128145
checkRawEncoding("UTF-16LE", null, "UTF-16LE", "UTF-16", null);
129146
}
130147

131148
/** BOM calculateRawEncoding() Test */
149+
@Test
132150
public void testCalculateRawEncodingStandard() throws IOException {
133151
// Standard BOM Checks BOM Other Default
134152
testCalculateRawEncodingStandard("UTF-8", "UTF-16BE", "UTF-16LE");
135153
testCalculateRawEncodingStandard("UTF-16BE", "UTF-8", "UTF-16LE");
136154
testCalculateRawEncodingStandard("UTF-16LE", "UTF-8", "UTF-16BE");
137155
}
138156

157+
/** BOM calculateRawEncoding() Test */
158+
@Test
159+
@Ignore
160+
public void testCalculateRawEncodingStandardUtf32() throws IOException {
161+
// Standard BOM Checks BOM Other Default
162+
testCalculateRawEncodingStandard("UTF-32BE", "UTF-8", "UTF-32LE");
163+
testCalculateRawEncodingStandard("UTF-32LE", "UTF-8", "UTF-32BE");
164+
}
165+
139166
private void testCalculateRawEncodingStandard(String bomEnc, String otherEnc, String defaultEnc) throws IOException {
140167
// Expected BOM Guess XMLEnc Default
141168
checkRawEncoding(bomEnc, bomEnc, null, null, defaultEnc);
@@ -150,6 +177,7 @@ private void testCalculateRawEncodingStandard(String bomEnc, String otherEnc, St
150177
}
151178

152179
/** Additional UTF-16 calculateRawEncoding() Test */
180+
@Test
153181
public void testCalculateRawEncodingAdditonalkUTF16() throws IOException {
154182
// BOM Guess XML Default
155183
checkRawError(RAWMGS1, "UTF-16BE", "UTF-16", null, null);
@@ -202,6 +230,7 @@ private void checkRawError(String msgSuffix,
202230
}
203231

204232
/** Test calculate HTTP Encoding */
233+
@Test
205234
public void testCalculateHttpEncoding() throws IOException {
206235
// No BOM Expected Lenient cType BOM Guess XML Default
207236
checkHttpError(HTTPMGS3, true, null, null, null, null, null);
@@ -226,6 +255,25 @@ public void testCalculateHttpEncoding() throws IOException {
226255
checkHttpEncoding("UTF-8", false, APPXML_UTF8, "UTF-16BE", "UTF-16BE", "UTF-16BE", "UTF-16BE");
227256
}
228257

258+
/** Test calculate HTTP Encoding */
259+
@Test
260+
@Ignore
261+
public void testCalculateHttpEncodingUtf32() throws IOException {
262+
// No BOM Expected Lenient cType BOM Guess XML Default
263+
checkHttpEncoding("UTF-32LE", true, null, null, null, "UTF-32LE", null);
264+
checkHttpEncoding("UTF-32BE", false, TXTXML, null, null, null, "UTF-32BE");
265+
checkHttpEncoding("UTF-32BE", false, APPXML, null, null, null, "UTF-32BE");
266+
checkHttpEncoding("UTF-32LE", false, APPXML_UTF32LE, null, null, null, null);
267+
checkHttpEncoding("UTF-32BE", false, APPXML_UTF32BE, null, null, null, null);
268+
checkHttpError(HTTPMGS1, false, APPXML_UTF32LE, "UTF-32LE", null, null, null);
269+
checkHttpError(HTTPMGS1, false, APPXML_UTF32BE, "UTF-32BE", null, null, null);
270+
checkHttpError(HTTPMGS2, false, APPXML_UTF32, null, null, null, null);
271+
checkHttpError(HTTPMGS2, false, APPXML_UTF32, "UTF-8", null, null, null);
272+
checkHttpEncoding("UTF-32LE", false, APPXML_UTF32, "UTF-32LE", null, null, null);
273+
checkHttpEncoding("UTF-32BE", false, APPXML_UTF32, "UTF-32BE", null, null, null);
274+
checkHttpEncoding("UTF-8", false, APPXML_UTF8, "UTF-32BE", "UTF-32BE", "UTF-32BE", "UTF-32BE");
275+
}
276+
229277
private void checkHttpEncoding(String expected, boolean lenient, String httpContentType,
230278
String bomEnc, String xmlGuessEnc, String xmlEnc, String defaultEncoding) throws IOException {
231279
StringBuilder builder = new StringBuilder();
@@ -247,10 +295,10 @@ protected String calculateHttpEncoding(String httpContentType, String bomEnc, St
247295
return encoding;
248296
}
249297

250-
private void checkHttpError(String msgSuffix, boolean lenienet, String httpContentType,
298+
private void checkHttpError(String msgSuffix, boolean lenient, String httpContentType,
251299
String bomEnc, String xmlGuessEnc, String xmlEnc, String defaultEncoding) {
252300
try {
253-
checkHttpEncoding("XmlStreamReaderException", lenienet, httpContentType, bomEnc, xmlGuessEnc, xmlEnc, defaultEncoding);
301+
checkHttpEncoding("XmlStreamReaderException", lenient, httpContentType, bomEnc, xmlGuessEnc, xmlEnc, defaultEncoding);
254302
fail("Expected XmlStreamReaderException");
255303
} catch (XmlStreamReaderException e) {
256304
assertTrue("Msg Start: " + e.getMessage(), e.getMessage().startsWith("Invalid encoding"));

0 commit comments

Comments
 (0)