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 java.io.File;
23  
24  import junit.framework.TestCase;
25  
26  import org.apache.commons.cli.ParseException;
27  
28  public class MavenCliTest
29      extends TestCase
30  {
31      private MavenCli cli;
32  
33      private String origBasedir;
34  
35      protected void setUp()
36      {
37          cli = new MavenCli();
38          origBasedir = System.getProperty( MavenCli.MULTIMODULE_PROJECT_DIRECTORY );
39      }
40  
41      @Override
42      protected void tearDown()
43          throws Exception
44      {
45          if ( origBasedir != null )
46          {
47              System.setProperty( MavenCli.MULTIMODULE_PROJECT_DIRECTORY, origBasedir );
48          }
49          else
50          {
51              System.getProperties().remove( MavenCli.MULTIMODULE_PROJECT_DIRECTORY );
52          }
53          super.tearDown();
54      }
55  
56      public void testCalculateDegreeOfConcurrencyWithCoreMultiplier()
57      {
58          int cores = Runtime.getRuntime().availableProcessors();
59          // -T2.2C
60          assertEquals( (int) ( cores * 2.2 ), cli.calculateDegreeOfConcurrencyWithCoreMultiplier( "C2.2" ) );
61          // -TC2.2
62          assertEquals( (int) ( cores * 2.2 ), cli.calculateDegreeOfConcurrencyWithCoreMultiplier( "2.2C" ) );
63  
64          try
65          {
66              cli.calculateDegreeOfConcurrencyWithCoreMultiplier( "CXXX" );
67              fail( "Should have failed with a NumberFormatException" );
68          }
69          catch ( NumberFormatException e )
70          {
71              // carry on
72          }
73      }
74  
75      public void testMavenConfig()
76          throws Exception
77      {
78          System.setProperty( MavenCli.MULTIMODULE_PROJECT_DIRECTORY, 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( "builder" ) );
85          assertEquals( "8", request.commandLine.getOptionValue( "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, new File( "src/test/projects/config-illegal" ).getCanonicalPath() );
97          CliRequest request = new CliRequest( new String[0], null );
98  
99          cli.initialize( request );
100         try
101         {
102             cli.cli( request );
103             fail();
104         }
105         catch ( ParseException expected )
106         {
107 
108         }
109     }
110 }