diff --git a/plugin/src/main/java/org/elasticsearch/license/plugin/LicenseVersion.java b/plugin/src/main/java/org/elasticsearch/license/plugin/LicenseVersion.java index 4e2cad0d5cd..b6e96c68e1e 100644 --- a/plugin/src/main/java/org/elasticsearch/license/plugin/LicenseVersion.java +++ b/plugin/src/main/java/org/elasticsearch/license/plugin/LicenseVersion.java @@ -62,7 +62,7 @@ public class LicenseVersion implements Serializable { return LicenseVersion.CURRENT; } - String[] parts = version.split("\\."); + 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"); } @@ -76,10 +76,10 @@ public class LicenseVersion implements Serializable { int build = 99; if (parts.length == 4) { String buildStr = parts[3]; - if (buildStr.startsWith("Beta")) { + if (buildStr.startsWith("beta")) { build = Integer.parseInt(buildStr.substring(4)); } - if (buildStr.startsWith("RC")) { + if (buildStr.startsWith("rc")) { build = Integer.parseInt(buildStr.substring(2)) + 50; } } @@ -155,9 +155,9 @@ public class LicenseVersion implements Serializable { StringBuilder sb = new StringBuilder(); sb.append(major).append('.').append(minor).append('.').append(revision); if (build < 50) { - sb.append(".Beta").append(build); + sb.append("-beta").append(build); } else if (build < 99) { - sb.append(".RC").append(build - 50); + sb.append("-rc").append(build - 50); } return sb.toString(); } diff --git a/plugin/src/test/java/org/elasticsearch/license/plugin/LicenseVersionTests.java b/plugin/src/test/java/org/elasticsearch/license/plugin/LicenseVersionTests.java new file mode 100644 index 00000000000..fa3080fd676 --- /dev/null +++ b/plugin/src/test/java/org/elasticsearch/license/plugin/LicenseVersionTests.java @@ -0,0 +1,54 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.license.plugin; + +import org.elasticsearch.test.ElasticsearchTestCase; +import org.junit.Test; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; + +/** + * + */ +public class LicenseVersionTests extends ElasticsearchTestCase { + + @Test + public void testStrings() throws Exception { + boolean beta = randomBoolean(); + int buildNumber = beta ? randomIntBetween(0, 49) : randomIntBetween(0, 48); + int major = randomIntBetween(0, 20); + int minor = randomIntBetween(0, 20); + int revision = randomIntBetween(0, 20); + + String build = buildNumber == 0 ? "" : + beta ? "-beta" + buildNumber : "-rc" + buildNumber; + + + String versionName = new StringBuilder() + .append(major) + .append(".").append(minor) + .append(".").append(revision) + .append(build).toString(); + LicenseVersion version = LicenseVersion.fromString(versionName); + + logger.info("version: {}", versionName); + + assertThat(version.major, is((byte) major)); + assertThat(version.minor, is((byte) minor)); + assertThat(version.revision, is((byte) revision)); + if (buildNumber == 0) { + assertThat(version.build, is((byte) 99)); + } else if (beta) { + assertThat(version.build, is((byte) buildNumber)); + } else { + assertThat(version.build, is((byte) (buildNumber + 50))); + } + + assertThat(version.number(), equalTo(versionName)); + } + +}