View Javadoc
1   package org.apache.maven.cli;
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 static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertFalse;
24  import static org.junit.Assert.assertTrue;
25  import static org.junit.Assert.fail;
26  import static org.junit.Assume.assumeTrue;
27  import static org.mockito.Mockito.any;
28  import static org.mockito.Mockito.inOrder;
29  import static org.mockito.Mockito.mock;
30  import static org.mockito.Mockito.times;
31  
32  import java.io.File;
33  
34  import org.apache.commons.cli.ParseException;
35  import org.apache.maven.Maven;
36  import org.apache.maven.eventspy.internal.EventSpyDispatcher;
37  import org.apache.maven.shared.utils.logging.MessageUtils;
38  import org.apache.maven.toolchain.building.ToolchainsBuildingRequest;
39  import org.apache.maven.toolchain.building.ToolchainsBuildingResult;
40  import org.codehaus.plexus.PlexusContainer;
41  import org.junit.After;
42  import org.junit.Before;
43  import org.junit.Test;
44  import org.mockito.InOrder;
45  
46  public class MavenCliTest
47  {
48      private MavenCli cli;
49  
50      private String origBasedir;
51  
52      @Before
53      public void setUp()
54      {
55          cli = new MavenCli();
56          origBasedir = System.getProperty( MavenCli.MULTIMODULE_PROJECT_DIRECTORY );
57      }
58  
59      @After
60      public void tearDown()
61          throws Exception
62      {
63          if ( origBasedir != null )
64          {
65              System.setProperty( MavenCli.MULTIMODULE_PROJECT_DIRECTORY, origBasedir );
66          }
67          else
68          {
69              System.getProperties().remove( MavenCli.MULTIMODULE_PROJECT_DIRECTORY );
70          }
71      }
72  
73      @Test
74      public void testCalculateDegreeOfConcurrencyWithCoreMultiplier()
75      {
76          int cores = Runtime.getRuntime().availableProcessors();
77          // -T2.2C
78          assertEquals( (int) ( cores * 2.2 ), cli.calculateDegreeOfConcurrencyWithCoreMultiplier( "C2.2" ) );
79          // -TC2.2
80          assertEquals( (int) ( cores * 2.2 ), cli.calculateDegreeOfConcurrencyWithCoreMultiplier( "2.2C" ) );
81  
82          try
83          {
84              cli.calculateDegreeOfConcurrencyWithCoreMultiplier( "CXXX" );
85              fail( "Should have failed with a NumberFormatException" );
86          }
87          catch ( NumberFormatException e )
88          {
89              // carry on
90          }
91      }
92  
93      @Test
94      public void testMavenConfig()
95          throws Exception
96      {
97          System.setProperty( MavenCli.MULTIMODULE_PROJECT_DIRECTORY,
98                              new File( "src/test/projects/config" ).getCanonicalPath() );
99          CliRequest request = new CliRequest( new String[0], null );
100 
101         // read .mvn/maven.config
102         cli.initialize( request );
103         cli.cli( request );
104         assertEquals( "multithreaded", request.commandLine.getOptionValue( CLIManager.BUILDER ) );
105         assertEquals( "8", request.commandLine.getOptionValue( CLIManager.THREADS ) );
106 
107         // override from command line
108         request = new CliRequest( new String[]{ "--builder", "foobar" }, null );
109         cli.cli( request );
110         assertEquals( "foobar", request.commandLine.getOptionValue( "builder" ) );
111     }
112 
113     @Test
114     public void testMavenConfigInvalid()
115         throws Exception
116     {
117         System.setProperty( MavenCli.MULTIMODULE_PROJECT_DIRECTORY,
118                             new File( "src/test/projects/config-illegal" ).getCanonicalPath() );
119         CliRequest request = new CliRequest( new String[0], null );
120 
121         cli.initialize( request );
122         try
123         {
124             cli.cli( request );
125             fail();
126         }
127         catch ( ParseException expected )
128         {
129 
130         }
131     }
132 
133     /**
134      * Read .mvn/maven.config with the following definitions:
135      * <pre>
136      *   -T 3
137      *   -Drevision=1.3.0
138      * </pre>
139      * and check if the {@code -T 3} option can be overwritten via command line
140      * argument.
141      *
142      * @throws Exception in case of failure.
143      */
144     @Test
145     public void testMVNConfigurationThreadCanBeOverwrittenViaCommandLine()
146         throws Exception
147     {
148         System.setProperty( MavenCli.MULTIMODULE_PROJECT_DIRECTORY,
149                             new File( "src/test/projects/mavenConfigProperties" ).getCanonicalPath() );
150         CliRequest request = new CliRequest( new String[]{ "-T", "5" }, null );
151 
152         cli.initialize( request );
153         // read .mvn/maven.config
154         cli.cli( request );
155 
156         assertEquals( "5", request.commandLine.getOptionValue( CLIManager.THREADS ) );
157     }
158 
159     /**
160      * Read .mvn/maven.config with the following definitions:
161      * <pre>
162      *   -T 3
163      *   -Drevision=1.3.0
164      * </pre>
165      * and check if the {@code -Drevision-1.3.0} option can be overwritten via command line
166      * argument.
167      *
168      * @throws Exception
169      */
170     @Test
171     public void testMVNConfigurationDefinedPropertiesCanBeOverwrittenViaCommandLine()
172         throws Exception
173     {
174         System.setProperty( MavenCli.MULTIMODULE_PROJECT_DIRECTORY,
175                             new File( "src/test/projects/mavenConfigProperties" ).getCanonicalPath() );
176         CliRequest request = new CliRequest( new String[]{ "-Drevision=8.1.0" }, null );
177 
178         cli.initialize( request );
179         // read .mvn/maven.config
180         cli.cli( request );
181         cli.properties( request );
182 
183         String revision = System.getProperty( "revision" );
184         assertEquals( "8.1.0", revision );
185     }
186 
187     /**
188      * Read .mvn/maven.config with the following definitions:
189      * <pre>
190      *   -T 3
191      *   -Drevision=1.3.0
192      * </pre>
193      * and check if the {@code -Drevision-1.3.0} option can be overwritten via command line
194      * argument.
195      *
196      * @throws Exception
197      */
198     @Test
199     public void testMVNConfigurationCLIRepeatedPropertiesLastWins()
200         throws Exception
201     {
202         System.setProperty( MavenCli.MULTIMODULE_PROJECT_DIRECTORY,
203                             new File( "src/test/projects/mavenConfigProperties" ).getCanonicalPath() );
204         CliRequest request = new CliRequest( new String[]{ "-Drevision=8.1.0", "-Drevision=8.2.0" }, null );
205 
206         cli.initialize( request );
207         // read .mvn/maven.config
208         cli.cli( request );
209         cli.properties( request );
210 
211         String revision = System.getProperty( "revision" );
212         assertEquals( "8.2.0", revision );
213     }
214 
215     /**
216      * Read .mvn/maven.config with the following definitions:
217      * <pre>
218      *   -T 3
219      *   -Drevision=1.3.0
220      * </pre>
221      * and check if the {@code -Drevision-1.3.0} option can be overwritten via command line argument when there are
222      * funky arguments present.
223      *
224      * @throws Exception
225      */
226     @Test
227     public void testMVNConfigurationFunkyArguments()
228         throws Exception
229     {
230         System.setProperty( MavenCli.MULTIMODULE_PROJECT_DIRECTORY,
231                             new File( "src/test/projects/mavenConfigProperties" ).getCanonicalPath() );
232         CliRequest request = new CliRequest(
233             new String[]{ "-Drevision=8.1.0", "--file=-Dpom.xml", "\"-Dfoo=bar ", "\"-Dfoo2=bar two\"",
234                 "-Drevision=8.2.0" }, null );
235 
236         cli.initialize( request );
237         // read .mvn/maven.config
238         cli.cli( request );
239         cli.properties( request );
240 
241         String revision = System.getProperty( "revision" );
242         assertEquals( "8.2.0", revision );
243 
244         assertEquals( "bar ", request.getSystemProperties().getProperty( "foo" ) );
245         assertEquals( "bar two", request.getSystemProperties().getProperty( "foo2" ) );
246 
247         assertEquals( "-Dpom.xml", request.getCommandLine().getOptionValue( CLIManager.ALTERNATE_POM_FILE ) );
248     }
249 
250     @Test
251     public void testStyleColors()
252         throws Exception
253     {
254         assumeTrue( "ANSI not supported", MessageUtils.isColorEnabled() );
255         CliRequest request;
256 
257         MessageUtils.setColorEnabled( true );
258         request = new CliRequest( new String[] { "-B" }, null );
259         cli.cli( request );
260         cli.properties( request );
261         cli.logging( request );
262         assertFalse( MessageUtils.isColorEnabled() );
263 
264         MessageUtils.setColorEnabled( true );
265         request = new CliRequest( new String[] { "-l", "target/temp/mvn.log" }, null );
266         cli.cli( request );
267         cli.properties( request );
268         cli.logging( request );
269         assertFalse( MessageUtils.isColorEnabled() );
270 
271         MessageUtils.setColorEnabled( false );
272         request = new CliRequest( new String[] { "-Dstyle.color=always" }, null );
273         cli.cli( request );
274         cli.properties( request );
275         cli.logging( request );
276         assertTrue( MessageUtils.isColorEnabled() );
277 
278         MessageUtils.setColorEnabled( true );
279         request = new CliRequest( new String[] { "-Dstyle.color=never" }, null );
280         cli.cli( request );
281         cli.properties( request );
282         cli.logging( request );
283         assertFalse( MessageUtils.isColorEnabled() );
284 
285         MessageUtils.setColorEnabled( false );
286         request = new CliRequest( new String[] { "-Dstyle.color=always", "-B", "-l", "target/temp/mvn.log" }, null );
287         cli.cli( request );
288         cli.properties( request );
289         cli.logging( request );
290         assertTrue( MessageUtils.isColorEnabled() );
291 
292         try
293         {
294             MessageUtils.setColorEnabled( false );
295             request = new CliRequest( new String[] { "-Dstyle.color=maybe", "-B", "-l", "target/temp/mvn.log" }, null );
296             cli.cli( request );
297             cli.properties( request );
298             cli.logging( request );
299             fail( "maybe is not a valid option" );
300         }
301         catch ( IllegalArgumentException e )
302         {
303             // noop
304         }
305     }
306 
307     /**
308      * Verifies MNG-6558
309      */
310     @Test
311     public void testToolchainsBuildingEvents() throws Exception {
312         final EventSpyDispatcher eventSpyDispatcherMock = mock(EventSpyDispatcher.class);
313         MavenCli customizedMavenCli = new MavenCli() {
314             @Override
315             protected void customizeContainer(PlexusContainer container) {
316                 super.customizeContainer(container);
317                 container.addComponent(eventSpyDispatcherMock, "org.apache.maven.eventspy.internal.EventSpyDispatcher");
318                 container.addComponent(mock(Maven.class), "org.apache.maven.Maven");
319             }
320         };
321 
322         CliRequest cliRequest = new CliRequest(new String[]{}, null);
323 
324         customizedMavenCli.cli(cliRequest);
325         customizedMavenCli.logging(cliRequest);
326         customizedMavenCli.container(cliRequest);
327         customizedMavenCli.toolchains(cliRequest);
328 
329         InOrder orderdEventSpyDispatcherMock = inOrder(eventSpyDispatcherMock);
330         orderdEventSpyDispatcherMock.verify(eventSpyDispatcherMock, times(1)).onEvent(any(ToolchainsBuildingRequest.class));
331         orderdEventSpyDispatcherMock.verify(eventSpyDispatcherMock, times(1)).onEvent(any(ToolchainsBuildingResult.class));
332     }
333 
334 }