SOLR-14283 ensure that the test only runs on JVM's where it can succeeed

This commit is contained in:
Gus Heck 2020-02-28 14:20:35 -05:00
parent a703c6e334
commit f083067328
1 changed files with 23 additions and 17 deletions

View File

@ -35,18 +35,21 @@ public class SourceHomeNullifier extends TestWatcher {
@Override
protected void starting(Description description) {
boolean nullify = RandomizedContext.current().getRandom().nextBoolean();
if (nullify) {
oldSourceHome = ExternalPaths.SOURCE_HOME;
try {
sourceHomeField = ExternalPaths.class.getDeclaredField("SOURCE_HOME");
setFinalStatic(sourceHomeField, null);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
// see https://stackoverflow.com/a/2591122
// this test is only meant to run under java < 9.0
if (System.getProperty("java.version").startsWith("1.")) {
boolean nullify = RandomizedContext.current().getRandom().nextBoolean();
if (nullify) {
oldSourceHome = ExternalPaths.SOURCE_HOME;
try {
sourceHomeField = ExternalPaths.class.getDeclaredField("SOURCE_HOME");
setFinalStatic(sourceHomeField, null);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
}
super.starting(description);
}
@SuppressForbidden(reason = "Need to tweak the value of a static final field on a per test basis")
@ -62,14 +65,17 @@ public class SourceHomeNullifier extends TestWatcher {
@Override
protected void finished(Description description) {
if (sourceHomeField != null) {
try {
sourceHomeField.set(null, oldSourceHome);
} catch (IllegalAccessException e) {
e.printStackTrace();
fail(e.getMessage());
// see https://stackoverflow.com/a/2591122
// this test is only meant to run under java < 9.0
if (System.getProperty("java.version").startsWith("1.")) {
if (sourceHomeField != null) {
try {
sourceHomeField.set(null, oldSourceHome);
} catch (IllegalAccessException e) {
e.printStackTrace();
fail(e.getMessage());
}
}
}
super.finished(description);
}
}