DATACMNS-719 - Version now parses strings with non-numeric suffixes correctly.
Extended Version value object to correctly dump a non-numeric last segment of a version string to parse. Incorrect source version strings now raise an IllegalArgumentException better explaining the cause. Related tickets: DATACMNS-496.
This commit is contained in:
@@ -1,3 +1,18 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013-2015 the original author or authors.
|
||||||
|
*
|
||||||
|
* 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 org.springframework.data.util;
|
package org.springframework.data.util;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -13,6 +28,8 @@ import org.springframework.util.StringUtils;
|
|||||||
*/
|
*/
|
||||||
public class Version implements Comparable<Version> {
|
public class Version implements Comparable<Version> {
|
||||||
|
|
||||||
|
private static final String VERSION_PARSE_ERROR = "Invalid version string! Could not parse segment %s within %s.";
|
||||||
|
|
||||||
private final int major;
|
private final int major;
|
||||||
private final int minor;
|
private final int minor;
|
||||||
private final int bugfix;
|
private final int bugfix;
|
||||||
@@ -53,7 +70,16 @@ public class Version implements Comparable<Version> {
|
|||||||
int[] intParts = new int[parts.length];
|
int[] intParts = new int[parts.length];
|
||||||
|
|
||||||
for (int i = 0; i < parts.length; i++) {
|
for (int i = 0; i < parts.length; i++) {
|
||||||
intParts[i] = Integer.parseInt(parts[i].replaceAll("\\D.*", ""));
|
|
||||||
|
String input = i == parts.length - 1 ? parts[i].replaceAll("\\D.*", "") : parts[i];
|
||||||
|
|
||||||
|
if (StringUtils.hasText(input)) {
|
||||||
|
try {
|
||||||
|
intParts[i] = Integer.parseInt(input);
|
||||||
|
} catch (IllegalArgumentException o_O) {
|
||||||
|
throw new IllegalArgumentException(String.format(VERSION_PARSE_ERROR, input, version), o_O);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Version(intParts);
|
return new Version(intParts);
|
||||||
|
|||||||
@@ -1,9 +1,27 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2015 the original author or authors.
|
||||||
|
*
|
||||||
|
* 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 org.springframework.data.util;
|
package org.springframework.data.util;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.*;
|
import static org.hamcrest.Matchers.*;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import org.hamcrest.Matchers;
|
||||||
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.rules.ExpectedException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unit tests for {@link Version}.
|
* Unit tests for {@link Version}.
|
||||||
@@ -12,6 +30,11 @@ import org.junit.Test;
|
|||||||
*/
|
*/
|
||||||
public class VersionUnitTests {
|
public class VersionUnitTests {
|
||||||
|
|
||||||
|
public @Rule ExpectedException exception = ExpectedException.none();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see DATCMNS-384
|
||||||
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void sameVersionsEqualOneDigits() {
|
public void sameVersionsEqualOneDigits() {
|
||||||
|
|
||||||
@@ -22,6 +45,9 @@ public class VersionUnitTests {
|
|||||||
assertThat(second, is(first));
|
assertThat(second, is(first));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see DATCMNS-384
|
||||||
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void sameVersionsEqualTwoDigits() {
|
public void sameVersionsEqualTwoDigits() {
|
||||||
|
|
||||||
@@ -32,6 +58,9 @@ public class VersionUnitTests {
|
|||||||
assertThat(second, is(first));
|
assertThat(second, is(first));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see DATCMNS-384
|
||||||
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void sameVersionsEqualThreeDigits() {
|
public void sameVersionsEqualThreeDigits() {
|
||||||
|
|
||||||
@@ -42,6 +71,9 @@ public class VersionUnitTests {
|
|||||||
assertThat(second, is(first));
|
assertThat(second, is(first));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see DATCMNS-384
|
||||||
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void sameVersionsEqualFourDigits() {
|
public void sameVersionsEqualFourDigits() {
|
||||||
|
|
||||||
@@ -52,6 +84,9 @@ public class VersionUnitTests {
|
|||||||
assertThat(second, is(first));
|
assertThat(second, is(first));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see DATCMNS-384
|
||||||
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void parsesVersionCorrectlyOneDigits() {
|
public void parsesVersionCorrectlyOneDigits() {
|
||||||
|
|
||||||
@@ -59,6 +94,9 @@ public class VersionUnitTests {
|
|||||||
assertThat(version, is(new Version(5)));
|
assertThat(version, is(new Version(5)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see DATCMNS-384
|
||||||
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void parsesVersionCorrectlyTwoDigits() {
|
public void parsesVersionCorrectlyTwoDigits() {
|
||||||
|
|
||||||
@@ -66,6 +104,9 @@ public class VersionUnitTests {
|
|||||||
assertThat(version, is(new Version(5, 2)));
|
assertThat(version, is(new Version(5, 2)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see DATCMNS-384
|
||||||
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void parsesVersionCorrectlyThreeDigits() {
|
public void parsesVersionCorrectlyThreeDigits() {
|
||||||
|
|
||||||
@@ -73,6 +114,9 @@ public class VersionUnitTests {
|
|||||||
assertThat(version, is(new Version(12, 1, 3)));
|
assertThat(version, is(new Version(12, 1, 3)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see DATCMNS-384
|
||||||
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void parsesVersionCorrectlyFourDigits() {
|
public void parsesVersionCorrectlyFourDigits() {
|
||||||
|
|
||||||
@@ -80,6 +124,9 @@ public class VersionUnitTests {
|
|||||||
assertThat(version, is(new Version(12, 1, 3, 1000)));
|
assertThat(version, is(new Version(12, 1, 3, 1000)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see DATCMNS-384
|
||||||
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void comparesToCorrectly() {
|
public void comparesToCorrectly() {
|
||||||
|
|
||||||
@@ -112,6 +159,9 @@ public class VersionUnitTests {
|
|||||||
assertThat(nextBuild.compareTo(version), is(greaterThan(0)));
|
assertThat(nextBuild.compareTo(version), is(greaterThan(0)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see DATCMNS-384
|
||||||
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void removesTrailingZerosAfterSecondValueForToString() {
|
public void removesTrailingZerosAfterSecondValueForToString() {
|
||||||
|
|
||||||
@@ -131,4 +181,25 @@ public class VersionUnitTests {
|
|||||||
public void parseShouldRemoveNonNumericVersionParts() {
|
public void parseShouldRemoveNonNumericVersionParts() {
|
||||||
assertThat(Version.parse("2.0.0-rc1"), is(new Version(2, 0, 0)));
|
assertThat(Version.parse("2.0.0-rc1"), is(new Version(2, 0, 0)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see DATACMNS-719, DATACMNS-496
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void removesNonNumericSuffix() {
|
||||||
|
assertThat(Version.parse("4.2.0.RELEASE"), is(new Version(4, 2, 0)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see DATACMNS-719, DATACMNS-496
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void rejectsNonNumericPartOnNonLastPosition() {
|
||||||
|
|
||||||
|
exception.expect(IllegalArgumentException.class);
|
||||||
|
exception.expectCause(Matchers.<Throwable> instanceOf(IllegalArgumentException.class));
|
||||||
|
exception.expectMessage("1.RELEASE.2");
|
||||||
|
|
||||||
|
Version.parse("1.RELEASE.2");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user