UnsupportedEncodingException when opening an ISO-8859-1 XML stream with Turkish as the default locale
improve tests by copying parts of the SystemDefaults rule from commons-lang
add changes.xml entry
MODIFY
src/changes/changes.xml
src/changes/changes.xml
@@ -50,6 +50,9 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="IO-553" dev="ggregory" type="add">
Add org.apache.commons.io.FilenameUtils.isIllegalWindowsFileName(char).
</action>
+ <action issue="IO-557" dev="pschumacher" type="fix" due-to="luccioman">
+ Perform locale independent upper case conversions.
+ </action>
</release>
<release version="2.6" date="2017-10-15" description="Java 7 required, Java 9 supported.">

MODIFY
src/test/java/org/apache/commons/io/input/XmlStreamReaderTest.java
src/test/java/org/apache/commons/io/input/XmlStreamReaderTest.java
import java.util.Map;
import org.apache.commons.io.IOUtils;
+import org.apache.commons.io.testtools.SystemDefaults;
+import org.apache.commons.io.testtools.SystemDefaultsSwitch;
+import org.junit.Rule;
import org.junit.Test;
public class XmlStreamReaderTest {
private static final String XML2 = "xml-prolog";
private static final String XML1 = "xml";
+ @Rule
+ public SystemDefaultsSwitch locale = new SystemDefaultsSwitch();
+
protected void _testRawNoBomValid(final String encoding) throws Exception {
InputStream is = getXmlStream("no-bom", XML1, encoding, encoding);
XmlStreamReader xmlReader = new XmlStreamReader(is, false);
@@ -277,14 +283,10 @@ public void testHttp() throws Exception {
_testHttpLenient("text/html;charset=UTF-32BE", "no-bom", "US-ASCII", "UTF-8", "UTF-8");
}
- /**
- * Check lower case encoding names are properly handled. Should be successfull
- * with any system default locale, notably with Turkish language
- * (-Duser.language=tr JVM parameter), which has specific rules to convert dotted and dottless
- * i character.
- */
+ // Turkish language has specific rules to convert dotted and dottless i character.
@Test
- public void testLowerCaseEncoding() throws Exception {
+ @SystemDefaults(locale="tr")
+ public void testLowerCaseEncodingWithTurkishLocale_IO_557() throws Exception {
final String[] encodings = { "iso8859-1", "us-ascii", "utf-8" };
for (final String encoding : encodings) {
final String xml = getXML("no-bom", XML3, encoding, encoding);

MODIFY
src/test/java/org/apache/commons/io/output/XmlStreamWriterTest.java
src/test/java/org/apache/commons/io/output/XmlStreamWriterTest.java
import java.io.IOException;
import java.util.Arrays;
+import org.apache.commons.io.testtools.SystemDefaults;
+import org.apache.commons.io.testtools.SystemDefaultsSwitch;
+import org.junit.Rule;
import org.junit.Test;
/**
private static final String TEXT_UNICODE = TEXT_LATIN1 + ", " + TEXT_LATIN7
+ ", " + TEXT_LATIN15 + ", " + TEXT_EUC_JP;
+ @Rule
+ public SystemDefaultsSwitch locale = new SystemDefaultsSwitch();
+
private static String createXmlContent(final String text, final String encoding) {
String xmlDecl = "<?xml version=\"1.0\"?>";
if (encoding != null) {
@@ -102,14 +108,10 @@ public void testDefaultEncoding() throws IOException {
checkXmlWriter(TEXT_UNICODE, null, "ISO-8859-1");
}
- /**
- * Check lower case encoding names are properly handled. Should be successfull
- * with any system default locale, notably with Turkish language
- * (-Duser.language=tr JVM parameter), which has specific rules to convert
- * dotted and dottless i character.
- */
+ // Turkish language has specific rules to convert dotted and dottless i character.
@Test
- public void testLowerCaseEncoding() throws IOException {
+ @SystemDefaults(locale="tr")
+ public void testLowerCaseEncoding_IO_557() throws IOException {
checkXmlWriter(TEXT_UNICODE, "utf-8");
checkXmlWriter(TEXT_LATIN1, "iso-8859-1");
checkXmlWriter(TEXT_LATIN7, "iso-8859-7");

ADD
src/test/java/org/apache/commons/io/testtools/SystemDefaults.java
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+package org.apache.commons.io.testtools;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Annotation used with {@link SystemDefaults} that specifies the
+ * system default Locale to be used in a test method.
+ */
+@Target(ElementType.METHOD)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface SystemDefaults {
+ /**
+ * The name of the Locale to be used while running a test method
+ */
+ String locale() default "";
+}

ADD
src/test/java/org/apache/commons/io/testtools/SystemDefaultsSwitch.java
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+package org.apache.commons.io.testtools;
+
+import java.util.Locale;
+
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+/**
+ * Test Rule used with {@link SystemDefaults} annotation that sets and restores the system default Locale.
+ *
+ * <p>
+ * Set up tests to use alternate system default Locale by creating an instance of this rule
+ * and annotating the test method with {@link SystemDefaults}
+ * </p>
+ *
+ * <pre>
+ * public class SystemDefaultsDependentTest {
+ *
+ * {@literal @}Rule
+ * public SystemDefaultsSwitch locale = new SystemDefaultsSwitch();
+ *
+ * {@literal @}Test
+ * {@literal @}SystemDefaults(local="zh_CN")
+ * public void testWithSimplifiedChinaDefaultLocale() {
+ * // Locale.getDefault() will return Locale.CHINA until the end of this test method
+ * }
+ * }
+ * </pre>
+ */
+public class SystemDefaultsSwitch implements TestRule {
+
+ @Override
+ public Statement apply(final Statement stmt, final Description description) {
+ final SystemDefaults defaults = description.getAnnotation(SystemDefaults.class);
+ if (defaults == null) {
+ return stmt;
+ }
+ return applyLocale(defaults, stmt);
+ }
+
+ private Statement applyLocale(final SystemDefaults defaults, final Statement stmt) {
+ if (defaults.locale().isEmpty()) {
+ return stmt;
+ }
+
+ final Locale newLocale = Locale.forLanguageTag(defaults.locale());
+
+ return new Statement() {
+ @Override
+ public void evaluate() throws Throwable {
+ final Locale save = Locale.getDefault();
+ try {
+ Locale.setDefault(newLocale);
+ stmt.evaluate();
+ } finally {
+ Locale.setDefault(save);
+ }
+ }
+ };
+ }
+
+}

IO-557: UnsupportedEncodingException when opening an ISO-8859-1 XML stream with Turkish as the default locale

improve tests by copying parts of the SystemDefaults rule from commons-lang

add changes.xml entry