1 package org.apache.maven.cli;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
59 assertEquals( (int) ( cores * 2.2 ), cli.calculateDegreeOfConcurrencyWithCoreMultiplier( "C2.2" ) );
60
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
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
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
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
114
115
116
117
118
119
120
121
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
132 cli.cli( request );
133
134 assertEquals( "5", request.commandLine.getOptionValue( CLIManager.THREADS ) );
135 }
136
137
138
139
140
141
142
143
144
145
146
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
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
166
167
168
169
170
171
172
173
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
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
193
194
195
196
197
198
199
200
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
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 }