|
34 | 34 | import org.eclipse.core.runtime.IPath; |
35 | 35 | import org.eclipse.core.runtime.IStatus; |
36 | 36 | import org.eclipse.core.runtime.Path; |
| 37 | +import org.eclipse.core.runtime.Status; |
37 | 38 | import org.eclipse.jdt.core.*; |
38 | 39 | import org.eclipse.jdt.core.dom.AST; |
39 | 40 | import org.eclipse.jdt.core.dom.CompilationUnit; |
|
45 | 46 | import org.eclipse.jdt.internal.core.ClasspathEntry; |
46 | 47 | import org.eclipse.jdt.internal.core.builder.ClasspathJrt; |
47 | 48 | import org.eclipse.jdt.internal.core.util.Messages; |
| 49 | +import org.eclipse.jdt.launching.IVMInstall; |
| 50 | +import org.eclipse.jdt.launching.JavaRuntime; |
| 51 | +import org.eclipse.jdt.launching.environments.IExecutionEnvironment; |
| 52 | +import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager; |
48 | 53 |
|
49 | 54 | public class ModuleBuilderTests extends ModifyingResourceTests { |
| 55 | + |
| 56 | + private static final String JAVASE_9 = "JavaSE-9"; |
| 57 | + |
50 | 58 | public ModuleBuilderTests(String name) { |
51 | 59 | super(name); |
52 | 60 | } |
@@ -691,6 +699,7 @@ public void _test019() throws CoreException, IOException { |
691 | 699 | } |
692 | 700 | public void testConvertToModule() throws CoreException, IOException { |
693 | 701 | Hashtable<String, String> javaCoreOptions = JavaCore.getOptions(); |
| 702 | + IVMInstall vm = prepareExecutionEnvironment(JAVASE_9); |
694 | 703 | try { |
695 | 704 | IJavaProject project = setUpJavaProject("ConvertToModule"); |
696 | 705 | Map<String, String> options = new HashMap<>(); |
@@ -721,10 +730,12 @@ else if (isJRE10) |
721 | 730 | assertStringsEqual("incorrect result", new String[]{"java.desktop", "java.rmi", "java.sql"}, modules); |
722 | 731 | } finally { |
723 | 732 | this.deleteProject("ConvertToModule"); |
| 733 | + restoreExecutionEnvironment(JAVASE_9, vm); |
724 | 734 | JavaCore.setOptions(javaCoreOptions); |
725 | 735 | } |
726 | 736 | } |
727 | 737 | public void testConvertToModuleWithRelease9() throws CoreException, IOException { |
| 738 | + IVMInstall vm = prepareExecutionEnvironment(JAVASE_9); |
728 | 739 | Hashtable<String, String> javaCoreOptions = JavaCore.getOptions(); |
729 | 740 | try { |
730 | 741 | IJavaProject project = setUpJavaProject("ConvertToModule"); |
@@ -756,6 +767,7 @@ else if (isJRE10) |
756 | 767 | assertStringsEqual("incorrect result", new String[]{"java.desktop", "java.rmi", "java.sql"}, modules); |
757 | 768 | } finally { |
758 | 769 | this.deleteProject("ConvertToModule"); |
| 770 | + restoreExecutionEnvironment(JAVASE_9, vm); |
759 | 771 | JavaCore.setOptions(javaCoreOptions); |
760 | 772 | } |
761 | 773 | } |
@@ -9136,4 +9148,25 @@ protected void sortMarkers(IMarker[] markers) { |
9136 | 9148 | Arrays.sort(markers, Comparator.comparingInt((IMarker a) -> a.getAttribute(IMarker.CHAR_START, 0)) |
9137 | 9149 | .thenComparing((IMarker a) -> a.getAttribute(IMarker.MESSAGE, ""))); |
9138 | 9150 | } |
| 9151 | + |
| 9152 | + private IVMInstall prepareExecutionEnvironment(String environmentId) { |
| 9153 | + IVMInstall vm = JavaRuntime.getDefaultVMInstall(); |
| 9154 | + IExecutionEnvironment environment = getExecutionEnvironment(environmentId); |
| 9155 | + IVMInstall defaultVM9 = environment.getDefaultVM(); |
| 9156 | + environment.setDefaultVM(vm); |
| 9157 | + JavaCore.getPlugin().getLog().log(Status.info("Set VM \"" + vm.getName() + "\" for execution environments: " + environment.getId())); |
| 9158 | + return defaultVM9; |
| 9159 | + } |
| 9160 | + |
| 9161 | + private void restoreExecutionEnvironment(String environmentId, IVMInstall defaultVM) { |
| 9162 | + IExecutionEnvironment environment = getExecutionEnvironment(environmentId); |
| 9163 | + environment.setDefaultVM(defaultVM); |
| 9164 | + JavaCore.getPlugin().getLog().log(Status.info("Restored default VM for execution environment: " + environment.getId())); |
| 9165 | + } |
| 9166 | + |
| 9167 | + private static IExecutionEnvironment getExecutionEnvironment(String id) { |
| 9168 | + IExecutionEnvironmentsManager manager = JavaRuntime.getExecutionEnvironmentsManager(); |
| 9169 | + IExecutionEnvironment[] environments = manager.getExecutionEnvironments(); |
| 9170 | + return Arrays.stream(environments).filter(e -> id.equals(e.getId())).findFirst().orElseThrow(); |
| 9171 | + } |
9139 | 9172 | } |
0 commit comments