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..a8376acdb 100644 --- a/src/test/java/org/apache/commons/validator/routines/ISINValidatorTest.java +++ b/src/test/java/org/apache/commons/validator/routines/ISINValidatorTest.java @@ -16,10 +16,13 @@ */ 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; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; /** * Tests {@link ISINValidator}. @@ -110,4 +113,15 @@ void testIsValidTrue() { } } + @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. + assertTrue(VALIDATOR_FALSE.isValid(code), code); + assertTrue(VALIDATOR_TRUE.isValid(code), code); + assertEquals("US0378331005", VALIDATOR_TRUE.validate(code), code); + } + }