Skip to content

Commit 8dd9ea2

Browse files
committed
Set default VM for execution environment in LongClassPathTests
Fixes: #851
1 parent 0e9d487 commit 8dd9ea2

File tree

3 files changed

+54
-38
lines changed

3 files changed

+54
-38
lines changed

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@
140140
import org.eclipse.jdt.launching.JavaRuntime;
141141
import org.eclipse.jdt.launching.PropertyChangeEvent;
142142
import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
143+
import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager;
143144
import org.eclipse.jface.dialogs.ErrorDialog;
144145
import org.eclipse.jface.dialogs.MessageDialogWithToggle;
145146
import org.eclipse.jface.preference.IPreferenceStore;
@@ -2928,7 +2929,7 @@ public void runBare() throws Throwable {
29282929
} catch (TestAgainException e) {
29292930
TestUtil.log(IStatus.ERROR, getName(), "Test failed attempt " + attempts + ". Re-testing.", e);
29302931
TestUtil.cleanUp(getName());
2931-
if (attempts > 4) {
2932+
if (attempts > 1) {
29322933
// the next attempt will fail
29332934
break;
29342935
}
@@ -3097,6 +3098,42 @@ private static String toString(Collection<IMarker> markers) {
30973098
return markersInfo.toString();
30983099
}
30993100

3101+
/**
3102+
* JDT tests run in different environments where different major JVM installations might be selected as "default" JVM for a specific Execution
3103+
* Environment (EE). Some test cases projects requires JavaSE-N EE, which can be resolved to e.g. Java 11, 17 or 21, depending on the installed
3104+
* JVMs. JVM modules vary between Java major versions, while we need a stable set of modules for the test case. Therefore we "pin" the JVM used
3105+
* for the JavaSE-N EE to the JVM on which the tests are executed - to avoid tests failing in different test environments.
3106+
*
3107+
* @param environmentId The ID of the EE, e.g.: "JavaSE-9"
3108+
* @return The default VM install for the EE, before we change it.
3109+
*/
3110+
protected static IVMInstall prepareExecutionEnvironment(String environmentId) {
3111+
IVMInstall vm = JavaRuntime.getDefaultVMInstall();
3112+
IExecutionEnvironment environment = getExecutionEnvironment(environmentId);
3113+
IVMInstall defaultVM = environment.getDefaultVM();
3114+
environment.setDefaultVM(vm);
3115+
TestUtil.logInfo("Set VM \"" + vm.getName() + "\" for execution environments: " + environment.getId());
3116+
return defaultVM;
3117+
}
3118+
3119+
/**
3120+
* Set the default VM of an EE.
3121+
*
3122+
* @param environmentId The ID of the EE, e.g.: "JavaSE-9"
3123+
* @param defaultVM The default VM to set.
3124+
*/
3125+
protected static void setExecutionEnvironment(String environmentId, IVMInstall defaultVM) {
3126+
IExecutionEnvironment environment = getExecutionEnvironment(environmentId);
3127+
environment.setDefaultVM(defaultVM);
3128+
TestUtil.logInfo("Set default VM for execution environment: " + environment.getId());
3129+
}
3130+
3131+
private static IExecutionEnvironment getExecutionEnvironment(String environmentId) {
3132+
IExecutionEnvironmentsManager manager = JavaRuntime.getExecutionEnvironmentsManager();
3133+
IExecutionEnvironment[] environments = manager.getExecutionEnvironments();
3134+
return Arrays.stream(environments).filter(e -> environmentId.equals(e.getId())).findFirst().orElseThrow();
3135+
}
3136+
31003137
public interface StackFrameSupplier {
31013138
IJavaStackFrame get() throws Exception;
31023139
}

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

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,13 @@
2424
import org.eclipse.jdt.core.IJavaProject;
2525
import org.eclipse.jdt.core.JavaCore;
2626
import org.eclipse.jdt.core.JavaModelException;
27+
import org.eclipse.jdt.debug.testplugin.JavaProjectHelper;
2728
import org.eclipse.jdt.debug.tests.AbstractDebugTest;
28-
import org.eclipse.jdt.debug.tests.TestUtil;
2929
import org.eclipse.jdt.launching.IVMInstall;
3030
import org.eclipse.jdt.launching.JavaRuntime;
31-
import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
32-
import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager;
3331

3432
public class ModuleOptionsTests extends AbstractDebugTest {
3533

36-
private static final String JAVASE_9 = "JavaSE-9";
37-
3834
private static final String ASSUMED_DEFAULT_MODULES_9 = "java.se," //
3935
// + "javafx.base,javafx.controls,javafx.fxml,javafx.graphics,javafx.media,javafx.swing,javafx.web," REMOVED in 10
4036
+ "jdk.accessibility,jdk.attach,jdk.compiler,jdk.dynalink,jdk.httpserver,"//
@@ -78,13 +74,13 @@ public ModuleOptionsTests(String name) {
7874
@Override
7975
protected void setUp() throws Exception {
8076
super.setUp();
81-
prepareExecutionEnvironment9();
77+
defaultVM9 = prepareExecutionEnvironment(JavaProjectHelper.JAVA_SE_9_EE_NAME);
8278
}
8379

8480
@Override
8581
protected void tearDown() throws Exception {
8682
try {
87-
restoreExecutionEnvironment9();
83+
setExecutionEnvironment(JavaProjectHelper.JAVA_SE_9_EE_NAME, defaultVM9);
8884
} finally {
8985
super.tearDown();
9086
}
@@ -250,34 +246,4 @@ private void checkVMInstall(IJavaProject javaProject) throws CoreException {
250246
IVMInstall vm = JavaRuntime.getVMInstall(javaProject);
251247
assertEquals("Expected default VM but got: " + vm.getInstallLocation(), defaultVm.getName(), vm.getName());
252248
}
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());
282-
}
283249
}

org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/launching/LongClassPathTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,16 @@
6060
* version and OS.
6161
*/
6262
public class LongClassPathTests extends AbstractDebugTest {
63+
6364
protected static final String MAIN_TYPE_NAME = "test.classpath.Main";
6465
protected static final IPath CLASSPATH_PROJECT_CONTENT_PATH = new Path("testresources/classpathProject");
6566
protected IJavaProject javaProject;
6667
protected ILaunchConfiguration launchConfiguration;
6768
protected IJavaThread thread;
6869

70+
private IVMInstall defaultVM1_6;
71+
private IVMInstall defaultVM9;
72+
6973
public LongClassPathTests(String name) {
7074
super(name);
7175
}
@@ -81,6 +85,13 @@ public static Test suite() {
8185
return suite;
8286
}
8387

88+
@Override
89+
protected void setUp() throws Exception {
90+
super.setUp();
91+
defaultVM1_6 = prepareExecutionEnvironment(JavaProjectHelper.JAVA_SE_1_6_EE_NAME);
92+
defaultVM9 = prepareExecutionEnvironment(JavaProjectHelper.JAVA_SE_9_EE_NAME);
93+
}
94+
8495
@Override
8596
protected void tearDown() throws Exception {
8697
try {
@@ -93,6 +104,8 @@ protected void tearDown() throws Exception {
93104
if (launchConfiguration != null) {
94105
launchConfiguration.delete();
95106
}
107+
setExecutionEnvironment(JavaProjectHelper.JAVA_SE_1_6_EE_NAME, defaultVM1_6);
108+
setExecutionEnvironment(JavaProjectHelper.JAVA_SE_9_EE_NAME, defaultVM9);
96109
} catch (CoreException ce) {
97110
// ignore
98111
} finally {

0 commit comments

Comments
 (0)