修复VersionComparator传入空字符串报错问题

This commit is contained in:
Looly 2024-06-12 00:31:25 +08:00
parent 6ff4309cce
commit 3aa9bdfcfa
4 changed files with 21 additions and 10 deletions

View File

@ -2,7 +2,7 @@
# 🚀Changelog # 🚀Changelog
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------
# 5.8.29(2024-06-07) # 5.8.29(2024-06-12)
### 🐣新特性 ### 🐣新特性
* 【core 】 DateUtil增加offsetYear方法 * 【core 】 DateUtil增加offsetYear方法
@ -11,7 +11,8 @@
### 🐞Bug修复 ### 🐞Bug修复
* 【core 】 修复AnnotationUtil可能的空指针错误 * 【core 】 修复AnnotationUtil可能的空指针错误
* 【core 】 修复BeanUtil.isBean判断Dict错误问题issue#I9VTZG@gitee * 【core 】 修复BeanUtil.isBean判断Dict错误问题issue#I9VTZG@Gitee
* 【core 】 修复VersionComparator传入空字符串报错问题pr#3614@Github
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------
# 5.8.28(2024-05-29) # 5.8.28(2024-05-29)

View File

@ -57,9 +57,9 @@ public class VersionComparator implements Comparator<String>, Serializable {
} }
if (version1 == null && version2 == null) { if (version1 == null && version2 == null) {
return 0; return 0;
} else if (version1 == null || "".equals(version1)) {// null或""视为最小版本排在前 } else if (version1 == null) {// null或""视为最小版本排在前
return -1; return -1;
} else if (version2 == null || "".equals(version2)) { } else if (version2 == null) {
return 1; return 1;
} }

View File

@ -60,14 +60,16 @@ public class Version implements Comparable<Version>, Serializable {
public Version(final String v) { public Version(final String v) {
Assert.notNull(v, "Null version string"); Assert.notNull(v, "Null version string");
final int n = v.length(); final int n = v.length();
if (n == 0){
throw new IllegalArgumentException("Empty version string");
}
this.version = v; this.version = v;
this.sequence = new ArrayList<>(4); this.sequence = new ArrayList<>(4);
this.pre = new ArrayList<>(2); this.pre = new ArrayList<>(2);
this.build = new ArrayList<>(2); this.build = new ArrayList<>(2);
if (n == 0){
return;
}
int i = 0; int i = 0;
char c = v.charAt(i); char c = v.charAt(i);
// 不检查开头字符为数字字母按照字典顺序的数字对待 // 不检查开头字符为数字字母按照字典顺序的数字对待

View File

@ -3,9 +3,6 @@ package cn.hutool.core.comparator;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import java.util.Arrays;
import java.util.stream.Collectors;
/** /**
* 版本比较单元测试 * 版本比较单元测试
* *
@ -13,6 +10,17 @@ import java.util.stream.Collectors;
*/ */
public class VersionComparatorTest { public class VersionComparatorTest {
@Test
public void compareEmptyTest() {
int compare = VersionComparator.INSTANCE.compare("", "1.12.1");
Assert.assertTrue(compare < 0);
compare = VersionComparator.INSTANCE.compare("", null);
Assert.assertTrue(compare > 0);
compare = VersionComparator.INSTANCE.compare(null, "");
Assert.assertTrue(compare < 0);
}
@Test @Test
public void versionComparatorTest1() { public void versionComparatorTest1() {
int compare = VersionComparator.INSTANCE.compare("1.2.1", "1.12.1"); int compare = VersionComparator.INSTANCE.compare("1.2.1", "1.12.1");