Skip to content

Commit 195f29e

Browse files
committed
Unified UI setting for toolchains
2 parents 044cb60 + 8e37c42 commit 195f29e

File tree

18 files changed

+583
-117
lines changed

18 files changed

+583
-117
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Require-Bundle: org.eclipse.m2e.tests.common,
1313
org.eclipse.debug.core,
1414
org.eclipse.jdt.core
1515
Import-Package: org.apache.commons.io,
16+
org.hamcrest;version="[2.2.0,3.0.0]",
17+
org.hamcrest.collection;version="[2.2.0,3.0.0]",
18+
org.hamcrest.core;version="[2.2.0,3.0.0]",
1619
org.junit,
1720
org.mockito,
1821
org.mockito.stubbing
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<settings/>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<settings/>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<toolchains/>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<toolchains/>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024 Georg Tsakumagos 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+
* Georg Tsakumagos - initial declaration
12+
*******************************************************************************/
13+
package org.eclipse.m2e.core;
14+
15+
import java.util.function.Consumer;
16+
17+
import org.eclipse.core.runtime.CoreException;
18+
19+
/**
20+
* Represents an operation that accepts two input arguments, returns no
21+
* result and throws a {@link CoreException}.
22+
* This is the two-arity specialization of {@link Consumer}.
23+
* Unlike most other functional interfaces, {@code BiConsumer} is expected
24+
* to operate via side-effects.
25+
*
26+
*
27+
* @param <T> the type of the first argument to the operation
28+
* @param <U> the type of the second argument to the operation
29+
*
30+
* @see Consumer
31+
* @author Georg Tsakumagos
32+
*/
33+
@FunctionalInterface
34+
public interface CoreBiConsumer<T, U> {
35+
36+
/**
37+
* Performs this operation on the given arguments.
38+
*
39+
* @param t the first input argument
40+
* @param u the second input argument
41+
* @throws CoreException If something went wrong.
42+
*/
43+
void accept(T t, U u) throws CoreException;
44+
}

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

Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* Contributors:
1111
* Hannes Wellmann - initial API and implementation
1212
* Konrad Windszus - Add tests for required java runtime version implied by enforcer rule
13+
* Georg Tsakumagos - Add tests for global- & user- settings and toolchains.
1314
*******************************************************************************/
1415

1516
package org.eclipse.m2e.internal.launch;
@@ -19,21 +20,31 @@
1920

2021
import java.io.File;
2122
import java.util.List;
23+
import java.util.Optional;
2224

