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 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
68 assertEquals( (int) ( cores * 2.2 ), cli.calculateDegreeOfConcurrencyWithCoreMultiplier( "C2.2" ) );
69
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
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
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
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
125
126
127
128
129
130
131
132
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
144 cli.cli( request );
145
146 assertEquals( "5", request.commandLine.getOptionValue( CLIManager.THREADS ) );
147 }
148
149
150
151
152
153
154
155
156
157
158
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
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
179
180
181
182
183
184
185
186
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
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
207
208
209
210
211
212
213
214
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
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
294 }
295 }
296 }