fixed version scheme in WatcherVersion

Original commit: elastic/x-pack-elasticsearch@1007cd71a3
This commit is contained in:
uboness 2015-06-18 21:45:59 +02:00
parent bc4ce6f153
commit d41815ca18
2 changed files with 58 additions and 3 deletions

View File

@ -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();
}

View File

@ -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));
}
}