From 49ec507e0fbb4c32d4a271c5bf10b557935fe8af Mon Sep 17 00:00:00 2001 From: sahvx655-wq Date: Mon, 15 Jun 2026 21:16:52 +0530 Subject: [PATCH 1/3] fix ISIN country-code check using untrimmed input --- .../commons/validator/routines/ISINValidator.java | 8 ++------ .../validator/routines/ISINValidatorTest.java | 12 ++++++++++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/apache/commons/validator/routines/ISINValidator.java b/src/main/java/org/apache/commons/validator/routines/ISINValidator.java index ce1c88fa9..093cf930f 100644 --- a/src/main/java/org/apache/commons/validator/routines/ISINValidator.java +++ b/src/main/java/org/apache/commons/validator/routines/ISINValidator.java @@ -460,11 +460,7 @@ private boolean checkCode(final String code) { * code, otherwise {@code false}. */ public boolean isValid(final String code) { - final boolean valid = VALIDATOR.isValid(code); - if (valid && checkCountryCode) { - return checkCode(code.substring(0, 2)); - } - return valid; + return validate(code) != null; } /** @@ -476,7 +472,7 @@ public boolean isValid(final String code) { public Object validate(final String code) { final Object validate = VALIDATOR.validate(code); if (validate != null && checkCountryCode) { - return checkCode(code.substring(0, 2)) ? validate : null; + return checkCode(validate.toString().substring(0, 2)) ? validate : null; } return validate; } diff --git a/src/test/java/org/apache/commons/validator/routines/ISINValidatorTest.java b/src/test/java/org/apache/commons/validator/routines/ISINValidatorTest.java index 758c7671b..eab94da2e 100644 --- a/src/test/java/org/apache/commons/validator/routines/ISINValidatorTest.java +++ b/src/test/java/org/apache/commons/validator/routines/ISINValidatorTest.java @@ -16,6 +16,7 @@ */ package org.apache.commons.validator.routines; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -110,4 +111,15 @@ void testIsValidTrue() { } } + @Test + void testValidWithSurroundingWhitespaceCountryCode() { + // The underlying CodeValidator trims the input, so the country code must be + // derived from the trimmed code. Otherwise a valid ISIN with leading whitespace + // is accepted without the country check but rejected with it. + final String leading = " US0378331005"; + assertTrue(VALIDATOR_FALSE.isValid(leading), leading); + assertTrue(VALIDATOR_TRUE.isValid(leading), leading); + assertEquals("US0378331005", VALIDATOR_TRUE.validate(leading)); + } + } From 652fbb024189195b1edfaf2cb583d4f64c747e15 Mon Sep 17 00:00:00 2001 From: sahvx655-wq Date: Tue, 16 Jun 2026 10:49:40 +0530 Subject: [PATCH 2/3] Cover trailing and surrounding whitespace in ISIN country-code test --- .../commons/validator/routines/ISINValidatorTest.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/test/java/org/apache/commons/validator/routines/ISINValidatorTest.java b/src/test/java/org/apache/commons/validator/routines/ISINValidatorTest.java index eab94da2e..1a99788b7 100644 --- a/src/test/java/org/apache/commons/validator/routines/ISINValidatorTest.java +++ b/src/test/java/org/apache/commons/validator/routines/ISINValidatorTest.java @@ -114,12 +114,13 @@ void testIsValidTrue() { @Test void testValidWithSurroundingWhitespaceCountryCode() { // The underlying CodeValidator trims the input, so the country code must be - // derived from the trimmed code. Otherwise a valid ISIN with leading whitespace + // derived from the trimmed code. Otherwise a valid ISIN with surrounding whitespace // is accepted without the country check but rejected with it. - final String leading = " US0378331005"; - assertTrue(VALIDATOR_FALSE.isValid(leading), leading); - assertTrue(VALIDATOR_TRUE.isValid(leading), leading); - assertEquals("US0378331005", VALIDATOR_TRUE.validate(leading)); + for (final String code : new String[] { " US0378331005", "US0378331005 ", " US0378331005 " }) { + assertTrue(VALIDATOR_FALSE.isValid(code), code); + assertTrue(VALIDATOR_TRUE.isValid(code), code); + assertEquals("US0378331005", VALIDATOR_TRUE.validate(code), code); + } } } From b9c117954f1c87bb9cf295f3d4452562cd79f909 Mon Sep 17 00:00:00 2001 From: sahvx655-wq Date: Wed, 17 Jun 2026 12:47:46 +0530 Subject: [PATCH 3/3] Use @ParameterizedTest for whitespace country-code test --- .../validator/routines/ISINValidatorTest.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/test/java/org/apache/commons/validator/routines/ISINValidatorTest.java b/src/test/java/org/apache/commons/validator/routines/ISINValidatorTest.java index 1a99788b7..a8376acdb 100644 --- a/src/test/java/org/apache/commons/validator/routines/ISINValidatorTest.java +++ b/src/test/java/org/apache/commons/validator/routines/ISINValidatorTest.java @@ -21,6 +21,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; /** * Tests {@link ISINValidator}. @@ -111,16 +113,15 @@ void testIsValidTrue() { } } - @Test - void testValidWithSurroundingWhitespaceCountryCode() { + @ParameterizedTest + @ValueSource(strings = { " US0378331005", "US0378331005 ", " US0378331005 " }) + void testValidWithSurroundingWhitespaceCountryCode(final String code) { // The underlying CodeValidator trims the input, so the country code must be // derived from the trimmed code. Otherwise a valid ISIN with surrounding whitespace // is accepted without the country check but rejected with it. - for (final String code : new String[] { " US0378331005", "US0378331005 ", " US0378331005 " }) { - assertTrue(VALIDATOR_FALSE.isValid(code), code); - assertTrue(VALIDATOR_TRUE.isValid(code), code); - assertEquals("US0378331005", VALIDATOR_TRUE.validate(code), code); - } + assertTrue(VALIDATOR_FALSE.isValid(code), code); + assertTrue(VALIDATOR_TRUE.isValid(code), code); + assertEquals("US0378331005", VALIDATOR_TRUE.validate(code), code); } }