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 import static org.mockito.Mockito.any;
28 import static org.mockito.Mockito.inOrder;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.times;
31
32 import java.io.File;
33
34 import org.apache.commons.cli.ParseException;
35 import org.apache.maven.Maven;
36 import org.apache.maven.eventspy.internal.EventSpyDispatcher;
37 import org.apache.maven.shared.utils.logging.MessageUtils;
38 import org.apache.maven.toolchain.building.ToolchainsBuildingRequest;
39 import org.apache.maven.toolchain.building.ToolchainsBuildingResult;
40 import org.codehaus.plexus.PlexusContainer;
41 import org.junit.After;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.mockito.InOrder;
45
46 public class MavenCliTest
47 {
48 private MavenCli cli;
49
50 private String origBasedir;
51
52 @Before
53 public void setUp()
54 {
55 cli = new MavenCli();
56 origBasedir = System.getProperty( MavenCli.MULTIMODULE_PROJECT_DIRECTORY );
57 }
58
59 @After
60 public void tearDown()
61 throws Exception
62 {
63 if ( origBasedir != null )
64 {
65 System.setProperty( MavenCli.MULTIMODULE_PROJECT_DIRECTORY, origBasedir );
66 }
67 else
68 {
69 System.getProperties().remove( MavenCli.MULTIMODULE_PROJECT_DIRECTORY );
70 }
71 }
72
73 @Test
74 public void testCalculateDegreeOfConcurrencyWithCoreMultiplier()
75 {
76 int cores = Runtime.getRuntime().availableProcessors();
77
78 assertEquals( (int) ( cores * 2.2 ), cli.calculateDegreeOfConcurrencyWithCoreMultiplier( "C2.2" ) );
79
80 assertEquals( (int) ( cores * 2.2 ), cli.calculateDegreeOfConcurrencyWithCoreMultiplier( "2.2C" ) );
81
82 try
83 {
84 cli.calculateDegreeOfConcurrencyWithCoreMultiplier( "CXXX" );
85 fail( "Should have failed with a NumberFormatException" );
86 }
87 catch ( NumberFormatException e )
88 {
89
90 }
91 }
92
93 @Test
94 public void testMavenConfig()
95 throws Exception
96 {
97 System.setProperty( MavenCli.MULTIMODULE_PROJECT_DIRECTORY,
98 new File( "src/test/projects/config" ).getCanonicalPath() );
99 CliRequest request = new CliRequest( new String[0], null );
100
101
102 cli.initialize( request );
103 cli.cli( request );
104 assertEquals( "multithreaded", request.commandLine.getOptionValue( CLIManager.BUILDER ) );
105 assertEquals( "8", request.commandLine.getOptionValue( CLIManager.THREADS ) );
106
107
108 request = new CliRequest( new String[]{ "--builder", "foobar" }, null );
109 cli.cli( request );
110 assertEquals( "foobar", request.commandLine.getOptionValue( "builder" ) );
111 }
112
113 @Test
114 public void testMavenConfigInvalid()
115 throws Exception
116 {
117 System.setProperty( MavenCli.MULTIMODULE_PROJECT_DIRECTORY,
118 new File( "src/test/projects/config-illegal" ).getCanonicalPath() );
119 CliRequest request = new CliRequest( new String[0], null );
120
121 cli.initialize( request );
122 try
123 {
124 cli.cli( request );
125 fail();
126 }
127 catch ( ParseException expected )
128 {
129
130 }
131 }
132
133
134
135
136
137
138
139
140
141
142
143
144 @Test
145 public void testMVNConfigurationThreadCanBeOverwrittenViaCommandLine()
146 throws Exception
147 {
148 System.setProperty( MavenCli.MULTIMODULE_PROJECT_DIRECTORY,
149 new File( "src/test/projects/mavenConfigProperties" ).getCanonicalPath() );
150 CliRequest request = new CliRequest( new String[]{ "-T", "5" }, null );
151
152 cli.initialize( request );
153
154 cli.cli( request );
155
156 assertEquals( "5", request.commandLine.getOptionValue( CLIManager.THREADS ) );
157 }
158
159
160
161
162
163
164
165
166
167
168
169
170 @Test
171 public void testMVNConfigurationDefinedPropertiesCanBeOverwrittenViaCommandLine()
172 throws Exception
173 {
174 System.setProperty( MavenCli.MULTIMODULE_PROJECT_DIRECTORY,
175 new File( "src/test/projects/mavenConfigProperties" ).getCanonicalPath() );
176 CliRequest request = new CliRequest( new String[]{ "-Drevision=8.1.0" }, null );
177
178 cli.initialize( request );
179
180 cli.cli( request );
181 cli.properties( request );
182
183 String revision = System.getProperty( "revision" );
184 assertEquals( "8.1.0", revision );
185 }
186
187
188
189
190
191
192
193
194
195
196
197
198 @Test
199 public void testMVNConfigurationCLIRepeatedPropertiesLastWins()
200 throws Exception
201 {
202 System.setProperty( MavenCli.MULTIMODULE_PROJECT_DIRECTORY,
203 new File( "src/test/projects/mavenConfigProperties" ).getCanonicalPath() );
204 CliRequest request = new CliRequest( new String[]{ "-Drevision=8.1.0", "-Drevision=8.2.0" }, null );
205
206 cli.initialize( request );
207
208 cli.cli( request );
209 cli.properties( request );
210
211 String revision = System.getProperty( "revision" );
212 assertEquals( "8.2.0", revision );
213 }
214
215
216
217
218
219
220
221
222
223
224
225
226 @Test
227 public void testMVNConfigurationFunkyArguments()
228 throws Exception
229 {
230 System.setProperty( MavenCli.MULTIMODULE_PROJECT_DIRECTORY,
231 new File( "src/test/projects/mavenConfigProperties" ).getCanonicalPath() );
232 CliRequest request = new CliRequest(
233 new String[]{ "-Drevision=8.1.0", "--file=-Dpom.xml", "\"-Dfoo=bar ", "\"-Dfoo2=bar two\"",
234 "-Drevision=8.2.0" }, null );
235
236 cli.initialize( request );
237
238 cli.cli( request );
239 cli.properties( request );
240
241 String revision = System.getProperty( "revision" );
242 assertEquals( "8.2.0", revision );
243
244 assertEquals( "bar ", request.getSystemProperties().getProperty( "foo" ) );
245 assertEquals( "bar two", request.getSystemProperties().getProperty( "foo2" ) );
246
247 assertEquals( "-Dpom.xml", request.getCommandLine().getOptionValue( CLIManager.ALTERNATE_POM_FILE ) );
248 }
249
250 @Test
251 public void testStyleColors()
252 throws Exception
253 {
254 assumeTrue( "ANSI not supported", MessageUtils.isColorEnabled() );
255 CliRequest request;
256
257 MessageUtils.setColorEnabled( true );
258 request = new CliRequest( new String[] { "-B" }, null );
259 cli.cli( request );
260 cli.properties( request );
261 cli.logging( request );
262 assertFalse( MessageUtils.isColorEnabled() );
263
264 MessageUtils.setColorEnabled( true );
265 request = new CliRequest( new String[] { "-l", "target/temp/mvn.log" }, null );
266 cli.cli( request );
267 cli.properties( request );
268 cli.logging( request );
269 assertFalse( MessageUtils.isColorEnabled() );
270
271 MessageUtils.setColorEnabled( false );
272 request = new CliRequest( new String[] { "-Dstyle.color=always" }, null );
273 cli.cli( request );
274 cli.properties( request );
275 cli.logging( request );
276 assertTrue( MessageUtils.isColorEnabled() );
277
278 MessageUtils.setColorEnabled( true );
279 request = new CliRequest( new String[] { "-Dstyle.color=never" }, null );
280 cli.cli( request );
281 cli.properties( request );
282 cli.logging( request );
283 assertFalse( MessageUtils.isColorEnabled() );
284
285 MessageUtils.setColorEnabled( false );
286 request = new CliRequest( new String[] { "-Dstyle.color=always", "-B", "-l", "target/temp/mvn.log" }, null );
287 cli.cli( request );
288 cli.properties( request );
289 cli.logging( request );
290 assertTrue( MessageUtils.isColorEnabled() );
291
292 try
293 {
294 MessageUtils.setColorEnabled( false );
295 request = new CliRequest( new String[] { "-Dstyle.color=maybe", "-B", "-l", "target/temp/mvn.log" }, null );
296 cli.cli( request );
297 cli.properties( request );
298 cli.logging( request );
299 fail( "maybe is not a valid option" );
300 }
301 catch ( IllegalArgumentException e )
302 {
303
304 }
305 }
306
307
308
309
310 @Test
311 public void testToolchainsBuildingEvents() throws Exception {
312 final EventSpyDispatcher eventSpyDispatcherMock = mock(EventSpyDispatcher.class);
313 MavenCli customizedMavenCli = new MavenCli() {
314 @Override
315 protected void customizeContainer(PlexusContainer container) {
316 super.customizeContainer(container);
317 container.addComponent(eventSpyDispatcherMock, "org.apache.maven.eventspy.internal.EventSpyDispatcher");
318 container.addComponent(mock(Maven.class), "org.apache.maven.Maven");
319 }
320 };
321
322 CliRequest cliRequest = new CliRequest(new String[]{}, null);
323
324 customizedMavenCli.cli(cliRequest);
325 customizedMavenCli.logging(cliRequest);
326 customizedMavenCli.container(cliRequest);
327 customizedMavenCli.toolchains(cliRequest);
328
329 InOrder orderdEventSpyDispatcherMock = inOrder(eventSpyDispatcherMock);
330 orderdEventSpyDispatcherMock.verify(eventSpyDispatcherMock, times(1)).onEvent(any(ToolchainsBuildingRequest.class));
331 orderdEventSpyDispatcherMock.verify(eventSpyDispatcherMock, times(1)).onEvent(any(ToolchainsBuildingResult.class));
332 }
333
334 }