From d41815ca18da271948ba7cf35bf3b940b32cd783 Mon Sep 17 00:00:00 2001 From: uboness Date: Thu, 18 Jun 2015 21:45:59 +0200 Subject: [PATCH] fixed version scheme in WatcherVersion Original commit: elastic/x-pack-elasticsearch@1007cd71a3ced312d1f01359765c56472ad3f8ac --- .../elasticsearch/watcher/WatcherVersion.java | 6 +- .../watcher/WatcherVersionTests.java | 55 +++++++++++++++++++ 2 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 src/test/java/org/elasticsearch/watcher/WatcherVersionTests.java diff --git a/src/main/java/org/elasticsearch/watcher/WatcherVersion.java b/src/main/java/org/elasticsearch/watcher/WatcherVersion.java index 09f3d651d89..4ead3d33d83 100644 --- a/src/main/java/org/elasticsearch/watcher/WatcherVersion.java +++ b/src/main/java/org/elasticsearch/watcher/WatcherVersion.java @@ -69,7 +69,7 @@ public class WatcherVersion implements Serializable { return WatcherVersion.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"); } @@ -179,9 +179,9 @@ public class WatcherVersion 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/watcher/WatcherVersionTests.java b/src/test/java/org/elasticsearch/watcher/WatcherVersionTests.java new file mode 100644 index 00000000000..93d11541085 --- /dev/null +++ b/src/test/java/org/elasticsearch/watcher/WatcherVersionTests.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.watcher; + +import com.carrotsearch.randomizedtesting.annotations.Repeat; +import org.elasticsearch.test.ElasticsearchTestCase; +import org.junit.Test; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; + +/** + * + */ +public class WatcherVersionTests extends ElasticsearchTestCase { + + @Test + @Repeat(iterations = 100) + 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(); + WatcherVersion version = WatcherVersion.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)); + } +}