add Code#asin and Code#imei (#167)
* add Code#asin and Code#imei * add Code#ean8, Code#gtin8, Code#ean13 and Code#gtin13
This commit is contained in:
committed by
Ricky Yim
parent
69ad288f3a
commit
cf7dc5e120
@@ -1,6 +1,9 @@
|
||||
package com.github.javafaker;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
|
||||
public class Code {
|
||||
|
||||
private final Faker faker;
|
||||
|
||||
Code(Faker faker) {
|
||||
@@ -43,4 +46,89 @@ public class Code {
|
||||
isbn13.append(check);
|
||||
return isbn13.toString();
|
||||
}
|
||||
|
||||
public String asin() {
|
||||
return faker.resolve("code.asin");
|
||||
}
|
||||
|
||||
public String imei() {
|
||||
char[] str = new char[15];
|
||||
int len = str.length;
|
||||
|
||||
// Fill in the first two values of the string based with the specified prefix.
|
||||
String arr = faker.options().option(REPORTING_BODY_IDENTIFIERS);
|
||||
str[0] = arr.charAt(0);
|
||||
str[1] = arr.charAt(1);
|
||||
|
||||
// Fill all the remaining numbers except for the last one with random values.
|
||||
for (int i=2; i < len - 1; i++) {
|
||||
str[i] = Character.forDigit(faker.number().numberBetween(0, 9), 10);
|
||||
}
|
||||
|
||||
// Calculate the Luhn checksum of the values thus far
|
||||
int lenOffset = (len + 1) % 2;
|
||||
int t = 0;
|
||||
int sum = 0;
|
||||
for (int i = 0; i < len - 1; i++) {
|
||||
if ((i + lenOffset) % 2 != 0) {
|
||||
t = Character.getNumericValue(str[i]) * 2;
|
||||
|
||||
if (t > 9) {
|
||||
t -= 9;
|
||||
}
|
||||
|
||||
sum += t;
|
||||
} else {
|
||||
sum += Character.getNumericValue(str[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// Choose the last digit so that it causes the entire string to pass the checksum.
|
||||
str[len - 1] = Character.forDigit(((10 - (sum % 10)) % 10), 10);
|
||||
|
||||
return new String(str);
|
||||
}
|
||||
|
||||
private static final String [] REPORTING_BODY_IDENTIFIERS
|
||||
= {"01", "10", "30", "33", "35", "44", "45", "49", "50", "51", "52", "53", "54", "86", "91", "98", "99"};
|
||||
|
||||
public String ean8() {
|
||||
return gtin8();
|
||||
}
|
||||
|
||||
public String gtin8() {
|
||||
char[] values = faker.regexify("\\d{7}").toCharArray();
|
||||
int sum = 0;
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
sum += Character.getNumericValue(values[i]) * GTIN_8_CHECK_DIGITS[i];
|
||||
}
|
||||
int checkDigit = 10 - sum % 10;
|
||||
if (checkDigit == 10) {
|
||||
return new String(ArrayUtils.add(values, Character.forDigit(0, 10)));
|
||||
} else {
|
||||
return new String(ArrayUtils.add(values, Character.forDigit(checkDigit, 10)));
|
||||
}
|
||||
}
|
||||
|
||||
public String ean13() {
|
||||
return gtin13();
|
||||
}
|
||||
|
||||
public String gtin13() {
|
||||
char[] values = faker.regexify("\\d{12}").toCharArray();
|
||||
int sum = 0;
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
sum += Character.getNumericValue(values[i]) * GTIN_13_CHECK_DIGITS[i];
|
||||
}
|
||||
int checkDigit = 10 - sum % 10;
|
||||
if (checkDigit == 10) {
|
||||
return new String(ArrayUtils.add(values, Character.forDigit(0, 10)));
|
||||
} else {
|
||||
return new String(ArrayUtils.add(values, Character.forDigit(checkDigit, 10)));
|
||||
}
|
||||
}
|
||||
|
||||
private static final int[] GTIN_8_CHECK_DIGITS = { 3, 1, 3, 1, 3, 1, 3 };
|
||||
private static final int[] GTIN_13_CHECK_DIGITS = { 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3 };
|
||||
|
||||
}
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
package com.github.javafaker;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static com.github.javafaker.matchers.MatchesRegularExpression.matchesRegularExpression;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import org.apache.commons.validator.routines.checkdigit.EAN13CheckDigit;
|
||||
import org.apache.commons.validator.routines.checkdigit.LuhnCheckDigit;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CodeTest extends AbstractFakerTest{
|
||||
|
||||
@Test
|
||||
@@ -50,4 +53,41 @@ public class CodeTest extends AbstractFakerTest{
|
||||
assertThat(sum % 10, is(0L));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void asin() {
|
||||
assertThat(faker.code().asin(), matchesRegularExpression("B000([A-Z]|\\d){6}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void imei() {
|
||||
String imei = faker.code().imei();
|
||||
|
||||
assertThat(imei, matchesRegularExpression("\\A[\\d\\.\\:\\-\\s]+\\z"));
|
||||
assertThat(LuhnCheckDigit.LUHN_CHECK_DIGIT.isValid(imei), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ean8() {
|
||||
assertThat(faker.code().ean8(), matchesRegularExpression("\\d{8}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void gtin8() {
|
||||
assertThat(faker.code().gtin8(), matchesRegularExpression("\\d{8}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ean13() {
|
||||
String ean13 = faker.code().ean13();
|
||||
assertThat(ean13, matchesRegularExpression("\\d{13}"));
|
||||
assertThat(EAN13CheckDigit.EAN13_CHECK_DIGIT.isValid(ean13), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void gtin13() {
|
||||
String gtin13 = faker.code().gtin13();
|
||||
assertThat(gtin13, matchesRegularExpression("\\d{13}"));
|
||||
assertThat(EAN13CheckDigit.EAN13_CHECK_DIGIT.isValid(gtin13), is(true));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user