Fix ISIN country-code check using untrimmed input#399
Conversation
| // 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"; |
There was a problem hiding this comment.
How about trailing whitespace?
There was a problem hiding this comment.
Trailing whitespace is handled the same way. CodeValidator.validate trims both ends before matching, and since the fix now reads the country code from the validated/trimmed value rather than the raw argument, the substring(0, 2) lookup sees "US" regardless of where the whitespace sits.
I have extended the regression test to loop over leading, trailing and surrounding whitespace so all three paths are covered. Full ISINValidatorTest still passes (Tests run: 5, Failures: 0).
garydgregory
left a comment
There was a problem hiding this comment.
@sahvx655-wq
Please see my comment.
TY.
| // 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 " }) { |
There was a problem hiding this comment.
Use @ParameterizedTest instead of a loop.
There was a problem hiding this comment.
Done. Swapped the loop for a @ParameterizedTest with @valuesource covering the leading, trailing and surrounding whitespace cases. Each case now runs as its own invocation, so a failure points straight at the offending input rather than aborting the whole loop. Full ISINValidatorTest is green (7 tests, 0 failures).
ISIN country code read from the raw argument
Whilst checking the country-code path I noticed that
isValid/validatedisagree with the underlyingCodeValidatorfor inputs with surrounding whitespace. The validator trims the argument before matching, sovalidate(" US0378331005")returns the trimmedUS0378331005, but the ISO-3166 lookup then takescode.substring(0, 2)from the original untrimmed string and reads" U", which is not a country code. The upshot is that the same valid ISIN passes withgetInstance(false)yet is rejected bygetInstance(true).The root cause is the country code being derived from the raw argument rather than from the code the validator actually accepted. Reading it from the validated value keeps both code paths consistent. Left unfixed, callers that enable the country check silently reject otherwise-valid codes purely on the presence of leading whitespace. Added a regression test for the trimmed lookup.