Allow -SNAPSHOT versions to be parsed by Version.fromString
This commit is contained in:
parent
cc8e8e6b89
commit
a49b39cc21
|
@ -435,17 +435,22 @@ public class Version implements Serializable {
|
|||
if (!Strings.hasLength(version)) {
|
||||
return Version.CURRENT;
|
||||
}
|
||||
|
||||
final boolean snapshot;
|
||||
if (snapshot = version.endsWith("-SNAPSHOT")) {
|
||||
version = version.substring(0, version.length() - 9);
|
||||
}
|
||||
String[] parts = version.split("\\.");
|
||||
if (parts.length < 3 || parts.length > 4) {
|
||||
throw new IllegalArgumentException("the version needs to contain major, minor and revision, and optionally the build");
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
//we reverse the version id calculation based on some assumption as we can't reliably reverse the modulo
|
||||
int major = Integer.parseInt(parts[0]) * 1000000;
|
||||
int minor = Integer.parseInt(parts[1]) * 10000;
|
||||
int revision = Integer.parseInt(parts[2]) * 100;
|
||||
final int major = Integer.parseInt(parts[0]) * 1000000;
|
||||
final int minor = Integer.parseInt(parts[1]) * 10000;
|
||||
final int revision = Integer.parseInt(parts[2]) * 100;
|
||||
|
||||
|
||||
int build = 99;
|
||||
if (parts.length == 4) {
|
||||
|
@ -458,7 +463,11 @@ public class Version implements Serializable {
|
|||
}
|
||||
}
|
||||
|
||||
return fromId(major + minor + revision + build);
|
||||
final Version versionFromId = fromId(major + minor + revision + build);
|
||||
if (snapshot != versionFromId.snapshot()) {
|
||||
return new Version(versionFromId.id, snapshot, versionFromId.luceneVersion);
|
||||
}
|
||||
return versionFromId;
|
||||
|
||||
} catch(NumberFormatException e) {
|
||||
throw new IllegalArgumentException("unable to parse version " + version, e);
|
||||
|
|
|
@ -92,7 +92,12 @@ public class VersionTests extends ElasticsearchTestCase {
|
|||
final int iters = scaledRandomIntBetween(100, 1000);
|
||||
for (int i = 0; i < iters; i++) {
|
||||
Version version = randomVersion();
|
||||
assertThat(Version.fromString(version.number()), sameInstance(version));
|
||||
if (version.snapshot()) { // number doesn't include SNAPSHOT but the parser checks for that
|
||||
assertEquals(Version.fromString(version.number()), version);
|
||||
} else {
|
||||
assertThat(Version.fromString(version.number()), sameInstance(version));
|
||||
}
|
||||
assertFalse(Version.fromString(version.number()).snapshot());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -131,6 +136,21 @@ public class VersionTests extends ElasticsearchTestCase {
|
|||
assertThat(Version.V_1_0_0_RC2.minimumCompatibilityVersion(), equalTo(Version.V_1_0_0_RC2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseVersion() {
|
||||
final int iters = scaledRandomIntBetween(100, 1000);
|
||||
for (int i = 0; i < iters; i++) {
|
||||
Version version = randomVersion();
|
||||
String stringVersion = version.toString();
|
||||
if (version.snapshot() == false && randomBoolean()) {
|
||||
version = new Version(version.id, true, version.luceneVersion);
|
||||
}
|
||||
Version parsedVersion = Version.fromString(version.toString());
|
||||
assertEquals(version, parsedVersion);
|
||||
assertEquals(version.snapshot(), parsedVersion.snapshot());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseLenient() {
|
||||
// note this is just a silly sanity check, we test it in lucene
|
||||
|
|
Loading…
Reference in New Issue