001package org.apache.maven.cli; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.io.File; 023 024import junit.framework.TestCase; 025 026import org.apache.commons.cli.ParseException; 027 028public class MavenCliTest 029 extends TestCase 030{ 031 private MavenCli cli; 032 033 private String origBasedir; 034 035 protected void setUp() 036 { 037 cli = new MavenCli(); 038 origBasedir = System.getProperty( MavenCli.MULTIMODULE_PROJECT_DIRECTORY ); 039 } 040 041 @Override 042 protected void tearDown() 043 throws Exception 044 { 045 if ( origBasedir != null ) 046 { 047 System.setProperty( MavenCli.MULTIMODULE_PROJECT_DIRECTORY, origBasedir ); 048 } 049 else 050 { 051 System.getProperties().remove( MavenCli.MULTIMODULE_PROJECT_DIRECTORY ); 052 } 053 super.tearDown(); 054 } 055 056 public void testCalculateDegreeOfConcurrencyWithCoreMultiplier() 057 { 058 int cores = Runtime.getRuntime().availableProcessors(); 059 // -T2.2C 060 assertEquals( (int) ( cores * 2.2 ), cli.calculateDegreeOfConcurrencyWithCoreMultiplier( "C2.2" ) ); 061 // -TC2.2 062 assertEquals( (int) ( cores * 2.2 ), cli.calculateDegreeOfConcurrencyWithCoreMultiplier( "2.2C" ) ); 063 064 try 065 { 066 cli.calculateDegreeOfConcurrencyWithCoreMultiplier( "CXXX" ); 067 fail( "Should have failed with a NumberFormatException" ); 068 } 069 catch ( NumberFormatException e ) 070 { 071 // carry on 072 } 073 } 074 075 public void testMavenConfig() 076 throws Exception 077 { 078 System.setProperty( MavenCli.MULTIMODULE_PROJECT_DIRECTORY, new File( "src/test/projects/config" ).getCanonicalPath() ); 079 CliRequest request = new CliRequest( new String[0], null ); 080 081 // read .mvn/maven.config 082 cli.initialize( request ); 083 cli.cli( request ); 084 assertEquals( "multithreaded", request.commandLine.getOptionValue( "builder" ) ); 085 assertEquals( "8", request.commandLine.getOptionValue( "threads" ) ); 086 087 // override from command line 088 request = new CliRequest( new String[] { "--builder", "foobar" }, null ); 089 cli.cli( request ); 090 assertEquals( "foobar", request.commandLine.getOptionValue( "builder" ) ); 091 } 092 093 public void testMavenConfigInvalid() 094 throws Exception 095 { 096 System.setProperty( MavenCli.MULTIMODULE_PROJECT_DIRECTORY, new File( "src/test/projects/config-illegal" ).getCanonicalPath() ); 097 CliRequest request = new CliRequest( new String[0], null ); 098 099 cli.initialize( request ); 100 try 101 { 102 cli.cli( request ); 103 fail(); 104 } 105 catch ( ParseException expected ) 106 { 107 108 } 109 } 110}