This is an automated email from the ASF dual-hosted git repository.
garydgregory 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 afaeeac4 Improve codec edge-case coverage and PercentCodec Javadoc
afaeeac4 is described below
commit afaeeac40291476fd0d055ac71e7951002a7cc08
Author: Gary Gregory <[email protected]>
AuthorDate: Wed Nov 18 17:37:53 2026 -0500
Improve codec edge-case coverage and PercentCodec Javadoc
Replace the truncation test with parameterized assertions that preserve
exception-cause checks. Test invalid hex digits independently, cover
existing RFC1522 empty-content handling, and clarify PercentCodec
thread safety.
Assisted-By: OpenAI Codex
---
.../org/apache/commons/codec/net/PercentCodec.java | 5 +++--
.../apache/commons/codec/net/PercentCodecTest.java | 24 +++++++++++++---------
.../apache/commons/codec/net/RFC1522CodecTest.java | 7 +++++++
3 files changed, 24 insertions(+), 12 deletions(-)
diff --git a/src/main/java/org/apache/commons/codec/net/PercentCodec.java
b/src/main/java/org/apache/commons/codec/net/PercentCodec.java
index e01a6fc5..667ab09f 100644
--- a/src/main/java/org/apache/commons/codec/net/PercentCodec.java
+++ b/src/main/java/org/apache/commons/codec/net/PercentCodec.java
@@ -29,9 +29,10 @@ import org.apache.commons.codec.EncoderException;
* Implements the Percent-Encoding scheme, as described in HTTP 1.1
specification. For extensibility, an array of
* special US-ASCII characters can be specified in order to perform proper URI
encoding for the different parts
* of the URI.
+ *
* <p>
- * This class is immutable. It is also thread-safe besides using BitSet which
is not thread-safe, but its public
- * interface only call the access
+ * This class is immutable and thread-safe. Although it uses a {@link BitSet},
which is not thread-safe, the bit set is initialized during construction and is
not
+ * modified afterward.
* </p>
*
* @see <a
href="https://tools.ietf.org/html/rfc3986#section-2.1">Percent-Encoding</a>
diff --git a/src/test/java/org/apache/commons/codec/net/PercentCodecTest.java
b/src/test/java/org/apache/commons/codec/net/PercentCodecTest.java
index ece15c16..8c14fb4b 100644
--- a/src/test/java/org/apache/commons/codec/net/PercentCodecTest.java
+++ b/src/test/java/org/apache/commons/codec/net/PercentCodecTest.java
@@ -24,12 +24,13 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.nio.charset.StandardCharsets;
-import java.util.Arrays;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.EncoderException;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
/**
* Percent codec test cases.
@@ -68,16 +69,19 @@ class PercentCodecTest {
assertEquals(new String(decoded, StandardCharsets.UTF_8), input,
"Configurable PercentCodec decoding test");
}
- @Test
- void testDecodeInvalidEncodedResultDecoding() throws Exception {
- final String inputS = "\u03B1\u03B2";
+ @ParameterizedTest
+ @ValueSource(strings = { "%", "%2", "%CE%B1%CE%B" })
+ void testDecodeIncompletePercentSequences(final String input) {
+ final PercentCodec percentCodec = new PercentCodec();
+ final DecoderException exception =
assertThrows(DecoderException.class, () ->
percentCodec.decode(input.getBytes(StandardCharsets.UTF_8)));
+ assertTrue(exception.getCause() instanceof
ArrayIndexOutOfBoundsException);
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = { "%G2", "%2G" })
+ void testDecodeInvalidPercentSequences(final String input) {
final PercentCodec percentCodec = new PercentCodec();
- final byte[] encoded =
percentCodec.encode(inputS.getBytes(StandardCharsets.UTF_8));
- try {
- percentCodec.decode(Arrays.copyOf(encoded, encoded.length - 1));
// exclude one byte
- } catch (final Exception e) {
- assertTrue(DecoderException.class.isInstance(e) &&
ArrayIndexOutOfBoundsException.class.isInstance(e.getCause()));
- }
+ assertThrows(DecoderException.class, () ->
percentCodec.decode(input.getBytes(StandardCharsets.UTF_8)));
}
@Test
diff --git a/src/test/java/org/apache/commons/codec/net/RFC1522CodecTest.java
b/src/test/java/org/apache/commons/codec/net/RFC1522CodecTest.java
index c4f63a29..c9fbef25 100644
--- a/src/test/java/org/apache/commons/codec/net/RFC1522CodecTest.java
+++ b/src/test/java/org/apache/commons/codec/net/RFC1522CodecTest.java
@@ -61,6 +61,13 @@ class RFC1522CodecTest {
assertThrows(DecoderException.class, () -> new
RFC1522TestCodec().decodeText(text));
}
+ @Test
+ void testDecodeEmptyEncodedContent() throws Exception {
+ // Preserve the existing lenient handling of empty content, although
RFC 1522 requires at least one encoded-text character.
+ final RFC1522TestCodec codec = new RFC1522TestCodec();
+ assertEquals("", codec.decodeText("=?UTF-8?T??="));
+ }
+
@ParameterizedTest
// @formatter:off
@ValueSource(strings = {