Chinese2ndGenIDCardNumber 继承自 ValidatableStringRecord;测试 ValidatableStringRecord。

unit-tests
ZhouXY108 2024-12-28 10:35:34 +08:00
parent 4630176641
commit 348c5f28e4
4 changed files with 44 additions and 53 deletions

View File

@ -16,11 +16,9 @@
package xyz.zhouxy.plusone.commons.model;
import java.io.Serializable;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import com.google.common.base.Strings;
@ -46,9 +44,9 @@ import xyz.zhouxy.plusone.commons.util.StringTools;
*/
@ValueObject
@Immutable
public class Chinese2ndGenIDCardNumber implements IDCardNumber, Serializable {
private final String value;
public class Chinese2ndGenIDCardNumber
extends ValidatableStringRecord<Chinese2ndGenIDCardNumber>
implements IDCardNumber {
/** 省份编码 */
private final String provinceCode;
@ -63,57 +61,49 @@ public class Chinese2ndGenIDCardNumber implements IDCardNumber, Serializable {
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd");
private Chinese2ndGenIDCardNumber(String value, String provinceCode, String cityCode, String countyCode,
Gender gender, LocalDate birthDate) {
this.value = value;
this.provinceCode = provinceCode;
this.cityCode = cityCode;
this.countyCode = countyCode;
this.gender = gender;
this.birthDate = birthDate;
}
private Chinese2ndGenIDCardNumber(String value) {
super(value.toUpperCase(), PatternConsts.CHINESE_2ND_ID_CARD_NUMBER, () -> "二代居民身份证校验失败:" + value);
public static Chinese2ndGenIDCardNumber of(final String value) {
AssertTools.checkArgument(StringTools.isNotBlank(value), "二代居民身份证校验失败:号码为空");
final String idNumber = value.toUpperCase();
final Matcher matcher = getMatcher();
final Matcher matcher = PatternConsts.CHINESE_2ND_ID_CARD_NUMBER.matcher(idNumber);
AssertTools.checkArgument(matcher.matches(), () -> "二代居民身份证校验失败:" + value);
final String provinceCodeValue = matcher.group("province");
AssertTools.checkArgument(Chinese2ndGenIDCardNumber.PROVINCE_CODES.containsKey(provinceCodeValue));
final String provinceCode = matcher.group("province");
AssertTools.checkArgument(Chinese2ndGenIDCardNumber.PROVINCE_CODES.containsKey(provinceCode));
final String cityCodeValue = matcher.group("city");
final String countyCodeValue = matcher.group("county");
final String cityCode = matcher.group("city");
final String countyCode = matcher.group("county");
final Gender gender;
final LocalDate birthDate;
final Gender genderValue;
final LocalDate birthDateValue;
try {
// 出生日期
final String birthDateStr = matcher.group("birthDate");
birthDate = LocalDate.parse(birthDateStr, DATE_FORMATTER);
birthDateValue = LocalDate.parse(birthDateStr, DATE_FORMATTER);
// 性别
final int genderCode = Integer.parseInt(matcher.group("gender"));
gender = genderCode % 2 == 0 ? Gender.FEMALE : Gender.MALE;
genderValue = genderCode % 2 == 0 ? Gender.FEMALE : Gender.MALE;
}
catch (Exception e) {
throw new IllegalArgumentException(e);
}
return new Chinese2ndGenIDCardNumber(idNumber, provinceCode, cityCode, countyCode, gender, birthDate);
this.provinceCode = provinceCodeValue;
this.cityCode = cityCodeValue;
this.countyCode = countyCodeValue;
this.gender = genderValue;
this.birthDate = birthDateValue;
}
public static Chinese2ndGenIDCardNumber of(final String value) {
AssertTools.checkArgument(StringTools.isNotBlank(value), "二代居民身份证校验失败:号码为空");
return new Chinese2ndGenIDCardNumber(value);
}
// ================================
// #region - reader methods
// ================================
@ReaderMethod
public String value() {
return value;
}
@ReaderMethod
public String getProvinceCode() {
return provinceCode;
@ -213,18 +203,11 @@ public class Chinese2ndGenIDCardNumber implements IDCardNumber, Serializable {
@Override
public int hashCode() {
return Objects.hashCode(value);
return super.hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!(obj instanceof Chinese2ndGenIDCardNumber))
return false;
Chinese2ndGenIDCardNumber other = (Chinese2ndGenIDCardNumber) obj;
return Objects.equals(value, other.value);
return super.equals(obj);
}
private static final long serialVersionUID = 8390082242712103716L;
}

View File

@ -23,6 +23,7 @@ import java.util.regex.Pattern;
import javax.annotation.Nonnull;
import xyz.zhouxy.plusone.commons.annotation.ReaderMethod;
import xyz.zhouxy.plusone.commons.util.AssertTools;
/**
@ -31,8 +32,8 @@ import xyz.zhouxy.plusone.commons.util.AssertTools;
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 0.1.0
*/
public abstract class ValidatableStringRecord
implements Comparable<ValidatableStringRecord> {
public abstract class ValidatableStringRecord<T extends ValidatableStringRecord<T>>
implements Comparable<T> {
@Nonnull
private final String value;
@ -62,18 +63,19 @@ public abstract class ValidatableStringRecord
*
* @return
*/
@ReaderMethod
public final String value() {
return this.value;
}
@Override
public int compareTo(ValidatableStringRecord o) {
return this.value.compareTo(o.value);
public int compareTo(T o) {
return this.value.compareTo(o.value());
}
@Override
public int hashCode() {
return Objects.hash(value);
return Objects.hash(getClass(), value);
}
@Override
@ -84,8 +86,9 @@ public abstract class ValidatableStringRecord
return false;
if (getClass() != obj.getClass())
return false;
ValidatableStringRecord other = (ValidatableStringRecord) obj;
return Objects.equals(value, other.value);
@SuppressWarnings("unchecked")
T other = (T) obj;
return Objects.equals(value, other.value());
}
@Override

View File

@ -51,7 +51,12 @@ public class Chinese2ndGenIDCardNumberTests {
assertEquals("1***************1X", idCardNumber.toDesensitizedString());
assertEquals("110***********111X", idCardNumber.toDesensitizedString(3, 4));
assertEquals("11############111X", idCardNumber.toDesensitizedString('#', 2, 4));
assertEquals("110###############", idCardNumber.toDesensitizedString('#', 3, 0));
assertEquals("11010520000101111X", idCardNumber.toDesensitizedString(10, 8));
assertThrows(IllegalArgumentException.class, () -> idCardNumber.toDesensitizedString(-1, 5));
assertThrows(IllegalArgumentException.class, () -> idCardNumber.toDesensitizedString(5, -1));
assertThrows(IllegalArgumentException.class, () -> idCardNumber.toDesensitizedString(10, 9));
}
@Test

View File

@ -84,7 +84,7 @@ class User {
}
@ValueObject
class Email extends ValidatableStringRecord {
class Email extends ValidatableStringRecord<Email> {
private Email(String value) {
super(value, PatternConsts.EMAIL);
}
@ -96,7 +96,7 @@ class Email extends ValidatableStringRecord {
}
@ValueObject
class Username extends ValidatableStringRecord {
class Username extends ValidatableStringRecord<Username> {
private Username(String username) {
super(username, PatternConsts.USERNAME);
}