![JAR search and dependency download from the Maven repository](/logo.png)
nl.hsac.fitnesse.util.BsnUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hsac-fitnesse-plugin Show documentation
Show all versions of hsac-fitnesse-plugin Show documentation
Plugin to add features to a FitNesse installation
The newest version!
package nl.hsac.fitnesse.util;
import java.util.HashSet;
import java.util.Set;
/**
* Helpers for BSNs.
*/
public class BsnUtil {
private RandomUtil randomUtil = new RandomUtil();
private final Set bsnsToExclude = new HashSet<>();
/**
* Generates random number that could be a BSN.
* @return random BSN.
*/
public String generateBsn() {
String result;
int attempts = 0;
do {
if (attempts > 1000) {
throw new RuntimeException("Unable to generate new random BSN");
}
attempts++;
result = generateNextBsn();
} while (!bsnsToExclude.add(result));
return result;
}
/**
* Checks whether BSN is valid.
* Based on: https://mxforum.mendix.com/questions/2162/
* @param bsn BSN to check.
* @return true if it is structurally sound.
*/
public boolean testBsn(String bsn) {
try {
Double.parseDouble(bsn);
} catch (Exception e) {
return false;
}
if (bsn.length() != 9) {
return false;
} else {
int checksum = 0;
for (int i = 0; i < 8; i++) {
checksum += (Integer.parseInt(Character.toString(bsn.charAt(i))) * (9 - i));
}
checksum -= Integer.parseInt(Character.toString(bsn.charAt(8)));
if (checksum % 11 != 0) {
return false;
}
}
return true;
}
public void addBsnToExclude(String bsn) {
bsnsToExclude.add(bsn);
}
public Set getBsnsToExclude() {
return bsnsToExclude;
}
public void resetExcludedBsns() {
bsnsToExclude.clear();
}
/**
* Based on: http://www.testnummers.nl/bsn.js
* @return String that passes BSN test.
*/
private String generateNextBsn() {
String Result1 = "";
int Nr9 = randomUtil.random(3);
int Nr8 = randomUtil.random(10);
int Nr7 = randomUtil.random(10);
int Nr6 = randomUtil.random(10);
int Nr5 = randomUtil.random(10);
int Nr4 = randomUtil.random(10);
int Nr3 = randomUtil.random(10);
int Nr2 = randomUtil.random(10);
int Nr1 = 0;
int SofiNr = 0;
if ((Nr9 == 0) && (Nr8 == 0) && (Nr7 == 0)) {
Nr8 = 1;
}
SofiNr = 9 * Nr9 + 8 * Nr8 + 7 * Nr7 + 6 * Nr6 + 5 * Nr5 + 4 * Nr4 + 3 * Nr3 + 2 * Nr2;
Nr1 = floor(SofiNr - (floor(SofiNr / 11)) * 11);
if (Nr1 > 9) {
if (Nr2 > 0) {
Nr2 -= 1;
Nr1 = 8;
} else {
Nr2 += 1;
Nr1 = 1;
}
}
Result1 += Nr9;
Result1 += Nr8;
Result1 += Nr7;
Result1 += Nr6;
Result1 += Nr5;
Result1 += Nr4;
Result1 += Nr3;
Result1 += Nr2;
Result1 += Nr1;
return Result1;
}
private int floor(double d) {
return (int) d;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy