Skip to content

Commit 50ff2d2

Browse files
CODEC-298 - Use Standard Charset object (#82)
1 parent 8ee878b commit 50ff2d2

4 files changed

Lines changed: 18 additions & 16 deletions

File tree

src/test/java/org/apache/commons/codec/CharsetsTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.commons.codec;
1919

2020
import java.nio.charset.Charset;
21+
import java.nio.charset.StandardCharsets;
2122

2223
import org.junit.Assert;
2324
import org.junit.Test;
@@ -33,7 +34,7 @@ public void testToCharset() {
3334
Assert.assertEquals(Charset.defaultCharset(), Charsets.toCharset((String) null));
3435
Assert.assertEquals(Charset.defaultCharset(), Charsets.toCharset((Charset) null));
3536
Assert.assertEquals(Charset.defaultCharset(), Charsets.toCharset(Charset.defaultCharset()));
36-
Assert.assertEquals(Charset.forName("UTF-8"), Charsets.toCharset(Charset.forName("UTF-8")));
37+
Assert.assertEquals(StandardCharsets.UTF_8, Charsets.toCharset(StandardCharsets.UTF_8));
3738
}
3839

3940
@SuppressWarnings("deprecation")

src/test/java/org/apache/commons/codec/digest/PureJavaCrc32Test.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.FileOutputStream;
2121
import java.io.PrintStream;
2222
import java.lang.reflect.Constructor;
23+
import java.nio.charset.StandardCharsets;
2324
import java.util.ArrayList;
2425
import java.util.List;
2526
import java.util.Properties;
@@ -51,7 +52,7 @@ public void testCorrectness() throws Exception {
5152

5253
checkOnBytes(new byte[] {40, 60, 97, -70}, false);
5354

54-
checkOnBytes("hello world!".getBytes("UTF-8"), false);
55+
checkOnBytes("hello world!".getBytes(StandardCharsets.UTF_8), false);
5556

5657
final Random random1 = new Random();
5758
final Random random2 = new Random();

src/test/java/org/apache/commons/codec/net/PercentCodecTest.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ public void testBasicEncodeDecode() throws Exception {
4141
final PercentCodec percentCodec = new PercentCodec();
4242
final String input = "abcdABCD";
4343
final byte[] encoded = percentCodec.encode(input.getBytes(StandardCharsets.UTF_8));
44-
final String encodedS = new String(encoded, "UTF-8");
44+
final String encodedS = new String(encoded, StandardCharsets.UTF_8);
4545
final byte[] decoded = percentCodec.decode(encoded);
46-
final String decodedS = new String(decoded, "UTF-8");
46+
final String decodedS = new String(decoded, StandardCharsets.UTF_8);
4747
assertEquals("Basic PercentCodec encoding test", input, encodedS);
4848
assertEquals("Basic PercentCodec decoding test", input, decodedS);
4949
}
@@ -60,19 +60,19 @@ public void testBasicSpace() throws Exception {
6060
@Test
6161
public void testConfigurablePercentEncoder() throws Exception {
6262
final String input = "abc123_-.*\u03B1\u03B2";
63-
final PercentCodec percentCodec = new PercentCodec("abcdef".getBytes("UTF-8"), false);
63+
final PercentCodec percentCodec = new PercentCodec("abcdef".getBytes(StandardCharsets.UTF_8), false);
6464
final byte[] encoded = percentCodec.encode(input.getBytes(StandardCharsets.UTF_8));
65-
final String encodedS = new String(encoded, "UTF-8");
65+
final String encodedS = new String(encoded, StandardCharsets.UTF_8);
6666
assertEquals("Configurable PercentCodec encoding test", "%61%62%63123_-.*%CE%B1%CE%B2", encodedS);
6767
final byte[] decoded = percentCodec.decode(encoded);
68-
assertEquals("Configurable PercentCodec decoding test", new String(decoded, "UTF-8"), input);
68+
assertEquals("Configurable PercentCodec decoding test", new String(decoded, StandardCharsets.UTF_8), input);
6969
}
7070

7171
@Test
7272
public void testDecodeInvalidEncodedResultDecoding() throws Exception {
7373
final String inputS = "\u03B1\u03B2";
7474
final PercentCodec percentCodec = new PercentCodec();
75-
final byte[] encoded = percentCodec.encode(inputS.getBytes("UTF-8"));
75+
final byte[] encoded = percentCodec.encode(inputS.getBytes(StandardCharsets.UTF_8));
7676
try {
7777
percentCodec.decode(Arrays.copyOf(encoded, encoded.length-1)); //exclude one byte
7878
} catch (final Exception e) {
@@ -110,7 +110,7 @@ public void testPercentEncoderDecoderWithNullOrEmptyInput() throws Exception {
110110
final PercentCodec percentCodec = new PercentCodec(null, true);
111111
assertNull("Null input value encoding test", percentCodec.encode(null));
112112
assertNull("Null input value decoding test", percentCodec.decode(null));
113-
final byte[] emptyInput = "".getBytes("UTF-8");
113+
final byte[] emptyInput = "".getBytes(StandardCharsets.UTF_8);
114114
assertEquals("Empty input value encoding test", percentCodec.encode(emptyInput), emptyInput);
115115
assertArrayEquals("Empty input value decoding test", percentCodec.decode(emptyInput), emptyInput);
116116
}
@@ -120,20 +120,20 @@ public void testPercentEncoderDecoderWithPlusForSpace() throws Exception {
120120
final String input = "a b c d";
121121
final PercentCodec percentCodec = new PercentCodec(null, true);
122122
final byte[] encoded = percentCodec.encode(input.getBytes(StandardCharsets.UTF_8));
123-
final String encodedS = new String(encoded, "UTF-8");
123+
final String encodedS = new String(encoded, StandardCharsets.UTF_8);
124124
assertEquals("PercentCodec plus for space encoding test", "a+b+c+d", encodedS);
125125
final byte[] decode = percentCodec.decode(encoded);
126-
assertEquals("PercentCodec plus for space decoding test", new String(decode, "UTF-8"), input);
126+
assertEquals("PercentCodec plus for space decoding test", new String(decode, StandardCharsets.UTF_8), input);
127127
}
128128

129129
@Test
130130
public void testSafeCharEncodeDecodeObject() throws Exception {
131131
final PercentCodec percentCodec = new PercentCodec(null, true);
132132
final String input = "abc123_-.*";
133133
final Object encoded = percentCodec.encode((Object) input.getBytes(StandardCharsets.UTF_8));
134-
final String encodedS = new String((byte[]) encoded, "UTF-8");
134+
final String encodedS = new String((byte[]) encoded, StandardCharsets.UTF_8);
135135
final Object decoded = percentCodec.decode(encoded);
136-
final String decodedS = new String((byte[]) decoded, "UTF-8");
136+
final String decodedS = new String((byte[]) decoded, StandardCharsets.UTF_8);
137137
assertEquals("Basic PercentCodec safe char encoding test", input, encodedS);
138138
assertEquals("Basic PercentCodec safe char decoding test", input, decodedS);
139139
}
@@ -143,9 +143,9 @@ public void testUnsafeCharEncodeDecode() throws Exception {
143143
final PercentCodec percentCodec = new PercentCodec();
144144
final String input = "\u03B1\u03B2\u03B3\u03B4\u03B5\u03B6% ";
145145
final byte[] encoded = percentCodec.encode(input.getBytes(StandardCharsets.UTF_8));
146-
final String encodedS = new String(encoded, "UTF-8");
146+
final String encodedS = new String(encoded, StandardCharsets.UTF_8);
147147
final byte[] decoded = percentCodec.decode(encoded);
148-
final String decodedS = new String(decoded, "UTF-8");
148+
final String decodedS = new String(decoded, StandardCharsets.UTF_8);
149149
assertEquals("Basic PercentCodec unsafe char encoding test", "%CE%B1%CE%B2%CE%B3%CE%B4%CE%B5%CE%B6%25 ", encodedS);
150150
assertEquals("Basic PercentCodec unsafe char decoding test", input, decodedS);
151151
}

src/test/java/org/apache/commons/codec/net/URLCodecTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public void testDecodeInvalid() throws Exception {
164164
public void testDecodeInvalidContent() throws UnsupportedEncodingException, DecoderException {
165165
final String ch_msg = constructString(SWISS_GERMAN_STUFF_UNICODE);
166166
final URLCodec urlCodec = new URLCodec();
167-
final byte[] input = ch_msg.getBytes("ISO-8859-1");
167+
final byte[] input = ch_msg.getBytes(StandardCharsets.ISO_8859_1);
168168
final byte[] output = urlCodec.decode(input);
169169
assertEquals(input.length, output.length);
170170
for (int i = 0; i < input.length; i++) {

0 commit comments

Comments
 (0)