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
1516package org .eclipse .m2e .internal .launch ;
1920
2021import java .io .File ;
2122import java .util .List ;
23+ import java .util .Optional ;
2224
2325import org .apache .maven .artifact .versioning .InvalidVersionSpecificationException ;
26+ import org .apache .maven .cli .CLIManager ;
2427import org .eclipse .core .resources .IProject ;
2528import org .eclipse .core .runtime .CoreException ;
29+ import org .eclipse .core .runtime .NullProgressMonitor ;
2630import org .eclipse .debug .core .DebugPlugin ;
2731import org .eclipse .debug .core .ILaunchConfigurationWorkingCopy ;
2832import org .eclipse .debug .core .ILaunchManager ;
33+ import org .eclipse .debug .core .Launch ;
2934import org .eclipse .jdt .internal .launching .StandardVMType ;
3035import org .eclipse .jdt .launching .AbstractVMInstall ;
3136import org .eclipse .jdt .launching .IJavaLaunchConfigurationConstants ;
3237import org .eclipse .jdt .launching .IVMInstall ;
3338import org .eclipse .jdt .launching .IVMInstallType ;
3439import org .eclipse .jdt .launching .JavaRuntime ;
3540import 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 ;
3644import org .eclipse .m2e .tests .common .AbstractMavenProjectTestCase ;
45+ import org .hamcrest .CoreMatchers ;
46+ import org .hamcrest .Matcher ;
47+ import org .hamcrest .MatcherAssert ;
3748import org .junit .Test ;
3849import org .mockito .MockedStatic ;
3950import 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