slawekjaranowski commented on code in PR #293:
URL: https://github.com/apache/commons-validator/pull/293#discussion_r1889339144
##########
src/test/java/org/apache/commons/validator/routines/IBANValidatorTest.java:
##########
@@ -309,95 +248,198 @@ private static String formatToRE(final String type,
final int len) {
}
}
- public static void main(final String[] a) throws Exception {
- final IBANValidator validator = new IBANValidator();
- final File ibanTsv = new File("target", "iban-registry.tsv");
- int countries = 0;
- if (ibanTsv.canRead()) {
- countries = checkIBAN(ibanTsv, validator);
- } else {
- System.out.println("Please load the file " +
ibanTsv.getCanonicalPath() + " from
https://www.swift.com/standards/data-standards/iban");
+ static Collection<Arguments> ibanRegistryV98Source() throws Exception {
+ Path ibanRegistryV98 =
Paths.get(IBANValidator.class.getResource("iban_registry_v98.txt").toURI());
+
+ final CSVFormat format =
CSVFormat.DEFAULT.builder().setDelimiter('\t').build();
+ final Reader rdr = Files.newBufferedReader(ibanRegistryV98,
StandardCharsets.ISO_8859_1);
+
+ CSVRecord country = null;
+ CSVRecord cc = null;
+ CSVRecord additionalCc = null;
+ CSVRecord structure = null;
+ CSVRecord length = null;
+
+ try (final CSVParser p = new CSVParser(rdr, format)) {
+ for (final CSVRecord o : p) {
+ final String item = o.get(0);
+ switch (item) {
+ case "Name of country":
+ country = o;
+ break;
+ case "IBAN prefix country code (ISO 3166)":
+ cc = o;
+ break;
+ case "Country code includes other countries/territories":
+ additionalCc = o;
+ break;
+ case "IBAN structure":
+ structure = o;
+ break;
+ case "IBAN length":
+ length = o;
+ break;
+ default:
+ break;
+ }
+ }
+ }
+
+ assertNotNull(country);
+ assertNotNull(cc);
+ assertNotNull(additionalCc);
+ assertNotNull(structure);
+ assertNotNull(length);
+
+ Collection<Arguments> result = new ArrayList<>();
+ for (int i = 1; i < country.size(); i++) {
+ String ac = additionalCc.get(i);
+ List<String> aCountry = Arrays.stream(ac.split(","))
+ .filter(s -> !"N/A".equals(s))
+ .map(s -> s.replace("(French part)", "")) // special case
+ .map(String::trim)
+ .filter(s -> !s.isEmpty())
+ .collect(Collectors.toList());
+ result.add(Arguments.of(country.get(i), cc.get(i), aCountry,
Integer.parseInt(length.get(i)), structure.get(i)));
+ }
+
+ return result;
+ }
+
+ static Collection<Arguments> ibanRegistryV98SourceExamples() throws
Exception {
+ Path ibanRegistryV98 =
Paths.get(IBANValidator.class.getResource("iban_registry_v98.txt").toURI());
+
+ final CSVFormat format =
CSVFormat.DEFAULT.builder().setDelimiter('\t').build();
+ final Reader rdr = Files.newBufferedReader(ibanRegistryV98,
StandardCharsets.ISO_8859_1);
+
+ CSVRecord country = null;
+ CSVRecord electronicExample = null;
+
+ try (final CSVParser p = new CSVParser(rdr, format)) {
+ for (final CSVRecord o : p) {
+ final String item = o.get(0);
+ switch (item) {
+ case "Name of country":
+ country = o;
+ break;
+ case "IBAN electronic format example":
+ electronicExample = o;
+ break;
+ default:
+ break;
+ }
+ }
}
- System.out.println("Processed " + countries + " countries.");
+
+ assertNotNull(country);
+ assertNotNull(electronicExample);
+
+ Collection<Arguments> result = new ArrayList<>();
+ for (int i = 1; i < country.size(); i++) {
+ result.add(Arguments.of(country.get(i), electronicExample.get(i)));
+ }
+
+ return result;
}
- private static void printEntry(final String ccode, final String length,
final String ib, final String country) {
- final String fmt = String.format("\"%s\"", ib);
- System.out.printf(" new Validator(\"%s\", %s, %-40s), //
%s\n", ccode, length, fmt, country);
+ @ParameterizedTest
+ @MethodSource("ibanRegistryV98Source")
+ void validatorShouldExistWithProperConfiguration(String countryName,
String countryCode, List<String> acountyCode, int ibanLength, String structure)
throws Exception {
+ String countryInfo = " countryCode: " + countryCode + ", countryName:
" + countryName;
+ Validator validator =
IBANValidator.getInstance().getValidator(countryCode);
+
+ assertNotNull(validator, "IBAN validator returned null for" +
countryInfo);
+ assertEquals(ibanLength, validator.getIbanLength(), "IBAN length
should be " + ibanLength + " for" + countryInfo);
+
+ List<String> allPatterns =
Arrays.stream(validator.getRegexValidator().getPatterns()).map(Pattern::pattern).collect(
+ Collectors.toList());
+
+ String re = fmtRE(structure.substring(2));
+ assertTrue(allPatterns.remove(countryCode + re), "No pattern " +
countryCode + re + " found for " + countryInfo);
+ for (String ac : acountyCode) {
+ assertTrue(allPatterns.remove(ac + re), "No additional country
code " + ac + " found for " + countryInfo);
+ }
+ assertTrue(allPatterns.isEmpty(), "Unrecognized patterns: " +
allPatterns + " for" + countryInfo);
+ }
+
+ @ParameterizedTest
+ @MethodSource("ibanRegistryV98SourceExamples")
+ void exampleAccountsShouldBeValid(String countryName, String example) {
+ Assumptions.assumeFalse(INVALID_IBAN_FIXTURES.contains(example), "Skip
invalid example: " + example + " for " + countryName);
+ assertTrue(IBANValidator.getInstance().isValid(example), "IBAN
validator returned false for " + example + " for " + countryName);
}
@Test
- public void testGetRegexValidatorPatterns() {
+ void testGetRegexValidatorPatterns() {
Review Comment:
@garydgregory - done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]