diff --git a/build/pom.xml b/build/pom.xml index 96665a7d4c1..b4c9719141e 100644 --- a/build/pom.xml +++ b/build/pom.xml @@ -82,7 +82,7 @@ - 1.0.5 + 1.1.2 3.1.0 3.8.1 diff --git a/core/pom.xml b/core/pom.xml index 011bfba9dd3..ed099dce135 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -69,4 +69,25 @@ + + + + icu.easyj.maven.plugins + easyj-maven-plugin + + + io/seata/core/protocol/VersionInfo.java.template + + + + + generate-java-by-template + + replace-java + + + + + + diff --git a/core/src/main/java/io/seata/core/protocol/Version.java b/core/src/main/java/io/seata/core/protocol/Version.java index 0ed4bbd15c7..2d17156c02c 100644 --- a/core/src/main/java/io/seata/core/protocol/Version.java +++ b/core/src/main/java/io/seata/core/protocol/Version.java @@ -36,7 +36,7 @@ public class Version { /** * The constant CURRENT. */ - private static final String CURRENT = "2.0.0-SNAPSHOT"; + private static final String CURRENT = VersionInfo.VERSION; private static final String VERSION_0_7_1 = "0.7.1"; private static final String VERSION_1_5_0 = "1.5.0"; private static final int MAX_VERSION_DOT = 3; @@ -113,13 +113,18 @@ public static boolean isAboveOrEqualVersion150(String version) { } public static long convertVersion(String version) throws IncompatibleVersionException { + if (StringUtils.isBlank(version)) { + throw new IllegalArgumentException("The version must not be blank."); + } + String[] parts = StringUtils.split(version, '.'); - long result = 0L; - int i = 1; int size = parts.length; if (size > MAX_VERSION_DOT + 1) { throw new IncompatibleVersionException("incompatible version format:" + version); } + + long result = 0L; + int i = 1; size = MAX_VERSION_DOT + 1; for (String part : parts) { if (StringUtils.isNumeric(part)) { @@ -140,7 +145,7 @@ public static long convertVersionNotThrowException(String version) { try { return convertVersion(version); } catch (Exception e) { - LOGGER.error("convert version error,version:{}",version,e); + LOGGER.error("convert version error,version:{}", version, e); } return -1; } diff --git a/core/src/main/java/io/seata/core/protocol/VersionInfo.java.template b/core/src/main/java/io/seata/core/protocol/VersionInfo.java.template new file mode 100644 index 00000000000..218631b6e4d --- /dev/null +++ b/core/src/main/java/io/seata/core/protocol/VersionInfo.java.template @@ -0,0 +1,27 @@ +/* + * Copyright 1999-2019 Seata.io Group. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.seata.core.protocol; + +/** + * The interface VersionInfo. + * + * @author wang.liang + */ +interface VersionInfo { + + String VERSION = "${project.version}"; + +} diff --git a/core/src/test/java/io/seata/core/protocol/VersionTest.java b/core/src/test/java/io/seata/core/protocol/VersionTest.java new file mode 100644 index 00000000000..442da8a0658 --- /dev/null +++ b/core/src/test/java/io/seata/core/protocol/VersionTest.java @@ -0,0 +1,66 @@ +/* + * Copyright 1999-2019 Seata.io Group. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.seata.core.protocol; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +/** + * Test {@link Version}. + * + * @author wang.liang + */ +public class VersionTest { + + @Test + public void testConvertVersion() { + // case: success + Assertions.assertDoesNotThrow(() -> { + long v = Version.convertVersion(Version.getCurrent()); + Assertions.assertTrue(v > 0); + }); + Assertions.assertDoesNotThrow(() -> { + long v = Version.convertVersion("1.7.0-SNAPSHOT"); + Assertions.assertEquals(1070000, v); + }); + Assertions.assertDoesNotThrow(() -> { + long v = Version.convertVersion("1.7.0"); + Assertions.assertEquals(1070000, v); + }); + Assertions.assertDoesNotThrow(() -> { + long v = Version.convertVersion("1.7.0-native-rc1-SNAPSHOT"); + Assertions.assertEquals(1070000, v); + }); + Assertions.assertDoesNotThrow(() -> { + long v = Version.convertVersion("1.7.0-native-rc1"); + Assertions.assertEquals(1070000, v); + }); + + // case: fail + Assertions.assertThrows(IllegalArgumentException.class, () -> { + Version.convertVersion(null); + }); + Assertions.assertThrows(IllegalArgumentException.class, () -> { + Version.convertVersion(" "); + }); + Assertions.assertThrows(IncompatibleVersionException.class, () -> { + Version.convertVersion("1.7.0.native.rc1-SNAPSHOT"); + }); + Assertions.assertThrows(IncompatibleVersionException.class, () -> { + Version.convertVersion("1.7.0.native.rc1"); + }); + } +}