Skip to content

Commit 92974e6

Browse files
committed
Use constant instead of magic string
Parameterize some tests
1 parent d2c5146 commit 92974e6

4 files changed

Lines changed: 36 additions & 24 deletions

File tree

src/main/java/org/apache/commons/codec/CharEncoding.java

Lines changed: 7 additions & 6 deletions
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
/**
2324
* Character encoding names required of every implementation of the Java platform.
@@ -65,7 +66,7 @@ public class CharEncoding {
6566
*
6667
* @see Charset
6768
*/
68-
public static final String ISO_8859_1 = "ISO-8859-1";
69+
public static final String ISO_8859_1 = StandardCharsets.ISO_8859_1.name();
6970

7071
/**
7172
* Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block of the Unicode character set.
@@ -75,7 +76,7 @@ public class CharEncoding {
7576
*
7677
* @see Charset
7778
*/
78-
public static final String US_ASCII = "US-ASCII";
79+
public static final String US_ASCII = StandardCharsets.US_ASCII.name();
7980

8081
/**
8182
* Sixteen-bit Unicode Transformation Format, The byte order specified by a mandatory initial byte-order mark
@@ -86,7 +87,7 @@ public class CharEncoding {
8687
*
8788
* @see Charset
8889
*/
89-
public static final String UTF_16 = "UTF-16";
90+
public static final String UTF_16 = StandardCharsets.UTF_16.name();
9091

9192
/**
9293
* Sixteen-bit Unicode Transformation Format, big-endian byte order.
@@ -96,7 +97,7 @@ public class CharEncoding {
9697
*
9798
* @see Charset
9899
*/
99-
public static final String UTF_16BE = "UTF-16BE";
100+
public static final String UTF_16BE = StandardCharsets.UTF_16BE.name();
100101

101102
/**
102103
* Sixteen-bit Unicode Transformation Format, little-endian byte order.
@@ -106,7 +107,7 @@ public class CharEncoding {
106107
*
107108
* @see Charset
108109
*/
109-
public static final String UTF_16LE = "UTF-16LE";
110+
public static final String UTF_16LE = StandardCharsets.UTF_16LE.name();
110111

111112
/**
112113
* Eight-bit Unicode Transformation Format.
@@ -116,5 +117,5 @@ public class CharEncoding {
116117
*
117118
* @see Charset
118119
*/
119-
public static final String UTF_8 = "UTF-8";
120+
public static final String UTF_8 = StandardCharsets.UTF_8.name();
120121
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,28 @@
2121

2222
import java.nio.charset.Charset;
2323
import java.nio.charset.StandardCharsets;
24+
import java.util.Collection;
25+
import java.util.SortedSet;
26+
import java.util.TreeSet;
2427

28+
import org.apache.commons.io.Charsets;
2529
import org.junit.jupiter.api.Test;
2630

2731
/**
2832
* Sanity checks for {@link Charsets}.
2933
*/
3034
public class CharsetsTest {
3135

36+
private static final TreeSet<String> AVAILABLE_CHARSET_NAMES = new TreeSet<>(Charset.availableCharsets().keySet());
37+
38+
public static SortedSet<String> getAvailableCharsetNames() {
39+
return AVAILABLE_CHARSET_NAMES;
40+
}
41+
42+
public static Collection<Charset> getRequiredCharsets() {
43+
return Charsets.requiredCharsets().values();
44+
}
45+
3246
@SuppressWarnings("deprecation")
3347
@Test
3448
public void testIso8859_1() {

src/test/java/org/apache/commons/codec/binary/HexTest.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import org.apache.commons.codec.DecoderException;
3535
import org.apache.commons.codec.EncoderException;
3636
import org.junit.jupiter.api.Test;
37+
import org.junit.jupiter.params.ParameterizedTest;
38+
import org.junit.jupiter.params.provider.MethodSource;
3739

3840
/**
3941
* Tests {@link org.apache.commons.codec.binary.Hex}.
@@ -143,20 +145,13 @@ private void log(final Throwable t) {
143145
}
144146
}
145147

146-
@Test
147-
public void testCustomCharset() throws UnsupportedEncodingException, DecoderException {
148-
for (final String name : Charset.availableCharsets().keySet()) {
149-
testCustomCharset(name, "testCustomCharset");
150-
}
151-
}
152-
153148
/**
154149
* @param name
155150
* @param parent
156151
* @throws UnsupportedEncodingException
157152
* @throws DecoderException
158153
*/
159-
private void testCustomCharset(final String name, final String parent) throws UnsupportedEncodingException,
154+
private void testCharset(final String name, final String parent) throws UnsupportedEncodingException,
160155
DecoderException {
161156
if (!charsetSanityCheck(name)) {
162157
return;
@@ -190,6 +185,12 @@ private void testCustomCharset(final String name, final String parent) throws Un
190185
assertEquals(sourceString, actualStringFromBytes, name);
191186
}
192187

188+
@ParameterizedTest
189+
@MethodSource("org.apache.commons.codec.CharsetsTest#getAvailableCharsetNames()")
190+
public void testCustomCharset(final String name) throws UnsupportedEncodingException, DecoderException {
191+
testCharset(name, "testCustomCharset");
192+
}
193+
193194
@Test
194195
public void testCustomCharsetBadName() {
195196
assertThrows(UnsupportedCharsetException.class, () -> new Hex(BAD_ENCODING_NAME));
@@ -663,13 +664,9 @@ public void testGetCharsetName() {
663664
assertEquals(StandardCharsets.UTF_8.name(), new Hex(StandardCharsets.UTF_8).getCharsetName());
664665
}
665666

666-
@Test
667-
public void testRequiredCharset() throws UnsupportedEncodingException, DecoderException {
668-
testCustomCharset("UTF-8", "testRequiredCharset");
669-
testCustomCharset("UTF-16", "testRequiredCharset");
670-
testCustomCharset("UTF-16BE", "testRequiredCharset");
671-
testCustomCharset("UTF-16LE", "testRequiredCharset");
672-
testCustomCharset("US-ASCII", "testRequiredCharset");
673-
testCustomCharset("ISO8859_1", "testRequiredCharset");
667+
@ParameterizedTest
668+
@MethodSource("org.apache.commons.codec.CharsetsTest#getRequiredCharsets()")
669+
public void testRequiredCharset(final Charset charset) throws UnsupportedEncodingException, DecoderException {
670+
testCharset(charset.name(), "testRequiredCharset");
674671
}
675672
}

src/test/java/org/apache/commons/codec/binary/StringUtilsTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public void testGetBytesUtf16Le() throws UnsupportedEncodingException {
154154

155155
@Test
156156
public void testGetBytesUtf8() throws UnsupportedEncodingException {
157-
final String charsetName = "UTF-8";
157+
final String charsetName = StandardCharsets.UTF_8.name();
158158
testGetBytesUnchecked(charsetName);
159159
final byte[] expected = STRING_FIXTURE.getBytes(charsetName);
160160
final byte[] actual = StringUtils.getBytesUtf8(STRING_FIXTURE);
@@ -234,7 +234,7 @@ public void testNewStringUtf16Le() throws UnsupportedEncodingException {
234234

235235
@Test
236236
public void testNewStringUtf8() throws UnsupportedEncodingException {
237-
final String charsetName = "UTF-8";
237+
final String charsetName = StandardCharsets.UTF_8.name();
238238
testNewString(charsetName);
239239
final String expected = new String(BYTES_FIXTURE, charsetName);
240240
final String actual = StringUtils.newStringUtf8(BYTES_FIXTURE);

0 commit comments

Comments
 (0)