You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/12/10 17:52:09 UTC
(commons-codec) branch master updated: Use constant instead of magic string
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-codec.git
The following commit(s) were added to refs/heads/master by this push:
new 92974e6e Use constant instead of magic string
92974e6e is described below
commit 92974e6ed0b887b674e89b69c3075afee093b06f
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Dec 10 12:52:05 2023 -0500
Use constant instead of magic string
Parameterize some tests
---
.../org/apache/commons/codec/CharEncoding.java | 13 +++++-----
.../org/apache/commons/codec/CharsetsTest.java | 14 +++++++++++
.../org/apache/commons/codec/binary/HexTest.java | 29 ++++++++++------------
.../commons/codec/binary/StringUtilsTest.java | 4 +--
4 files changed, 36 insertions(+), 24 deletions(-)
diff --git a/src/main/java/org/apache/commons/codec/CharEncoding.java b/src/main/java/org/apache/commons/codec/CharEncoding.java
index c751bf7a..d1666d47 100644
--- a/src/main/java/org/apache/commons/codec/CharEncoding.java
+++ b/src/main/java/org/apache/commons/codec/CharEncoding.java
@@ -18,6 +18,7 @@
package org.apache.commons.codec;
import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
/**
* Character encoding names required of every implementation of the Java platform.
@@ -65,7 +66,7 @@ public class CharEncoding {
*
* @see Charset
*/
- public static final String ISO_8859_1 = "ISO-8859-1";
+ public static final String ISO_8859_1 = StandardCharsets.ISO_8859_1.name();
/**
* 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 {
*
* @see Charset
*/
- public static final String US_ASCII = "US-ASCII";
+ public static final String US_ASCII = StandardCharsets.US_ASCII.name();
/**
* Sixteen-bit Unicode Transformation Format, The byte order specified by a mandatory initial byte-order mark
@@ -86,7 +87,7 @@ public class CharEncoding {
*
* @see Charset
*/
- public static final String UTF_16 = "UTF-16";
+ public static final String UTF_16 = StandardCharsets.UTF_16.name();
/**
* Sixteen-bit Unicode Transformation Format, big-endian byte order.
@@ -96,7 +97,7 @@ public class CharEncoding {
*
* @see Charset
*/
- public static final String UTF_16BE = "UTF-16BE";
+ public static final String UTF_16BE = StandardCharsets.UTF_16BE.name();
/**
* Sixteen-bit Unicode Transformation Format, little-endian byte order.
@@ -106,7 +107,7 @@ public class CharEncoding {
*
* @see Charset
*/
- public static final String UTF_16LE = "UTF-16LE";
+ public static final String UTF_16LE = StandardCharsets.UTF_16LE.name();
/**
* Eight-bit Unicode Transformation Format.
@@ -116,5 +117,5 @@ public class CharEncoding {
*
* @see Charset
*/
- public static final String UTF_8 = "UTF-8";
+ public static final String UTF_8 = StandardCharsets.UTF_8.name();
}
diff --git a/src/test/java/org/apache/commons/codec/CharsetsTest.java b/src/test/java/org/apache/commons/codec/CharsetsTest.java
index 834413c4..2a0b99ad 100644
--- a/src/test/java/org/apache/commons/codec/CharsetsTest.java
+++ b/src/test/java/org/apache/commons/codec/CharsetsTest.java
@@ -21,7 +21,11 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
+import java.util.Collection;
+import java.util.SortedSet;
+import java.util.TreeSet;
+import org.apache.commons.io.Charsets;
import org.junit.jupiter.api.Test;
/**
@@ -29,6 +33,16 @@ import org.junit.jupiter.api.Test;
*/
public class CharsetsTest {
+ private static final TreeSet<String> AVAILABLE_CHARSET_NAMES = new TreeSet<>(Charset.availableCharsets().keySet());
+
+ public static SortedSet<String> getAvailableCharsetNames() {
+ return AVAILABLE_CHARSET_NAMES;
+ }
+
+ public static Collection<Charset> getRequiredCharsets() {
+ return Charsets.requiredCharsets().values();
+ }
+
@SuppressWarnings("deprecation")
@Test
public void testIso8859_1() {
diff --git a/src/test/java/org/apache/commons/codec/binary/HexTest.java b/src/test/java/org/apache/commons/codec/binary/HexTest.java
index e0775183..d41d57a2 100644
--- a/src/test/java/org/apache/commons/codec/binary/HexTest.java
+++ b/src/test/java/org/apache/commons/codec/binary/HexTest.java
@@ -34,6 +34,8 @@ import java.util.concurrent.ThreadLocalRandom;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.EncoderException;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
/**
* Tests {@link org.apache.commons.codec.binary.Hex}.
@@ -143,20 +145,13 @@ public class HexTest {
}
}
- @Test
- public void testCustomCharset() throws UnsupportedEncodingException, DecoderException {
- for (final String name : Charset.availableCharsets().keySet()) {
- testCustomCharset(name, "testCustomCharset");
- }
- }
-
/**
* @param name
* @param parent
* @throws UnsupportedEncodingException
* @throws DecoderException
*/
- private void testCustomCharset(final String name, final String parent) throws UnsupportedEncodingException,
+ private void testCharset(final String name, final String parent) throws UnsupportedEncodingException,
DecoderException {
if (!charsetSanityCheck(name)) {
return;
@@ -190,6 +185,12 @@ public class HexTest {
assertEquals(sourceString, actualStringFromBytes, name);
}
+ @ParameterizedTest
+ @MethodSource("org.apache.commons.codec.CharsetsTest#getAvailableCharsetNames()")
+ public void testCustomCharset(final String name) throws UnsupportedEncodingException, DecoderException {
+ testCharset(name, "testCustomCharset");
+ }
+
@Test
public void testCustomCharsetBadName() {
assertThrows(UnsupportedCharsetException.class, () -> new Hex(BAD_ENCODING_NAME));
@@ -663,13 +664,9 @@ public class HexTest {
assertEquals(StandardCharsets.UTF_8.name(), new Hex(StandardCharsets.UTF_8).getCharsetName());
}
- @Test
- public void testRequiredCharset() throws UnsupportedEncodingException, DecoderException {
- testCustomCharset("UTF-8", "testRequiredCharset");
- testCustomCharset("UTF-16", "testRequiredCharset");
- testCustomCharset("UTF-16BE", "testRequiredCharset");
- testCustomCharset("UTF-16LE", "testRequiredCharset");
- testCustomCharset("US-ASCII", "testRequiredCharset");
- testCustomCharset("ISO8859_1", "testRequiredCharset");
+ @ParameterizedTest
+ @MethodSource("org.apache.commons.codec.CharsetsTest#getRequiredCharsets()")
+ public void testRequiredCharset(final Charset charset) throws UnsupportedEncodingException, DecoderException {
+ testCharset(charset.name(), "testRequiredCharset");
}
}
diff --git a/src/test/java/org/apache/commons/codec/binary/StringUtilsTest.java b/src/test/java/org/apache/commons/codec/binary/StringUtilsTest.java
index 83e3566e..616ca16e 100644
--- a/src/test/java/org/apache/commons/codec/binary/StringUtilsTest.java
+++ b/src/test/java/org/apache/commons/codec/binary/StringUtilsTest.java
@@ -154,7 +154,7 @@ public class StringUtilsTest {
@Test
public void testGetBytesUtf8() throws UnsupportedEncodingException {
- final String charsetName = "UTF-8";
+ final String charsetName = StandardCharsets.UTF_8.name();
testGetBytesUnchecked(charsetName);
final byte[] expected = STRING_FIXTURE.getBytes(charsetName);
final byte[] actual = StringUtils.getBytesUtf8(STRING_FIXTURE);
@@ -234,7 +234,7 @@ public class StringUtilsTest {
@Test
public void testNewStringUtf8() throws UnsupportedEncodingException {
- final String charsetName = "UTF-8";
+ final String charsetName = StandardCharsets.UTF_8.name();
testNewString(charsetName);
final String expected = new String(BYTES_FIXTURE, charsetName);
final String actual = StringUtils.newStringUtf8(BYTES_FIXTURE);