Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ The <action> type attribute can be add,update,fix,remove.
<body>
<release version="1.11.1" date="YYYY-MM-DD" description="This is a feature and maintenance release. Java 8 or later is required.">
<!-- FIX -->
<action type="fix" issue="VALIDATOR-510" due-to="Aryan Ganjoo">IBANValidator.setValidator() preserves unrelated secondary country codes when changing an alias.</action>
<action type="fix" dev="ggregory" due-to="sahvx655-wq, Gary Gregory">Drop the symbol separator in the currency and percent re-parse (#430).</action>
<action type="fix" dev="ggregory" due-to="sahvx655-wq, Gary Gregory">Count unpaired carriage returns in adjustForLineEnding (#431).</action>
<action type="fix" dev="ggregory" due-to="sahvx655-wq, Gary Gregory">Validate path, query and fragment for authority-less file URLs (#432).</action>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,25 @@ public boolean isValid(final String code) {
return validate(code) == IBANValidatorStatus.VALID;
}

/**
* Removes the other country codes of a validator that was replaced or removed under the given country code.
* <p>
* Nothing is removed when the country code was one of the other country codes of the previous validator (for example
* AX for FI), since the previous validator remains installed under its own country code. Only entries that still map to
* the previous validator are removed, so other country codes that have since been given their own validator are kept.
* </p>
*
* @param countryCode The country code that was replaced or removed.
* @param prev The validator previously installed for the country code, may be null.
*/
private void removeOtherCountryCodes(final String countryCode, final Validator prev) {
if (prev != null && prev.countryCode.equals(countryCode)) {
for (final String otherCC : prev.otherCountryCodes) {
validatorMap.remove(otherCC, prev);
}
}
}

/**
* Installs a validator. Will replace any existing entry which has the same countryCode.
*
Expand All @@ -376,11 +395,7 @@ public Validator setValidator(final String countryCode, final int length, final
}
if (length < 0) {
final Validator prev = validatorMap.remove(countryCode);
if (prev != null) {
for (final String otherCC : prev.otherCountryCodes) {
validatorMap.remove(otherCC);
}
}
removeOtherCountryCodes(countryCode, prev);
return prev;
}
return setValidator(new Validator(countryCode, length, format));
Expand All @@ -398,11 +413,7 @@ public Validator setValidator(final Validator validator) {
throw new IllegalStateException("The singleton validator cannot be modified");
}
final Validator prev = validatorMap.put(validator.countryCode, validator);
if (prev != null) {
for (final String otherCC : prev.otherCountryCodes) {
validatorMap.remove(otherCC);
}
}
removeOtherCountryCodes(validator.countryCode, prev);
for (final String otherCC : validator.otherCountryCodes) {
validatorMap.put(otherCC, validator);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,37 @@ void testSetValidatorReplaceOtherCountryCodes() {
assertFalse(validator.hasValidator("IM"));
}

@Test
void testSetValidatorForOtherCountryCode() {
final IBANValidator validator = new IBANValidator();
// AX (Aland Islands) is handled by the FI (Finland) validator by default
assertEquals(validator.getValidator("FI"), validator.getValidator("AX"));
// Give AX its own validator
final Validator axValidator = new Validator("AX", 18, "AX\\d{16}");
assertEquals(validator.getValidator("FI"), validator.setValidator(axValidator));
assertEquals(axValidator, validator.getValidator("AX"));
assertTrue(validator.isValid("AX2112345600000785"));
assertTrue(validator.isValid("FI2112345600000785"));
// Replacing FI must not remove the AX validator, which no longer belongs to FI
validator.setValidator(new Validator("FI", 18, "FI\\d{16}"));
assertEquals(axValidator, validator.getValidator("AX"));
}

@Test
void testSetValidatorRemoveOtherCountryCode() {
final IBANValidator validator = new IBANValidator();
// GF (French Guiana) and GP (Guadeloupe) are handled by the FR (France) validator by default
final Validator frValidator = validator.getValidator("FR");
assertEquals(frValidator, validator.getValidator("GF"));
assertEquals(frValidator, validator.getValidator("GP"));
// Removing GF must only remove GF
assertEquals(frValidator, validator.setValidator("GF", -1, null));
assertFalse(validator.hasValidator("GF"));
assertEquals(frValidator, validator.getValidator("FR"));
assertEquals(frValidator, validator.getValidator("GP"));
assertTrue(validator.isValid("GP1120041010050500013M02606"));
}

@Test
void testSorted() {
final IBANValidator validator = new IBANValidator();
Expand Down