Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

National check digit #73

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -57,8 +57,8 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jdkVersion>1.6</jdkVersion>
<jdk6Signature>java16</jdk6Signature>
<jdkVersion>1.8</jdkVersion>
<jdk8Signature>java18</jdk8Signature>
<skipSigning>true</skipSigning>
</properties>

@@ -102,7 +102,7 @@
<!-- Sets the VM argument line used when unit tests are run. -->
<argLine>${surefireArgLine}</argLine>
<!-- Skips unit tests if the value of skip.unit.tests property is true -->
<skipTests>${skip.unit.tests}</skipTests>
<skipTests>${skipTests}</skipTests>
<!-- Excludes integration tests when unit tests are run. -->
<excludes>
<exclude>**/IT*.java</exclude>
@@ -243,7 +243,7 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>animal-sniffer-maven-plugin</artifactId>
<version>1.11</version>
<version>1.19</version>
<executions>
<execution>
<id>check-java-api</id>
@@ -255,7 +255,7 @@
<signature>
<groupId>org.codehaus.mojo.signature
</groupId>
<artifactId>${jdk6Signature}</artifactId>
<artifactId>${jdk8Signature}</artifactId>
<version>1.0</version>
</signature>
</configuration>
17 changes: 12 additions & 5 deletions src/main/java/org/iban4j/Iban.java
Original file line number Diff line number Diff line change
@@ -15,12 +15,12 @@
*/
package org.iban4j;

import java.util.List;
import java.util.Random;

import org.iban4j.bban.BbanStructure;
import org.iban4j.bban.BbanStructureEntry;

import java.util.List;
import java.util.Random;

import static org.iban4j.IbanFormatException.IbanFormatViolation.*;


@@ -351,7 +351,7 @@ public Iban build(boolean validate) throws IbanFormatException,
IllegalArgumentException, UnsupportedCountryException {

// null checks
require(countryCode, bankCode, accountNumber);
require(countryCode, bankCode, accountNumber, nationalCheckDigit);

// iban is formatted with default check digit.
final String formattedIban = formatIban();
@@ -439,7 +439,8 @@ private String formatIban() {

private void require(final CountryCode countryCode,
final String bankCode,
final String accountNumber)
final String accountNumber,
final String nationalCheckDigit)
throws IbanFormatException {
if(countryCode == null) {
throw new IbanFormatException(COUNTRY_CODE_NOT_NULL,
@@ -455,6 +456,12 @@ private void require(final CountryCode countryCode,
throw new IbanFormatException(ACCOUNT_NUMBER_NOT_NULL,
"accountNumber is required; it cannot be null");
}
if (BbanStructure.hasNationalCheckDigit(countryCode)) {
if (nationalCheckDigit == null) {
throw new IbanFormatException(NATIONAL_CHECK_DIGIT_NOT_NULL,
"nationalCheckDigit is required; it cannot be null");
}
}
}

private void fillMissingFieldsRandomly() {
3 changes: 2 additions & 1 deletion src/main/java/org/iban4j/IbanFormatException.java
Original file line number Diff line number Diff line change
@@ -183,7 +183,8 @@ public static enum IbanFormatViolation {
BBAN_ONLY_DIGITS_OR_LETTERS,

BANK_CODE_NOT_NULL,
ACCOUNT_NUMBER_NOT_NULL
ACCOUNT_NUMBER_NOT_NULL,
NATIONAL_CHECK_DIGIT_NOT_NULL

}
}
14 changes: 14 additions & 0 deletions src/main/java/org/iban4j/bban/BbanStructure.java
Original file line number Diff line number Diff line change
@@ -481,6 +481,20 @@ public static BbanStructure forCountry(final CountryCode countryCode) {
return structures.get(countryCode);
}

/**
* Checks whether national Check digit is mandatory for specific country
*
* @param countryCode the country code
* @return true/false
*/
public static boolean hasNationalCheckDigit(final CountryCode countryCode) {
Optional<BbanStructure> bbanStructure = Optional.ofNullable(forCountry(countryCode));
return bbanStructure.map(structure -> structure.getEntries()
.stream()
.anyMatch(e -> BbanEntryType.national_check_digit.equals(e.getEntryType())))
.orElse(false);
}

public List<BbanStructureEntry> getEntries() {
return Collections.unmodifiableList(Arrays.asList(entries));
}
13 changes: 10 additions & 3 deletions src/test/java/org/iban4j/IbanTest.java
Original file line number Diff line number Diff line change
@@ -22,9 +22,7 @@

import java.util.Collection;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertThat;

@@ -465,5 +463,14 @@ public void ibanContructionRandomDoesNotOverwriteIdentificationNumber() {
.buildRandom();
assertThat(iban.getIdentificationNumber(), is(equalTo("1234567890")));
}

@Test(expected = IbanFormatException.class)
public void ibanConstructionWithLackingNationalCheckDigitShouldThrowExceptionIfValidationRequested() {
new Iban.Builder()
.countryCode(CountryCode.NO)
.bankCode("4435")
.accountNumber("0343730")
.build(true);
}
}
}