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

Fix Zh-CN id number invalid #515

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
15 changes: 15 additions & 0 deletions src/main/java/com/github/javafaker/IdNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 2,10 @@

import com.github.javafaker.idnumbers.EnIdNumber;
import com.github.javafaker.idnumbers.SvSEIdNumber;
import com.github.javafaker.idnumbers.ZhCnIdNumber;
import org.yaml.snakeyaml.Yaml;

import java.text.ParseException;

public class IdNumber {
private final Faker faker;
Expand Down Expand Up @@ -38,4 42,15 @@ public String invalidSvSeSsn() {
SvSEIdNumber svSEIdNumber = new SvSEIdNumber();
return svSEIdNumber.getInvalidSsn(faker);
}

/**
* Generate a valid Zh-CN id number.
* @return A Zh-CN id number
* @throws ParseException An exception caused by parse time
*/

public String validZhCNSsn () throws ParseException {
ZhCnIdNumber zhCnIdNumber = new ZhCnIdNumber();
return zhCnIdNumber.getValidSsn(faker);
}
}
74 changes: 74 additions & 0 deletions src/main/java/com/github/javafaker/idnumbers/ZhCnIdNumber.java

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/main/resources/zh-CN.yml

Large diffs are not rendered by default.

106 changes: 106 additions & 0 deletions src/test/java/com/github/javafaker/idnumbers/ZhCnIdNumberTest.java
Original file line number Diff line number Diff line change
@@ -0,0 1,106 @@
package com.github.javafaker.idnumbers;

import com.github.javafaker.Faker;
import com.github.javafaker.idnumbers.ZhCnIdNumber;
import com.github.javafaker.matchers.MatchesRegularExpression;
import org.junit.Test;

import java.lang.reflect.Field;
import java.text.ParseException;
import java.util.Locale;

import static org.junit.Assert.*;

public class ZhCnIdNumberTest {
@Test
public void testValidChineseIdNumber(){
for (int i=0;i<100; i ){
Faker faker = new Faker(new Locale("zh_CN"));
String idNumber=faker.idNumber().valid();
boolean isSatisfied=true;
if(idNumber.length()!=18)isSatisfied=false;
for(int j=0;j<idNumber.length();j ){
char ch=idNumber.charAt(j);
if(j!=idNumber.length()-1){
if(ch>'9'||ch<'0'){isSatisfied=false;break;}
}
else{
if((ch>'9'||ch<'0')&&ch!='X'){isSatisfied=false;break;}
}
}
assertTrue(isSatisfied);
}
}

@Test
public void testChecksumOfChineseIdNumber(){
for (int i=0;i<100; i ){
Faker faker = new Faker(new Locale("zh_CN"));
String s=faker.idNumber().valid();
boolean isSatisfied=true;
int count=0;
count =(s.charAt(0)-'0')*7;
count =(s.charAt(1)-'0')*9;
count =(s.charAt(2)-'0')*10;
count =(s.charAt(3)-'0')*5;
count =(s.charAt(4)-'0')*8;
count =(s.charAt(5)-'0')*4;
count =(s.charAt(6)-'0')*2;
count =(s.charAt(7)-'0');
count =(s.charAt(8)-'0')*6;
count =(s.charAt(9)-'0')*3;
count =(s.charAt(10)-'0')*7;
count =(s.charAt(11)-'0')*9;
count =(s.charAt(12)-'0')*10;
count =(s.charAt(13)-'0')*5;
count =(s.charAt(14)-'0')*8;
count =(s.charAt(15)-'0')*4;
count =(s.charAt(16)-'0')*2;
count%=11;
if(count==10){
if(s.charAt(17)!='X') isSatisfied=false;
}
else if((int)(s.charAt(17)-'0')!=count)isSatisfied=false;
assertTrue(isSatisfied);
}
}

@Test(expected = ParseException.class)
public void testParseExcpetion() throws Throwable {
Faker faker=new Faker();
ZhCnIdNumber idNumber=new ZhCnIdNumber();
Class<?> cls=ZhCnIdNumber.class;
Field startTime=cls.getDeclaredField("startTime");
Field endTime=cls.getDeclaredField("endTime");
startTime.setAccessible(true);
endTime.setAccessible(true);
startTime.set(idNumber,"abcde");
endTime.set(idNumber,"abcde");
startTime.setAccessible(false);
endTime.setAccessible(false);
System.out.println(idNumber.getValidSsn(faker));
fail("Should throw ParseExpection");
}

@Test
public void testValidZhCnIdNumber() throws ParseException {
for (int i=0;i<100; i ){
Faker faker=new Faker();
ZhCnIdNumber id=new ZhCnIdNumber();
String idNumber=id.getValidSsn(faker);
boolean isSatisfied=true;
if(idNumber.length()!=18)isSatisfied=false;
for(int j=0;j<idNumber.length();j ){
char ch=idNumber.charAt(j);
if(j!=idNumber.length()-1){
if(ch>'9'||ch<'0'){isSatisfied=false;break;}
}
else{
if((ch>'9'||ch<'0')&&ch!='X'){isSatisfied=false;break;}
}
}
assertTrue(isSatisfied);
}
}

}