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 @Override
protected void starting(Description description) { protected void starting(Description description) {
boolean nullify = RandomizedContext.current().getRandom().nextBoolean(); // see https://stackoverflow.com/a/2591122
if (nullify) { // this test is only meant to run under java < 9.0
oldSourceHome = ExternalPaths.SOURCE_HOME; if (System.getProperty("java.version").startsWith("1.")) {
try { boolean nullify = RandomizedContext.current().getRandom().nextBoolean();
sourceHomeField = ExternalPaths.class.getDeclaredField("SOURCE_HOME"); if (nullify) {
setFinalStatic(sourceHomeField, null); oldSourceHome = ExternalPaths.SOURCE_HOME;
} catch (Exception e) { try {
e.printStackTrace(); sourceHomeField = ExternalPaths.class.getDeclaredField("SOURCE_HOME");
fail(e.getMessage()); 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") @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 @Override
protected void finished(Description description) { protected void finished(Description description) {
if (sourceHomeField != null) { // see https://stackoverflow.com/a/2591122
try { // this test is only meant to run under java < 9.0
sourceHomeField.set(null, oldSourceHome); if (System.getProperty("java.version").startsWith("1.")) {
} catch (IllegalAccessException e) { if (sourceHomeField != null) {
e.printStackTrace(); try {
fail(e.getMessage()); sourceHomeField.set(null, oldSourceHome);
} catch (IllegalAccessException e) {
e.printStackTrace();
fail(e.getMessage());
}
} }
} }
super.finished(description);
} }
} }