Skip to content

Commit

Permalink
optimize: unified project version (#5384)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangliang181230 committed Feb 22, 2023
1 parent 1ab95d7 commit d1c4b78
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 5 deletions.
2 changes: 1 addition & 1 deletion build/pom.xml
Expand Up @@ -82,7 +82,7 @@

<!-- Maven plugin versions -->
<!-- Build -->
<easyj-maven-plugin.version>1.0.5</easyj-maven-plugin.version>
<easyj-maven-plugin.version>1.1.2</easyj-maven-plugin.version>
<maven-clean-plugin.version>3.1.0</maven-clean-plugin.version>
<!-- Compiler -->
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
Expand Down
21 changes: 21 additions & 0 deletions core/pom.xml
Expand Up @@ -69,4 +69,25 @@
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>icu.easyj.maven.plugins</groupId>
<artifactId>easyj-maven-plugin</artifactId>
<configuration>
<mainPaths>
<path>io/seata/core/protocol/VersionInfo.java.template</path>
</mainPaths>
</configuration>
<executions>
<execution>
<id>generate-java-by-template</id>
<goals>
<goal>replace-java</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
13 changes: 9 additions & 4 deletions core/src/main/java/io/seata/core/protocol/Version.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand All @@ -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;
}
Expand Down
@@ -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}";

}
66 changes: 66 additions & 0 deletions 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");
});
}
}

0 comments on commit d1c4b78

Please sign in to comment.