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.DefaultArtifact;
24 import org.apache.maven.artifact.handler.ArtifactHandler;
25 import org.apache.maven.artifact.versioning.VersionRange;
26 import org.apache.maven.plugin.MojoExecutionException;
27 import org.apache.maven.plugin.MojoFailureException;
28 import org.apache.maven.plugin.surefire.log.PluginConsoleLogger;
29 import org.apache.maven.surefire.booter.ClassLoaderConfiguration;
30 import org.apache.maven.surefire.booter.Classpath;
31 import org.apache.maven.surefire.booter.ModularClasspathConfiguration;
32 import org.apache.maven.surefire.booter.StartupConfiguration;
33 import org.apache.maven.surefire.suite.RunResult;
34 import org.apache.maven.surefire.util.DefaultScanResult;
35 import org.codehaus.plexus.languages.java.jpms.LocationManager;
36 import org.codehaus.plexus.languages.java.jpms.ResolvePathsRequest;
37 import org.codehaus.plexus.languages.java.jpms.ResolvePathsResult;
38 import org.codehaus.plexus.languages.java.jpms.ResolvePathsResult.ModuleNameSource;
39 import org.codehaus.plexus.logging.Logger;
40 import org.junit.BeforeClass;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.ArgumentCaptor;
44 import org.mockito.Mock;
45 import org.powermock.core.classloader.annotations.PrepareForTest;
46 import org.powermock.modules.junit4.PowerMockRunner;
47
48 import java.io.File;
49 import java.io.IOException;
50 import java.nio.file.Path;
51 import java.util.LinkedHashMap;
52 import java.util.List;
53 import java.util.Map;
54 import java.util.Set;
55
56 import static java.util.Arrays.asList;
57 import static java.util.Collections.singleton;
58 import static org.apache.commons.lang3.JavaVersion.JAVA_1_7;
59 import static org.apache.commons.lang3.JavaVersion.JAVA_RECENT;
60 import static org.apache.maven.artifact.versioning.VersionRange.createFromVersion;
61 import static org.apache.maven.surefire.booter.SystemUtils.isBuiltInJava7AtLeast;
62 import static org.fest.assertions.Assertions.assertThat;
63 import static org.junit.Assume.assumeTrue;
64 import static org.mockito.ArgumentMatchers.anyString;
65 import static org.mockito.ArgumentMatchers.eq;
66 import static org.mockito.Mockito.times;
67 import static org.mockito.Mockito.verify;
68 import static org.mockito.Mockito.when;
69 import static org.powermock.api.mockito.PowerMockito.doReturn;
70 import static org.powermock.api.mockito.PowerMockito.doNothing;
71 import static org.powermock.api.mockito.PowerMockito.mock;
72 import static org.powermock.api.mockito.PowerMockito.mockStatic;
73 import static org.powermock.api.mockito.PowerMockito.spy;
74 import static org.powermock.api.mockito.PowerMockito.verifyPrivate;
75 import static org.powermock.api.mockito.PowerMockito.verifyStatic;
76 import static org.powermock.reflect.Whitebox.invokeMethod;
77
78
79
80
81 @RunWith( PowerMockRunner.class )
82 @PrepareForTest( { AbstractSurefireMojo.class, ResolvePathsRequest.class } )
83 public class AbstractSurefireMojoJava7PlusTest
84 {
85 @Mock
86 private ArtifactHandler handler;
87
88 @Mock
89 private LocationManager locationManager;
90
91 @BeforeClass
92 public static void withJava7Plus()
93 {
94 assumeTrue( JAVA_RECENT.atLeast( JAVA_1_7 ) );
95 }
96
97 @Test
98 public void shouldHaveStartupConfigForModularClasspath()
99 throws Exception
100 {
101 AbstractSurefireMojo mojo = spy( new Mojo() );
102 doReturn( locationManager )
103 .when( mojo, "getLocationManager" );
104
105 when( handler.isAddedToClasspath() ).thenReturn( true );
106
107 VersionRange v1 = createFromVersion( "1" );
108 Artifact modular = new DefaultArtifact( "x", "modular", v1, "compile", "jar", "", handler );
109 modular.setFile( mockFile( "modular.jar" ) );
110
111 VersionRange v2 = createFromVersion( "1" );
112 Artifact nonModular = new DefaultArtifact( "x", "non-modular", v2, "test", "jar", "", handler );
113 nonModular.setFile( mockFile( "non-modular.jar" ) );
114
115 VersionRange v3 = createFromVersion( "4.12" );
116 Artifact junit = new DefaultArtifact( "junit", "junit", v3, "test", "jar", "", handler );
117 junit.setFile( mockFile( "junit.jar" ) );
118
119 VersionRange v4 = createFromVersion( "1.3.0" );
120 Artifact hamcrest = new DefaultArtifact( "org.hamcrest", "hamcrest-core", v4, "test", "jar", "", handler );
121 hamcrest.setFile( mockFile( "hamcrest.jar" ) );
122
123 File classesDir = mockFile( "classes" );
124 File testClassesDir = mockFile( "test-classes" );
125
126 TestClassPath testClasspath =
127 new TestClassPath( asList( modular, nonModular, junit, hamcrest ), classesDir, testClassesDir,
128 null, null );
129
130 doReturn( testClasspath ).when( mojo, "generateTestClasspath" );
131 doReturn( 1 ).when( mojo, "getEffectiveForkCount" );
132 doReturn( true ).when( mojo, "effectiveIsEnableAssertions" );
133 when( mojo.isChildDelegation() ).thenReturn( false );
134 when( mojo.getTestClassesDirectory() ).thenReturn( testClassesDir );
135
136 DefaultScanResult scanResult = mock( DefaultScanResult.class );
137 when( scanResult.getClasses() ).thenReturn( asList( "org.apache.A", "org.apache.B" ) );
138
139 ClassLoaderConfiguration classLoaderConfiguration = new ClassLoaderConfiguration( false, true );
140
141 VersionRange v5 = createFromVersion( "1" );
142 Artifact provider = new DefaultArtifact( "org.apache.maven.surefire", "surefire-provider", v5, "runtime",
143 "jar", "", handler );
144 provider.setFile( mockFile( "surefire-provider.jar" ) );
145 Set<Artifact> providerClasspath = singleton( provider );
146
147 File moduleInfo = mockFile( "classes/module-info.class" );
148
149 @SuppressWarnings( "unchecked" )
150 ResolvePathsRequest<String> req = mock( ResolvePathsRequest.class );
151 mockStatic( ResolvePathsRequest.class );
152 when( ResolvePathsRequest.ofStrings( eq( testClasspath.toClasspath().getClassPath() ) ) ).thenReturn( req );
153 when( req.setMainModuleDescriptor( eq( moduleInfo.getAbsolutePath() ) ) ).thenReturn( req );
154
155 @SuppressWarnings( "unchecked" )
156 ResolvePathsResult<String> res = mock( ResolvePathsResult.class );
157 when( res.getClasspathElements() ).thenReturn( asList( "non-modular.jar", "junit.jar", "hamcrest.jar" ) );
158 Map<String, ModuleNameSource> mod = new LinkedHashMap<String, ModuleNameSource>();
159 mod.put( "modular.jar", null );
160 mod.put( "classes", null );
161 when( res.getModulepathElements() ).thenReturn( mod );
162 when( locationManager.resolvePaths( eq( req ) ) ).thenReturn( res );
163
164 Logger logger = mock( Logger.class );
165 when( logger.isDebugEnabled() ).thenReturn( true );
166 doNothing().when( logger ).debug( anyString() );
167 when( mojo.getConsoleLogger() ).thenReturn( new PluginConsoleLogger( logger ) );
168
169 StartupConfiguration conf = invokeMethod( mojo, "newStartupConfigWithModularPath",
170 classLoaderConfiguration, providerClasspath, "org.asf.Provider", moduleInfo, scanResult );
171
172 verify( mojo, times( 1 ) ).effectiveIsEnableAssertions();
173 verify( mojo, times( 1 ) ).isChildDelegation();
174 verifyPrivate( mojo, times( 1 ) ).invoke( "generateTestClasspath" );
175 verify( mojo, times( 1 ) ).getEffectiveForkCount();
176 verify( mojo, times( 1 ) ).getTestClassesDirectory();
177 verify( scanResult, times( 1 ) ).getClasses();
178 verifyStatic( ResolvePathsRequest.class, times( 1 ) );
179 ResolvePathsRequest.ofStrings( eq( testClasspath.toClasspath().getClassPath() ) );
180 verify( req, times( 1 ) ).setMainModuleDescriptor( eq( moduleInfo.getAbsolutePath() ) );
181 verify( res, times( 1 ) ).getClasspathElements();
182 verify( res, times( 1 ) ).getModulepathElements();
183 verify( locationManager, times( 1 ) ).resolvePaths( eq( req ) );
184 ArgumentCaptor<String> argument = ArgumentCaptor.forClass( String.class );
185 verify( logger, times( 6 ) ).debug( argument.capture() );
186 assertThat( argument.getAllValues() )
187 .containsExactly( "test classpath: non-modular.jar junit.jar hamcrest.jar",
188 "test modulepath: modular.jar classes",
189 "provider classpath: surefire-provider.jar",
190 "test(compact) classpath: non-modular.jar junit.jar hamcrest.jar",
191 "test(compact) modulepath: modular.jar classes",
192 "provider(compact) classpath: surefire-provider.jar"
193 );
194
195 assertThat( conf ).isNotNull();
196 assertThat( conf.isShadefire() ).isFalse();
197 assertThat( conf.isProviderMainClass() ).isFalse();
198 assertThat( conf.isManifestOnlyJarRequestedAndUsable() ).isFalse();
199 assertThat( conf.getClassLoaderConfiguration() ).isSameAs( classLoaderConfiguration );
200 assertThat( conf.getProviderClassName() ).isEqualTo( "org.asf.Provider" );
201 assertThat( conf.getActualClassName() ).isEqualTo( "org.asf.Provider" );
202 assertThat( conf.getClasspathConfiguration() ).isNotNull();
203 assertThat( ( Object ) conf.getClasspathConfiguration().getTestClasspath() )
204 .isEqualTo( new Classpath( res.getClasspathElements() ) );
205 assertThat( ( Object ) conf.getClasspathConfiguration().getProviderClasspath() )
206 .isEqualTo( new Classpath( singleton( "surefire-provider.jar" ) ) );
207 assertThat( conf.getClasspathConfiguration() ).isInstanceOf( ModularClasspathConfiguration.class );
208 ModularClasspathConfiguration mcc = ( ModularClasspathConfiguration ) conf.getClasspathConfiguration();
209 assertThat( mcc.getModularClasspath().getModuleDescriptor() ).isEqualTo( moduleInfo );
210 assertThat( mcc.getModularClasspath().getPackages() ).containsOnly( "org.apache" );
211 assertThat( mcc.getModularClasspath().getPatchFile().getAbsolutePath() )
212 .isEqualTo( "test-classes" );
213 assertThat( mcc.getModularClasspath().getModulePath() )
214 .containsExactly( "modular.jar", "classes" );
215 assertThat( ( Object ) mcc.getTestClasspath() ).isEqualTo( new Classpath( res.getClasspathElements() ) );
216 }
217
218 @Test
219 public void shouldHaveTmpDirectory() throws IOException
220 {
221 Path path = ( Path ) AbstractSurefireMojo.createTmpDirectoryWithJava7( "surefire" );
222
223 assertThat( path )
224 .isNotNull();
225
226 assertThat( path.startsWith( System.getProperty( "java.io.tmpdir" ) ) )
227 .isTrue();
228
229 String dir = path.getName( path.getNameCount() - 1 ).toString();
230
231 assertThat( dir )
232 .startsWith( "surefire" );
233
234 assertThat( dir )
235 .matches( "^surefire[\\d]+$" );
236 }
237
238 @Test
239 public void shouldHaveTmpDirectoryName() throws IOException
240 {
241 String dir = AbstractSurefireMojo.createTmpDirectoryNameWithJava7( "surefire" );
242
243 assertThat( dir )
244 .isNotNull();
245
246 assertThat( dir )
247 .startsWith( "surefire" );
248
249 assertThat( dir )
250 .matches( "^surefire[\\d]+$" );
251 }
252
253 @Test
254 public void shouldTestIsJava7()
255 {
256 assertThat( isBuiltInJava7AtLeast() )
257 .isTrue();
258 }
259
260 private static File mockFile( String absolutePath )
261 {
262 File f = mock( File.class );
263 when( f.getAbsolutePath() ).thenReturn( absolutePath );
264 return f;
265 }
266
267 public static class Mojo
268 extends AbstractSurefireMojo
269 {
270
271 @Override
272 protected String getPluginName()
273 {
274 return null;
275 }
276
277 @Override
278 protected int getRerunFailingTestsCount()
279 {
280 return 0;
281 }
282
283 @Override
284 public boolean isSkipTests()
285 {
286 return false;
287 }
288
289 @Override
290 public void setSkipTests( boolean skipTests )
291 {
292
293 }
294
295 @Override
296 public boolean isSkipExec()
297 {
298 return false;
299 }
300
301 @Override
302 public void setSkipExec( boolean skipExec )
303 {
304
305 }
306
307 @Override
308 public boolean isSkip()
309 {
310 return false;
311 }
312
313 @Override
314 public void setSkip( boolean skip )
315 {
316
317 }
318
319 @Override
320 public File getBasedir()
321 {
322 return null;
323 }
324
325 @Override
326 public void setBasedir( File basedir )
327 {
328
329 }
330
331 @Override
332 public File getTestClassesDirectory()
333 {
334 return null;
335 }
336
337 @Override
338 public void setTestClassesDirectory( File testClassesDirectory )
339 {
340
341 }
342
343 @Override
344 public File getClassesDirectory()
345 {
346 return null;
347 }
348
349 @Override
350 public void setClassesDirectory( File classesDirectory )
351 {
352
353 }
354
355 @Override
356 public File getReportsDirectory()
357 {
358 return null;
359 }
360
361 @Override
362 public void setReportsDirectory( File reportsDirectory )
363 {
364
365 }
366
367 @Override
368 public String getTest()
369 {
370 return null;
371 }
372
373 @Override
374 public void setTest( String test )
375 {
376
377 }
378
379 @Override
380 public List<String> getIncludes()
381 {
382 return null;
383 }
384
385 @Override
386 public File getIncludesFile()
387 {
388 return null;
389 }
390
391 @Override
392 public void setIncludes( List<String> includes )
393 {
394
395 }
396
397 @Override
398 public boolean isPrintSummary()
399 {
400 return false;
401 }
402
403 @Override
404 public void setPrintSummary( boolean printSummary )
405 {
406
407 }
408
409 @Override
410 public String getReportFormat()
411 {
412 return null;
413 }
414
415 @Override
416 public void setReportFormat( String reportFormat )
417 {
418
419 }
420
421 @Override
422 public boolean isUseFile()
423 {
424 return false;
425 }
426
427 @Override
428 public void setUseFile( boolean useFile )
429 {
430
431 }
432
433 @Override
434 public String getDebugForkedProcess()
435 {
436 return null;
437 }
438
439 @Override
440 public void setDebugForkedProcess( String debugForkedProcess )
441 {
442
443 }
444
445 @Override
446 public int getForkedProcessTimeoutInSeconds()
447 {
448 return 0;
449 }
450
451 @Override
452 public void setForkedProcessTimeoutInSeconds( int forkedProcessTimeoutInSeconds )
453 {
454
455 }
456
457 @Override
458 public int getForkedProcessExitTimeoutInSeconds()
459 {
460 return 0;
461 }
462
463 @Override
464 public void setForkedProcessExitTimeoutInSeconds( int forkedProcessTerminationTimeoutInSeconds )
465 {
466
467 }
468
469 @Override
470 public double getParallelTestsTimeoutInSeconds()
471 {
472 return 0;
473 }
474
475 @Override
476 public void setParallelTestsTimeoutInSeconds( double parallelTestsTimeoutInSeconds )
477 {
478
479 }
480
481 @Override
482 public double getParallelTestsTimeoutForcedInSeconds()
483 {
484 return 0;
485 }
486
487 @Override
488 public void setParallelTestsTimeoutForcedInSeconds( double parallelTestsTimeoutForcedInSeconds )
489 {
490
491 }
492
493 @Override
494 public boolean isUseSystemClassLoader()
495 {
496 return false;
497 }
498
499 @Override
500 public void setUseSystemClassLoader( boolean useSystemClassLoader )
501 {
502
503 }
504
505 @Override
506 public boolean isUseManifestOnlyJar()
507 {
508 return false;
509 }
510
511 @Override
512 public void setUseManifestOnlyJar( boolean useManifestOnlyJar )
513 {
514
515 }
516
517 @Override
518 public String getEncoding()
519 {
520 return null;
521 }
522
523 @Override
524 public void setEncoding( String encoding )
525 {
526
527 }
528
529 @Override
530 public Boolean getFailIfNoSpecifiedTests()
531 {
532 return null;
533 }
534
535 @Override
536 public void setFailIfNoSpecifiedTests( boolean failIfNoSpecifiedTests )
537 {
538
539 }
540
541 @Override
542 public int getSkipAfterFailureCount()
543 {
544 return 0;
545 }
546
547 @Override
548 public String getShutdown()
549 {
550 return null;
551 }
552
553 @Override
554 public File getExcludesFile()
555 {
556 return null;
557 }
558
559 @Override
560 protected List<File> suiteXmlFiles()
561 {
562 return null;
563 }
564
565 @Override
566 protected boolean hasSuiteXmlFiles()
567 {
568 return false;
569 }
570
571 @Override
572 public File[] getSuiteXmlFiles()
573 {
574 return new File[0];
575 }
576
577 @Override
578 public void setSuiteXmlFiles( File[] suiteXmlFiles )
579 {
580
581 }
582
583 @Override
584 public String getRunOrder()
585 {
586 return null;
587 }
588
589 @Override
590 public void setRunOrder( String runOrder )
591 {
592
593 }
594
595 @Override
596 protected void handleSummary( RunResult summary, Exception firstForkException )
597 throws MojoExecutionException, MojoFailureException
598 {
599
600 }
601
602 @Override
603 protected boolean isSkipExecution()
604 {
605 return false;
606 }
607
608 @Override
609 protected String[] getDefaultIncludes()
610 {
611 return new String[0];
612 }
613
614 @Override
615 protected String getReportSchemaLocation()
616 {
617 return null;
618 }
619
620 @Override
621 protected Artifact getMojoArtifact()
622 {
623 return null;
624 }
625 }
626 }