forked from plusone/plusone-commons
完成身份证号的单元测试
parent
62e3769802
commit
c4077ccb70
|
@ -226,5 +226,5 @@ public class Chinese2ndGenIDCardNumber implements IDCardNumber, Serializable {
|
||||||
return Objects.equals(value, other.value);
|
return Objects.equals(value, other.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final long serialVersionUID = 20241202095400L;
|
private static final long serialVersionUID = 8390082242712103716L;
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,7 +43,7 @@ public interface IDCardNumber {
|
||||||
LocalDate getBirthDate();
|
LocalDate getBirthDate();
|
||||||
|
|
||||||
/** 计算年龄 */
|
/** 计算年龄 */
|
||||||
default int calculateAge() {
|
default int getAge() {
|
||||||
LocalDate now = LocalDate.now();
|
LocalDate now = LocalDate.now();
|
||||||
return Period.between(getBirthDate(), now).getYears();
|
return Period.between(getBirthDate(), now).getYears();
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,35 +17,27 @@
|
||||||
package xyz.zhouxy.plusone.commons.model;
|
package xyz.zhouxy.plusone.commons.model;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.format.DateTimeParseException;
|
import java.time.format.DateTimeParseException;
|
||||||
import java.util.regex.Matcher;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.ValueSource;
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
|
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class Chinese2ndGenIDCardNumberTests {
|
public class Chinese2ndGenIDCardNumberTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testPattern() {
|
void testOf_success() {
|
||||||
Matcher matcher = PatternConsts.CHINESE_2ND_ID_CARD_NUMBER.matcher("11010520000101111X");
|
|
||||||
assertTrue(matcher.matches());
|
|
||||||
for (int i = 0; i < matcher.groupCount(); i++) {
|
|
||||||
log.info("{}: {}", i, matcher.group(i));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void test() {
|
|
||||||
Chinese2ndGenIDCardNumber idCardNumber = Chinese2ndGenIDCardNumber.of("11010520000101111X");
|
Chinese2ndGenIDCardNumber idCardNumber = Chinese2ndGenIDCardNumber.of("11010520000101111X");
|
||||||
assertEquals("11010520000101111X", idCardNumber.value());
|
assertEquals("11010520000101111X", idCardNumber.value());
|
||||||
assertEquals(LocalDate.of(2000, 1, 1), idCardNumber.getBirthDate());
|
assertEquals(LocalDate.of(2000, 1, 1), idCardNumber.getBirthDate());
|
||||||
assertEquals(Gender.MALE, idCardNumber.getGender());
|
assertSame(Gender.MALE, idCardNumber.getGender());
|
||||||
assertEquals("110105", idCardNumber.getCountyCode());
|
assertEquals("110105", idCardNumber.getCountyCode());
|
||||||
assertEquals("110105000000", idCardNumber.getFullCountyCode());
|
assertEquals("110105000000", idCardNumber.getFullCountyCode());
|
||||||
|
|
||||||
|
@ -57,17 +49,38 @@ public class Chinese2ndGenIDCardNumberTests {
|
||||||
|
|
||||||
assertEquals("北京", idCardNumber.getProvinceName());
|
assertEquals("北京", idCardNumber.getProvinceName());
|
||||||
|
|
||||||
assertThrows(IllegalArgumentException.class,
|
assertEquals("1***************1X", idCardNumber.toDesensitizedString());
|
||||||
() -> Chinese2ndGenIDCardNumber.of("1101520000101111"));
|
assertEquals("110***********111X", idCardNumber.toDesensitizedString(3, 4));
|
||||||
|
assertEquals("11############111X", idCardNumber.toDesensitizedString('#', 2, 4));
|
||||||
assertThrows(IllegalArgumentException.class,
|
|
||||||
() -> Chinese2ndGenIDCardNumber.of("11010520002101111X"));
|
|
||||||
|
|
||||||
try {
|
|
||||||
Chinese2ndGenIDCardNumber.of("11010520002101111X");
|
|
||||||
}
|
}
|
||||||
catch (IllegalArgumentException e) {
|
|
||||||
|
@Test
|
||||||
|
void testOf_blankValue() {
|
||||||
|
String[] strings = { null, "", " " };
|
||||||
|
for (String value : strings) {
|
||||||
|
IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
|
||||||
|
() -> Chinese2ndGenIDCardNumber.of(value));
|
||||||
|
assertEquals("二代居民身份证校验失败:号码为空", e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = { "1101520000101111", "110A0520000101111X", "110105220000101111X" })
|
||||||
|
void testOf_mismatched(String value) {
|
||||||
|
IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
|
||||||
|
() -> Chinese2ndGenIDCardNumber.of(value));
|
||||||
|
assertEquals("二代居民身份证校验失败:" + value, e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testOf_wrongBirthDate() {
|
||||||
|
IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
|
||||||
|
() -> Chinese2ndGenIDCardNumber.of("11010520002101111X"));
|
||||||
assertTrue(e.getCause() instanceof DateTimeParseException);
|
assertTrue(e.getCause() instanceof DateTimeParseException);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testOf_wrongProvince() {
|
||||||
|
assertThrows(IllegalArgumentException.class,
|
||||||
|
() -> Chinese2ndGenIDCardNumber.of("99010520000101111X"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue