Skip to content

Commit ae10f7a

Browse files
committed
Derive Test JRE from configuration of maven-enforcer-plugin
Similar to what is done for the Maven Launch Configurations. Move logic for Maven Execution JRE to org.eclipse.m2e.jdt. This closes #2059
1 parent 27eae6a commit ae10f7a

File tree

10 files changed

+363
-231
lines changed

10 files changed

+363
-231
lines changed

org.eclipse.m2e.core.tests/src/org/eclipse/m2e/internal/launch/MavenLaunchDelegateTest.java

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@
1717
import static org.junit.Assert.assertEquals;
1818
import static org.mockito.Mockito.when;
1919

20-
import java.io.File;
2120
import java.util.List;
2221

23-
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
2422
import org.eclipse.core.resources.IProject;
2523
import org.eclipse.core.runtime.CoreException;
2624
import org.eclipse.debug.core.DebugPlugin;
@@ -47,68 +45,30 @@ public class MavenLaunchDelegateTest extends AbstractMavenProjectTestCase {
4745
private static final String DEFAULT_VM = "defaultVM";
4846
private static final List<String> AVAILABLE_VM_VERSIONS = List.of("17.0.4", "11.0.7", "13.0.5", "11.0.1", "1.8.0");
4947

50-
@Test
51-
public void testGetBestMatchingVM_majorOnly() throws InvalidVersionSpecificationException {
52-
try (var mock = mockJavaRuntime()) {
53-
assertEquals("11.0.7", MavenLaunchDelegate.getBestMatchingVM("11").getId());
54-
}
55-
}
56-
57-
@Test
58-
public void testGetBestMatchingVM_rangeWithOnlyMajorLowerBound() throws InvalidVersionSpecificationException {
59-
try (var mock = mockJavaRuntime()) {
60-
assertEquals("11.0.7", MavenLaunchDelegate.getBestMatchingVM("[11,)").getId());
61-
}
62-
}
63-
64-
@Test
65-
public void testGetBestMatchingVM_9versionRange() throws InvalidVersionSpecificationException {
66-
try (var mock = mockJavaRuntime()) {
67-
assertEquals("17.0.4", MavenLaunchDelegate.getBestMatchingVM("[11,18)").getId());
68-
}
69-
}
70-
71-
@Test
72-
public void testGetBestMatchingVM_1XversionRange() throws InvalidVersionSpecificationException {
73-
try (var mock = mockJavaRuntime()) {
74-
assertEquals("1.8.0", MavenLaunchDelegate.getBestMatchingVM("[1.8,9)").getId());
75-
}
76-
}
77-
78-
@Test
79-
public void testGetBestMatchingVM_versionRangeWithNoMajorVersionMatch() {
80-
try (var mock = mockJavaRuntime()) {
81-
assertEquals("13.0.5", MavenLaunchDelegate.getBestMatchingVM("[12,)").getId());
82-
}
83-
}
8448

8549
@Test
8650
public void testRequiredJavaVersionFromEnforcerRule_Version() throws Exception {
8751
IProject project = importProject("resources/projects/enforcerSettingsWithVersion/pom.xml");
88-
assertRequiredJavaBuildVersion(project, "13.0.3", "13.0.5");
52+
assertRequiredJavaBuildVersion(project, "13.0.5");
8953
}
9054

9155
@Test
9256
public void testRequiredJavaVersionFromEnforcerRule_VersionRange() throws Exception {
9357
IProject project = importProject("resources/projects/enforcerSettingsWithVersionRange/pom.xml");
94-
assertRequiredJavaBuildVersion(project, "[11.0.6,13)", "11.0.7");
58+
assertRequiredJavaBuildVersion(project, "11.0.7");
9559
}
9660

9761
@Test
9862
public void testRequiredJavaVersionFromEnforcerRule_NoVersionRange() throws Exception {
9963
IProject project = importProject("resources/projects/enforcerSettingsWithoutRequiredJavaVersion/pom.xml");
100-
assertRequiredJavaBuildVersion(project, null, DEFAULT_VM);
64+
assertRequiredJavaBuildVersion(project, DEFAULT_VM);
10165
}
10266

103-
private void assertRequiredJavaBuildVersion(IProject project, String expectedVersionRange, String expectedVMVersion)
67+
private void assertRequiredJavaBuildVersion(IProject project, String expectedVMVersion)
10468
throws Exception {
10569

10670
waitForJobsToComplete();
10771

108-
File pomFile = project.getLocation().toFile();
109-
110-
assertEquals(expectedVersionRange, MavenLaunchDelegate.readEnforcedJavaVersion(pomFile, monitor));
111-
11272
String pomDir = "${workspace_loc:/" + project.getName() + "}";
11373

11474
try (var mock = mockJavaRuntime()) {

org.eclipse.m2e.jdt.tests/META-INF/MANIFEST.MF

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ Require-Bundle: org.eclipse.core.runtime,
1616
org.eclipse.ui.tests.harness,
1717
org.eclipse.ui,
1818
org.eclipse.debug.ui;bundle-version="3.18.600"
19+
Import-Package:
20+
org.mockito,
21+
org.mockito.stubbing
1922
Bundle-RequiredExecutionEnvironment: JavaSE-21
2023
Bundle-Vendor: Eclipse.org - m2e
2124
Automatic-Module-Name: org.eclipse.m2e.jdt.tests
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2022, 2023 Hannes Wellmann and others
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License 2.0
5+
* which accompanies this distribution, and is available at
6+
* https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* Hannes Wellmann - initial API and implementation
12+
* Konrad Windszus - Add tests for required java runtime version implied by enforcer rule
13+
*******************************************************************************/
14+
15+
package org.eclipse.m2e.jdt.tests;
16+
17+
import static org.junit.Assert.assertEquals;
18+
import static org.mockito.Mockito.when;
19+
20+
import java.util.List;
21+
22+
import org.eclipse.jdt.internal.launching.StandardVMType;
23+
import org.eclipse.jdt.launching.AbstractVMInstall;
24+
import org.eclipse.jdt.launching.IVMInstall;
25+
import org.eclipse.jdt.launching.IVMInstallType;
26+
import org.eclipse.jdt.launching.JavaRuntime;
27+
import org.eclipse.m2e.jdt.MavenExecutionJre;
28+
import org.eclipse.m2e.tests.common.AbstractMavenProjectTestCase;
29+
import org.junit.Test;
30+
import org.mockito.MockedStatic;
31+
import org.mockito.Mockito;
32+
33+
@SuppressWarnings("restriction")
34+
public class MavenExecutionJreTest extends AbstractMavenProjectTestCase {
35+
36+
private static final String DEFAULT_VM = "defaultVM";
37+
private static final List<String> AVAILABLE_VM_VERSIONS = List.of("17.0.4", "11.0.7", "13.0.5", "11.0.1", "1.8.0");
38+
39+
@Test
40+
public void testGetBestMatchingVM_majorOnly() {
41+
try (var mock = mockJavaRuntime()) {
42+
assertEquals("11.0.7", MavenExecutionJre.getBestMatchingVM("11").get().getId());
43+
}
44+
}
45+
46+
@Test
47+
public void testGetBestMatchingVM_rangeWithOnlyMajorLowerBound() {
48+
try (var mock = mockJavaRuntime()) {
49+
assertEquals("11.0.7", MavenExecutionJre.getBestMatchingVM("[11,)").get().getId());
50+
}
51+
}
52+
53+
@Test
54+
public void testGetBestMatchingVM_9versionRange() {
55+
try (var mock = mockJavaRuntime()) {
56+
assertEquals("17.0.4", MavenExecutionJre.getBestMatchingVM("[11,18)").get().getId());
57+
}
58+
}
59+
60+
@Test
61+
public void testGetBestMatchingVM_1XversionRange() {
62+
try (var mock = mockJavaRuntime()) {
63+
assertEquals("1.8.0", MavenExecutionJre.getBestMatchingVM("[1.8,9)").get().getId());
64+
}
65+
}
66+
67+
@Test
68+
public void testGetBestMatchingVM_versionRangeWithNoMajorVersionMatch() {
69+
try (var mock = mockJavaRuntime()) {
70+
assertEquals("13.0.5", MavenExecutionJre.getBestMatchingVM("[12,)").get().getId());
71+
}
72+
}
73+
74+
private static MockedStatic<JavaRuntime> mockJavaRuntime() {
75+
IVMInstall defaultVM = Mockito.mock(IVMInstall.class);
76+
Mockito.when(defaultVM.getId()).thenReturn(DEFAULT_VM);
77+
78+
IVMInstallType standardVMType = Mockito.mock(StandardVMType.class, Mockito.CALLS_REAL_METHODS);
79+
IVMInstall[] installs = AVAILABLE_VM_VERSIONS.stream().map(version -> {
80+
AbstractVMInstall vm = Mockito.mock(AbstractVMInstall.class, Mockito.CALLS_REAL_METHODS);
81+
when(vm.getId()).thenReturn(version);
82+
when(vm.getJavaVersion()).thenReturn(version);
83+
when(vm.getVMInstallType()).thenReturn(standardVMType);
84+
when(vm.getName()).thenReturn("JDK " + version);
85+
return vm;
86+
}).toArray(IVMInstall[]::new);
87+
Mockito.doReturn(installs).when(standardVMType).getVMInstalls();
88+
89+
MockedStatic<JavaRuntime> javaRuntimeMock = Mockito.mockStatic(JavaRuntime.class, Mockito.CALLS_REAL_METHODS);
90+
javaRuntimeMock.when(JavaRuntime::getVMInstallTypes).thenReturn(new IVMInstallType[] { standardVMType });
91+
javaRuntimeMock.when(() -> JavaRuntime.computeVMInstall(Mockito.any())).thenReturn(defaultVM);
92+
return javaRuntimeMock;
93+
}
94+
95+
}

org.eclipse.m2e.jdt.tests/src/org/eclipse/m2e/jdt/tests/UnitTestLaunchConfigConfigurationTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public void test_configuration_must_be_updated_with_surefire_config()
149149
ILaunchConfiguration config = updatedConfigurations[0];
150150

151151
// check argLine
152-
String argLine = config.getAttribute(UnitTestSupport.LAUNCH_CONFIG_VM_ARGUMENTS, "");
152+
String argLine = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, "");
153153
assertTrue(argLine.contains("--argLineItem=surefireArgLineValue"));
154154

155155
// check environmentVariables
@@ -208,7 +208,7 @@ public void test_configuration_must_be_updated_with_failsafe_config()
208208
ILaunchConfiguration config = updatedConfigurations[0];
209209

210210
// check argLine
211-
String argLine = config.getAttribute(UnitTestSupport.LAUNCH_CONFIG_VM_ARGUMENTS, "");
211+
String argLine = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, "");
212212
assertTrue(argLine.contains("--argLineItem=failsafeArgLineValue"));
213213

214214
// check environmentVariables
@@ -260,7 +260,7 @@ public void test_configuration_must_be_updated_with_surefire_config_when_created
260260
ILaunchConfiguration config = updatedConfigurations[0];
261261

262262
// check argLine
263-
String argLine = config.getAttribute(UnitTestSupport.LAUNCH_CONFIG_VM_ARGUMENTS, "");
263+
String argLine = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, "");
264264
assertTrue(argLine.contains("--argLineItem=surefireArgLineValue"));
265265

266266
// check environmentVariables
@@ -309,7 +309,7 @@ public void test_configuration_must_be_updated_with_failSafe_config_when_created
309309
ILaunchConfiguration config = updatedConfigurations[0];
310310

311311
// check argLine
312-
String argLine = config.getAttribute(UnitTestSupport.LAUNCH_CONFIG_VM_ARGUMENTS, "");
312+
String argLine = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, "");
313313
assertTrue(argLine.contains("--argLineItem=failsafeArgLineValue"));
314314

315315
// check environmentVariables
@@ -353,7 +353,7 @@ public void test_deferred_variable_are_resolved() throws CoreException, IOExcept
353353
assertTrue(updatedConfigurations.length == 1);
354354

355355
ILaunchConfiguration config = updatedConfigurations[0];
356-
String argLine = config.getAttribute(UnitTestSupport.LAUNCH_CONFIG_VM_ARGUMENTS, "");
356+
String argLine = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, "");
357357
assertTrue(argLine.contains("-javaagent")); // resolved jacoco agent
358358
assertTrue(argLine.contains("@{titi.tata}")); // unresolved property is unchanged as in CLI
359359
}

org.eclipse.m2e.jdt/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: %Bundle-Name
44
Bundle-SymbolicName: org.eclipse.m2e.jdt;singleton:=true
5-
Bundle-Version: 2.4.200.qualifier
5+
Bundle-Version: 2.5.0.qualifier
66
Bundle-Localization: plugin
77
Export-Package: org.eclipse.m2e.jdt,
88
org.eclipse.m2e.jdt.internal;x-friends:="org.eclipse.m2e.jdt.ui",

0 commit comments

Comments
 (0)