2325
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
26+
import org.apache.maven.cli.CLIManager;
2427
import org.eclipse.core.resources.IProject;
2528
import org.eclipse.core.runtime.CoreException;
29+
import org.eclipse.core.runtime.NullProgressMonitor;
2630
import org.eclipse.debug.core.DebugPlugin;
2731
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
2832
import org.eclipse.debug.core.ILaunchManager;
33+
import org.eclipse.debug.core.Launch;
2934
import org.eclipse.jdt.internal.launching.StandardVMType;
3035
import org.eclipse.jdt.launching.AbstractVMInstall;
3136
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
3237
import org.eclipse.jdt.launching.IVMInstall;
3338
import org.eclipse.jdt.launching.IVMInstallType;
3439
import org.eclipse.jdt.launching.JavaRuntime;
3540
import org.eclipse.m2e.actions.MavenLaunchConstants;
41+
import org.eclipse.m2e.core.CoreBiConsumer;
42+
import org.eclipse.m2e.core.MavenPlugin;
43+
import org.eclipse.m2e.core.embedder.IMavenConfiguration;
3644
import org.eclipse.m2e.tests.common.AbstractMavenProjectTestCase;
45+
import org.hamcrest.CoreMatchers;
46+
import org.hamcrest.Matcher;
47+
import org.hamcrest.MatcherAssert;
3748
import org.junit.Test;
3849
import org.mockito.MockedStatic;
3950
import org.mockito.Mockito;
@@ -47,6 +58,8 @@ public class MavenLaunchDelegateTest extends AbstractMavenProjectTestCase {
4758
private static final String DEFAULT_VM = "defaultVM";
4859
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");
4960

61+
62+
5063
@Test
5164
public void testGetBestMatchingVM_majorOnly() throws InvalidVersionSpecificationException {
5265
try (var mock = mockJavaRuntime()) {
@@ -75,6 +88,63 @@ public void testGetBestMatchingVM_1XversionRange() throws InvalidVersionSpecific
7588
}
7689
}
7790

91+
/**
92+
* Tests rendering of maven cli args for <em>global settings (-gs,--global-settings)</em>.
93+
* @throws Exception On errors.
94+
*/
95+
@Test
96+
public void testGlobalSettings() throws Exception {
97+
assertMavenLaunchFileSetting(IMavenConfiguration::setGlobalSettingsFile, CLIManager.ALTERNATE_GLOBAL_SETTINGS, "./resources/settings/empty_settings/settings_empty.xml");
98+
}
99+
100+
/**
101+
* Tests rendering of maven cli args for <em>global settings (-gs,--global-settings)</em>
102+
* if setting is overridden by direct parameterization in the goal input
103+
* @throws Exception On errors.
104+
*/
105+
@Test
106+
public void testGlobalSettings_GoalOverride() throws Exception {
107+
assertMavenLaunchFileSettingGoalOverride(IMavenConfiguration::setGlobalSettingsFile, CLIManager.ALTERNATE_GLOBAL_SETTINGS, "./resources/settings/empty_settings/settings_empty.xml");
108+
}
109+
110+
/**
111+
* Tests rendering of maven cli args for <em>global settings (-gs,--global-settings)</em>
112+
* if an invalid path was provided.
113+
* @throws Exception On errors.
114+
*/
115+
@Test
116+
public void testGlobalSettings_Invalid() throws Exception {
117+
assertMavenLaunchFileSettingPathInvalid(IMavenConfiguration::setGlobalSettingsFile);
118+
}
119+
120+
/**
121+
* Tests rendering of maven cli args for <em>global toolchains (-gt,--global-toolchains)</em>.
122+
* @throws Exception On errors.
123+
*/
124+
@Test
125+
public void testGlobalToolchains() throws Exception {
126+
assertMavenLaunchFileSetting(IMavenConfiguration::setGlobalToolchainsFile, CLIManager.ALTERNATE_GLOBAL_TOOLCHAINS, "./resources/settings/empty_settings/toolchains_empty.xml");
127+
}
128+
129+
/**
130+
* Tests rendering of maven cli args for <em>global toolchains (-gt,--global-toolchains)</em>
131+
* if setting is overridden by direct parameterization in the goal input
132+
* @throws Exception On errors.
133+
*/
134+
@Test
135+
public void testGlobalToolchains_GoalOverride() throws Exception {
136+
assertMavenLaunchFileSettingGoalOverride(IMavenConfiguration::setGlobalToolchainsFile, CLIManager.ALTERNATE_GLOBAL_TOOLCHAINS, "./resources/settings/empty_settings/toolchains_empty.xml");
137+
}
138+
139+
/**
140+
* Tests rendering of maven cli args for <em>global toolchains (-gt,--global-toolchains)</em> if an invalid path was provided.
141+
* @throws Exception On errors.
142+
*/
143+
@Test
144+
public void testGlobalToolchains_Invalid() throws Exception {
145+
assertMavenLaunchFileSettingPathInvalid(IMavenConfiguration::setGlobalToolchainsFile);
146+
}
147+
78148
@Test
79149
public void testRequiredJavaVersionFromEnforcerRule_Version() throws Exception {
80150
IProject project = importProject("resources/projects/enforcerSettingsWithVersion/pom.xml");
@@ -93,6 +163,179 @@ public void testRequiredJavaVersionFromEnforcerRule_NoVersionRange() throws Exce
93163
assertRequiredJavaBuildVersion(project, null, DEFAULT_VM);
94164
}
95165

166+
/**
167+
* Tests rendering of maven cli args for <em>global settings (-s,--settings)</em>
168+
* @throws Exception On errors.
169+
*/
170+
@Test
171+
public void testUserSettings() throws Exception {
172+
assertMavenLaunchFileSetting(IMavenConfiguration::setUserSettingsFile, String.valueOf(CLIManager.ALTERNATE_USER_SETTINGS), "./resources/settings/empty_settings/settings_empty.xml");
173+
}
174+
175+
/**
176+
* Tests rendering of maven cli args for <em>global settings (-s,--settings)</em>
177+
* if setting is overridden by direct parameterization in the goal input
178+
* @throws Exception On errors.
179+
*/
180+
@Test
181+
public void testUserSettings_GoalOverride() throws Exception {
182+
assertMavenLaunchFileSettingGoalOverride(IMavenConfiguration::setUserSettingsFile, String.valueOf(CLIManager.ALTERNATE_USER_SETTINGS), "./resources/settings/empty_settings/settings_empty.xml");
183+
}
184+
185+
/**
186+
* Tests rendering of maven cli args for <em>global settings (-s,--settings)</em>
187+
* if an invalid path was provided.
188+
* @throws Exception On errors.
189+
*/
190+
@Test
191+
public void testUserSettings_Invalid() throws Exception {
192+
assertMavenLaunchFileSettingPathInvalid(IMavenConfiguration::setUserSettingsFile);
193+
}
194+
195+
196+
/**
197+
* Tests rendering of maven cli args for <em>global toolchains (-t,--toolchains)</em>.
198+
* @throws Exception On errors.
199+
*/
200+
@Test
201+
public void testUserToolchains() throws Exception {
202+
assertMavenLaunchFileSetting(IMavenConfiguration::setUserToolchainsFile, String.valueOf(CLIManager.ALTERNATE_USER_TOOLCHAINS), "./resources/settings/empty_settings/toolchains_empty.xml");
203+
}
204+
205+
/**
206+
* Tests rendering of maven cli args for <em>global toolchains (-t,--toolchains)</em>.
207+
* if setting is overridden by direct parameterization in the goal input
208+
* @throws Exception On errors.
209+
*/
210+
@Test
211+
public void testUserToolchains_GoalOverride() throws Exception {
212+
assertMavenLaunchFileSettingGoalOverride(IMavenConfiguration::setUserToolchainsFile, String.valueOf(CLIManager.ALTERNATE_USER_TOOLCHAINS), "./resources/settings/empty_settings/toolchains_empty.xml");
213+
}
214+
215+
/**
216+
* Tests rendering of maven cli args for <em>global toolchains (-t,--toolchains)</em>
217+
* if an invalid path was provided.
218+
* @throws Exception On errors.
219+
*/
220+
@Test
221+
public void testUserToolchains_Invalid() throws Exception {
222+
assertMavenLaunchFileSettingPathInvalid(IMavenConfiguration::setUserToolchainsFile);
223+
}
224+
225+
226+
/**
227+
* assertion shortcut for launch configuration.
228+
* @param configSetter Setter for the configuration accepting the relativePath. Must not be <code>null</code>.
229+
* @param key Key of the configuration. Must not be <code>null</code>.
230+
* @param relativePath Relative path for the file. Must not be <code>null</code>.
231+
* @throws Exception Usually only on missed assertions.
232+
*/
233+
private void assertMavenLaunchConfig(CoreBiConsumer<IMavenConfiguration, String> configSetter, String goal, CoreBiConsumer<MavenLaunchDelegate, ILaunchConfigurationWorkingCopy> verifier, String relativePath)
234+
throws Exception {
235+
236+
waitForJobsToComplete();
237+
IProject project = importProject("resources/projects/simplePomOK/pom.xml");
238+
String pomDir = "${workspace_loc:/" + project.getName() + "}";
239+
240+
try (var mock = mockJavaRuntime()) {
241+
IMavenConfiguration mavenConfig = MavenPlugin.getMavenConfiguration();
242+
243+
try {
244+
configSetter.accept(mavenConfig, relativePath);
245+
ILaunchConfigurationWorkingCopy config = createMavenLaunchConfig(pomDir);
246+
247+
try {
248+
Optional.ofNullable(goal).ifPresent((g) -> config.setAttribute(MavenLaunchConstants.ATTR_GOALS, g));
249+
250+
// Prepare Mocks to capture VM configuration
251+
Launch launch = new Launch(config, "run", new MavenSourceLocator());
252+
NullProgressMonitor mockMonitor = Mockito.spy(new NullProgressMonitor());
253+
Mockito.doReturn(true).when(mockMonitor).isCanceled();
254+
255+
// mock launch
256+
MavenLaunchDelegate launcher = new MavenLaunchDelegate();
257+
launcher.launch(config, "run", launch, mockMonitor);
258+
259+
verifier.accept(launcher, config);
260+
} finally {
261+
Optional.ofNullable(goal).ifPresent((g) -> config.removeAttribute(MavenLaunchConstants.ATTR_GOALS));
262+
}
263+
} finally {
264+
// Reset property to avoid conflicts with other test cases.
265+
configSetter.accept(mavenConfig, null);
266+
}
267+
}
268+
}
269+
270+
/**
271+
* assertion shortcut for launch configuration.
272+
* @param configSetter Setter for the configuration accepting the relativePath. Must not be <code>null</code>.
273+
* @param key Key of the configuration. Must not be <code>null</code>.
274+
* @param relativePath Relative path for the file. Must not be <code>null</code>.
275+
* @throws Exception Usually only on missed assertions.
276+
*/
277+
private void assertMavenLaunchFileSetting(CoreBiConsumer<IMavenConfiguration, String> configSetter, String key, String relativePath)
278+
throws Exception {
279+
final String param = "-" + key;
280+
this.assertMavenLaunchConfig(configSetter, null, (launcher, config) -> {
281+
String programArguments = launcher.getProgramArguments(config);
282+
283+
// prepare assert
284+
Matcher<String> allSettings = CoreMatchers.allOf(
285+
CoreMatchers.containsString(param),
286+
CoreMatchers.containsString(new File(relativePath).getAbsolutePath())
287+
);
288+
289+
// assert
290+
MatcherAssert.assertThat(programArguments, allSettings);
291+
}, relativePath);
292+
}
293+
294+
/**
295+
* assertion shortcut for launch configuration.
296+
* @param configSetter Setter for the configuration accepting the relativePath. Must not be <code>null</code>.
297+
* @param key Key of the configuration. Must not be <code>null</code>.
298+
* @param relativePath Relative path for the file. Must not be <code>null</code>.
299+
* @throws Exception Usually only on missed assertions.
300+
*/
301+
private void assertMavenLaunchFileSettingGoalOverride(CoreBiConsumer<IMavenConfiguration, String> configSetter, String key, String relativePath)
302+
throws Exception {
303+
final String userDerivedPath = "./resources/settings/empty_settings/this_do_not_exists.xml";
304+
final String param = "-" + key;
305+
final String goalConfig = "clean " + param + " " + userDerivedPath;
306+
307+
this.assertMavenLaunchConfig(configSetter, goalConfig, (launcher, config) -> {
308+
String programArguments = launcher.getProgramArguments(config);
309+
310+
// prepare assert
311+
Matcher<String> allSettings = CoreMatchers.allOf(
312+
CoreMatchers.containsString(param),
313+
CoreMatchers.containsString(userDerivedPath),
314+
CoreMatchers.not(CoreMatchers.containsString(relativePath))
315+
);
316+
317+
// assert
318+
MatcherAssert.assertThat(programArguments, allSettings);
319+
}, relativePath);
320+
}
321+
322+
323+
/**
324+
* assertion shortcut for launch configuration if an invalid path was provided.
325+
* @param configSetter Setter for the configuration accepting the relativePath. Must not be <code>null</code>.
326+
* @param key Key of the configuration. Must not be <code>null</code>.
327+
* @throws Exception Usually only on missed assertions.
328+
*/
329+
private void assertMavenLaunchFileSettingPathInvalid(CoreBiConsumer<IMavenConfiguration, String> configSetter) throws Exception {
330+
final String path = "./resources/settings/empty_settings/this_do_not_exists.xml";
331+
try {
332+
this.assertMavenLaunchConfig(configSetter, null, (launcher, config) -> {}, path);
333+
} catch(IllegalArgumentException expected) {
334+
MatcherAssert.assertThat(expected.getMessage(), CoreMatchers.containsString(path));
335+
}
336+
}
337+
338+
96339
private void assertRequiredJavaBuildVersion(IProject project, String expectedVersionRange, String expectedVMVersion)
97340
throws Exception {
98341

0 commit comments

Comments
 (0)