Skip to content

Commit

Permalink
add VersionTest
Browse files Browse the repository at this point in the history
  • Loading branch information
wangliang181230 committed Feb 22, 2023
1 parent c4d3c1b commit df83b2e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
11 changes: 8 additions & 3 deletions core/src/main/java/io/seata/core/protocol/Version.java
Original file line number Diff line number Diff line change
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
66 changes: 66 additions & 0 deletions core/src/test/java/io/seata/core/protocol/VersionTest.java
Original file line number Diff line number Diff line change
@@ -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 df83b2e

Please sign in to comment.