Skip to content

Commit 3eefc70

Browse files
committed
Add assertions that a compatible JVM is installed and used for the test
1 parent d44db2f commit 3eefc70

4 files changed

Lines changed: 74 additions & 4 deletions

File tree

org.eclipse.jdt.debug.tests/multirelease/src/p/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
public class Main {
44

55
public static void main(String[] args) {
6-
System.out.println("Main: "+java.lang.Runtime.version());
6+
System.out.println("Java: "+java.lang.Runtime.version().feature());
77
new X().greet();
88
}
99

org.eclipse.jdt.debug.tests/test plugin/org/eclipse/jdt/debug/testplugin/JavaProjectHelper.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public class JavaProjectHelper {
108108
public static final IPath TEST_24_SRC_DIR = new Path("java24");
109109

110110
/**
111-
* path to the 21 test source
111+
* path to the multirelease test source
112112
*/
113113
public static final IPath TEST_MR_SRC_DIR = new Path("multirelease");
114114

@@ -315,7 +315,8 @@ public static void delete(IJavaProject jproject) throws CoreException {
315315
/**
316316
* Adds a new source container specified by the container name to the source path of the specified project
317317
*
318-
* @param extra optional extra classpaht attributes
318+
* @param extra
319+
* optional extra classpath attributes
319320
* @return the package fragment root of the container name
320321
*/
321322
public static IPackageFragmentRoot addSourceContainer(IJavaProject jproject, String containerName, IClasspathAttribute... extra) throws CoreException {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,6 @@ synchronized void assertMRProject() {
643643
File root = JavaTestPlugin.getDefault().getFileInPlugin(JavaProjectHelper.TEST_MR_SRC_DIR);
644644
JavaProjectHelper.importFilesFromDirectory(new File(root, src17.getPath().lastSegment()), src17.getPath(), null);
645645
JavaProjectHelper.importFilesFromDirectory(new File(root, src21.getPath().lastSegment()), src21.getPath(), null);
646-
// TODO
647646
cfgs.add(createLaunchConfiguration(jp, "p.Main"));
648647
loadedMR = true;
649648
waitForBuild();

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
package org.eclipse.jdt.debug.tests.core;
1515

1616
import java.io.StringReader;
17+
import java.util.ArrayList;
18+
import java.util.Collection;
19+
import java.util.Iterator;
20+
import java.util.List;
1721
import java.util.Properties;
1822

1923
import org.eclipse.debug.core.DebugPlugin;
@@ -23,16 +27,37 @@
2327
import org.eclipse.jdt.core.IJavaProject;
2428
import org.eclipse.jdt.debug.core.IJavaDebugTarget;
2529
import org.eclipse.jdt.debug.tests.ui.AbstractDebugUiTests;
30+
import org.eclipse.jdt.launching.IVMInstall;
31+
import org.eclipse.jdt.launching.IVMInstall2;
32+
import org.eclipse.jdt.launching.IVMInstallType;
33+
import org.eclipse.jdt.launching.JavaRuntime;
2634
import org.eclipse.jface.text.IDocument;
2735
import org.eclipse.ui.console.IConsole;
2836
import org.eclipse.ui.console.TextConsole;
2937

38+
/**
39+
* <b>IMPORTANT</b> This test requires some different JVM installs to be present (see {@link #JAVA_11}, {@link #JAVA_17}, {@link #JAVA_21})) if such
40+
* JVMs can not be found, the test will fail!
41+
*/
3042
public class MultiReleaseLaunchTests extends AbstractDebugUiTests {
3143

44+
private static final RequiredJavaVersion JAVA_11 = new RequiredJavaVersion(11, 16);
45+
private static final RequiredJavaVersion JAVA_17 = new RequiredJavaVersion(17, 20);
46+
private static final RequiredJavaVersion JAVA_21 = new RequiredJavaVersion(21, Integer.MAX_VALUE);
47+
3248
public MultiReleaseLaunchTests(String name) {
3349
super(name);
3450
}
3551

52+
@Override
53+
protected void setUp() throws Exception {
54+
super.setUp();
55+
List<RequiredJavaVersion> requiredJavaVersions = new ArrayList<>(List.of(JAVA_11, JAVA_17, JAVA_21));
56+
removeExistingJavaVersions(requiredJavaVersions);
57+
assertTrue("The following java versions are required by this test but can not be found: "
58+
+ requiredJavaVersions, requiredJavaVersions.isEmpty());
59+
}
60+
3661
@Override
3762
protected IJavaProject getProjectContext() {
3863
return getMultireleaseProject();
@@ -41,14 +66,17 @@ protected IJavaProject getProjectContext() {
4166
public void testMultiReleaseLaunch() throws Exception {
4267
ILaunchConfiguration config = getLaunchConfiguration("p.Main");
4368
Properties result = launchAndReadResult(config, 11);
69+
assertTrue("Was not launched with a proper Java installation " + result, JAVA_11.matches(result.getProperty("Java")));
4470
assertEquals("X should be executed from Java 11 version: " + result, "11", result.get("X"));
4571
assertNull("Y should not be executed from Java 11 version: " + result, result.get("Y"));
4672
assertNull("Z should not be executed from Java 11 version: " + result, result.get("Z"));
4773
Properties result17 = launchAndReadResult(config, 17);
74+
assertTrue("Was not launched with a proper Java installation " + result17, JAVA_17.matches(result17.getProperty("Java")));
4875
assertEquals("X should be executed from Java 17 version: " + result17, "17", result17.get("X"));
4976
assertEquals("Y should be executed from Java 11 version: " + result17, "11", result17.get("Y"));
5077
assertNull("Z should not be executed from Java 17 version: " + result17, result17.get("Z"));
5178
Properties result21 = launchAndReadResult(config, 21);
79+
assertTrue("Was not launched with a proper Java installation " + result21, JAVA_21.matches(result21.getProperty("Java")));
5280
assertEquals("X should be executed from Java 17 version: " + result21, "17", result21.get("X"));
5381
assertEquals("Y should be executed from Java 21 version: " + result21, "21", result21.get("Y"));
5482
assertEquals("Z should be executed from Java 17 version: " + result21, "17", result21.get("Z"));
@@ -70,4 +98,46 @@ private Properties launchAndReadResult(ILaunchConfiguration config, int javaVers
7098
return properties;
7199
}
72100

101+
private static int getJavaVersion(IVMInstall install) {
102+
if (install instanceof IVMInstall2 vm) {
103+
try {
104+
String javaVersion = vm.getJavaVersion().split("\\.")[0]; //$NON-NLS-1$
105+
return Integer.parseInt(javaVersion);
106+
} catch (RuntimeException rte) {
107+
// can't know then...
108+
}
109+
}
110+
return -1;
111+
}
112+
113+
private static void removeExistingJavaVersions(Collection<RequiredJavaVersion> requiredJavaVersions) {
114+
IVMInstallType[] installTypes = JavaRuntime.getVMInstallTypes();
115+
for (IVMInstallType installType : installTypes) {
116+
IVMInstall[] vmInstalls = installType.getVMInstalls();
117+
for (IVMInstall install : vmInstalls) {
118+
if (requiredJavaVersions.isEmpty()) {
119+
return;
120+
}
121+
int javaVersion = getJavaVersion(install);
122+
for (Iterator<RequiredJavaVersion> iterator = requiredJavaVersions.iterator(); iterator.hasNext();) {
123+
if (iterator.next().matches(javaVersion)) {
124+
iterator.remove();
125+
break;
126+
}
127+
}
128+
}
129+
}
130+
}
131+
132+
private static record RequiredJavaVersion(int from, int to) {
133+
134+
public boolean matches(int version) {
135+
return (version >= from() && version <= to());
136+
}
137+
138+
public boolean matches(String v) {
139+
return matches(Integer.parseInt(v));
140+
}
141+
}
142+
73143
}

0 commit comments

Comments
 (0)