diff --git a/src/main/java/org/elasticsearch/shield/ShieldVersion.java b/src/main/java/org/elasticsearch/shield/ShieldVersion.java index 5c31e9645c5..2d075cf0de5 100644 --- a/src/main/java/org/elasticsearch/shield/ShieldVersion.java +++ b/src/main/java/org/elasticsearch/shield/ShieldVersion.java @@ -82,7 +82,7 @@ public class ShieldVersion implements Serializable { return ShieldVersion.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"); } @@ -96,10 +96,10 @@ public class ShieldVersion 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; } } @@ -191,9 +191,9 @@ public class ShieldVersion 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/src/test/java/org/elasticsearch/shield/ShieldVersionTests.java b/src/test/java/org/elasticsearch/shield/ShieldVersionTests.java new file mode 100644 index 00000000000..1523e7181fc --- /dev/null +++ b/src/test/java/org/elasticsearch/shield/ShieldVersionTests.java @@ -0,0 +1,55 @@ +/* + * 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.shield; + +import org.elasticsearch.test.ElasticsearchTestCase; +import org.junit.Test; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; + +/** + * + */ +public class ShieldVersionTests extends ElasticsearchTestCase { + + @Test + public void testStrings() throws Exception { + for (int i = 0; i < 100; i++) { + 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(); + ShieldVersion version = ShieldVersion.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)); + } + } +}