This commit is contained in:
Looly 2020-02-11 16:03:43 +08:00
parent 562416e90d
commit 57fd7f8a2d
5 changed files with 77 additions and 43 deletions

View File

@ -18,9 +18,6 @@ import java.util.regex.Pattern;
*/ */
public class Validator { public class Validator {
private Validator() {
}
/** /**
* 英文字母 数字和下划线 * 英文字母 数字和下划线
*/ */
@ -364,11 +361,24 @@ public class Validator {
* @param pattern 正则模式 * @param pattern 正则模式
* @param value * @param value
* @return 是否匹配正则 * @return 是否匹配正则
* @deprecated 请使用 {@link #isMatchRegex(Pattern, CharSequence)}
*/ */
@Deprecated
public static boolean isMactchRegex(Pattern pattern, CharSequence value) { public static boolean isMactchRegex(Pattern pattern, CharSequence value) {
return ReUtil.isMatch(pattern, value); return ReUtil.isMatch(pattern, value);
} }
/**
* 通过正则表达式验证
*
* @param pattern 正则模式
* @param value
* @return 是否匹配正则
*/
public static boolean isMatchRegex(Pattern pattern, CharSequence value) {
return ReUtil.isMatch(pattern, value);
}
/** /**
* 验证是否为英文字母 数字和下划线 * 验证是否为英文字母 数字和下划线
* *
@ -376,7 +386,7 @@ public class Validator {
* @return 是否为英文字母 数字和下划线 * @return 是否为英文字母 数字和下划线
*/ */
public static boolean isGeneral(CharSequence value) { public static boolean isGeneral(CharSequence value) {
return isMactchRegex(GENERAL, value); return isMatchRegex(GENERAL, value);
} }
/** /**
@ -574,7 +584,7 @@ public class Validator {
* @since 4.1.8 * @since 4.1.8
*/ */
public static boolean isWord(CharSequence value) { public static boolean isWord(CharSequence value) {
return isMactchRegex(PatternPool.WORD, value); return isMatchRegex(PatternPool.WORD, value);
} }
/** /**
@ -601,7 +611,7 @@ public class Validator {
* @return 是否为货币 * @return 是否为货币
*/ */
public static boolean isMoney(CharSequence value) { public static boolean isMoney(CharSequence value) {
return isMactchRegex(MONEY, value); return isMatchRegex(MONEY, value);
} }
/** /**
@ -628,7 +638,7 @@ public class Validator {
* @return 是否为邮政编码中国 * @return 是否为邮政编码中国
*/ */
public static boolean isZipCode(CharSequence value) { public static boolean isZipCode(CharSequence value) {
return isMactchRegex(ZIP_CODE, value); return isMatchRegex(ZIP_CODE, value);
} }
/** /**
@ -654,7 +664,7 @@ public class Validator {
* @return 否为可用邮箱地址 * @return 否为可用邮箱地址
*/ */
public static boolean isEmail(CharSequence value) { public static boolean isEmail(CharSequence value) {
return isMactchRegex(EMAIL, value); return isMatchRegex(EMAIL, value);
} }
/** /**
@ -680,7 +690,7 @@ public class Validator {
* @return 是否为手机号码中国 * @return 是否为手机号码中国
*/ */
public static boolean isMobile(CharSequence value) { public static boolean isMobile(CharSequence value) {
return isMactchRegex(MOBILE, value); return isMatchRegex(MOBILE, value);
} }
/** /**
@ -707,7 +717,7 @@ public class Validator {
* @return 是否为身份证号码18位中国 * @return 是否为身份证号码18位中国
*/ */
public static boolean isCitizenId(CharSequence value) { public static boolean isCitizenId(CharSequence value) {
return isMactchRegex(CITIZEN_ID, value); return isMatchRegex(CITIZEN_ID, value);
} }
/** /**
@ -777,14 +787,12 @@ public class Validator {
* @return 是否为生日 * @return 是否为生日
*/ */
public static boolean isBirthday(CharSequence value) { public static boolean isBirthday(CharSequence value) {
if (isMactchRegex(BIRTHDAY, value)) { final Matcher matcher = BIRTHDAY.matcher(value);
Matcher matcher = BIRTHDAY.matcher(value); if (matcher.find()) {
if (matcher.find()) { int year = Integer.parseInt(matcher.group(1));
int year = Integer.parseInt(matcher.group(1)); int month = Integer.parseInt(matcher.group(3));
int month = Integer.parseInt(matcher.group(3)); int day = Integer.parseInt(matcher.group(5));
int day = Integer.parseInt(matcher.group(5)); return isBirthday(year, month, day);
return isBirthday(year, month, day);
}
} }
return false; return false;
} }
@ -812,7 +820,7 @@ public class Validator {
* @return 是否为IPV4地址 * @return 是否为IPV4地址
*/ */
public static boolean isIpv4(CharSequence value) { public static boolean isIpv4(CharSequence value) {
return isMactchRegex(IPV4, value); return isMatchRegex(IPV4, value);
} }
/** /**
@ -838,7 +846,7 @@ public class Validator {
* @return 是否为IPV6地址 * @return 是否为IPV6地址
*/ */
public static boolean isIpv6(CharSequence value) { public static boolean isIpv6(CharSequence value) {
return isMactchRegex(IPV6, value); return isMatchRegex(IPV6, value);
} }
/** /**
@ -865,7 +873,7 @@ public class Validator {
* @since 4.1.3 * @since 4.1.3
*/ */
public static boolean isMac(CharSequence value) { public static boolean isMac(CharSequence value) {
return isMactchRegex(PatternPool.MAC_ADDRESS, value); return isMatchRegex(PatternPool.MAC_ADDRESS, value);
} }
/** /**
@ -893,7 +901,7 @@ public class Validator {
* @since 3.0.6 * @since 3.0.6
*/ */
public static boolean isPlateNumber(CharSequence value) { public static boolean isPlateNumber(CharSequence value) {
return isMactchRegex(PLATE_NUMBER, value); return isMatchRegex(PLATE_NUMBER, value);
} }
/** /**
@ -977,7 +985,7 @@ public class Validator {
* @return 是否为中文字英文字母数字和下划线 * @return 是否为中文字英文字母数字和下划线
*/ */
public static boolean isGeneralWithChinese(CharSequence value) { public static boolean isGeneralWithChinese(CharSequence value) {
return isMactchRegex(GENERAL_WITH_CHINESE, value); return isMatchRegex(GENERAL_WITH_CHINESE, value);
} }
/** /**
@ -1004,7 +1012,7 @@ public class Validator {
* @return 是否为UUID * @return 是否为UUID
*/ */
public static boolean isUUID(CharSequence value) { public static boolean isUUID(CharSequence value) {
return isMactchRegex(UUID, value) || isMactchRegex(UUID_SIMPLE, value); return isMatchRegex(UUID, value) || isMatchRegex(UUID_SIMPLE, value);
} }
/** /**
@ -1032,7 +1040,7 @@ public class Validator {
* @since 4.3.3 * @since 4.3.3
*/ */
public static boolean isHex(CharSequence value) { public static boolean isHex(CharSequence value) {
return isMactchRegex(PatternPool.HEX, value); return isMatchRegex(PatternPool.HEX, value);
} }
/** /**

View File

@ -75,7 +75,7 @@ public class TextSimilarity {
} }
/** /**
* 判断字符是否为汉字数字和字母 因为对符号进行相似度比较没有实际意义故符号不加入考虑范围 * 判断字符是否为汉字数字和字母 因为对符号进行相似度比较没有实际意义故符号不加入考虑范围
* *
* @param charValue 字符 * @param charValue 字符
* @return true表示为非汉字数字和字母false反之 * @return true表示为非汉字数字和字母false反之

View File

@ -1,11 +1,9 @@
package cn.hutool.core.lang; package cn.hutool.core.lang;
import cn.hutool.core.exceptions.ValidateException;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import cn.hutool.core.exceptions.ValidateException;
import cn.hutool.core.lang.Validator;
/** /**
* 验证器单元测试 * 验证器单元测试
* *
@ -108,13 +106,13 @@ public class ValidatorTest {
@Test @Test
public void isMatchTest() { public void isMatchTest() {
String url = "http://aaa-bbb.somthing.com/a.php?a=b&c=2"; String url = "http://aaa-bbb.somthing.com/a.php?a=b&c=2";
Assert.assertTrue(Validator.isMactchRegex(PatternPool.URL_HTTP, url)); Assert.assertTrue(Validator.isMatchRegex(PatternPool.URL_HTTP, url));
url = "https://aaa-bbb.somthing.com/a.php?a=b&c=2"; url = "https://aaa-bbb.somthing.com/a.php?a=b&c=2";
Assert.assertTrue(Validator.isMactchRegex(PatternPool.URL_HTTP, url)); Assert.assertTrue(Validator.isMatchRegex(PatternPool.URL_HTTP, url));
url = "https://aaa-bbb.somthing.com:8080/a.php?a=b&c=2"; url = "https://aaa-bbb.somthing.com:8080/a.php?a=b&c=2";
Assert.assertTrue(Validator.isMactchRegex(PatternPool.URL_HTTP, url)); Assert.assertTrue(Validator.isMatchRegex(PatternPool.URL_HTTP, url));
} }
@Test @Test

View File

@ -24,8 +24,17 @@ public class IdcardUtilTest {
boolean valid15 = IdcardUtil.isValidCard(ID_15); boolean valid15 = IdcardUtil.isValidCard(ID_15);
Assert.assertTrue(valid15); Assert.assertTrue(valid15);
// 无效
String idCard = "360198910283844"; String idCard = "360198910283844";
Assert.assertFalse(IdcardUtil.isValidCard(idCard)); Assert.assertFalse(IdcardUtil.isValidCard(idCard));
// 生日无效
idCard = "201511221897205960";
Assert.assertFalse(IdcardUtil.isValidCard(idCard));
// 生日无效
idCard = "815727834224151";
Assert.assertFalse(IdcardUtil.isValidCard(idCard));
} }
@Test @Test

View File

@ -10,9 +10,8 @@ import java.lang.reflect.Method;
/** /**
* 反射工具类单元测试 * 反射工具类单元测试
*
* @author Looly
* *
* @author Looly
*/ */
public class ReflectUtilTest { public class ReflectUtilTest {
@ -20,17 +19,17 @@ public class ReflectUtilTest {
public void getMethodsTest() { public void getMethodsTest() {
Method[] methods = ReflectUtil.getMethods(ExamInfoDict.class); Method[] methods = ReflectUtil.getMethods(ExamInfoDict.class);
Assert.assertEquals(22, methods.length); Assert.assertEquals(22, methods.length);
//过滤器测试 //过滤器测试
methods = ReflectUtil.getMethods(ExamInfoDict.class, t -> Integer.class.equals(t.getReturnType())); methods = ReflectUtil.getMethods(ExamInfoDict.class, t -> Integer.class.equals(t.getReturnType()));
Assert.assertEquals(4, methods.length); Assert.assertEquals(4, methods.length);
final Method method = methods[0]; final Method method = methods[0];
Assert.assertNotNull(method); Assert.assertNotNull(method);
//null过滤器测试 //null过滤器测试
methods = ReflectUtil.getMethods(ExamInfoDict.class, null); methods = ReflectUtil.getMethods(ExamInfoDict.class, null);
Assert.assertEquals(22, methods.length); Assert.assertEquals(22, methods.length);
final Method method2 = methods[0]; final Method method2 = methods[0];
Assert.assertNotNull(method2); Assert.assertNotNull(method2);
@ -41,22 +40,22 @@ public class ReflectUtilTest {
Method method = ReflectUtil.getMethod(ExamInfoDict.class, "getId"); Method method = ReflectUtil.getMethod(ExamInfoDict.class, "getId");
Assert.assertEquals("getId", method.getName()); Assert.assertEquals("getId", method.getName());
Assert.assertEquals(0, method.getParameterTypes().length); Assert.assertEquals(0, method.getParameterTypes().length);
method = ReflectUtil.getMethod(ExamInfoDict.class, "getId", Integer.class); method = ReflectUtil.getMethod(ExamInfoDict.class, "getId", Integer.class);
Assert.assertEquals("getId", method.getName()); Assert.assertEquals("getId", method.getName());
Assert.assertEquals(1, method.getParameterTypes().length); Assert.assertEquals(1, method.getParameterTypes().length);
} }
@Test @Test
public void getMethodIgnoreCaseTest() { public void getMethodIgnoreCaseTest() {
Method method = ReflectUtil.getMethodIgnoreCase(ExamInfoDict.class, "getId"); Method method = ReflectUtil.getMethodIgnoreCase(ExamInfoDict.class, "getId");
Assert.assertEquals("getId", method.getName()); Assert.assertEquals("getId", method.getName());
Assert.assertEquals(0, method.getParameterTypes().length); Assert.assertEquals(0, method.getParameterTypes().length);
method = ReflectUtil.getMethodIgnoreCase(ExamInfoDict.class, "GetId"); method = ReflectUtil.getMethodIgnoreCase(ExamInfoDict.class, "GetId");
Assert.assertEquals("getId", method.getName()); Assert.assertEquals("getId", method.getName());
Assert.assertEquals(0, method.getParameterTypes().length); Assert.assertEquals(0, method.getParameterTypes().length);
method = ReflectUtil.getMethodIgnoreCase(ExamInfoDict.class, "setanswerIs", Integer.class); method = ReflectUtil.getMethodIgnoreCase(ExamInfoDict.class, "setanswerIs", Integer.class);
Assert.assertEquals("setAnswerIs", method.getName()); Assert.assertEquals("setAnswerIs", method.getName());
Assert.assertEquals(1, method.getParameterTypes().length); Assert.assertEquals(1, method.getParameterTypes().length);
@ -75,7 +74,7 @@ public class ReflectUtilTest {
final Field[] fields = ReflectUtil.getFields(TestSubClass.class); final Field[] fields = ReflectUtil.getFields(TestSubClass.class);
Assert.assertEquals(4, fields.length); Assert.assertEquals(4, fields.length);
} }
@Test @Test
public void setFieldTest() { public void setFieldTest() {
TestClass testClass = new TestClass(); TestClass testClass = new TestClass();
@ -90,6 +89,13 @@ public class ReflectUtilTest {
Assert.assertEquals(10, testClass.getA()); Assert.assertEquals(10, testClass.getA());
} }
@Test
public void noneStaticInnerClassTest() {
final TestAClass testAClass = ReflectUtil.newInstanceIfPossible(TestAClass.class);
Assert.assertNotNull(testAClass);
Assert.assertEquals(2, testAClass.getA());
}
static class TestClass { static class TestClass {
private int a; private int a;
@ -101,4 +107,17 @@ public class ReflectUtilTest {
this.a = a; this.a = a;
} }
} }
@SuppressWarnings("InnerClassMayBeStatic")
class TestAClass {
private int a = 2;
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
}
} }