Explain how unreleased versions should be added to the codebase without adding it to Version.java (#20892)

Sometimes it's useful / needed to use unreleased Version constants but we should not add those to the Version.java class for several reasons ie. BWC tests and assertions along those lines. Yet, it's not really obvious how to do that so I added some comments and a simple test for this.
This commit is contained in:
Simon Willnauer 2016-10-12 17:49:24 +02:00 committed by GitHub
parent bbe6555b7a
commit 06cfffa0a9
2 changed files with 22 additions and 0 deletions

View File

@ -95,6 +95,15 @@ public class Version {
public static final Version V_6_0_0_alpha1 = new Version(V_6_0_0_alpha1_ID, org.apache.lucene.util.Version.LUCENE_6_2_0);
public static final Version CURRENT = V_6_0_0_alpha1;
/* NOTE: don't add unreleased version to this list except of the version assigned to CURRENT.
* If you need a version that doesn't exist here for instance V_5_1_0 then go and create such a version
* as a constant where you need it:
* <pre>
* public static final Version V_5_1_0_UNRELEASED = new Version(5010099, Version.CURRENT.luceneVersion);
* </pre>
* Then go to VersionsTest.java and add a test for this constant VersionTests#testUnknownVersions().
* This is particularly useful if you are building a feature that needs a BWC layer for this unreleased version etc.*/
static {
assert CURRENT.luceneVersion.equals(org.apache.lucene.util.Version.LATEST) : "Version must be upgraded to ["
+ org.apache.lucene.util.Version.LATEST + "] is still set to [" + CURRENT.luceneVersion + "]";

View File

@ -25,6 +25,7 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.VersionUtils;
import org.hamcrest.Matchers;
import org.junit.Assert;
import java.lang.reflect.Modifier;
import java.util.HashMap;
@ -279,4 +280,16 @@ public class VersionTests extends ESTestCase {
}
}
}
private static final Version V_20_0_0_UNRELEASED = new Version(20000099, Version.CURRENT.luceneVersion);
// see comment in Version.java about this test
public void testUnknownVersions() {
assertUnknownVersion(V_20_0_0_UNRELEASED);
expectThrows(AssertionError.class, () -> assertUnknownVersion(Version.CURRENT));
}
public static void assertUnknownVersion(Version version) {
assertFalse("Version " + version + " has been releaed don't use a new instance of this version",
VersionUtils.allVersions().contains(version));
}
}