Explorar o código

优化版本解析

zhouhao %!s(int64=6) %!d(string=hai) anos
pai
achega
667a8020af

+ 11 - 7
hsweb-starter/hsweb-spring-boot-starter/src/main/java/org/hswebframework/web/starter/SystemVersion.java

@@ -202,18 +202,22 @@ class Version implements Comparable<Version> {
         if (null == version) {
         if (null == version) {
             return;
             return;
         }
         }
+        version = version.toLowerCase();
+
         boolean snapshot = version.toLowerCase().contains("snapshot");
         boolean snapshot = version.toLowerCase().contains("snapshot");
-        version = version.toLowerCase()
-                .replace(".snapshot", "")
-                .replace("-snapshot", "")
-                .replace("-rc", "")
-                .replace("-release", "");
-        String[] ver = version.split("[.]");
+
+        String[] ver = version.split("[-]")[0].split("[.]");
         Integer[] numberVer = ListUtils.stringArr2intArr(ver);
         Integer[] numberVer = ListUtils.stringArr2intArr(ver);
-        if (numberVer.length < 1 || Arrays.stream(numberVer).anyMatch(Objects::isNull)) {
+        if (numberVer.length == 0) {
             numberVer = new Integer[]{1, 0, 0};
             numberVer = new Integer[]{1, 0, 0};
             log.warn("解析版本号失败:{},将使用默认版本号:1.0.0,请检查hsweb-starter.js配置内容!", version);
             log.warn("解析版本号失败:{},将使用默认版本号:1.0.0,请检查hsweb-starter.js配置内容!", version);
         }
         }
+
+        for (int i = 0; i < numberVer.length; i++) {
+            if (numberVer[i] == null) {
+                numberVer[i] = 0;
+            }
+        }
         setVersion(numberVer[0],
         setVersion(numberVer[0],
                 numberVer.length <= 1 ? 0 : numberVer[1],
                 numberVer.length <= 1 ? 0 : numberVer[1],
                 numberVer.length <= 2 ? 0 : numberVer[2],
                 numberVer.length <= 2 ? 0 : numberVer[2],

+ 46 - 0
hsweb-starter/hsweb-spring-boot-starter/src/test/java/org/hswebframework/web/starter/SystemVersionTest.java

@@ -0,0 +1,46 @@
+package org.hswebframework.web.starter;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ * @author zhouhao
+ * @since 3.0.8
+ */
+public class SystemVersionTest {
+
+    @Test
+    public void test() {
+        SystemVersion version = new SystemVersion();
+
+        version.setVersion("3.0-RELEASE");
+        Assert.assertEquals(version.getMajorVersion(), 3);
+        Assert.assertEquals(version.getMinorVersion(), 0);
+        Assert.assertEquals(version.getRevisionVersion(), 0);
+
+        version.setVersion("3.0.1-RELEASE");
+        Assert.assertEquals(version.getMajorVersion(), 3);
+        Assert.assertEquals(version.getMinorVersion(), 0);
+        Assert.assertEquals(version.getRevisionVersion(), 1);
+        version.setVersion("3.2.1-SNAPSHOT");
+        Assert.assertEquals(version.getMajorVersion(), 3);
+        Assert.assertEquals(version.getMinorVersion(), 2);
+        Assert.assertEquals(version.getRevisionVersion(), 1);
+        Assert.assertTrue(version.isSnapshot());
+
+        version.setVersion("3.1.2");
+        Assert.assertEquals(version.getMajorVersion(), 3);
+        Assert.assertEquals(version.getMinorVersion(), 1);
+        Assert.assertEquals(version.getRevisionVersion(), 2);
+
+        version.setVersion("3.1.2.RELEASE");
+        Assert.assertEquals(version.getMajorVersion(), 3);
+        Assert.assertEquals(version.getMinorVersion(), 1);
+        Assert.assertEquals(version.getRevisionVersion(), 2);
+
+    }
+
+
+}