mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
修复VersionComparator传入空字符串报错问题
This commit is contained in:
@@ -57,9 +57,9 @@ public class VersionComparator implements Comparator<String>, Serializable {
|
||||
}
|
||||
if (version1 == null && version2 == null) {
|
||||
return 0;
|
||||
} else if (version1 == null || "".equals(version1)) {// null或""视为最小版本,排在前
|
||||
} else if (version1 == null) {// null或""视为最小版本,排在前
|
||||
return -1;
|
||||
} else if (version2 == null || "".equals(version2)) {
|
||||
} else if (version2 == null) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@@ -60,14 +60,16 @@ public class Version implements Comparable<Version>, Serializable {
|
||||
public Version(final String v) {
|
||||
Assert.notNull(v, "Null version string");
|
||||
final int n = v.length();
|
||||
if (n == 0){
|
||||
throw new IllegalArgumentException("Empty version string");
|
||||
}
|
||||
|
||||
this.version = v;
|
||||
this.sequence = new ArrayList<>(4);
|
||||
this.pre = new ArrayList<>(2);
|
||||
this.build = new ArrayList<>(2);
|
||||
|
||||
if (n == 0){
|
||||
return;
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
char c = v.charAt(i);
|
||||
// 不检查开头字符为数字,字母按照字典顺序的数字对待
|
||||
|
@@ -3,9 +3,6 @@ package cn.hutool.core.comparator;
|
||||
import org.junit.Assert;
|
||||
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 {
|
||||
|
||||
@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
|
||||
public void versionComparatorTest1() {
|
||||
int compare = VersionComparator.INSTANCE.compare("1.2.1", "1.12.1");
|
||||
|
Reference in New Issue
Block a user