From a6ffb7a62f263db7de24317329be9f04de6e4757 Mon Sep 17 00:00:00 2001 From: LuisStruggle <18300767078@163.com> Date: Mon, 12 Dec 2022 19:06:04 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix=EF=BC=9AReUtil.replaceAll()=E6=96=B9?= =?UTF-8?q?=E6=B3=95=EF=BC=8C=E5=BD=93replacementTemplate=E4=B8=BAnull?= =?UTF-8?q?=E5=AF=B9=E8=B1=A1=E6=97=B6=EF=BC=8C=E5=87=BA=E7=8E=B0=E7=A9=BA?= =?UTF-8?q?=E6=8C=87=E9=92=88=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/cn/hutool/core/regex/ReUtil.java | 3 +++ .../src/test/java/cn/hutool/core/util/ReUtilTest.java | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/hutool-core/src/main/java/cn/hutool/core/regex/ReUtil.java b/hutool-core/src/main/java/cn/hutool/core/regex/ReUtil.java index 4ecbedc41..c31bc2c7e 100755 --- a/hutool-core/src/main/java/cn/hutool/core/regex/ReUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/regex/ReUtil.java @@ -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) { diff --git a/hutool-core/src/test/java/cn/hutool/core/util/ReUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/util/ReUtilTest.java index e9504494d..79de3f2cc 100755 --- a/hutool-core/src/test/java/cn/hutool/core/util/ReUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/util/ReUtilTest.java @@ -115,6 +115,16 @@ public class ReUtilTest { Assert.assertEquals("ZZZaaabbbccc中文->1234<-", replaceAll); } + @Test + public void replaceAllTest3() { + // 修改前:ReUtil.replaceAll()方法,当replacementTemplate为null对象时,出现空指针异常 + final String str = null; + // Assert.assertThrows(NullPointerException.class, () -> ReUtil.replaceAll(content, "(\\d+)", str)); + + // 修改后:判断ReUtil.replaceAll()方法,当replacementTemplate为null对象时,提示为非法的参数异常:ReplacementTemplate must be not null ! + Assert.assertThrows(IllegalArgumentException.class, () -> ReUtil.replaceAll(content, "(\\d+)", str)); + } + @Test public void replaceTest() { final String str = "AAABBCCCBBDDDBB"; From 7bc020607f1bca9493f4f961a02ea92b5e5525e0 Mon Sep 17 00:00:00 2001 From: LuisStruggle <18300767078@163.com> Date: Mon, 12 Dec 2022 19:30:02 +0800 Subject: [PATCH 2/2] =?UTF-8?q?test=EF=BC=9AReUtil.replaceAll()=E6=96=B9?= =?UTF-8?q?=E6=B3=95=EF=BC=8C=E5=BD=93replacementTemplate=E4=B8=BAnull?= =?UTF-8?q?=E5=AF=B9=E8=B1=A1=E6=97=B6=EF=BC=8C=E5=87=BA=E7=8E=B0=E7=A9=BA?= =?UTF-8?q?=E6=8C=87=E9=92=88=E5=BC=82=E5=B8=B8=EF=BC=88=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E6=96=B9=E6=B3=95=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/test/java/cn/hutool/core/util/ReUtilTest.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/hutool-core/src/test/java/cn/hutool/core/util/ReUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/util/ReUtilTest.java index 79de3f2cc..41c9c0db3 100755 --- a/hutool-core/src/test/java/cn/hutool/core/util/ReUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/util/ReUtilTest.java @@ -119,10 +119,15 @@ public class ReUtilTest { public void replaceAllTest3() { // 修改前:ReUtil.replaceAll()方法,当replacementTemplate为null对象时,出现空指针异常 final String str = null; - // Assert.assertThrows(NullPointerException.class, () -> ReUtil.replaceAll(content, "(\\d+)", str)); + 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, "(\\d+)", str)); + Assert.assertThrows(IllegalArgumentException.class, () -> ReUtil.replaceAll(content, pattern, str)); } @Test