This is an automated email from the ASF dual-hosted git repository.

sebb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git


The following commit(s) were added to refs/heads/master by this push:
     new 18fa7cd6 Test to show IO-815: not all encodings are matched
18fa7cd6 is described below

commit 18fa7cd6926616b4098be2723379e858ef92e432
Author: Sebb <s...@apache.org>
AuthorDate: Tue Oct 3 21:59:40 2023 +0100

    Test to show IO-815: not all encodings are matched
---
 .../commons/io/input/XmlStreamReaderTest.java      | 25 ++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/src/test/java/org/apache/commons/io/input/XmlStreamReaderTest.java 
b/src/test/java/org/apache/commons/io/input/XmlStreamReaderTest.java
index 555cf7ca..8f08e795 100644
--- a/src/test/java/org/apache/commons/io/input/XmlStreamReaderTest.java
+++ b/src/test/java/org/apache/commons/io/input/XmlStreamReaderTest.java
@@ -30,6 +30,7 @@ import java.io.OutputStreamWriter;
 import java.io.Writer;
 import java.net.URL;
 import java.net.URLConnection;
+import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Path;
@@ -37,6 +38,7 @@ import java.nio.file.Paths;
 import java.nio.file.StandardOpenOption;
 import java.text.MessageFormat;
 import java.util.HashMap;
+import java.util.Locale;
 import java.util.Map;
 
 import org.apache.commons.io.IOUtils;
@@ -571,6 +573,29 @@ public class XmlStreamReaderTest {
         testRawNoBomValid(UTF_8);
     }
 
+    private void parseCharset(String hdr, String enc) throws Exception {
+            try (InputStream stream = new 
ByteArrayInputStream(hdr.getBytes(StandardCharsets.UTF_8))) {
+                try (XmlStreamReader xml = new XmlStreamReader(stream)) {
+                    String getenc = xml.getEncoding();
+                    assertEquals(enc.toUpperCase(Locale.ROOT), getenc, enc);
+                }
+            };
+    }
+    @Test
+    public void testIO_815() throws Exception {
+        System.out.println(XmlStreamReader.ENCODING_PATTERN);
+        MessageFormat fmt = new MessageFormat("<?xml version=\"1.0\" 
encoding=''{0}''?>\n<root>text</root>");
+        for (final Map.Entry<String, Charset> entry : 
Charset.availableCharsets().entrySet()) {
+            String csName = entry.getKey();
+            String header = fmt.format(new Object[]{csName});
+            parseCharset(header, csName);
+            for (final String alias : entry.getValue().aliases()) {
+                header = fmt.format(new Object[]{alias});
+                parseCharset(header, alias);
+            }
+        }
+    }
+
     protected void testRawNoBomValid(final String encoding) throws Exception {
         InputStream is = getXmlInputStream("no-bom", XML1, encoding, encoding);
         XmlStreamReader xmlReader = new XmlStreamReader(is, false);

Reply via email to