Skip to content

Commit

Permalink
Fix Java LS tests
Browse files Browse the repository at this point in the history
  • Loading branch information
snjeza authored and rgrunber committed Sep 5, 2023
1 parent 3b32a4e commit 27691eb
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
import org.eclipse.core.resources.IWorkspaceDescription;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.m2e.jdt.MavenJdtPlugin;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

Expand All @@ -27,12 +30,16 @@
public class JavaLanguageServerTestPlugin implements BundleActivator {

public static final String PLUGIN_ID = "org.eclipse.jdt.ls.tests";
// see org.eclipse.m2e.jdt.MavenJdtPlugin.PREFERENCE_LOOKUP_JVM_IN_TOOLCHAINS
private static final String PREFERENCE_LOOKUP_JVM_IN_TOOLCHAINS = "lookupJVMInToolchains"; //$NON-NLS-1$

/* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
*/
@Override
public void start(BundleContext context) throws Exception {
IEclipsePreferences prefs = InstanceScope.INSTANCE.getNode(MavenJdtPlugin.PLUGIN_ID);
prefs.put(PREFERENCE_LOOKUP_JVM_IN_TOOLCHAINS, Boolean.FALSE.toString());
TestVMType.setTestJREAsDefault("17");
JavaCore.initializeAfterLoad(new NullProgressMonitor());
IWorkspace workspace = ResourcesPlugin.getWorkspace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.util.Collections;
import java.util.List;

Expand All @@ -33,11 +34,13 @@
import org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest;
import org.eclipse.jdt.ls.core.internal.preferences.Preferences;
import org.hamcrest.core.IsNull;
import org.junit.After;
import org.junit.Assume;
import org.junit.Test;

public class JavaFXTest extends AbstractProjectsManagerBasedTest {

private static final String JAVA8FX_HOME = "java8fx.home";
private static final String JAVA_SE_8 = "JavaSE-1.8";
private static final String JAVA_SE_17 = "JavaSE-17";

Expand All @@ -47,7 +50,7 @@ public class JavaFXTest extends AbstractProjectsManagerBasedTest {
*/
@Test
public void testJavaFX() throws Exception {
String jdkFXHome = System.getProperty("java8fx.home");
String jdkFXHome = System.getProperty(JAVA8FX_HOME);
Assume.assumeThat("No java8fx.home path set, skipping test", jdkFXHome, IsNull.notNullValue());

IVMInstall defaultJRE = JavaRuntime.getDefaultVMInstall();
Expand All @@ -66,7 +69,7 @@ public void testJavaFX() throws Exception {
assertNoErrors(project);

// Delete JavaFX runtime, project should fail to compile
IVMInstall vm = JVMConfigurator.findVM(null, JAVA_SE_8);
IVMInstall vm = JVMConfigurator.findVM(new File(jdkFXHome), null);
vm.getVMInstallType().disposeVMInstall(vm.getId());
TestVMType.setTestJREAsDefault("1.8");
IVMInstallType vmInstallType = JavaRuntime.getVMInstallType(TestVMType.VMTYPE_ID);
Expand All @@ -89,6 +92,24 @@ public void testJavaFX() throws Exception {
}
}

@After
public void cleanJdkfxHome() throws Exception {
String jdkFXHome = System.getProperty(JAVA8FX_HOME);
if (jdkFXHome != null && !jdkFXHome.isBlank()) {
IVMInstall vm = JVMConfigurator.findVM(new File(jdkFXHome), null);
if (vm != null) {
vm.getVMInstallType().disposeVMInstall(vm.getId());
}
IVMInstallType vmInstallType = JavaRuntime.getVMInstallType(TestVMType.VMTYPE_ID);
IVMInstall jvm8 = vmInstallType.findVMInstall("1.8");
IExecutionEnvironment environment = JVMConfigurator.getExecutionEnvironment(JAVA_SE_8);
if (environment != null) {
environment.setDefaultVM(jvm8);
}
}
TestVMType.setTestJREAsDefault("17");
}

private Preferences createJavaFXRuntimePrefs(String path) {
Preferences prefs = new Preferences();
RuntimeEnvironment runtime = new RuntimeEnvironment();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.WorkingCopyOwner;
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
import org.eclipse.jdt.core.manipulation.CoreASTProvider;
import org.eclipse.jdt.ls.core.internal.DocumentAdapter;
import org.eclipse.jdt.ls.core.internal.JDTUtils;
import org.eclipse.jdt.ls.core.internal.JavaClientConnection.JavaLanguageClient;
Expand Down Expand Up @@ -296,6 +297,7 @@ public void cleanUp() throws Exception {
JavaLanguageServerPlugin.logException(e);
}
ResourcesPlugin.getWorkspace().save(true/*full save*/, null/*no progress*/);
CoreASTProvider.getInstance().disposeAST();
}

protected void assertIsJavaProject(IProject project) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public void testGetJdkToLaunchDaemon() {
@Test
public void testGetMajorJavaVersion() {
File javaHome = GradleUtils.getJdkToLaunchDaemon("10");
assertTrue(javaHome.getAbsolutePath().contains("fakejdk" + File.separator + "10"));
assertTrue("javaHome=" + javaHome.getAbsolutePath(), javaHome.getAbsolutePath().contains("fakejdk" + File.separator + "10"));
}
}

0 comments on commit 27691eb

Please sign in to comment.