Skip to content

Commit

Permalink
Merge branch 'aSemy-fix/independent-iban-random'
Browse files Browse the repository at this point in the history
  • Loading branch information
hajk1 committed Jan 10, 2024
2 parents cf63318 + f9be101 commit b0623d9
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 33 deletions.
10 changes: 8 additions & 2 deletions src/main/java/org/iban4j/Iban.java
Original file line number Diff line number Diff line change
Expand Up @@ -413,11 +413,17 @@ public Iban build(boolean validate) throws IbanFormatException,
*/
public Iban buildRandom() throws IbanFormatException,
IllegalArgumentException, UnsupportedCountryException {

// Create a new seeded Random, so it doesn't matter how this Random is used, it won't affect subsequent usages
// of the original Random. (which can impact seeded behaviour when many IBANs are generated or the number of
// IBAN entries change).
final Random random = new Random(this.random.nextInt());

if (countryCode == null) {
List<CountryCode> countryCodes = BbanStructure.supportedCountries();
this.countryCode(countryCodes.get(random.nextInt(countryCodes.size())));
}
fillMissingFieldsRandomly();
fillMissingFieldsRandomly(random);
return build();
}

Expand Down Expand Up @@ -511,7 +517,7 @@ private void require(final CountryCode countryCode,
}
}

