Merge pull request #2799 from LuisStruggle/v6-dev-1

fix:ReUtil.replaceAll()方法,当replacementTemplate为null对象时,出现空指针异常
This commit is contained in:
Golden Looly 2022-12-14 11:55:12 +08:00 committed by GitHub
commit bcc0605036
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 0 deletions

View File

@ -876,6 +876,9 @@ public class ReUtil {
return StrUtil.str(content);
}
// replacementTemplate字段不能为null否则无法抉择如何处理结果
Assert.notNull(replacementTemplate, "ReplacementTemplate must be not null !");
final Matcher matcher = pattern.matcher(content);
boolean result = matcher.find();
if (result) {

View File

@ -115,6 +115,21 @@ public class ReUtilTest {
Assert.assertEquals("ZZZaaabbbccc中文->1234<-", replaceAll);
}
@Test
public void replaceAllTest3() {
// 修改前ReUtil.replaceAll()方法当replacementTemplate为null对象时出现空指针异常
final String str = null;
final Pattern pattern = Pattern.compile("(\\d+)");
// Assert.assertThrows(NullPointerException.class, () -> ReUtil.replaceAll(content, pattern, str));
// 修改后测试正常的方法访问是否有效
final String replaceAll = ReUtil.replaceAll(content, pattern, parameters -> "->" + parameters.group(1) + "<-");
Assert.assertEquals("ZZZaaabbbccc中文->1234<-", replaceAll);
// 修改后判断ReUtil.replaceAll()方法当replacementTemplate为null对象时提示为非法的参数异常ReplacementTemplate must be not null !
Assert.assertThrows(IllegalArgumentException.class, () -> ReUtil.replaceAll(content, pattern, str));
}
@Test
public void replaceTest() {
final String str = "AAABBCCCBBDDDBB";