1 package org.apache.maven.plugin.surefire;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.artifact.Artifact;
23 import org.apache.maven.artifact.handler.ArtifactHandler;
24 import org.apache.maven.plugin.MojoExecutionException;
25 import org.apache.maven.plugin.MojoFailureException;
26 import org.apache.maven.plugin.surefire.log.PluginConsoleLogger;
27 import org.apache.maven.project.MavenProject;
28 import org.apache.maven.surefire.booter.ClassLoaderConfiguration;
29 import org.apache.maven.surefire.booter.Classpath;
30 import org.apache.maven.surefire.booter.StartupConfiguration;
31 import org.apache.maven.surefire.suite.RunResult;
32 import org.codehaus.plexus.logging.Logger;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.ArgumentCaptor;
36 import org.powermock.core.classloader.annotations.PrepareForTest;
37 import org.powermock.modules.junit4.PowerMockRunner;
38
39 import java.io.File;
40 import java.io.IOException;
41 import java.util.HashSet;
42 import java.util.List;
43 import java.util.Set;
44
45 import static java.io.File.separatorChar;
46 import static java.util.Arrays.asList;
47 import static java.util.Collections.singleton;
48 import static org.apache.commons.lang3.SystemUtils.IS_OS_WINDOWS;
49 import static org.fest.assertions.Assertions.assertThat;
50 import static org.mockito.ArgumentMatchers.anyString;
51 import static org.mockito.ArgumentMatchers.any;
52 import static org.mockito.ArgumentMatchers.eq;
53 import static org.mockito.Mockito.times;
54 import static org.mockito.Mockito.when;
55 import static org.mockito.Mockito.verify;
56 import static org.powermock.api.mockito.PowerMockito.doNothing;
57 import static org.powermock.api.mockito.PowerMockito.doReturn;
58 import static org.powermock.api.mockito.PowerMockito.mock;
59 import static org.powermock.api.mockito.PowerMockito.spy;
60 import static org.powermock.api.mockito.PowerMockito.verifyPrivate;
61 import static org.powermock.reflect.Whitebox.invokeMethod;
62
63
64
65
66 @RunWith( PowerMockRunner.class )
67 @PrepareForTest( AbstractSurefireMojo.class )
68 public class AbstractSurefireMojoTest
69 {
70 private final Mojo mojo = new Mojo();
71
72 @Test
73 public void shouldGenerateTestClasspath() throws Exception
74 {
75 AbstractSurefireMojo mojo = spy( this.mojo );
76
77 when( mojo.getClassesDirectory() ).thenReturn( new File( "target" + separatorChar + "classes" ) );
78 when( mojo.getTestClassesDirectory() ).thenReturn( new File( "target" + separatorChar + "test-classes" ) );
79 when( mojo.getClasspathDependencyScopeExclude() ).thenReturn( "runtime" );
80 when( mojo.getClasspathDependencyExcludes() ).thenReturn( new String[]{ "g3:a3" } );
81 doReturn( mock( Artifact.class ) ).when( mojo, "getTestNgArtifact" );
82 doNothing().when( mojo, "addTestNgUtilsArtifacts", any() );
83
84 Set<Artifact> artifacts = new HashSet<Artifact>();
85
86 Artifact a1 = mock( Artifact.class );
87 when( a1.getGroupId() ).thenReturn( "g1" );
88 when( a1.getArtifactId() ).thenReturn( "a1" );
89 when( a1.getVersion() ).thenReturn( "1" );
90 when( a1.getScope() ).thenReturn( "runtime" );
91 when( a1.getDependencyConflictId() ).thenReturn( "g1:a1:jar" );
92 when( a1.getId() ).thenReturn( "g1:a1:jar:1" );
93 artifacts.add( a1 );
94
95 ArtifactHandler artifactHandler = mock( ArtifactHandler.class );
96 when( artifactHandler.isAddedToClasspath() ).thenReturn( true );
97
98 Artifact a2 = mock( Artifact.class );
99 when( a2.getGroupId() ).thenReturn( "g2" );
100 when( a2.getArtifactId() ).thenReturn( "a2" );
101 when( a2.getVersion() ).thenReturn( "2" );
102 when( a2.getScope() ).thenReturn( "test" );
103 when( a2.getDependencyConflictId() ).thenReturn( "g2:a2:jar" );
104 when( a2.getId() ).thenReturn( "g2:a2:jar:2" );
105 when( a2.getFile() ).thenReturn( new File( "a2-2.jar" ) );
106 when( a2.getArtifactHandler() ).thenReturn( artifactHandler );
107 artifacts.add( a2 );
108
109 Artifact a3 = mock( Artifact.class );
110 when( a3.getGroupId() ).thenReturn( "g3" );
111 when( a3.getArtifactId() ).thenReturn( "a3" );
112 when( a3.getVersion() ).thenReturn( "3" );
113 when( a3.getScope() ).thenReturn( "test" );
114 when( a3.getDependencyConflictId() ).thenReturn( "g3:a3:jar" );
115 when( a3.getId() ).thenReturn( "g3:a3:jar:3" );
116 when( a3.getFile() ).thenReturn( new File( "a3-3.jar" ) );
117 when( a3.getArtifactHandler() ).thenReturn( artifactHandler );
118 artifacts.add( a3 );
119
120 MavenProject project = mock( MavenProject.class );
121 when( project.getArtifacts() ).thenReturn( artifacts );
122 when( mojo.getProject() ).thenReturn( project );
123
124 Classpath cp = invokeMethod( mojo, "generateTestClasspath" );
125
126 verifyPrivate( mojo, times( 1 ) ).invoke( "generateTestClasspath" );
127 verify( mojo, times( 1 ) ).getClassesDirectory();
128 verify( mojo, times( 1 ) ).getTestClassesDirectory();
129 verify( mojo, times( 3 ) ).getClasspathDependencyScopeExclude();
130 verify( mojo, times( 2 ) ).getClasspathDependencyExcludes();
131 verify( artifactHandler, times( 1 ) ).isAddedToClasspath();
132 verifyPrivate( mojo, times( 1 ) ).invoke( "getTestNgArtifact" );
133 verifyPrivate( mojo, times( 1 ) ).invoke( "addTestNgUtilsArtifacts", eq( cp.getClassPath() ) );
134
135 assertThat( cp.getClassPath() ).hasSize( 3 );
136 assertThat( cp.getClassPath().get( 0 ) ).endsWith( "test-classes" );
137 assertThat( cp.getClassPath().get( 1 ) ).endsWith( "classes" );
138 assertThat( cp.getClassPath().get( 2 ) ).endsWith( "a2-2.jar" );
139 }
140
141 @Test
142 public void shouldHaveStartupConfigForNonModularClasspath()
143 throws Exception
144 {
145 AbstractSurefireMojo mojo = spy( this.mojo );
146 Classpath testClasspath = new Classpath( asList( "junit.jar", "hamcrest.jar" ) );
147
148 doReturn( testClasspath ).when( mojo, "generateTestClasspath" );
149 doReturn( 1 ).when( mojo, "getEffectiveForkCount" );
150 doReturn( true ).when( mojo, "effectiveIsEnableAssertions" );
151 when( mojo.isChildDelegation() ).thenReturn( false );
152
153 ClassLoaderConfiguration classLoaderConfiguration = new ClassLoaderConfiguration( false, true );
154
155 Classpath providerClasspath = new Classpath( singleton( "surefire-provider.jar" ) );
156
157 Classpath inprocClasspath =
158 new Classpath( asList( "surefire-api.jar", "surefire-common.jar", "surefire-provider.jar" ) );
159
160 Logger logger = mock( Logger.class );
161 when( logger.isDebugEnabled() ).thenReturn( true );
162 doNothing().when( logger ).debug( anyString() );
163 when( mojo.getConsoleLogger() ).thenReturn( new PluginConsoleLogger( logger ) );
164
165 StartupConfiguration conf = invokeMethod( mojo, "newStartupConfigForNonModularClasspath",
166 classLoaderConfiguration, providerClasspath, inprocClasspath, "org.asf.Provider" );
167
168 verify( mojo, times( 1 ) ).effectiveIsEnableAssertions();
169 verify( mojo, times( 1 ) ).isChildDelegation();
170 verifyPrivate( mojo, times( 1 ) ).invoke( "generateTestClasspath" );
171 verify( mojo, times( 1 ) ).getEffectiveForkCount();
172 verify( logger, times( 4 ) ).isDebugEnabled();
173 ArgumentCaptor<String> argument = ArgumentCaptor.forClass( String.class );
174 verify( logger, times( 4 ) ).debug( argument.capture() );
175 assertThat( argument.getAllValues() )
176 .containsExactly( "test classpath: junit.jar hamcrest.jar",
177 "provider classpath: surefire-provider.jar",
178 "test(compact) classpath: junit.jar hamcrest.jar",
179 "provider(compact) classpath: surefire-provider.jar"
180 );
181
182 assertThat( conf.getClassLoaderConfiguration() )
183 .isSameAs( classLoaderConfiguration );
184
185 assertThat( ( Object ) conf.getClasspathConfiguration().getTestClasspath() )
186 .isSameAs( testClasspath );
187
188 assertThat( ( Object ) conf.getClasspathConfiguration().getProviderClasspath() )
189 .isSameAs( providerClasspath );
190
191 assertThat( ( Object ) conf.getClasspathConfiguration().isClassPathConfig() )
192 .isEqualTo( true );
193
194 assertThat( ( Object ) conf.getClasspathConfiguration().isModularPathConfig() )
195 .isEqualTo( false );
196
197 assertThat( ( Object ) conf.getClasspathConfiguration().isEnableAssertions() )
198 .isEqualTo( true );
199
200 assertThat( conf.getProviderClassName() )
201 .isEqualTo( "org.asf.Provider" );
202 }
203
204 @Test
205 public void shouldExistTmpDirectory() throws IOException
206 {
207 String systemTmpDir = System.getProperty( "java.io.tmpdir" );
208 String usrDir = new File( System.getProperty( "user.dir" ) ).getCanonicalPath();
209
210 String tmpDir = "surefireX" + System.currentTimeMillis();
211
212
213 new File( systemTmpDir, tmpDir ).delete();
214
215 File targetDir = new File( usrDir, "target" );
216
217 new File( targetDir, tmpDir ).delete();
218
219 AbstractSurefireMojo mojo = mock( AbstractSurefireMojo.class );
220 when( mojo.getTempDir() ).thenReturn( tmpDir );
221 when( mojo.getProjectBuildDirectory() ).thenReturn( targetDir );
222 when( mojo.createSurefireBootDirectoryInTemp() ).thenCallRealMethod();
223 when( mojo.createSurefireBootDirectoryInBuild() ).thenCallRealMethod();
224 when( mojo.getSurefireTempDir() ).thenCallRealMethod();
225
226 File bootDir = mojo.createSurefireBootDirectoryInTemp();
227 assertThat( bootDir ).isNotNull();
228 assertThat( bootDir ).isDirectory();
229
230 assertThat( new File( systemTmpDir, bootDir.getName() ) ).isDirectory();
231 assertThat( bootDir.getName() )
232 .startsWith( tmpDir );
233
234 File buildTmp = mojo.createSurefireBootDirectoryInBuild();
235 assertThat( buildTmp ).isNotNull();
236 assertThat( buildTmp ).isDirectory();
237 assertThat( buildTmp.getParentFile().getCanonicalFile().getParent() ).isEqualTo( usrDir );
238 assertThat( buildTmp.getName() ).isEqualTo( tmpDir );
239
240 File tmp = mojo.getSurefireTempDir();
241 assertThat( tmp ).isNotNull();
242 assertThat( tmp ).isDirectory();
243 assertThat( IS_OS_WINDOWS ? new File( systemTmpDir, bootDir.getName() ) : new File( targetDir, tmpDir ) )
244 .isDirectory();
245 }
246
247 public static class Mojo
248 extends AbstractSurefireMojo
249 {
250 @Override
251 protected String getPluginName()
252 {
253 return null;
254 }
255
256 @Override
257 protected int getRerunFailingTestsCount()
258 {
259 return 0;
260 }
261
262 @Override
263 public boolean isSkipTests()
264 {
265 return false;
266 }
267
268 @Override
269 public void setSkipTests( boolean skipTests )
270 {
271
272 }
273
274 @Override
275 public boolean isSkipExec()
276 {
277 return false;
278 }
279
280 @Override
281 public void setSkipExec( boolean skipExec )
282 {
283
284 }
285
286 @Override
287 public boolean isSkip()
288 {
289 return false;
290 }
291
292 @Override
293 public void setSkip( boolean skip )
294 {
295
296 }
297
298 @Override
299 public File getBasedir()
300 {
301 return null;
302 }
303
304 @Override
305 public void setBasedir( File basedir )
306 {
307
308 }
309
310 @Override
311 public File getTestClassesDirectory()
312 {
313 return null;
314 }
315
316 @Override
317 public void setTestClassesDirectory( File testClassesDirectory )
318 {
319
320 }
321
322 @Override
323 public File getClassesDirectory()
324 {
325 return null;
326 }
327
328 @Override
329 public void setClassesDirectory( File classesDirectory )
330 {
331
332 }
333
334 @Override
335 public File getReportsDirectory()
336 {
337 return null;
338 }
339
340 @Override
341 public void setReportsDirectory( File reportsDirectory )
342 {
343
344 }
345
346 @Override
347 public String getTest()
348 {
349 return null;
350 }
351
352 @Override
353 public void setTest( String test )
354 {
355
356 }
357
358 @Override
359 public List<String> getIncludes()
360 {
361 return null;
362 }
363
364 @Override
365 public File getIncludesFile()
366 {
367 return null;
368 }
369
370 @Override
371 public void setIncludes( List<String> includes )
372 {
373
374 }
375
376 @Override
377 public boolean isPrintSummary()
378 {
379 return false;
380 }
381
382 @Override
383 public void setPrintSummary( boolean printSummary )
384 {
385
386 }
387
388 @Override
389 public String getReportFormat()
390 {
391 return null;
392 }
393
394 @Override
395 public void setReportFormat( String reportFormat )
396 {
397
398 }
399
400 @Override
401 public boolean isUseFile()
402 {
403 return false;
404 }
405
406 @Override
407 public void setUseFile( boolean useFile )
408 {
409
410 }
411
412 @Override
413 public String getDebugForkedProcess()
414 {
415 return null;
416 }
417
418 @Override
419 public void setDebugForkedProcess( String debugForkedProcess )
420 {
421
422 }
423
424 @Override
425 public int getForkedProcessTimeoutInSeconds()
426 {
427 return 0;
428 }
429
430 @Override
431 public void setForkedProcessTimeoutInSeconds( int forkedProcessTimeoutInSeconds )
432 {
433
434 }
435
436 @Override
437 public int getForkedProcessExitTimeoutInSeconds()
438 {
439 return 0;
440 }
441
442 @Override
443 public void setForkedProcessExitTimeoutInSeconds( int forkedProcessTerminationTimeoutInSeconds )
444 {
445
446 }
447
448 @Override
449 public double getParallelTestsTimeoutInSeconds()
450 {
451 return 0;
452 }
453
454 @Override
455 public void setParallelTestsTimeoutInSeconds( double parallelTestsTimeoutInSeconds )
456 {
457
458 }
459
460 @Override
461 public double getParallelTestsTimeoutForcedInSeconds()
462 {
463 return 0;
464 }
465
466 @Override
467 public void setParallelTestsTimeoutForcedInSeconds( double parallelTestsTimeoutForcedInSeconds )
468 {
469
470 }
471
472 @Override
473 public boolean isUseSystemClassLoader()
474 {
475 return false;
476 }
477
478 @Override
479 public void setUseSystemClassLoader( boolean useSystemClassLoader )
480 {
481
482 }
483
484 @Override
485 public boolean isUseManifestOnlyJar()
486 {
487 return false;
488 }
489
490 @Override
491 public void setUseManifestOnlyJar( boolean useManifestOnlyJar )
492 {
493
494 }
495
496 @Override
497 public String getEncoding()
498 {
499 return null;
500 }
501
502 @Override
503 public void setEncoding( String encoding )
504 {
505
506 }
507
508 @Override
509 public Boolean getFailIfNoSpecifiedTests()
510 {
511 return null;
512 }
513
514 @Override
515 public void setFailIfNoSpecifiedTests( boolean failIfNoSpecifiedTests )
516 {
517
518 }
519
520 @Override
521 public int getSkipAfterFailureCount()
522 {
523 return 0;
524 }
525
526 @Override
527 public String getShutdown()
528 {
529 return null;
530 }
531
532 @Override
533 public File getExcludesFile()
534 {
535 return null;
536 }
537
538 @Override
539 protected List<File> suiteXmlFiles()
540 {
541 return null;
542 }
543
544 @Override
545 protected boolean hasSuiteXmlFiles()
546 {
547 return false;
548 }
549
550 @Override
551 public File[] getSuiteXmlFiles()
552 {
553 return new File[0];
554 }
555
556 @Override
557 public void setSuiteXmlFiles( File[] suiteXmlFiles )
558 {
559
560 }
561
562 @Override
563 public String getRunOrder()
564 {
565 return null;
566 }
567
568 @Override
569 public void setRunOrder( String runOrder )
570 {
571
572 }
573
574 @Override
575 protected void handleSummary( RunResult summary, Exception firstForkException )
576 throws MojoExecutionException, MojoFailureException
577 {
578
579 }
580
581 @Override
582 protected boolean isSkipExecution()
583 {
584 return false;
585 }
586
587 @Override
588 protected String[] getDefaultIncludes()
589 {
590 return new String[0];
591 }
592
593 @Override
594 protected String getReportSchemaLocation()
595 {
596 return null;
597 }
598
599 @Override
600 protected Artifact getMojoArtifact()
601 {
602 return null;
603 }
604 }
605 }