private void fillMissingFieldsRandomly() {
private void fillMissingFieldsRandomly(final Random random) {
final BbanStructure structure = BbanStructure.forCountry(countryCode);

if (structure == null) {
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/iban4j/bban/BbanStructureEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ public String getRandom() {
}

public String getRandom(Random random) {

// Create a new seeded Random, so it doesn't matter how this Random is used, it won't affect subsequent usages
// of the original Random. (which can impact seeded behaviour when many IBANs are generated or the number of
// IBAN entries change).
random = new Random(random.nextInt());

StringBuilder s = new StringBuilder();
char[] charChoices = charByCharacterType.get(characterType);
if (charChoices == null) {
Expand Down
45 changes: 25 additions & 20 deletions src/test/java/org/iban4j/IbanTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,26 @@
*/
package org.iban4j;

import org.junit.jupiter.api.Disabled;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.iban4j.TestDataHelper.defaultExceptionMessage;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.Random;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.util.Random;

import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.*;
import static org.iban4j.TestDataHelper.defaultExceptionMessage;
import static org.junit.jupiter.api.Assertions.*;

@DisplayName("Iban general test")
public class IbanTest {

public static class IbanGenerationTest {

@DisplayName("IBANs With Same Data Should Be Equal")
@DisplayName("IBANs With Same Data Should Be Equal")
@Test
public void ibansWithSameDataShouldBeEqual() {
Iban iban1 = new Iban.Builder()
Expand Down Expand Up @@ -287,14 +291,15 @@ public void ibanConstructionRandom() {
}

@Test
@Disabled
public void ibanConstructionSeeded() {
assertIbanUtilRandomWithSeedEquals("FR87 8734 4468 89P1 RIYK UO5K 809", 1);
assertIbanUtilRandomWithSeedEquals("FI79 2079 0697 8464 44", 2);
assertIbanUtilRandomWithSeedEquals("FO71 0018 2949 1527 41", 3);
assertAll(
() -> assertIbanUtilRandomWithSeedEquals("GL41 1918 0836 9682 13", 1),
() -> assertIbanUtilRandomWithSeedEquals("FR17 0679 7098 8804 5NYW S75F D50", 2),
() -> assertIbanUtilRandomWithSeedEquals("EG45 0882 2804 0304 6660 9507 6091 3", 3)
);
}

private static void assertIbanUtilRandomWithSeedEquals(
private void assertIbanUtilRandomWithSeedEquals(
String expected,
int seed
) {
Expand All @@ -307,11 +312,12 @@ private static void assertIbanUtilRandomWithSeedEquals(
}

@Test
@Disabled
public void ibanBuilderConstructionSeeded() {
assertIbanBuilderRandomWithSeedEquals("FR87 8734 4468 89P1 RIYK UO5K 809", 1);
assertIbanBuilderRandomWithSeedEquals("FI79 2079 0697 8464 44", 2);
assertIbanBuilderRandomWithSeedEquals("FO71 0018 2949 1527 41", 3);
assertAll(
() -> assertIbanBuilderRandomWithSeedEquals("GL41 1918 0836 9682 13", 1),
() -> assertIbanBuilderRandomWithSeedEquals("FR17 0679 7098 8804 5NYW S75F D50", 2),
() -> assertIbanBuilderRandomWithSeedEquals("EG45 0882 2804 0304 6660 9507 6091 3", 3)
);
}

private static void assertIbanBuilderRandomWithSeedEquals(
Expand Down Expand Up @@ -430,5 +436,4 @@ public void ibanConstructionWithLackingNationalCheckDigitShouldThrowExceptionIfV
defaultExceptionMessage);
assertThat(thrown.getMessage(), containsString("nationalCheckDigit is required; it cannot be null"));
}
}
}
29 changes: 18 additions & 11 deletions src/test/java/org/iban4j/bban/BbanStructureEntryTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package org.iban4j.bban;

import org.junit.jupiter.api.Test;

import java.nio.CharBuffer;
import java.util.Objects;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

Expand All @@ -15,27 +16,33 @@ public class BbanStructureEntryTest {
public void expectRandomAccountNumberIsDeterministicWhenSeeded() {
BbanStructureEntry entry = BbanStructureEntry.accountNumber(10, 'a');

assertSeededRandomBbanStructureEntryEquals(entry, "RAHJMYUWWK", 1);
assertSeededRandomBbanStructureEntryEquals(entry, "SGAVREIZNE", 2);
assertSeededRandomBbanStructureEntryEquals(entry, "SMMHQUVGJX", 3);
assertAll(
() -> assertSeededRandomBbanStructureEntryEquals(entry, "GYNPNTQMPP", 1),
() -> assertSeededRandomBbanStructureEntryEquals(entry, "ZBFUFVOHNJ", 2),
() -> assertSeededRandomBbanStructureEntryEquals(entry, "FHTOSEFCAR", 3)
);
}

@Test
public void expectRandomOwnerAccountNumberIsDeterministicWhenSeeded() {
BbanStructureEntry entry = BbanStructureEntry.ownerAccountNumber(11, 'n');

assertSeededRandomBbanStructureEntryEquals(entry, "58734446889", 1);
assertSeededRandomBbanStructureEntryEquals(entry, "82079069784", 2);
assertSeededRandomBbanStructureEntryEquals(entry, "40018294915", 3);
assertAll(
() -> assertSeededRandomBbanStructureEntryEquals(entry, "88511786533", 1),
() -> assertSeededRandomBbanStructureEntryEquals(entry, "33705581952", 2),
() -> assertSeededRandomBbanStructureEntryEquals(entry, "13164650831", 3)
);
}

@Test
public void expectRandomBankCodeIsDeterministicWhenSeeded() {
BbanStructureEntry entry = BbanStructureEntry.bankCode(12, 'c');

assertSeededRandomBbanStructureEntryEquals(entry, "XSJXQ4EAASPP", 1);
assertSeededRandomBbanStructureEntryEquals(entry, "4OKJX66R7O22", 2);
assertSeededRandomBbanStructureEntryEquals(entry, "EK6POILGJDLA", 3);
assertAll(
() -> assertSeededRandomBbanStructureEntryEquals(entry, "88FV9Z62HZ1T", 1),
() -> assertSeededRandomBbanStructureEntryEquals(entry, "T7F8TRELZ3I9", 2),
() -> assertSeededRandomBbanStructureEntryEquals(entry, "TXTOAA7SCPXB", 3)
);
}

private static void assertSeededRandomBbanStructureEntryEquals(
Expand Down Expand Up @@ -119,7 +126,7 @@ public void expectAlphabeticEntryGeneratesAllUppercaseLetters() {
String distinctChars = getDistinctSortedChars(generated);

assertEquals(
"ABCDEFGHIJKLMNOPQRSTUVWXYZ",
"ABCDEFGHIJKLMNOPQRSTUVWXYZ",
distinctChars
);
}
Expand Down

0 comments on commit b0623d9

Please sign in to comment.