Skip to content

Commit ffa9eaa

Browse files
trancexpressiloveeclipse
authored andcommitted
Set default VM for execution environment in LongClassPathTests
Fixes: #851
1 parent 0e9d487 commit ffa9eaa

File tree

3 files changed

+55
-40
lines changed

3 files changed

+55
-40
lines changed

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

Lines changed: 37 additions & 0 deletions
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;
@@ -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: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
*******************************************************************************/
1414
package org.eclipse.jdt.debug.tests.launching;
1515

16-
import static org.junit.Assume.assumeFalse;
1716
import static org.junit.Assume.assumeTrue;
1817

1918
import java.io.File;
@@ -60,12 +59,16 @@
6059
* version and OS.
6160
*/
6261
public class LongClassPathTests extends AbstractDebugTest {
62+
6363
protected static final String MAIN_TYPE_NAME = "test.classpath.Main";
6464
protected static final IPath CLASSPATH_PROJECT_CONTENT_PATH = new Path("testresources/classpathProject");
6565
protected IJavaProject javaProject;
6666
protected ILaunchConfiguration launchConfiguration;
6767
protected IJavaThread thread;
6868

69+
private IVMInstall defaultVM1_6;
70+
private IVMInstall defaultVM9;
71+
6972
public LongClassPathTests(String name) {
7073
super(name);
7174
}
@@ -81,6 +84,13 @@ public static Test suite() {
8184
return suite;
8285
}
8386

87+
@Override
88+
protected void setUp() throws Exception {
89+
super.setUp();
90+
defaultVM1_6 = prepareExecutionEnvironment(JavaProjectHelper.JAVA_SE_1_6_EE_NAME);
91+
defaultVM9 = prepareExecutionEnvironment(JavaProjectHelper.JAVA_SE_9_EE_NAME);
92+
}
93+
8494
@Override
8595
protected void tearDown() throws Exception {
8696
try {
@@ -93,6 +103,8 @@ protected void tearDown() throws Exception {
93103
if (launchConfiguration != null) {
94104
launchConfiguration.delete();
95105
}
106+
setExecutionEnvironment(JavaProjectHelper.JAVA_SE_1_6_EE_NAME, defaultVM1_6);
107+
setExecutionEnvironment(JavaProjectHelper.JAVA_SE_9_EE_NAME, defaultVM9);
96108
} catch (CoreException ce) {
97109
// ignore
98110
} finally {
@@ -145,7 +157,7 @@ public void testVeryLongClasspathWithArgumentFile() throws Exception {
145157
}
146158
javaProject = createJavaProjectClone("testVeryLongClasspathWithArgumentFile", CLASSPATH_PROJECT_CONTENT_PATH.toString(), JavaProjectHelper.JAVA_SE_9_EE_NAME, true);
147159
launchConfiguration = createLaunchConfigurationStopInMain(javaProject, MAIN_TYPE_NAME);
148-
assumeTrue(isArgumentFileSupported(launchConfiguration));
160+
assertTrue(isArgumentFileSupported(launchConfiguration));
149161
int minClasspathLength = 300000;
150162

151163
// Given
@@ -180,7 +192,7 @@ public void testVeryLongClasspathWithEnvironmentVariable() throws Exception {
180192
// Given
181193
javaProject = createJavaProjectClone("testVeryLongClasspath", CLASSPATH_PROJECT_CONTENT_PATH.toString(), JavaProjectHelper.JAVA_SE_1_6_EE_NAME, true);
182194
launchConfiguration = createLaunchConfigurationStopInMain(javaProject, MAIN_TYPE_NAME);
183-
assumeFalse(isArgumentFileSupported(launchConfiguration));
195+
assertFalse(isArgumentFileSupported(launchConfiguration));
184196
int minClasspathLength = 300000;
185197
setLongClasspath(javaProject, minClasspathLength);
186198
waitForBuild();

0 commit comments

Comments
 (0)