View Javadoc
1   package org.apache.maven.plugins.invoker;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.util.Arrays;
24  import java.util.Collection;
25  import java.util.Collections;
26  import java.util.Properties;
27  
28  import org.apache.maven.shared.invoker.InvocationRequest;
29  import org.apache.maven.shared.invoker.InvocationRequest.ReactorFailureBehavior;
30  import org.junit.Test;
31  import org.junit.runner.RunWith;
32  import org.mockito.Mock;
33  import org.mockito.junit.MockitoJUnitRunner;
34  
35  import static org.assertj.core.api.Assertions.assertThat;
36  import static org.assertj.core.api.Assertions.assertThatCode;
37  import static org.mockito.ArgumentMatchers.anyList;
38  import static org.mockito.ArgumentMatchers.eq;
39  import static org.mockito.Mockito.clearInvocations;
40  import static org.mockito.Mockito.never;
41  import static org.mockito.Mockito.verify;
42  import static org.mockito.Mockito.verifyNoInteractions;
43  import static org.mockito.Mockito.verifyNoMoreInteractions;
44  import static org.mockito.Mockito.when;
45  
46  /**
47   * Tests the invoker properties facade.
48   *
49   * @author Benjamin Bentmann
50   */
51  @RunWith( MockitoJUnitRunner.class )
52  public class InvokerPropertiesTest
53  {
54  
55      @Mock
56      private InvocationRequest request;
57  
58      @Test
59      public void testConstructorNullSafe()
60      {
61          InvokerProperties facade = new InvokerProperties( null );
62          assertThat( facade.getProperties() ).isNotNull();
63      }
64  
65      @Test
66      public void testGetInvokerProperty()
67      {
68          Properties props = new Properties();
69          InvokerProperties facade = new InvokerProperties( props );
70  
71          assertThat( facade.get( "undefined-key", 0 ) ).isEmpty();
72  
73          props.setProperty( "key", "value" );
74          assertThat( facade.get( "key", 1 ) ).hasValue( "value" );
75  
76          props.setProperty( "key.1", "another-value" );
77          assertThat( facade.get( "key", 1 ) ).hasValue( "another-value" );
78          assertThat( facade.get( "key", 2 ) ).hasValue( "value" );
79      }
80  
81      @Test
82      public void testGetJobName()
83      {
84          Properties props = new Properties();
85          final String jobName = "Build Job name";
86          props.put( "invoker.name", jobName );
87          InvokerProperties facade = new InvokerProperties( props );
88  
89          assertThat( facade.getJobName() ).isEqualTo( jobName );
90      }
91  
92      @Test
93      public void testIsExpectedResult()
94      {
95          Properties props = new Properties();
96          InvokerProperties facade = new InvokerProperties( props );
97  
98          assertThat( facade.isExpectedResult( 0, 0 ) ).isTrue();
99          assertThat( facade.isExpectedResult( 1, 0 ) ).isFalse();
100 
101         props.setProperty( "invoker.buildResult", "success" );
102         assertThat( facade.isExpectedResult( 0, 0 ) ).isTrue();
103         assertThat( facade.isExpectedResult( 1, 0 ) ).isFalse();
104 
105         props.setProperty( "invoker.buildResult", "failure" );
106         assertThat( facade.isExpectedResult( 0, 0 ) ).isFalse();
107         assertThat( facade.isExpectedResult( 1, 0 ) ).isTrue();
108     }
109 
110     @Test
111     public void testConfigureRequestEmptyProperties()
112     {
113 
114         InvokerProperties facade = new InvokerProperties( null );
115 
116         facade.configureInvocation( request, 0 );
117         verifyNoInteractions( request );
118     }
119 
120     @Test
121     public void testConfigureRequestGoals()
122     {
123         Properties props = new Properties();
124         InvokerProperties facade = new InvokerProperties( props );
125 
126         props.setProperty( "invoker.goals", "verify" );
127         facade.configureInvocation( request, 0 );
128         verify( request ).setGoals( Collections.singletonList( "verify" ) );
129         verifyNoMoreInteractions( request );
130         clearInvocations( request );
131 
132         props.setProperty( "invoker.goals", "   " );
133         facade.configureInvocation( request, 0 );
134         verify( request, never() ).setGoals( anyList() );
135         verifyNoMoreInteractions( request );
136         clearInvocations( request );
137 
138         props.setProperty( "invoker.goals", "" );
139         facade.configureInvocation( request, 0 );
140         verify( request, never() ).setGoals( anyList() );
141         verifyNoMoreInteractions( request );
142         clearInvocations( request );
143 
144         props.setProperty( "invoker.goals", "  clean , test   verify " );
145         facade.configureInvocation( request, 0 );
146         verify( request ).setGoals( Arrays.asList( "clean", "test", "verify" ) );
147         verifyNoMoreInteractions( request );
148         clearInvocations( request );
149 
150         props.clear();
151 
152         facade.setDefaultGoals( Arrays.asList( "clean", "test" ) );
153         facade.configureInvocation( request, 0 );
154         verify( request ).setGoals( Arrays.asList( "clean", "test" ) );
155         verifyNoMoreInteractions( request );
156     }
157 
158     @Test
159     public void testConfigureRequestProfiles()
160     {
161         Properties props = new Properties();
162         InvokerProperties facade = new InvokerProperties( props );
163 
164         props.setProperty( "invoker.profiles", "verify" );
165         facade.configureInvocation( request, 0 );
166         verify( request ).setProfiles( Collections.singletonList( "verify" )  );
167         verifyNoMoreInteractions( request );
168         clearInvocations( request );
169 
170         props.setProperty( "invoker.profiles", "   " );
171         facade.configureInvocation( request, 0 );
172         verify( request, never() ).setProfiles( anyList()  );
173         verifyNoMoreInteractions( request );
174         clearInvocations( request );
175 
176         props.setProperty( "invoker.profiles", "" );
177         facade.configureInvocation( request, 0 );
178         verify( request, never() ).setProfiles( anyList() );
179         verifyNoMoreInteractions( request );
180         clearInvocations( request );
181 
182         props.setProperty( "invoker.profiles", "  clean , test   verify  ," );
183         facade.configureInvocation( request, 0 );
184         verify( request ).setProfiles( Arrays.asList( "clean", "test", "verify" ) );
185         verifyNoMoreInteractions( request );
186         clearInvocations( request );
187 
188         props.clear();
189         facade.setDefaultProfiles( Arrays.asList( "profile1",  "profile2" ) );
190         facade.configureInvocation( request, 0 );
191         verify( request ).setProfiles( Arrays.asList( "profile1",  "profile2" ) );
192         verifyNoMoreInteractions( request );
193 
194     }
195 
196     @Test
197     public void testConfigureRequestProject() throws Exception
198     {
199         Properties props = new Properties();
200         InvokerProperties facade = new InvokerProperties( props );
201 
202         File tempPom = File.createTempFile( "maven-invoker-plugin-test", ".pom" );
203         try
204         {
205             File tempDir = tempPom.getParentFile();
206             when( request.getBaseDirectory() ).thenReturn( tempDir );
207 
208             props.setProperty( "invoker.project", tempPom.getName() );
209             facade.configureInvocation( request, 0 );
210             verify( request ).getBaseDirectory();
211             verify( request ).setBaseDirectory( tempDir  );
212             verify( request ).setPomFile( tempPom  );
213             verifyNoMoreInteractions( request );
214             clearInvocations( request );
215 
216             props.setProperty( "invoker.project", "" );
217             facade.configureInvocation( request, 0 );
218             verifyNoInteractions( request );
219         }
220         finally
221         {
222             tempPom.delete();
223         }
224     }
225 
226     @Test
227     public void testConfigureRequestMavenExecutable()
228     {
229         Properties props = new Properties();
230 
231         InvokerProperties facade = new InvokerProperties( props );
232         File aDefExecutable = new File( "defExecutable" );
233         facade.setDefaultMavenExecutable( aDefExecutable );
234 
235         props.setProperty( "invoker.mavenExecutable", "aPropExecutable" );
236         facade.configureInvocation( request, 0 );
237         verify( request ).setMavenExecutable( new File( "aPropExecutable" ) );
238         verifyNoMoreInteractions( request );
239         clearInvocations( request );
240 
241         props.clear();
242 
243         facade.configureInvocation( request, 0 );
244         verify( request ).setMavenExecutable( aDefExecutable );
245         verifyNoMoreInteractions( request );
246     }
247 
248     @Test
249     public void testConfigureRequestMavenOpts()
250     {
251         Properties props = new Properties();
252 
253         InvokerProperties facade = new InvokerProperties( props );
254         facade.setDefaultMavenOpts( "-XxxDef" );
255 
256         props.setProperty( "invoker.mavenOpts", "-Xmx512m" );
257         facade.configureInvocation( request, 0 );
258         verify( request ).setMavenOpts( "-Xmx512m" );
259         verifyNoMoreInteractions( request );
260         clearInvocations( request );
261 
262         props.clear();
263 
264         facade.configureInvocation( request, 0 );
265         verify( request ).setMavenOpts( "-XxxDef" );
266         verifyNoMoreInteractions( request );
267     }
268 
269     @Test
270     public void testConfigureRequestFailureBehavior()
271     {
272         Properties props = new Properties();
273         InvokerProperties facade = new InvokerProperties( props );
274 
275         props.setProperty( "invoker.failureBehavior", ReactorFailureBehavior.FailNever.getLongOption() );
276         facade.configureInvocation( request, 0 );
277         verify( request ).setReactorFailureBehavior( eq( ReactorFailureBehavior.FailNever ) );
278 
279         verifyNoMoreInteractions( request );
280     }
281 
282     @Test
283     public void testConfigureRequestFailureBehaviorUnKnownName()
284     {
285         Properties props = new Properties();
286         InvokerProperties facade = new InvokerProperties( props );
287 
288         props.setProperty( "invoker.failureBehavior", "xxxUnKnown" );
289 
290         assertThatCode( () -> facade.configureInvocation( request, 0 ) )
291             .isExactlyInstanceOf( IllegalArgumentException.class  )
292             .hasMessage( "The string 'xxxUnKnown' can not be converted to enumeration." );
293 
294         verifyNoInteractions( request );
295     }
296 
297 
298     @Test
299     public void testConfigureRequestRecursion()
300     {
301         Properties props = new Properties();
302         InvokerProperties facade = new InvokerProperties( props );
303 
304         props.setProperty( "invoker.nonRecursive", "true" );
305         facade.configureInvocation( request, 0 );
306         verify( request ).setRecursive( false );
307         verifyNoMoreInteractions( request );
308         clearInvocations( request );
309 
310         props.setProperty( "invoker.nonRecursive", "false" );
311         facade.configureInvocation( request, 0 );
312         verify( request ).setRecursive( true );
313         verifyNoMoreInteractions( request );
314     }
315 
316     @Test
317     public void testConfigureRequestOffline()
318     {
319         Properties props = new Properties();
320         InvokerProperties facade = new InvokerProperties( props );
321 
322         props.setProperty( "invoker.offline", "true" );
323         facade.configureInvocation( request, 0 );
324         verify( request ).setOffline( true );
325         verifyNoMoreInteractions( request );
326         clearInvocations( request );
327 
328         props.setProperty( "invoker.offline", "false" );
329         facade.configureInvocation( request, 0 );
330         verify( request ).setOffline( false );
331         verifyNoMoreInteractions( request );
332     }
333 
334     @Test
335     public void testConfigureRequestDebug()
336     {
337         Properties props = new Properties();
338         InvokerProperties facade = new InvokerProperties( props );
339 
340         props.setProperty( "invoker.debug", "true" );
341         facade.configureInvocation( request, 0 );
342         verify( request ).setDebug( true );
343         verifyNoMoreInteractions( request );
344         clearInvocations( request );
345 
346         props.setProperty( "invoker.debug", "false" );
347         facade.configureInvocation( request, 0 );
348         verify( request ).setDebug( false );
349         verifyNoMoreInteractions( request );
350 
351         props.clear();
352 
353         facade.setDefaultDebug( true );
354         facade.configureInvocation( request, 0 );
355         verify( request ).setDebug( true );
356         verifyNoMoreInteractions( request );
357         clearInvocations( request );
358 
359         facade.setDefaultDebug( false );
360         facade.configureInvocation( request, 0 );
361         verify( request ).setDebug( false );
362         verifyNoMoreInteractions( request );
363         clearInvocations( request );
364     }
365 
366     @Test
367     public void testConfigureRequestQuiet()
368     {
369         Properties props = new Properties();
370         InvokerProperties facade = new InvokerProperties( props );
371 
372         props.setProperty( "invoker.quiet", "true" );
373         facade.configureInvocation( request, 0 );
374         verify( request ).setQuiet( true );
375         verifyNoMoreInteractions( request );
376         clearInvocations( request );
377 
378         props.setProperty( "invoker.quiet", "false" );
379         facade.configureInvocation( request, 0 );
380         verify( request ).setQuiet( false );
381         verifyNoMoreInteractions( request );
382 
383         props.clear();
384 
385         facade.setDefaultQuiet( true );
386         facade.configureInvocation( request, 0 );
387         verify( request ).setQuiet( true );
388         verifyNoMoreInteractions( request );
389         clearInvocations( request );
390 
391         facade.setDefaultQuiet( false );
392         facade.configureInvocation( request, 0 );
393         verify( request ).setQuiet( false );
394         verifyNoMoreInteractions( request );
395         clearInvocations( request );
396     }
397 
398     @Test
399     public void testConfigureRequestTimeoutInSeconds()
400     {
401         Properties props = new Properties();
402         InvokerProperties facade = new InvokerProperties( props );
403 
404         props.setProperty( "invoker.timeoutInSeconds", "5" );
405         facade.configureInvocation( request, 0 );
406         verify( request ).setTimeoutInSeconds( 5 );
407         verifyNoMoreInteractions( request );
408         clearInvocations( request );
409 
410         props.clear();
411 
412         facade.setDefaultTimeoutInSeconds( 3 );
413         facade.configureInvocation( request, 0 );
414         verify( request ).setTimeoutInSeconds( 3 );
415         verifyNoMoreInteractions( request );
416 
417     }
418 
419     @Test
420     public void testConfigureEnvironmentVariables()
421     {
422         Properties props = new Properties();
423         InvokerProperties facade = new InvokerProperties( props );
424 
425         props.setProperty( "invoker.abcdef", "abcdf" );
426         props.setProperty( "invoker.environmentVariables.KEY1.1", "value1.1" );
427         props.setProperty( "invoker.environmentVariables.KEY1", "value1" );
428         props.setProperty( "invoker.environmentVariables.KEY2", "value2" );
429         props.setProperty( "invoker.environmentVariables.KEY2.1", "value2.1" );
430         props.setProperty( "invoker.environmentVariables.KEY3", "value3" );
431         facade.configureInvocation( request, 0 );
432         verify( request ).addShellEnvironment( "KEY1", "value1" );
433         verify( request ).addShellEnvironment( "KEY2", "value2" );
434         verify( request ).addShellEnvironment( "KEY3", "value3" );
435         verifyNoMoreInteractions( request );
436     }
437 
438     @Test
439     public void testConfigureEnvironmentVariablesWithIndex()
440     {
441         Properties props = new Properties();
442         InvokerProperties facade = new InvokerProperties( props );
443 
444         props.setProperty( "invoker.abcdef", "abcdf" );
445         props.setProperty( "invoker.environmentVariables.KEY1.1", "value1.1" );
446         props.setProperty( "invoker.environmentVariables.KEY1", "value1" );
447         props.setProperty( "invoker.environmentVariables.KEY2", "value2" );
448         props.setProperty( "invoker.environmentVariables.KEY2.1", "value2.1" );
449         props.setProperty( "invoker.environmentVariables.KEY3", "value3" );
450         facade.configureInvocation( request, 1 );
451         verify( request ).addShellEnvironment( "KEY1", "value1.1" );
452         verify( request ).addShellEnvironment( "KEY2", "value2.1" );
453         verify( request ).addShellEnvironment( "KEY3", "value3" );
454         verifyNoMoreInteractions( request );
455     }
456 
457     @Test
458     public void testIsInvocationDefined()
459     {
460         Properties props = new Properties();
461         InvokerProperties facade = new InvokerProperties( props );
462 
463         assertThat( facade.isInvocationDefined( 1 ) ).isFalse();
464 
465         props.setProperty( "invoker.goals", "install" );
466         assertThat( facade.isInvocationDefined( 1 ) ).isFalse();
467 
468         props.setProperty( "invoker.goals.2", "install" );
469         assertThat( facade.isInvocationDefined( 1 ) ).isFalse();
470         assertThat( facade.isInvocationDefined( 2 ) ).isTrue();
471         assertThat( facade.isInvocationDefined( 3 ) ).isFalse();
472 
473         props.setProperty( "invoker.goals.3", "install" );
474         assertThat( facade.isInvocationDefined( 1 ) ).isFalse();
475         assertThat( facade.isInvocationDefined( 2 ) ).isTrue();
476         assertThat( facade.isInvocationDefined( 3 ) ).isTrue();
477         assertThat( facade.isInvocationDefined( 4 ) ).isFalse();
478     }
479 
480     @Test
481     public void testIsSelectedDefined()
482     {
483         Properties props = new Properties();
484         InvokerProperties facade = new InvokerProperties( props );
485 
486         assertThat( facade.isSelectorDefined( 1 ) ).isFalse();
487 
488         props.setProperty( "invoker.java.version", "1.6+" );
489         props.setProperty( "invoker.maven.version", "3.0+" );
490         props.setProperty( "invoker.os.family", "windows" );
491         assertThat( facade.isSelectorDefined( 1 ) ).isFalse();
492 
493         props.setProperty( "selector.2.java.version", "1.6+" );
494         props.setProperty( "selector.3.maven.version", "3.0+" );
495         props.setProperty( "selector.4.os.family", "windows" );
496         assertThat( facade.isSelectorDefined( 1 ) ).isFalse();
497         assertThat( facade.isSelectorDefined( 2 ) ).isTrue();
498         assertThat( facade.isSelectorDefined( 3 ) ).isTrue();
499         assertThat( facade.isSelectorDefined( 4 ) ).isTrue();
500         assertThat( facade.isSelectorDefined( 5 ) ).isFalse();
501     }
502 
503     @Test
504     public void testGetToolchainsForEmptyProperties()
505     {
506 
507         Properties props = new Properties();
508         InvokerProperties facade = new InvokerProperties( props );
509 
510         Collection<InvokerToolchain> toolchains = facade.getToolchains();
511         assertThat( toolchains ).isEmpty();
512 
513         toolchains = facade.getToolchains( 1 );
514         assertThat(  toolchains ).isEmpty();
515     }
516 
517     @Test
518     public void testGetToolchains()
519     {
520         Properties props = new Properties();
521         props.put( "invoker.toolchain.jdk.version", "11" );
522         InvokerProperties facade = new InvokerProperties( props );
523 
524         Collection<InvokerToolchain> toolchains = facade.getToolchains();
525         assertThat( toolchains ).hasSize( 1 );
526         InvokerToolchain toolchain = toolchains.iterator().next();
527         assertThat( toolchain.getType() ).isEqualTo( "jdk" );
528         assertThat( toolchain.getProvides() ).containsExactlyEntriesOf( Collections.singletonMap( "version", "11" ) );
529     }
530 
531     @Test
532     public void testGetToolchainsWithIndex()
533     {
534         Properties props = new Properties();
535         props.put( "selector.1.invoker.toolchain.jdk.version", "11" );
536         InvokerProperties facade = new InvokerProperties( props );
537 
538         Collection<InvokerToolchain> toolchains = facade.getToolchains( 1 );
539         assertThat( toolchains ).hasSize( 1 );
540         InvokerToolchain toolchain = toolchains.iterator().next();
541         assertThat( toolchain.getType() ).isEqualTo( "jdk" );
542         assertThat( toolchain.getProvides() ).containsExactlyEntriesOf( Collections.singletonMap( "version", "11" ) );
543     }
544 
545 }