diff options
author | Nik Everett <nik9000@gmail.com> | 2017-06-08 10:19:06 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-08 10:19:06 -0400 |
commit | 4a8c09c5f143e584b7d51a510f15a61cd3f59560 (patch) | |
tree | b72e4aec60f1b8a2c732d77e636c2df04dec7fcb /test | |
parent | 542b0616acd66771281fe2bfdcc2993ec518ed5d (diff) |
Make randomVersionBetween work with unreleased versions (#25042)
Test: randomVersionBetween works with unreleased
Modifies randomVersionBetween so that it works with unreleased
versions. This should make switching a version from unreleased
to released much simpler.
Diffstat (limited to 'test')
-rw-r--r-- | test/framework/src/main/java/org/elasticsearch/test/VersionUtils.java | 51 | ||||
-rw-r--r-- | test/framework/src/test/java/org/elasticsearch/test/VersionUtilsTests.java | 8 |
2 files changed, 39 insertions, 20 deletions
diff --git a/test/framework/src/main/java/org/elasticsearch/test/VersionUtils.java b/test/framework/src/main/java/org/elasticsearch/test/VersionUtils.java index 9f93a63919..107abcd1d1 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/VersionUtils.java +++ b/test/framework/src/main/java/org/elasticsearch/test/VersionUtils.java @@ -20,6 +20,7 @@ package org.elasticsearch.test; import org.elasticsearch.Version; +import org.elasticsearch.common.Nullable; import org.elasticsearch.common.collect.Tuple; import java.lang.reflect.Field; @@ -110,17 +111,21 @@ public class VersionUtils { private static final List<Version> RELEASED_VERSIONS; private static final List<Version> UNRELEASED_VERSIONS; + private static final List<Version> ALL_VERSIONS; static { Tuple<List<Version>, List<Version>> versions = resolveReleasedVersions(Version.CURRENT, Version.class); RELEASED_VERSIONS = versions.v1(); UNRELEASED_VERSIONS = versions.v2(); + List<Version> allVersions = new ArrayList<>(RELEASED_VERSIONS.size() + UNRELEASED_VERSIONS.size()); + allVersions.addAll(RELEASED_VERSIONS); + allVersions.addAll(UNRELEASED_VERSIONS); + Collections.sort(allVersions); + ALL_VERSIONS = unmodifiableList(allVersions); } /** * Returns an immutable, sorted list containing all released versions. - * - * @return all released versions */ public static List<Version> allReleasedVersions() { return RELEASED_VERSIONS; @@ -128,27 +133,40 @@ public class VersionUtils { /** * Returns an immutable, sorted list containing all unreleased versions. - * - * @return all unreleased versions */ public static List<Version> allUnreleasedVersions() { return UNRELEASED_VERSIONS; } + /** + * Returns an immutable, sorted list containing all versions, both released and unreleased. + */ + public static List<Version> allVersions() { + return ALL_VERSIONS; + } + + /** + * Get the released version before {@code version}. + */ public static Version getPreviousVersion(Version version) { int index = RELEASED_VERSIONS.indexOf(version); assert index > 0; return RELEASED_VERSIONS.get(index - 1); } - /** Returns the {@link Version} before the {@link Version#CURRENT} */ + /** + * Get the released version before {@link Version#CURRENT}. + */ public static Version getPreviousVersion() { Version version = getPreviousVersion(Version.CURRENT); assert version.before(Version.CURRENT); return version; } - /** Returns the {@link Version} before the {@link Version#CURRENT} where the minor version is less than the currents minor version. */ + /** + * Returns the released {@link Version} before the {@link Version#CURRENT} + * where the minor version is less than the currents minor version. + */ public static Version getPreviousMinorVersion() { Version version = Version.CURRENT; do { @@ -158,25 +176,25 @@ public class VersionUtils { return version; } - /** Returns the oldest {@link Version} */ + /** Returns the oldest released {@link Version} */ public static Version getFirstVersion() { return RELEASED_VERSIONS.get(0); } /** Returns a random {@link Version} from all available versions. */ public static Version randomVersion(Random random) { - return RELEASED_VERSIONS.get(random.nextInt(RELEASED_VERSIONS.size())); + return ALL_VERSIONS.get(random.nextInt(ALL_VERSIONS.size())); } /** Returns a random {@link Version} between <code>minVersion</code> and <code>maxVersion</code> (inclusive). */ - public static Version randomVersionBetween(Random random, Version minVersion, Version maxVersion) { + public static Version randomVersionBetween(Random random, @Nullable Version minVersion, @Nullable Version maxVersion) { int minVersionIndex = 0; if (minVersion != null) { - minVersionIndex = RELEASED_VERSIONS.indexOf(minVersion); + minVersionIndex = ALL_VERSIONS.indexOf(minVersion); } - int maxVersionIndex = RELEASED_VERSIONS.size() - 1; + int maxVersionIndex = ALL_VERSIONS.size() - 1; if (maxVersion != null) { - maxVersionIndex = RELEASED_VERSIONS.indexOf(maxVersion); + maxVersionIndex = ALL_VERSIONS.indexOf(maxVersion); } if (minVersionIndex == -1) { throw new IllegalArgumentException("minVersion [" + minVersion + "] does not exist."); @@ -187,14 +205,7 @@ public class VersionUtils { } else { // minVersionIndex is inclusive so need to add 1 to this index int range = maxVersionIndex + 1 - minVersionIndex; - return RELEASED_VERSIONS.get(minVersionIndex + random.nextInt(range)); - } - } - - public static boolean isSnapshot(Version version) { - if (Version.CURRENT.equals(version)) { - return true; + return ALL_VERSIONS.get(minVersionIndex + random.nextInt(range)); } - return false; } } diff --git a/test/framework/src/test/java/org/elasticsearch/test/VersionUtilsTests.java b/test/framework/src/test/java/org/elasticsearch/test/VersionUtilsTests.java index 1726ebf8ac..4f1e10ef12 100644 --- a/test/framework/src/test/java/org/elasticsearch/test/VersionUtilsTests.java +++ b/test/framework/src/test/java/org/elasticsearch/test/VersionUtilsTests.java @@ -28,6 +28,8 @@ import java.util.List; import static java.util.Collections.emptyList; import static java.util.Collections.singletonList; import static java.util.stream.Collectors.toList; +import static org.hamcrest.Matchers.greaterThanOrEqualTo; +import static org.hamcrest.Matchers.lessThanOrEqualTo; public class VersionUtilsTests extends ESTestCase { @@ -86,6 +88,12 @@ public class VersionUtilsTests extends ESTestCase { assertEquals(got, VersionUtils.getFirstVersion()); got = VersionUtils.randomVersionBetween(random(), Version.CURRENT, null); assertEquals(got, Version.CURRENT); + + // max or min can be an unreleased version + Version unreleased = randomFrom(VersionUtils.allUnreleasedVersions()); + assertThat(VersionUtils.randomVersionBetween(random(), null, unreleased), lessThanOrEqualTo(unreleased)); + assertThat(VersionUtils.randomVersionBetween(random(), unreleased, null), greaterThanOrEqualTo(unreleased)); + assertEquals(unreleased, VersionUtils.randomVersionBetween(random(), unreleased, unreleased)); } static class TestReleaseBranch { |