Skip to content

Commit ed15b09

Browse files
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
1 parent d19259a commit ed15b09

5 files changed

Lines changed: 138 additions & 16 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ The <action> type attribute can be add,update,fix,remove.
5050
<action issue="IO-553" dev="ggregory" type="add">
5151
Add org.apache.commons.io.FilenameUtils.isIllegalWindowsFileName(char).
5252
</action>
53+
<action issue="IO-557" dev="pschumacher" type="fix" due-to="luccioman">
54+
Perform locale independent upper case conversions.
55+
</action>
5356
</release>
5457

5558
<release version="2.6" date="2017-10-15" description="Java 7 required, Java 9 supported.">

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
import java.util.Map;
3232

3333
import org.apache.commons.io.IOUtils;
34+
import org.apache.commons.io.testtools.SystemDefaults;
35+
import org.apache.commons.io.testtools.SystemDefaultsSwitch;
36+
import org.junit.Rule;
3437
import org.junit.Test;
3538

3639
public class XmlStreamReaderTest {
@@ -40,6 +43,9 @@ public class XmlStreamReaderTest {
4043
private static final String XML2 = "xml-prolog";
4144
private static final String XML1 = "xml";
4245

46+
@Rule
47+
public SystemDefaultsSwitch locale = new SystemDefaultsSwitch();
48+
4349
protected void _testRawNoBomValid(final String encoding) throws Exception {
4450
InputStream is = getXmlStream("no-bom", XML1, encoding, encoding);
4551
XmlStreamReader xmlReader = new XmlStreamReader(is, false);
@@ -276,15 +282,11 @@ public void testHttp() throws Exception {
276282
_testHttpLenient("text/html;charset=UTF-16BE", "no-bom", "US-ASCII", "UTF-8", "UTF-8");
277283
_testHttpLenient("text/html;charset=UTF-32BE", "no-bom", "US-ASCII", "UTF-8", "UTF-8");
278284
}
279-
280-
/**
281-
* Check lower case encoding names are properly handled. Should be successfull
282-
* with any system default locale, notably with Turkish language
283-
* (-Duser.language=tr JVM parameter), which has specific rules to convert dotted and dottless
284-
* i character.
285-
*/
285+
286+
// Turkish language has specific rules to convert dotted and dottless i character.
286287
@Test
287-
public void testLowerCaseEncoding() throws Exception {
288+
@SystemDefaults(locale="tr")
289+
public void testLowerCaseEncodingWithTurkishLocale_IO_557() throws Exception {
288290
final String[] encodings = { "iso8859-1", "us-ascii", "utf-8" };
289291
for (final String encoding : encodings) {
290292
final String xml = getXML("no-bom", XML3, encoding, encoding);

src/test/java/org/apache/commons/io/output/XmlStreamWriterTest.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
import java.io.IOException;
2323
import java.util.Arrays;
2424

25+
import org.apache.commons.io.testtools.SystemDefaults;
26+
import org.apache.commons.io.testtools.SystemDefaultsSwitch;
27+
import org.junit.Rule;
2528
import org.junit.Test;
2629

2730
/**
@@ -40,6 +43,9 @@ public class XmlStreamWriterTest {
4043
private static final String TEXT_UNICODE = TEXT_LATIN1 + ", " + TEXT_LATIN7
4144
+ ", " + TEXT_LATIN15 + ", " + TEXT_EUC_JP;
4245

46+
@Rule
47+
public SystemDefaultsSwitch locale = new SystemDefaultsSwitch();
48+
4349
private static String createXmlContent(final String text, final String encoding) {
4450
String xmlDecl = "<?xml version=\"1.0\"?>";
4551
if (encoding != null) {
@@ -101,15 +107,11 @@ public void testDefaultEncoding() throws IOException {
101107
checkXmlWriter(TEXT_UNICODE, null, "UTF-16BE");
102108
checkXmlWriter(TEXT_UNICODE, null, "ISO-8859-1");
103109
}
104-
105-
/**
106-
* Check lower case encoding names are properly handled. Should be successfull
107-
* with any system default locale, notably with Turkish language
108-
* (-Duser.language=tr JVM parameter), which has specific rules to convert
109-
* dotted and dottless i character.
110-
*/
110+
111+
// Turkish language has specific rules to convert dotted and dottless i character.
111112
@Test
112-
public void testLowerCaseEncoding() throws IOException {
113+
@SystemDefaults(locale="tr")
114+
public void testLowerCaseEncoding_IO_557() throws IOException {
113115
checkXmlWriter(TEXT_UNICODE, "utf-8");
114116
checkXmlWriter(TEXT_LATIN1, "iso-8859-1");
115117
checkXmlWriter(TEXT_LATIN7, "iso-8859-7");
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
18+
package org.apache.commons.io.testtools;
19+
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
25+
/**
26+
* Annotation used with {@link SystemDefaults} that specifies the
27+
* system default Locale to be used in a test method.
28+
*/
29+
@Target(ElementType.METHOD)
30+
@Retention(RetentionPolicy.RUNTIME)
31+
public @interface SystemDefaults {
32+
/**
33+
* The name of the Locale to be used while running a test method
34+
*/
35+
String locale() default "";
36+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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.testtools;
18+
19+
import java.util.Locale;
20+
21+
import org.junit.rules.TestRule;
22+
import org.junit.runner.Description;
23+
import org.junit.runners.model.Statement;
24+
25+
/**
26+
* Test Rule used with {@link SystemDefaults} annotation that sets and restores the system default Locale.
27+
*
28+
* <p>
29+
* Set up tests to use alternate system default Locale by creating an instance of this rule
30+
* and annotating the test method with {@link SystemDefaults}
31+
* </p>
32+
*
33+
* <pre>
34+
* public class SystemDefaultsDependentTest {
35+
*
36+
* {@literal @}Rule
37+
* public SystemDefaultsSwitch locale = new SystemDefaultsSwitch();
38+
*
39+
* {@literal @}Test
40+
* {@literal @}SystemDefaults(local="zh_CN")
41+
* public void testWithSimplifiedChinaDefaultLocale() {
42+
* // Locale.getDefault() will return Locale.CHINA until the end of this test method
43+
* }
44+
* }
45+
* </pre>
46+
*/
47+
public class SystemDefaultsSwitch implements TestRule {
48+
49+
@Override
50+
public Statement apply(final Statement stmt, final Description description) {
51+
final SystemDefaults defaults = description.getAnnotation(SystemDefaults.class);
52+
if (defaults == null) {
53+
return stmt;
54+
}
55+
return applyLocale(defaults, stmt);
56+
}
57+
58+
private Statement applyLocale(final SystemDefaults defaults, final Statement stmt) {
59+
if (defaults.locale().isEmpty()) {
60+
return stmt;
61+
}
62+
63+
final Locale newLocale = Locale.forLanguageTag(defaults.locale());
64+
65+
return new Statement() {
66+
@Override
67+
public void evaluate() throws Throwable {
68+
final Locale save = Locale.getDefault();
69+
try {
70+
Locale.setDefault(newLocale);
71+
stmt.evaluate();
72+
} finally {
73+
Locale.setDefault(save);
74+
}
75+
}
76+
};
77+
}
78+
79+
}

0 commit comments

Comments
 (0)