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