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 junit.framework.TestCase;
23  import org.apache.commons.cli.ParseException;
24  
25  import java.io.File;
26  
27  public class MavenCliTest
28      extends TestCase
29  {
30      private MavenCli cli;
31  
32      private String origBasedir;
33  
34      protected void setUp()
35      {
36          cli = new MavenCli();
37          origBasedir = System.getProperty( MavenCli.MULTIMODULE_PROJECT_DIRECTORY );
38      }
39  
40      @Override
41      protected void tearDown()
42          throws Exception
43      {
44          if ( origBasedir != null )
45          {
46              System.setProperty( MavenCli.MULTIMODULE_PROJECT_DIRECTORY, origBasedir );
47          }
48          else
49          {
50              System.getProperties().remove( MavenCli.MULTIMODULE_PROJECT_DIRECTORY );
51          }
52          super.tearDown();
53      }
54  
55      public void testCalculateDegreeOfConcurrencyWithCoreMultiplier()
56      {
57          int cores = Runtime.getRuntime().availableProcessors();
58          // -T2.2C
59          assertEquals( (int) ( cores * 2.2 ), cli.calculateDegreeOfConcurrencyWithCoreMultiplier( "C2.2" ) );
60          // -TC2.2
61          assertEquals( (int) ( cores * 2.2 ), cli.calculateDegreeOfConcurrencyWithCoreMultiplier( "2.2C" ) );
62  
63          try
64          {
65              cli.calculateDegreeOfConcurrencyWithCoreMultiplier( "CXXX" );
66              fail( "Should have failed with a NumberFormatException" );
67          }
68          catch ( NumberFormatException e )
69          {
70              // carry on
71          }
72      }
73  
74      public void testMavenConfig()
75          throws Exception
76      {
77          System.setProperty( MavenCli.MULTIMODULE_PROJECT_DIRECTORY,
78                              new File( "src/test/projects/config" ).getCanonicalPath() );
79          CliRequest request = new CliRequest( new String[0], null );
80  
81          // read .mvn/maven.config
82          cli.initialize( request );
83          cli.cli( request );
84          assertEquals( "multithreaded", request.commandLine.getOptionValue( CLIManager.BUILDER ) );
85          assertEquals( "8", request.commandLine.getOptionValue( CLIManager.THREADS ) );
86  
87          // override from command line
88          request = new CliRequest( new String[]{ "--builder", "foobar" }, null );
89          cli.cli( request );
90          assertEquals( "foobar", request.commandLine.getOptionValue( "builder" ) );
91      }
92  
93      public void testMavenConfigInvalid()
94          throws Exception
95      {
96          System.setProperty( MavenCli.MULTIMODULE_PROJECT_DIRECTORY,
97                              new File( "src/test/projects/config-illegal" ).getCanonicalPath() );
98          CliRequest request = new CliRequest( new String[0], null );
99  
100         cli.initialize( request );
101         try
102         {
103             cli.cli( request );
104             fail();
105         }
106         catch ( ParseException expected )
107         {
108 
109         }
110     }
111 
112     /**
113      * Read .mvn/maven.config with the following definitions:
114      * <pre>
115      *   -T 3
116      *   -Drevision=1.3.0
117      * </pre>
118      * and check if the {@code -T 3} option can be overwritten via command line
119      * argument.
120      *
121      * @throws Exception in case of failure.
122      */
123     public void testMVNConfigurationThreadCanBeOverwrittenViaCommandLine()
124         throws Exception
125     {
126         System.setProperty( MavenCli.MULTIMODULE_PROJECT_DIRECTORY,
127                             new File( "src/test/projects/mavenConfigProperties" ).getCanonicalPath() );
128         CliRequest request = new CliRequest( new String[]{ "-T", "5" }, null );
129 
130         cli.initialize( request );
131         // read .mvn/maven.config
132         cli.cli( request );
133 
134         assertEquals( "5", request.commandLine.getOptionValue( CLIManager.THREADS ) );
135     }
136 
137     /**
138      * Read .mvn/maven.config with the following definitions:
139      * <pre>
140      *   -T 3
141      *   -Drevision=1.3.0
142      * </pre>
143      * and check if the {@code -Drevision-1.3.0} option can be overwritten via command line
144      * argument.
145      *
146      * @throws Exception
147      */
148     public void testMVNConfigurationDefinedPropertiesCanBeOverwrittenViaCommandLine()
149         throws Exception
150     {
151         System.setProperty( MavenCli.MULTIMODULE_PROJECT_DIRECTORY,
152                             new File( "src/test/projects/mavenConfigProperties" ).getCanonicalPath() );
153         CliRequest request = new CliRequest( new String[]{ "-Drevision=8.1.0" }, null );
154 
155         cli.initialize( request );
156         // read .mvn/maven.config
157         cli.cli( request );
158         cli.properties( request );
159 
160         String revision = System.getProperty( "revision" );
161         assertEquals( "8.1.0", revision );
162     }
163 
164     /**
165      * Read .mvn/maven.config with the following definitions:
166      * <pre>
167      *   -T 3
168      *   -Drevision=1.3.0
169      * </pre>
170      * and check if the {@code -Drevision-1.3.0} option can be overwritten via command line
171      * argument.
172      *
173      * @throws Exception
174      */
175     public void testMVNConfigurationCLIRepeatedPropertiesLastWins()
176         throws Exception
177     {
178         System.setProperty( MavenCli.MULTIMODULE_PROJECT_DIRECTORY,
179                             new File( "src/test/projects/mavenConfigProperties" ).getCanonicalPath() );
180         CliRequest request = new CliRequest( new String[]{ "-Drevision=8.1.0", "-Drevision=8.2.0" }, null );
181 
182         cli.initialize( request );
183         // read .mvn/maven.config
184         cli.cli( request );
185         cli.properties( request );
186 
187         String revision = System.getProperty( "revision" );
188         assertEquals( "8.2.0", revision );
189     }
190 
191     /**
192      * Read .mvn/maven.config with the following definitions:
193      * <pre>
194      *   -T 3
195      *   -Drevision=1.3.0
196      * </pre>
197      * and check if the {@code -Drevision-1.3.0} option can be overwritten via command line argument when there are
198      * funky arguments present.
199      *
200      * @throws Exception
201      */
202     public void testMVNConfigurationFunkyArguments()
203         throws Exception
204     {
205         System.setProperty( MavenCli.MULTIMODULE_PROJECT_DIRECTORY,
206                             new File( "src/test/projects/mavenConfigProperties" ).getCanonicalPath() );
207         CliRequest request = new CliRequest(
208             new String[]{ "-Drevision=8.1.0", "--file=-Dpom.xml", "\"-Dfoo=bar ", "\"-Dfoo2=bar two\"",
209                 "-Drevision=8.2.0" }, null );
210 
211         cli.initialize( request );
212         // read .mvn/maven.config
213         cli.cli( request );
214         cli.properties( request );
215 
216         String revision = System.getProperty( "revision" );
217         assertEquals( "8.2.0", revision );
218 
219         assertEquals( "bar ", request.getSystemProperties().getProperty( "foo" ) );
220         assertEquals( "bar two", request.getSystemProperties().getProperty( "foo2" ) );
221 
222         assertEquals( "-Dpom.xml", request.getCommandLine().getOptionValue( CLIManager.ALTERNATE_POM_FILE ) );
223     }
224 }