|
| 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.input; |
| 18 | + |
| 19 | +import java.io.ByteArrayInputStream; |
| 20 | +import java.io.IOException; |
| 21 | + |
| 22 | +import junit.framework.TestCase; |
| 23 | + |
| 24 | +/** |
| 25 | + * Test the Encoding Utilities part of {@link XmlStreamReader}. |
| 26 | + */ |
| 27 | +public class XmlStreamReaderUtilitiesTest extends TestCase { |
| 28 | + |
| 29 | + private static String RAWMGS1 = "encoding mismatch"; |
| 30 | + private static String RAWMGS2 = "unknown BOM"; |
| 31 | + private static String HTTPMGS1 = "BOM must be NULL"; |
| 32 | + private static String HTTPMGS2 = "encoding mismatch"; |
| 33 | + private static String HTTPMGS3 = "Invalid MIME"; |
| 34 | + |
| 35 | + private static String APPXML = "application/xml"; |
| 36 | + private static String APPXML_UTF8 = "application/xml;charset=UTF-8"; |
| 37 | + private static String APPXML_UTF16 = "application/xml;charset=UTF-16"; |
| 38 | + private static String APPXML_UTF16BE = "application/xml;charset=UTF-16BE"; |
| 39 | + private static String APPXML_UTF16LE = "application/xml;charset=UTF-16LE"; |
| 40 | + private static String TXTXML = "text/xml"; |
| 41 | + |
| 42 | + @Override |
| 43 | + protected void setUp() { |
| 44 | + } |
| 45 | + |
| 46 | + /** Test for {@link XmlStreamReader#getContentTypeEncoding(String)}. */ |
| 47 | + public void testContentTypeEncoding() throws IOException { |
| 48 | + checkContentTypeEncoding(null, null); |
| 49 | + checkContentTypeEncoding(null, ""); |
| 50 | + checkContentTypeEncoding(null, "application/xml"); |
| 51 | + checkContentTypeEncoding(null, "application/xml;"); |
| 52 | + checkContentTypeEncoding(null, "multipart/mixed;boundary=frontier"); |
| 53 | + checkContentTypeEncoding(null, "multipart/mixed;boundary='frontier'"); |
| 54 | + checkContentTypeEncoding(null, "multipart/mixed;boundary=\"frontier\""); |
| 55 | + checkContentTypeEncoding("UTF-16", "application/xml;charset=utf-16"); |
| 56 | + checkContentTypeEncoding("UTF-16", "application/xml;charset=UTF-16"); |
| 57 | + checkContentTypeEncoding("UTF-16", "application/xml;charset='UTF-16'"); |
| 58 | + checkContentTypeEncoding("UTF-16", "application/xml;charset=\"UTF-16\""); |
| 59 | + } |
| 60 | + private void checkContentTypeEncoding(String expected, String httpContentType) { |
| 61 | + assertEquals("ContentTypeEncoding=[" + httpContentType + "]", expected, XmlStreamReader.getContentTypeEncoding(httpContentType)); |
| 62 | + } |
| 63 | + |
| 64 | + /** Test for {@link XmlStreamReader#getContentTypeEncoding(String)}. */ |
| 65 | + public void testContentTypeMime() throws IOException { |
| 66 | + checkContentTypeMime(null, null); |
| 67 | + checkContentTypeMime("", ""); |
| 68 | + checkContentTypeMime("application/xml", "application/xml"); |
| 69 | + checkContentTypeMime("application/xml", "application/xml;"); |
| 70 | + checkContentTypeMime("application/xml", "application/xml;charset=utf-16"); |
| 71 | + } |
| 72 | + private void checkContentTypeMime(String expected, String httpContentType) { |
| 73 | + assertEquals("ContentTypeMime=[" + httpContentType + "]", expected, XmlStreamReader.getContentTypeMime(httpContentType)); |
| 74 | + } |
| 75 | + |
| 76 | + /** Test for {@link XmlStreamReader#isAppXml(String)}. */ |
| 77 | + public void testAppXml() throws IOException { |
| 78 | + checkAppXml(false, null); |
| 79 | + checkAppXml(false, ""); |
| 80 | + checkAppXml(true, "application/xml"); |
| 81 | + checkAppXml(true, "application/xml-dtd"); |
| 82 | + checkAppXml(true, "application/xml-external-parsed-entity"); |
| 83 | + checkAppXml(true, "application/soap+xml"); |
| 84 | + checkAppXml(true, "application/atom+xml"); |
| 85 | + checkAppXml(false, "application/atomxml"); |
| 86 | + checkAppXml(false, "text/xml"); |
| 87 | + checkAppXml(false, "text/atom+xml"); |
| 88 | + checkAppXml(true, "application/xml-dtd"); |
| 89 | + checkAppXml(true, "application/xml-external-parsed-entity"); |
| 90 | + } |
| 91 | + private void checkAppXml(boolean expected, String mime) { |
| 92 | + assertEquals("Mime=[" + mime + "]", expected, XmlStreamReader.isAppXml(mime)); |
| 93 | + } |
| 94 | + |
| 95 | + /** Test for {@link XmlStreamReader#isTextXml(String)}. */ |
| 96 | + public void testTextXml() throws IOException { |
| 97 | + checkTextXml(false, null); |
| 98 | + checkTextXml(false, ""); |
| 99 | + checkTextXml(true, "text/xml"); |
| 100 | + checkTextXml(true, "text/xml-external-parsed-entity"); |
| 101 | + checkTextXml(true, "text/soap+xml"); |
| 102 | + checkTextXml(true, "text/atom+xml"); |
| 103 | + checkTextXml(false, "text/atomxml"); |
| 104 | + checkTextXml(false, "application/xml"); |
| 105 | + checkTextXml(false, "application/atom+xml"); |
| 106 | + } |
| 107 | + private void checkTextXml(boolean expected, String mime) { |
| 108 | + assertEquals("Mime=[" + mime + "]", expected, XmlStreamReader.isTextXml(mime)); |
| 109 | + } |
| 110 | + |
| 111 | + /** No BOM calculateRawEncoding() Test */ |
| 112 | + public void testCalculateRawEncodingNoBOM() throws IOException { |
| 113 | + // No BOM Expected BOM Guess XML Default |
| 114 | + checkRawError(RAWMGS2, "UTF-32", null, null, null); |
| 115 | + checkRawEncoding("UTF-8", null, null, null, null); |
| 116 | + checkRawEncoding("UTF-8", null, "UTF-16BE", null, null); /* why default & not Guess? */ |
| 117 | + checkRawEncoding("UTF-8", null, null, "UTF-16BE", null); /* why default & not XMLEnc? */ |
| 118 | + checkRawEncoding("UTF-16BE", null, "UTF-16BE", "UTF-16BE", null); |
| 119 | + checkRawEncoding("UTF-16BE", null, null, null, "UTF-16BE"); |
| 120 | + checkRawEncoding("UTF-16BE", null, "UTF-8", null, "UTF-16BE"); /* why default & not Guess? */ |
| 121 | + checkRawEncoding("UTF-16BE", null, null, "UTF-8", "UTF-16BE"); /* why default & not Guess? */ |
| 122 | + checkRawEncoding("UTF-8", null, "UTF-8", "UTF-8", "UTF-16BE"); |
| 123 | + checkRawEncoding("UTF-16BE", null, "UTF-16BE", "UTF-16", null); |
| 124 | + checkRawEncoding("UTF-16LE", null, "UTF-16LE", "UTF-16", null); |
| 125 | + } |
| 126 | + |
| 127 | + /** BOM calculateRawEncoding() Test */ |
| 128 | + public void testCalculateRawEncodingStandard() throws IOException { |
| 129 | + // Standard BOM Checks BOM Other Default |
| 130 | + testCalculateRawEncodingStandard("UTF-8", "UTF-16BE", "UTF-16LE"); |
| 131 | + testCalculateRawEncodingStandard("UTF-16BE", "UTF-8", "UTF-16LE"); |
| 132 | + testCalculateRawEncodingStandard("UTF-16LE", "UTF-8", "UTF-16BE"); |
| 133 | + } |
| 134 | + private void testCalculateRawEncodingStandard(String bomEnc, String otherEnc, String defaultEnc) throws IOException { |
| 135 | + // Expected BOM Guess XMLEnc Default |
| 136 | + checkRawEncoding(bomEnc, bomEnc, null, null, defaultEnc); |
| 137 | + checkRawEncoding(bomEnc, bomEnc, bomEnc, null, defaultEnc); |
| 138 | + //checkRawError(RAWMGS1, bomEnc, otherEnc, null, defaultEnc); throws IOException for UTF-16BE/LE |
| 139 | + checkRawEncoding(bomEnc, bomEnc, null, bomEnc, defaultEnc); |
| 140 | + checkRawError(RAWMGS1, bomEnc, null, otherEnc, defaultEnc); |
| 141 | + checkRawEncoding(bomEnc, bomEnc, bomEnc, bomEnc, defaultEnc); |
| 142 | + checkRawError(RAWMGS1, bomEnc, bomEnc, otherEnc, defaultEnc); |
| 143 | + //checkRawError(RAWMGS1, bomEnc, otherEnc, bomEnc, defaultEnc); throws IOException for UTF-16BE/LE |
| 144 | + |
| 145 | + } |
| 146 | + |
| 147 | + /** Additional UTF-16 calculateRawEncoding() Test */ |
| 148 | + public void testCalculateRawEncodingAdditonalkUTF16() throws IOException { |
| 149 | + // BOM Guess XML Default |
| 150 | +// checkRawError(RAWMGS1, "UTF-16BE", "UTF-16", null, null); throws IOException |
| 151 | + checkRawEncoding("UTF-16BE", "UTF-16BE", null, "UTF-16", null); |
| 152 | + checkRawEncoding("UTF-16BE", "UTF-16BE", "UTF-16BE", "UTF-16", null); |
| 153 | + checkRawError(RAWMGS1, "UTF-16BE", null, "UTF-16LE", null); |
| 154 | + checkRawError(RAWMGS1, "UTF-16BE", "UTF-16BE", "UTF-16LE", null); |
| 155 | +// checkRawError(RAWMGS1, "UTF-16LE", "UTF-16", null, null); throws IOException |
| 156 | + checkRawEncoding("UTF-16LE", "UTF-16LE", null, "UTF-16", null); |
| 157 | + checkRawEncoding("UTF-16LE", "UTF-16LE", "UTF-16LE", "UTF-16", null); |
| 158 | + checkRawError(RAWMGS1, "UTF-16LE", null, "UTF-16BE", null); |
| 159 | + checkRawError(RAWMGS1, "UTF-16LE", "UTF-16LE", "UTF-16BE", null); |
| 160 | + } |
| 161 | + private void checkRawEncoding(String expected, |
| 162 | + String bomEnc, String xmlGuessEnc, String xmlEnc, String defaultEncoding) throws IOException { |
| 163 | + StringBuilder builder = new StringBuilder(); |
| 164 | + builder.append("RawEncoding: ").append(bomEnc).append("], "); |
| 165 | + builder.append("bomEnc=[").append(bomEnc).append("], "); |
| 166 | + builder.append("xmlGuessEnc=[").append(xmlGuessEnc).append("], "); |
| 167 | + builder.append("xmlEnc=[").append(xmlEnc).append("], "); |
| 168 | + builder.append("defaultEncoding=[").append(defaultEncoding).append("],"); |
| 169 | + MockXmlStreamReader mock = new MockXmlStreamReader(defaultEncoding); |
| 170 | + String encoding = mock.calculateRawEncoding(bomEnc,xmlGuessEnc,xmlEnc); |
| 171 | + assertEquals(builder.toString(), expected, encoding); |
| 172 | + } |
| 173 | + private void checkRawError(String msgSuffix, |
| 174 | + String bomEnc, String xmlGuessEnc, String xmlEnc, String defaultEncoding) { |
| 175 | + try { |
| 176 | + checkRawEncoding("XmlStreamReaderException", bomEnc, xmlGuessEnc, xmlEnc, defaultEncoding); |
| 177 | + fail("Expected XmlStreamReaderException"); |
| 178 | + } catch (XmlStreamReaderException e) { |
| 179 | + assertTrue("Msg Start: " + e.getMessage(), e.getMessage().startsWith("Invalid encoding")); |
| 180 | + assertTrue("Msg End: " + e.getMessage(), e.getMessage().endsWith(msgSuffix)); |
| 181 | + assertEquals("bomEnc", bomEnc, e.getBomEncoding()); |
| 182 | + assertEquals("xmlGuessEnc", xmlGuessEnc, e.getXmlGuessEncoding()); |
| 183 | + assertEquals("xmlEnc", xmlEnc, e.getXmlEncoding()); |
| 184 | + assertNull("ContentTypeEncoding", e.getContentTypeEncoding()); |
| 185 | + assertNull("ContentTypeMime", e.getContentTypeMime()); |
| 186 | + } catch (Exception e) { |
| 187 | + fail("Expected XmlStreamReaderException, but threw " + e); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + /** Test calculate HTTP Encoding */ |
| 192 | + public void testCalculateHttpEncoding() throws IOException { |
| 193 | + // No BOM Expected Lenient cType BOM Guess XML Default |
| 194 | + checkHttpError(HTTPMGS3, true, null, null, null, null, null); |
| 195 | + checkHttpError(HTTPMGS3, false, null, null, null, "UTF-8", null); |
| 196 | + checkHttpEncoding("UTF-8", true, null, null, null, "UTF-8", null); |
| 197 | + checkHttpEncoding("UTF-16LE", true, null, null, null, "UTF-16LE", null); |
| 198 | + checkHttpError(HTTPMGS3, false, "text/css", null, null, null, null); |
| 199 | + checkHttpEncoding("US-ASCII", false, TXTXML, null, null, null, null); |
| 200 | + checkHttpEncoding("UTF-16BE", false, TXTXML, null, null, null, "UTF-16BE"); |
| 201 | + checkHttpEncoding("UTF-8", false, APPXML, null, null, null, null); |
| 202 | + checkHttpEncoding("UTF-16BE", false, APPXML, null, null, null, "UTF-16BE"); |
| 203 | + checkHttpEncoding("UTF-8", false, APPXML, "UTF-8", null, null, "UTF-16BE"); |
| 204 | + checkHttpEncoding("UTF-16LE", false, APPXML_UTF16LE, null, null, null, null); |
| 205 | + checkHttpEncoding("UTF-16BE", false, APPXML_UTF16BE, null, null, null, null); |
| 206 | + checkHttpError(HTTPMGS1, false, APPXML_UTF16LE, "UTF-16LE", null, null, null); |
| 207 | + checkHttpError(HTTPMGS1, false, APPXML_UTF16BE, "UTF-16BE", null, null, null); |
| 208 | + checkHttpError(HTTPMGS2, false, APPXML_UTF16, null, null, null, null); |
| 209 | + checkHttpError(HTTPMGS2, false, APPXML_UTF16, "UTF-8", null, null, null); |
| 210 | + checkHttpEncoding("UTF-16LE", false, APPXML_UTF16, "UTF-16LE", null, null, null); |
| 211 | + checkHttpEncoding("UTF-16BE", false, APPXML_UTF16, "UTF-16BE", null, null, null); |
| 212 | + checkHttpEncoding("UTF-8", false, APPXML_UTF8, null, null, null, null); |
| 213 | + checkHttpEncoding("UTF-8", false, APPXML_UTF8, "UTF-16BE", "UTF-16BE", "UTF-16BE", "UTF-16BE"); |
| 214 | + } |
| 215 | + private void checkHttpEncoding(String expected, boolean lenienet, String httpContentType, |
| 216 | + String bomEnc, String xmlGuessEnc, String xmlEnc, String defaultEncoding) throws IOException { |
| 217 | + StringBuilder builder = new StringBuilder(); |
| 218 | + builder.append("HttpEncoding: ").append(bomEnc).append("], "); |
| 219 | + builder.append("lenienet=[").append(lenienet).append("], "); |
| 220 | + builder.append("httpContentType=[").append(httpContentType).append("], "); |
| 221 | + builder.append("bomEnc=[").append(bomEnc).append("], "); |
| 222 | + builder.append("xmlGuessEnc=[").append(xmlGuessEnc).append("], "); |
| 223 | + builder.append("xmlEnc=[").append(xmlEnc).append("], "); |
| 224 | + builder.append("defaultEncoding=[").append(defaultEncoding).append("],"); |
| 225 | + MockXmlStreamReader mock = new MockXmlStreamReader(defaultEncoding); |
| 226 | + String encoding = mock.calculateHttpEncoding(httpContentType, bomEnc, xmlGuessEnc, xmlEnc, lenienet); |
| 227 | + assertEquals(builder.toString(), expected, encoding); |
| 228 | + } |
| 229 | + private void checkHttpError(String msgSuffix, boolean lenienet, String httpContentType, |
| 230 | + String bomEnc, String xmlGuessEnc, String xmlEnc, String defaultEncoding) { |
| 231 | + try { |
| 232 | + checkHttpEncoding("XmlStreamReaderException", lenienet, httpContentType, bomEnc, xmlGuessEnc, xmlEnc, defaultEncoding); |
| 233 | + fail("Expected XmlStreamReaderException"); |
| 234 | + } catch (XmlStreamReaderException e) { |
| 235 | + assertTrue("Msg Start: " + e.getMessage(), e.getMessage().startsWith("Invalid encoding")); |
| 236 | + assertTrue("Msg End: " + e.getMessage(), e.getMessage().endsWith(msgSuffix)); |
| 237 | + assertEquals("bomEnc", bomEnc, e.getBomEncoding()); |
| 238 | + assertEquals("xmlGuessEnc", xmlGuessEnc, e.getXmlGuessEncoding()); |
| 239 | + assertEquals("xmlEnc", xmlEnc, e.getXmlEncoding()); |
| 240 | + assertEquals("ContentTypeEncoding", XmlStreamReader.getContentTypeEncoding(httpContentType), |
| 241 | + e.getContentTypeEncoding()); |
| 242 | + assertEquals("ContentTypeMime", XmlStreamReader.getContentTypeMime(httpContentType), |
| 243 | + e.getContentTypeMime()); |
| 244 | + } catch (Exception e) { |
| 245 | + fail("Expected XmlStreamReaderException, but threw " + e); |
| 246 | + } |
| 247 | + } |
| 248 | + |
| 249 | + /** Mock {@link XmlStreamReader} implementation */ |
| 250 | + private static class MockXmlStreamReader extends XmlStreamReader { |
| 251 | + MockXmlStreamReader(String defaultEncoding) throws IOException { |
| 252 | + super(new ByteArrayInputStream("".getBytes()), null, true, defaultEncoding); |
| 253 | + } |
| 254 | + } |
| 255 | +} |
0 commit comments