Skip to content

Commit bed975f

Browse files
committed
Log VM install changes during JDT debug tests, set default VM for execution environment in test
See: #782
1 parent 9ba1b48 commit bed975f

File tree

2 files changed

+94
-2
lines changed

2 files changed

+94
-2
lines changed

org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/AbstractDebugTest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,17 @@
128128
import org.eclipse.jdt.debug.tests.core.LiteralTests17;
129129
import org.eclipse.jdt.debug.tests.refactoring.MemberParser;
130130
import org.eclipse.jdt.debug.ui.IJavaDebugUIConstants;
131+
import org.eclipse.jdt.internal.debug.core.JDIDebugPlugin;
131132
import org.eclipse.jdt.internal.debug.core.model.JDIDebugTarget;
132133
import org.eclipse.jdt.internal.debug.eval.ast.engine.ASTEvaluationEngine;
133134
import org.eclipse.jdt.internal.debug.ui.BreakpointUtils;
134135
import org.eclipse.jdt.internal.debug.ui.IJDIPreferencesConstants;
135136
import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
136137
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
137138
import org.eclipse.jdt.launching.IVMInstall;
139+
import org.eclipse.jdt.launching.IVMInstallChangedListener;
138140
import org.eclipse.jdt.launching.JavaRuntime;
141+
import org.eclipse.jdt.launching.PropertyChangeEvent;
139142
import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
140143
import org.eclipse.jface.dialogs.ErrorDialog;
141144
import org.eclipse.jface.dialogs.MessageDialogWithToggle;
@@ -170,6 +173,8 @@
170173
@SuppressWarnings("deprecation")
171174
public abstract class AbstractDebugTest extends TestCase implements IEvaluationListener {
172175

176+
private static boolean setupFirstTest = false;
177+
173178
public static final String MULTI_OUTPUT_PROJECT_NAME = "MultiOutput";
174179
public static final String BOUND_EE_PROJECT_NAME = "BoundEE";
175180
public static final String ONE_FOUR_PROJECT_NAME = "DebugTests";
@@ -264,6 +269,11 @@ public AbstractDebugTest(String name) {
264269

265270
@Override
266271
protected void setUp() throws Exception {
272+
if (!setupFirstTest) {
273+
setupFirstTest = true;
274+
TestUtil.logInfo("SETTING UP TESTS");
275+
JavaRuntime.addVMInstallChangedListener(new LogVMInstallChanges());
276+
}
267277
TestUtil.logInfo("SETUP " + getClass().getSimpleName() + "." + getName());
268278
super.setUp();
269279
setPreferences();
@@ -3090,4 +3100,35 @@ private static String toString(Collection<IMarker> markers) {
30903100
public interface StackFrameSupplier {
30913101
IJavaStackFrame get() throws Exception;
30923102
}
3103+
3104+
private static void logVMChange(String message, IVMInstall vm) {
3105+
IStatus status = new Status(IStatus.INFO, JDIDebugPlugin.getUniqueIdentifier(),
3106+
message + " " + vm.getName() + ", location: " + vm.getInstallLocation(),
3107+
new RuntimeException("strack trace info"));
3108+
JDIDebugPlugin.log(status);
3109+
}
3110+
3111+
private static class LogVMInstallChanges implements IVMInstallChangedListener {
3112+
3113+
@Override
3114+
public void vmRemoved(IVMInstall vm) {
3115+
logVMChange("VM removed", vm);
3116+
}
3117+
3118+
@Override
3119+
public void vmChanged(PropertyChangeEvent event) {
3120+
}
3121+
3122+
@Override
3123+
public void vmAdded(IVMInstall vm) {
3124+
logVMChange("VM added", vm);
3125+
3126+
}
3127+
3128+
@Override
3129+
public void defaultVMInstallChanged(IVMInstall previous, IVMInstall current) {
3130+
logVMChange("Default VM changed", current);
3131+
}
3132+
3133+
}
30933134
}

org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/core/ModuleOptionsTests.java

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,16 @@
2525
import org.eclipse.jdt.core.JavaCore;
2626
import org.eclipse.jdt.core.JavaModelException;
2727
import org.eclipse.jdt.debug.tests.AbstractDebugTest;
28+
import org.eclipse.jdt.debug.tests.TestUtil;
2829
import org.eclipse.jdt.launching.IVMInstall;
2930
import org.eclipse.jdt.launching.JavaRuntime;
31+
import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
32+
import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager;
3033

3134
public class ModuleOptionsTests extends AbstractDebugTest {
3235

36+
private static final String JAVASE_9 = "JavaSE-9";
37+
3338
private static final String ASSUMED_DEFAULT_MODULES_9 = "java.se," //
3439
// + "javafx.base,javafx.controls,javafx.fxml,javafx.graphics,javafx.media,javafx.swing,javafx.web," REMOVED in 10
3540
+ "jdk.accessibility,jdk.attach,jdk.compiler,jdk.dynalink,jdk.httpserver,"//
@@ -64,11 +69,27 @@ public class ModuleOptionsTests extends AbstractDebugTest {
6469
+ "jdk.unsupported.desktop," //
6570
+ "jdk.xml.dom";
6671

72+
private IVMInstall defaultVM9;
6773

6874
public ModuleOptionsTests(String name) {
6975
super(name);
7076
}
7177

78+
@Override
79+
protected void setUp() throws Exception {
80+
super.setUp();
81+
prepareExecutionEnvironment9();
82+
}
83+
84+
@Override
85+
protected void tearDown() throws Exception {
86+
try {
87+
restoreExecutionEnvironment9();
88+
} finally {
89+
super.tearDown();
90+
}
91+
}
92+
7293
@Override
7394
protected IJavaProject getProjectContext() {
7495
return get9Project();
@@ -225,8 +246,38 @@ public void testLimitModules1() throws Exception {
225246
}
226247

227248
private void checkVMInstall(IJavaProject javaProject) throws CoreException {
249+
IVMInstall defaultVm = JavaRuntime.getDefaultVMInstall();
228250
IVMInstall vm = JavaRuntime.getVMInstall(javaProject);
229-
assertTrue("Expected at least Java 21 for project JVM but got: " + vm.getName() + ", with location: "
230-
+ vm.getInstallLocation(), JavaRuntime.compareJavaVersions(vm, JavaCore.VERSION_21) >= 0);
251+
assertEquals("Expected default VM but got: " + vm.getInstallLocation(), defaultVm.getName(), vm.getName());
252+
}
253+
254+
/**
255+
* JDT tests run in different environments where different major JVM installations might be selected as "default" JVM for a specific Execution Environment (EE).
256+
* This test cases project requires JavaSE-9 EE, which can be resolved to e.g. Java 11, 17 or 21, depending on the installed JVMs.
257+
* JVM modules vary between Java major versions, while we need a stable set of modules for the test case.
258+
* Therefore we "pin" the JVM used for the JavaSE-9 EE to the JVM on which the tests are executed - to avoid tests failing in different test environments.
259+
*/
260+
private void prepareExecutionEnvironment9() {
261+
IVMInstall vm = JavaRuntime.getDefaultVMInstall();
262+
IExecutionEnvironment environment9 = getExecutionEnvironment9();
263+
defaultVM9 = environment9.getDefaultVM();
264+
environment9.setDefaultVM(vm);
265+
TestUtil.logInfo("Set VM \"" + vm.getName() + "\" for execution environments: " + environment9.getId());
266+
}
267+
268+
private void restoreExecutionEnvironment9() {
269+
IExecutionEnvironment environment9 = getExecutionEnvironment9();
270+
environment9.setDefaultVM(defaultVM9);
271+
TestUtil.logInfo("Restored default VM for execution environment: " + environment9.getId());
272+
}
273+
274+
private static IExecutionEnvironment getExecutionEnvironment9() {
275+
IExecutionEnvironmentsManager manager = JavaRuntime.getExecutionEnvironmentsManager();
276+
IExecutionEnvironment[] environments = manager.getExecutionEnvironments();
277+
return Arrays.stream(environments).filter(ModuleOptionsTests::isEnvironment9).findFirst().orElseThrow();
278+
}
279+
280+
private static boolean isEnvironment9(IExecutionEnvironment environment) {
281+
return JAVASE_9.equals(environment.getId());
231282
}
232283
}

0 commit comments

Comments
 (0)