mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-24 18:04:54 +08:00
Merge pull request #2799 from LuisStruggle/v6-dev-1
fix:ReUtil.replaceAll()方法,当replacementTemplate为null对象时,出现空指针异常
This commit is contained in:
commit
bcc0605036
@ -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) {
|
||||
|
@ -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";
|
||||
|
Loading…
Reference in New Issue
Block a user