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.repository.ArtifactRepository;
26 import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
27 import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
28 import org.apache.maven.artifact.versioning.VersionRange;
29 import org.apache.maven.execution.MavenSession;
30 import org.apache.maven.model.Dependency;
31 import org.apache.maven.model.Model;
32 import org.apache.maven.model.Plugin;
33 import org.apache.maven.plugin.MojoFailureException;
34 import org.apache.maven.plugin.descriptor.PluginDescriptor;
35 import org.apache.maven.plugin.surefire.AbstractSurefireMojo.JUnitPlatformProviderInfo;
36 import org.apache.maven.plugin.surefire.booterclient.Platform;
37 import org.apache.maven.plugin.surefire.log.PluginConsoleLogger;
38 import org.apache.maven.project.MavenProject;
39 import org.apache.maven.project.ProjectBuildingRequest;
40 import org.apache.maven.repository.RepositorySystem;
41 import org.apache.maven.shared.artifact.filter.resolve.TransformableFilter;
42 import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult;
43 import org.apache.maven.shared.transfer.dependencies.DependableCoordinate;
44 import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolver;
45 import org.apache.maven.surefire.booter.ClassLoaderConfiguration;
46 import org.apache.maven.surefire.booter.Classpath;
47 import org.apache.maven.surefire.booter.ModularClasspathConfiguration;
48 import org.apache.maven.surefire.booter.StartupConfiguration;
49 import org.apache.maven.surefire.extensions.ForkNodeFactory;
50 import org.apache.maven.surefire.api.suite.RunResult;
51 import org.apache.maven.surefire.api.util.DefaultScanResult;
52 import org.apache.maven.toolchain.Toolchain;
53 import org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor;
54 import org.codehaus.plexus.languages.java.jpms.LocationManager;
55 import org.codehaus.plexus.languages.java.jpms.ResolvePathRequest;
56 import org.codehaus.plexus.languages.java.jpms.ResolvePathResult;
57 import org.codehaus.plexus.languages.java.jpms.ResolvePathsRequest;
58 import org.codehaus.plexus.languages.java.jpms.ResolvePathsResult;
59 import org.codehaus.plexus.logging.Logger;
60 import org.junit.Before;
61 import org.junit.Rule;
62 import org.junit.Test;
63 import org.junit.rules.ExpectedException;
64 import org.junit.rules.TemporaryFolder;
65 import org.junit.runner.RunWith;
66 import org.mockito.ArgumentCaptor;
67 import org.mockito.Mock;
68 import org.mockito.invocation.InvocationOnMock;
69 import org.mockito.stubbing.Answer;
70 import org.powermock.core.classloader.annotations.PowerMockIgnore;
71 import org.powermock.core.classloader.annotations.PrepareForTest;
72 import org.powermock.modules.junit4.PowerMockRunner;
73
74 import java.io.File;
75 import java.io.IOException;
76 import java.nio.file.Path;
77 import java.nio.file.Paths;
78 import java.util.ArrayList;
79 import java.util.Collection;
80 import java.util.Collections;
81 import java.util.HashMap;
82 import java.util.HashSet;
83 import java.util.LinkedHashMap;
84 import java.util.LinkedHashSet;
85 import java.util.List;
86 import java.util.Map;
87 import java.util.Set;
88
89 import static java.io.File.separatorChar;
90 import static java.nio.file.Files.write;
91 import static java.util.Arrays.asList;
92 import static java.util.Collections.emptyList;
93 import static java.util.Collections.emptyMap;
94 import static java.util.Collections.emptySet;
95 import static java.util.Collections.singleton;
96 import static java.util.Collections.singletonList;
97 import static org.apache.maven.artifact.versioning.VersionRange.createFromVersion;
98 import static org.apache.maven.artifact.versioning.VersionRange.createFromVersionSpec;
99 import static org.apache.maven.surefire.shared.lang3.JavaVersion.JAVA_9;
100 import static org.apache.maven.surefire.shared.lang3.JavaVersion.JAVA_RECENT;
101 import static org.apache.maven.surefire.shared.lang3.SystemUtils.IS_OS_WINDOWS;
102 import static org.codehaus.plexus.languages.java.jpms.ModuleNameSource.MODULEDESCRIPTOR;
103 import static org.fest.assertions.Assertions.assertThat;
104 import static org.fest.assertions.MapAssert.entry;
105 import static org.junit.Assert.fail;
106 import static org.mockito.ArgumentMatchers.any;
107 import static org.mockito.ArgumentMatchers.anyString;
108 import static org.mockito.Mockito.times;
109 import static org.mockito.Mockito.verify;
110 import static org.mockito.Mockito.when;
111 import static org.powermock.api.mockito.PowerMockito.doNothing;
112 import static org.powermock.api.mockito.PowerMockito.doReturn;
113 import static org.powermock.api.mockito.PowerMockito.mock;
114 import static org.powermock.api.mockito.PowerMockito.spy;
115 import static org.powermock.api.mockito.PowerMockito.verifyPrivate;
116 import static org.powermock.reflect.Whitebox.invokeMethod;
117 import static org.powermock.reflect.Whitebox.setInternalState;
118
119
120
121
122 @RunWith( PowerMockRunner.class )
123 @PrepareForTest( AbstractSurefireMojo.class )
124 @PowerMockIgnore( { "org.jacoco.agent.rt.*", "com.vladium.emma.rt.*" } )
125 public class AbstractSurefireMojoTest
126 {
127 @Rule
128 public final ExpectedException e = ExpectedException.none();
129
130 @Rule
131 public final TemporaryFolder tempFolder = new TemporaryFolder();
132
133 @Mock
134 private ArtifactHandler handler;
135
136 private final Mojo mojo = new Mojo();
137
138 @Before
139 public void setupMojo()
140 {
141 Artifact mojoArtifact = mojo.getMojoArtifact();
142
143 mojo.setPluginArtifactMap( new LinkedHashMap<String, Artifact>() );
144 mojo.getPluginArtifactMap().put( mojoArtifact.getGroupId() + ":" + mojoArtifact.getArtifactId(), mojoArtifact );
145 Artifact forkedBooter = new DefaultArtifact( "org.apache.maven.surefire", "surefire-booter",
146 mojoArtifact.getVersion(), null, "jar", null, mock( ArtifactHandler.class ) );
147 mojo.getPluginArtifactMap().put( "org.apache.maven.surefire:surefire-booter", forkedBooter );
148
149 mojo.setProjectArtifactMap( new LinkedHashMap<String, Artifact>() );
150
151 MavenSession session = mock( MavenSession.class );
152 mojo.setSession( session );
153
154 PluginDescriptor pluginDescriptor = mock( PluginDescriptor.class );
155 Plugin plugin = new Plugin();
156 plugin.setGroupId( mojoArtifact.getGroupId() );
157 plugin.setArtifactId( mojoArtifact.getArtifactId() );
158 plugin.setVersion( mojoArtifact.getVersion() );
159 when( pluginDescriptor.getPlugin() ).thenReturn( plugin );
160 mojo.setPluginDescriptor( pluginDescriptor );
161 }
162
163 @Test
164 public void noModuleDescriptorFile() throws Exception
165 {
166 AbstractSurefireMojo mojo = spy( new Mojo() );
167 mojo.setMainBuildPath( tempFolder.newFolder() );
168 File testClassesDir = tempFolder.newFolder();
169 mojo.setTestClassesDirectory( testClassesDir );
170 File jdkHome = new File( System.getProperty( "java.home" ) );
171 ResolvePathResultWrapper wrapper = invokeMethod( mojo, "findModuleDescriptor", jdkHome );
172
173 assertThat( wrapper )
174 .isNotNull();
175
176 assertThat( wrapper.getResolvePathResult() )
177 .isNull();
178
179 assertThat( invokeMethod( mojo, "existsModuleDescriptor", wrapper ) )
180 .isEqualTo( false );
181
182 when( mojo.useModulePath() ).thenReturn( true );
183
184 File jvmExecutable = new File( jdkHome, IS_OS_WINDOWS ? "bin\\java.exe" : "bin/java" );
185 JdkAttributes jdkAttributes = new JdkAttributes( jvmExecutable, jdkHome, true );
186 Platform platform = new Platform().withJdkExecAttributesForTests( jdkAttributes );
187 assertThat( invokeMethod( mojo, "canExecuteProviderWithModularPath", platform, wrapper ) )
188 .isEqualTo( false );
189 }
190
191 @Test
192 public void correctModuleDescriptor() throws Exception
193 {
194 AbstractSurefireMojo mojo = spy( new Mojo() );
195 LocationManager locationManager = mock( LocationManager.class );
196 ResolvePathResult result = mock( ResolvePathResult.class );
197 when( result.getModuleNameSource() ).thenReturn( MODULEDESCRIPTOR );
198 JavaModuleDescriptor descriptor = mock( JavaModuleDescriptor.class );
199 when( result.getModuleDescriptor() ).thenReturn( descriptor );
200 when( locationManager.resolvePath( any( ResolvePathRequest.class ) ) ).thenReturn( result );
201 doReturn( locationManager )
202 .when( mojo, "getLocationManager" );
203 File classesDir = tempFolder.newFolder();
204 mojo.setMainBuildPath( classesDir );
205 File testClassesDir = tempFolder.newFolder();
206 mojo.setTestClassesDirectory( testClassesDir );
207 File descriptorFile = new File( classesDir, "module-info.class" );
208 assertThat( descriptorFile.createNewFile() ).isTrue();
209 File jdkHome = new File( System.getProperty( "java.home" ) );
210 ResolvePathResultWrapper wrapper = invokeMethod( mojo, "findModuleDescriptor", jdkHome );
211
212 assertThat( wrapper )
213 .isNotNull();
214
215 assertThat( wrapper.getResolvePathResult() )
216 .isSameAs( result );
217
218 assertThat( wrapper.getResolvePathResult().getModuleNameSource() )
219 .isSameAs( MODULEDESCRIPTOR );
220
221 assertThat( wrapper.getResolvePathResult().getModuleDescriptor() )
222 .isSameAs( descriptor );
223
224 assertThat( invokeMethod( mojo, "existsModuleDescriptor", wrapper ) )
225 .isEqualTo( true );
226
227 when( mojo.useModulePath() ).thenReturn( true );
228
229 File jvmExecutable = new File( jdkHome, IS_OS_WINDOWS ? "bin\\java.exe" : "bin/java" );
230 JdkAttributes jdkAttributes = new JdkAttributes( jvmExecutable, jdkHome, true );
231 Platform platform = new Platform().withJdkExecAttributesForTests( jdkAttributes );
232 assertThat( invokeMethod( mojo, "canExecuteProviderWithModularPath", platform, wrapper ) )
233 .isEqualTo( true );
234
235 jdkAttributes = new JdkAttributes( jvmExecutable, jdkHome, false );
236 platform = new Platform().withJdkExecAttributesForTests( jdkAttributes );
237 assertThat( invokeMethod( mojo, "canExecuteProviderWithModularPath", platform, wrapper ) )
238 .isEqualTo( false );
239
240 when( mojo.useModulePath() ).thenReturn( false );
241
242 jdkAttributes = new JdkAttributes( jvmExecutable, jdkHome, true );
243 platform = new Platform().withJdkExecAttributesForTests( jdkAttributes );
244 assertThat( invokeMethod( mojo, "canExecuteProviderWithModularPath", platform, wrapper ) )
245 .isEqualTo( false );
246 }
247
248 @Test
249 @SuppressWarnings( "checkstyle:magicnumber" )
250 public void corruptedModuleDescriptor() throws Exception
251 {
252 if ( !JAVA_RECENT.atLeast( JAVA_9 ) )
253 {
254 return;
255 }
256
257 AbstractSurefireMojo mojo = spy( new Mojo() );
258 doReturn( new LocationManager() )
259 .when( mojo, "getLocationManager" );
260 File classesDir = tempFolder.newFolder();
261 mojo.setMainBuildPath( classesDir );
262 File testClassesDir = tempFolder.newFolder();
263 mojo.setTestClassesDirectory( testClassesDir );
264
265 File descriptorFile = new File( classesDir, "module-info.class" );
266 assertThat( descriptorFile.createNewFile() ).isTrue();
267 write( descriptorFile.toPath(), new byte[]{(byte) 0xCA, (byte) 0xFE, (byte) 0xBA, (byte) 0xBE} );
268
269 File jdkHome = new File( System.getProperty( "java.home" ) );
270 ResolvePathResultWrapper wrapper = invokeMethod( mojo, "findModuleDescriptor", jdkHome );
271
272 assertThat( wrapper )
273 .isNotNull();
274
275 assertThat( wrapper.getResolvePathResult() )
276 .isNull();
277
278 assertThat( invokeMethod( mojo, "existsModuleDescriptor", wrapper ) )
279 .isEqualTo( false );
280 }
281
282 @Test
283 public void shouldShowArray() throws Exception
284 {
285 Logger logger = mock( Logger.class );
286 when( logger.isDebugEnabled() ).thenReturn( true );
287 doNothing().when( logger ).debug( anyString() );
288
289 AbstractSurefireMojo mojo = spy( this.mojo );
290
291 when( mojo.getConsoleLogger() ).thenReturn( new PluginConsoleLogger( logger ) );
292
293 Object[] array = { "ABC", "XYZ" };
294 invokeMethod( mojo, "showArray", array, "prefix" );
295
296 ArgumentCaptor<String> argument = ArgumentCaptor.forClass( String.class );
297 verify( logger, times( 2 ) ).debug( argument.capture() );
298 assertThat( argument.getAllValues() )
299 .containsExactly( "Setting prefix [ABC]", "Setting prefix [XYZ]" );
300 }
301
302 @Test
303 public void shouldShowMap() throws Exception
304 {
305 Logger logger = mock( Logger.class );
306 when( logger.isDebugEnabled() ).thenReturn( true );
307 doNothing().when( logger ).debug( anyString() );
308
309 AbstractSurefireMojo mojo = spy( this.mojo );
310
311 when( mojo.getConsoleLogger() ).thenReturn( new PluginConsoleLogger( logger ) );
312
313 Map<String, String> map = new LinkedHashMap<>();
314 map.put( "ABC", "123" );
315 map.put( "XYZ", "987" );
316 invokeMethod( mojo, "showMap", map, "prefix" );
317
318 ArgumentCaptor<String> argument = ArgumentCaptor.forClass( String.class );
319 verify( logger, times( 2 ) ).debug( argument.capture() );
320 assertThat( argument.getAllValues() )
321 .containsExactly( "Setting prefix [ABC]=[123]", "Setting prefix [XYZ]=[987]" );
322 }
323
324 @Test
325 public void shouldRetainInPluginArtifacts() throws Exception
326 {
327 Artifact provider = new DefaultArtifact( "g", "a", createFromVersionSpec( "1" ), "compile", "jar", "", null );
328 Artifact common = new DefaultArtifact( "g", "c", createFromVersionSpec( "1" ), "compile", "jar", "", null );
329 Artifact api = new DefaultArtifact( "g", "a", createFromVersionSpec( "1" ), "compile", "jar", "", null );
330
331 Set<Artifact> providerArtifacts = singleton( provider );
332 Artifact[] inPluginArtifacts = { common, api };
333 Set<Artifact> inPluginClasspath = invokeMethod( AbstractSurefireMojo.class,
334 "retainInProcArtifactsUnique", providerArtifacts, inPluginArtifacts );
335
336 assertThat( inPluginClasspath )
337 .containsOnly( common );
338 }
339
340 @Test
341 public void shouldRetainInProcArtifactsUnique() throws Exception
342 {
343 Artifact provider = new DefaultArtifact( "g", "p", createFromVersionSpec( "1" ), "compile", "jar", "", null );
344 Artifact common = new DefaultArtifact( "g", "c", createFromVersionSpec( "1" ), "compile", "jar", "", null );
345 Artifact api = new DefaultArtifact( "g", "a", createFromVersionSpec( "1" ), "compile", "jar", "", null );
346
347 Set<Artifact> providerArtifacts = singleton( provider );
348 Artifact[] inPluginArtifacts = { common, api };
349 Set<Artifact> inPluginClasspath = invokeMethod( AbstractSurefireMojo.class,
350 "retainInProcArtifactsUnique", providerArtifacts, inPluginArtifacts );
351
352 assertThat( inPluginClasspath )
353 .containsOnly( common, api );
354 }
355
356 @Test
357 public void shouldCreateInProcClasspath() throws Exception
358 {
359 Artifact provider = new DefaultArtifact( "g", "p", createFromVersionSpec( "1" ), "compile", "jar", "", null );
360 provider.setFile( mockFile( "provider.jar" ) );
361
362 Artifact common = new DefaultArtifact( "g", "c", createFromVersionSpec( "1" ), "compile", "jar", "", null );
363 common.setFile( mockFile( "maven-surefire-common.jar" ) );
364
365 Artifact api = new DefaultArtifact( "g", "a", createFromVersionSpec( "1" ), "compile", "jar", "", null );
366 api.setFile( mockFile( "surefire-api.jar" ) );
367
368 Set<Artifact> newArtifacts = new LinkedHashSet<>();
369 newArtifacts.add( common );
370 newArtifacts.add( api );
371
372 Classpath providerClasspath = new Classpath( singleton( provider.getFile().getAbsolutePath() ) );
373
374 Classpath inPluginClasspath = invokeMethod( AbstractSurefireMojo.class,
375 "createInProcClasspath", providerClasspath, newArtifacts );
376
377 Classpath expectedClasspath =
378 new Classpath( asList( provider.getFile().getAbsolutePath(),
379 common.getFile().getAbsolutePath(),
380 api.getFile().getAbsolutePath() ) );
381
382 assertThat( (Object ) inPluginClasspath )
383 .isEqualTo( expectedClasspath );
384
385 }
386
387 @Test
388 public void shouldGenerateTestClasspath() throws Exception
389 {
390 AbstractSurefireMojo mojo = spy( this.mojo );
391
392 when( mojo.getMainBuildPath() ).thenReturn( new File( "target" + separatorChar + "classes" ) );
393 when( mojo.getTestClassesDirectory() ).thenReturn( new File( "target" + separatorChar + "test-classes" ) );
394 when( mojo.getClasspathDependencyScopeExclude() ).thenReturn( "runtime" );
395 when( mojo.getClasspathDependencyExcludes() ).thenReturn( new String[]{ "g3:a3" } );
396 doReturn( mock( Artifact.class ) ).when( mojo, "getTestNgArtifact" );
397
398 Set<Artifact> artifacts = new HashSet<>();
399
400 Artifact a1 = mock( Artifact.class );
401 when( a1.getGroupId() ).thenReturn( "g1" );
402 when( a1.getArtifactId() ).thenReturn( "a1" );
403 when( a1.getVersion() ).thenReturn( "1" );
404 when( a1.getScope() ).thenReturn( "runtime" );
405 when( a1.getDependencyConflictId() ).thenReturn( "g1:a1:jar" );
406 when( a1.getId() ).thenReturn( "g1:a1:jar:1" );
407 artifacts.add( a1 );
408
409 ArtifactHandler artifactHandler = mock( ArtifactHandler.class );
410 when( artifactHandler.isAddedToClasspath() ).thenReturn( true );
411
412 Artifact a2 = mock( Artifact.class );
413 when( a2.getGroupId() ).thenReturn( "g2" );
414 when( a2.getArtifactId() ).thenReturn( "a2" );
415 when( a2.getVersion() ).thenReturn( "2" );
416 when( a2.getScope() ).thenReturn( "test" );
417 when( a2.getDependencyConflictId() ).thenReturn( "g2:a2:jar" );
418 when( a2.getId() ).thenReturn( "g2:a2:jar:2" );
419 when( a2.getFile() ).thenReturn( new File( "a2-2.jar" ) );
420 when( a2.getArtifactHandler() ).thenReturn( artifactHandler );
421 artifacts.add( a2 );
422
423 Artifact a3 = mock( Artifact.class );
424 when( a3.getGroupId() ).thenReturn( "g3" );
425 when( a3.getArtifactId() ).thenReturn( "a3" );
426 when( a3.getVersion() ).thenReturn( "3" );
427 when( a3.getScope() ).thenReturn( "test" );
428 when( a3.getDependencyConflictId() ).thenReturn( "g3:a3:jar" );
429 when( a3.getId() ).thenReturn( "g3:a3:jar:3" );
430 when( a3.getFile() ).thenReturn( new File( "a3-3.jar" ) );
431 when( a3.getArtifactHandler() ).thenReturn( artifactHandler );
432 artifacts.add( a3 );
433
434 MavenProject project = mock( MavenProject.class );
435 when( project.getArtifacts() ).thenReturn( artifacts );
436 when( mojo.getProject() ).thenReturn( project );
437
438 TestClassPath cp = invokeMethod( mojo, "generateTestClasspath" );
439
440 verifyPrivate( mojo, times( 1 ) ).invoke( "generateTestClasspath" );
441 verify( mojo, times( 1 ) ).getMainBuildPath();
442 verify( mojo, times( 1 ) ).getTestClassesDirectory();
443 verify( mojo, times( 3 ) ).getClasspathDependencyScopeExclude();
444 verify( mojo, times( 2 ) ).getClasspathDependencyExcludes();
445 verify( mojo, times( 1 ) ).getAdditionalClasspathElements();
446
447 assertThat( cp.toClasspath().getClassPath() ).hasSize( 3 );
448 assertThat( cp.toClasspath().getClassPath().get( 0 ) ).endsWith( "test-classes" );
449 assertThat( cp.toClasspath().getClassPath().get( 1 ) ).endsWith( "classes" );
450 assertThat( cp.toClasspath().getClassPath().get( 2 ) ).endsWith( "a2-2.jar" );
451 }
452
453 @Test
454 @SuppressWarnings( "checkstyle:linelength" )
455 public void shouldHaveStartupConfigForNonModularClasspath()
456 throws Exception
457 {
458 AbstractSurefireMojo mojo = spy( this.mojo );
459
460 Artifact common = new DefaultArtifact( "org.apache.maven.surefire", "maven-surefire-common",
461 createFromVersion( "1" ), "runtime", "jar", "", handler );
462 common.setFile( mockFile( "maven-surefire-common.jar" ) );
463
464 Artifact ext = new DefaultArtifact( "org.apache.maven.surefire", "surefire-extensions-api",
465 createFromVersion( "1" ), "runtime", "jar", "", handler );
466 ext.setFile( mockFile( "surefire-extensions-api.jar" ) );
467
468 Artifact api = new DefaultArtifact( "org.apache.maven.surefire", "surefire-api",
469 createFromVersion( "1" ), "runtime", "jar", "", handler );
470 api.setFile( mockFile( "surefire-api.jar" ) );
471
472 Artifact loggerApi = new DefaultArtifact( "org.apache.maven.surefire", "surefire-logger-api",
473 createFromVersion( "1" ), "runtime", "jar", "", handler );
474 loggerApi.setFile( mockFile( "surefire-logger-api.jar" ) );
475
476 Artifact spi = new DefaultArtifact( "org.apache.maven.surefire", "surefire-extensions-spi",
477 createFromVersion( "1" ), "runtime", "jar", "", handler );
478 spi.setFile( mockFile( "surefire-extensions-spi.jar" ) );
479
480 Artifact booter = new DefaultArtifact( "org.apache.maven.surefire", "surefire-booter",
481 createFromVersion( "1" ), "runtime", "jar", "", handler );
482 booter.setFile( mockFile( "surefire-booter.jar" ) );
483
484 Artifact utils = new DefaultArtifact( "org.apache.maven.surefire", "surefire-shared-utils",
485 createFromVersion( "1" ), "runtime", "jar", "", handler );
486 utils.setFile( mockFile( "surefire-shared-utils.jar" ) );
487
488 Map<String, Artifact> providerArtifactsMap = new HashMap<>();
489 providerArtifactsMap.put( "org.apache.maven.surefire:maven-surefire-common", common );
490 providerArtifactsMap.put( "org.apache.maven.surefire:surefire-extensions-api", ext );
491 providerArtifactsMap.put( "org.apache.maven.surefire:surefire-api", api );
492 providerArtifactsMap.put( "org.apache.maven.surefire:surefire-logger-api", loggerApi );
493 providerArtifactsMap.put( "org.apache.maven.surefire:surefire-extensions-spi", spi );
494 providerArtifactsMap.put( "org.apache.maven.surefire:surefire-booter", booter );
495 providerArtifactsMap.put( "org.apache.maven.surefire:surefire-shared-utils", utils );
496
497 when( mojo.getPluginArtifactMap() )
498 .thenReturn( providerArtifactsMap );
499
500 when( handler.isAddedToClasspath() ).thenReturn( true );
501
502 VersionRange v1 = createFromVersion( "4.12" );
503 Artifact junit = new DefaultArtifact( "junit", "junit", v1, "test", "jar", "", handler );
504 junit.setFile( mockFile( "junit.jar" ) );
505
506 VersionRange v2 = createFromVersion( "1.3.0" );
507 Artifact hamcrest = new DefaultArtifact( "org.hamcrest", "hamcrest-core", v2, "test", "jar", "", handler );
508 hamcrest.setFile( mockFile( "hamcrest.jar" ) );
509
510 File classesDir = mockFile( "classes" );
511 File testClassesDir = mockFile( "test-classes" );
512 TestClassPath testClasspath =
513 new TestClassPath( asList( junit, hamcrest ), classesDir, testClassesDir, null );
514
515 doReturn( testClasspath ).when( mojo, "generateTestClasspath" );
516 doReturn( 1 ).when( mojo, "getEffectiveForkCount" );
517 doReturn( true ).when( mojo, "effectiveIsEnableAssertions" );
518 when( mojo.isChildDelegation() ).thenReturn( false );
519
520 ClassLoaderConfiguration classLoaderConfiguration = new ClassLoaderConfiguration( false, true );
521
522 VersionRange v3 = createFromVersion( "1" );
523 Artifact provider = new DefaultArtifact( "x", "surefire-provider", v3, "runtime", "jar", "", handler );
524 provider.setFile( mockFile( "surefire-provider.jar" ) );
525 Set<Artifact> providerArtifacts = singleton( provider );
526
527 Logger logger = mock( Logger.class );
528 when( logger.isDebugEnabled() ).thenReturn( true );
529 doNothing().when( logger ).debug( anyString() );
530 when( mojo.getConsoleLogger() ).thenReturn( new PluginConsoleLogger( logger ) );
531
532 ProviderInfo providerInfo = mock( ProviderInfo.class );
533 when( providerInfo.getProviderName() ).thenReturn( "org.asf.Provider" );
534 when( providerInfo.getProviderClasspath() ).thenReturn( providerArtifacts );
535
536 StartupConfiguration conf = invokeMethod( mojo, "newStartupConfigWithClasspath",
537 classLoaderConfiguration, providerInfo, testClasspath );
538
539 verify( mojo, times( 1 ) ).effectiveIsEnableAssertions();
540 verify( mojo, times( 1 ) ).isChildDelegation();
541 ArgumentCaptor<String> argument = ArgumentCaptor.forClass( String.class );
542 verify( logger, times( 6 ) ).debug( argument.capture() );
543 assertThat( argument.getAllValues() )
544 .containsExactly( "test classpath: test-classes classes junit.jar hamcrest.jar",
545 "provider classpath: surefire-provider.jar",
546 "test(compact) classpath: test-classes classes junit.jar hamcrest.jar",
547 "provider(compact) classpath: surefire-provider.jar",
548 "in-process classpath: surefire-provider.jar maven-surefire-common.jar surefire-booter.jar surefire-extensions-api.jar surefire-api.jar surefire-extensions-spi.jar surefire-logger-api.jar surefire-shared-utils.jar",
549 "in-process(compact) classpath: surefire-provider.jar maven-surefire-common.jar surefire-booter.jar surefire-extensions-api.jar surefire-api.jar surefire-extensions-spi.jar surefire-logger-api.jar surefire-shared-utils.jar"
550 );
551
552 assertThat( conf.getClassLoaderConfiguration() )
553 .isSameAs( classLoaderConfiguration );
554
555 assertThat( ( Object ) conf.getClasspathConfiguration().getTestClasspath() )
556 .isEqualTo( testClasspath.toClasspath() );
557
558 Collection<String> files = new ArrayList<>();
559 for ( Artifact providerArtifact : providerArtifacts )
560 {
561 files.add( providerArtifact.getFile().getAbsolutePath() );
562 }
563 assertThat( ( Object ) conf.getClasspathConfiguration().getProviderClasspath() )
564 .isEqualTo( new Classpath( files ) );
565
566 assertThat( ( Object ) conf.getClasspathConfiguration().isClassPathConfig() )
567 .isEqualTo( true );
568
569 assertThat( ( Object ) conf.getClasspathConfiguration().isModularPathConfig() )
570 .isEqualTo( false );
571
572 assertThat( ( Object ) conf.getClasspathConfiguration().isEnableAssertions() )
573 .isEqualTo( true );
574
575 assertThat( conf.getProviderClassName() )
576 .isEqualTo( "org.asf.Provider" );
577 }
578
579 @Test
580 public void providerClasspathCachingIsNotSharedAcrossMojoInstances() throws Exception
581 {
582 ProviderInfo providerInfo = mock( ProviderInfo.class );
583 when( providerInfo.getProviderName() ).thenReturn( "test-provider" );
584 Artifact provider = new DefaultArtifact( "com.example", "provider", createFromVersion( "1" ), "runtime",
585 "jar", "", handler );
586 provider.setFile( mockFile( "original-test-provider.jar" ) );
587 Set<Artifact> providerClasspath = singleton( provider );
588 when( providerInfo.getProviderClasspath() ).thenReturn( providerClasspath );
589
590 StartupConfiguration startupConfiguration = startupConfigurationForProvider( providerInfo );
591 assertThat( startupConfiguration.getClasspathConfiguration().getProviderClasspath().getClassPath() )
592 .containsExactly( "original-test-provider.jar" );
593
594 provider.setFile( mockFile( "modified-test-provider.jar" ) );
595 startupConfiguration = startupConfigurationForProvider( providerInfo );
596 assertThat( startupConfiguration.getClasspathConfiguration().getProviderClasspath().getClassPath() )
597 .containsExactly( "modified-test-provider.jar" );
598 }
599
600 private StartupConfiguration startupConfigurationForProvider( ProviderInfo providerInfo ) throws Exception
601 {
602 AbstractSurefireMojo mojo = spy( new Mojo() );
603
604 Logger logger = mock( Logger.class );
605 when( logger.isDebugEnabled() ).thenReturn( true );
606 doNothing().when( logger ).debug( anyString() );
607 when( mojo.getConsoleLogger() ).thenReturn( new PluginConsoleLogger( logger ) );
608
609 File classesDir = mockFile( "classes" );
610 File testClassesDir = mockFile( "test-classes" );
611 TestClassPath testClassPath =
612 new TestClassPath( new ArrayList<Artifact>(), classesDir, testClassesDir, new String[0] );
613
614 Artifact common = new DefaultArtifact( "org.apache.maven.surefire", "maven-surefire-common",
615 createFromVersion( "1" ), "runtime", "jar", "", handler );
616 common.setFile( mockFile( "maven-surefire-common.jar" ) );
617
618 Artifact ext = new DefaultArtifact( "org.apache.maven.surefire", "surefire-extensions-api",
619 createFromVersion( "1" ), "runtime", "jar", "", handler );
620 ext.setFile( mockFile( "surefire-extensions-api.jar" ) );
621
622 Artifact api = new DefaultArtifact( "org.apache.maven.surefire", "surefire-api",
623 createFromVersion( "1" ), "runtime", "jar", "", handler );
624 api.setFile( mockFile( "surefire-api.jar" ) );
625
626 Artifact loggerApi = new DefaultArtifact( "org.apache.maven.surefire", "surefire-logger-api",
627 createFromVersion( "1" ), "runtime", "jar", "", handler );
628 loggerApi.setFile( mockFile( "surefire-logger-api.jar" ) );
629
630 Artifact spi = new DefaultArtifact( "org.apache.maven.surefire", "surefire-extensions-spi",
631 createFromVersion( "1" ), "runtime", "jar", "", handler );
632 spi.setFile( mockFile( "surefire-extensions-spi.jar" ) );
633
634 Artifact booter = new DefaultArtifact( "org.apache.maven.surefire", "surefire-booter",
635 createFromVersion( "1" ), "runtime", "jar", "", handler );
636 booter.setFile( mockFile( "surefire-booter.jar" ) );
637
638 Artifact utils = new DefaultArtifact( "org.apache.maven.surefire", "surefire-shared-utils",
639 createFromVersion( "1" ), "runtime", "jar", "", handler );
640 utils.setFile( mockFile( "surefire-shared-utils.jar" ) );
641
642 Map<String, Artifact> providerArtifactsMap = new HashMap<>();
643 providerArtifactsMap.put( "org.apache.maven.surefire:maven-surefire-common", common );
644 providerArtifactsMap.put( "org.apache.maven.surefire:surefire-extensions-api", ext );
645 providerArtifactsMap.put( "org.apache.maven.surefire:surefire-api", api );
646 providerArtifactsMap.put( "org.apache.maven.surefire:surefire-logger-api", loggerApi );
647 providerArtifactsMap.put( "org.apache.maven.surefire:surefire-extensions-spi", spi );
648 providerArtifactsMap.put( "org.apache.maven.surefire:surefire-booter", booter );
649 providerArtifactsMap.put( "org.apache.maven.surefire:surefire-shared-utils", utils );
650
651 when( mojo.getPluginArtifactMap() ).thenReturn( providerArtifactsMap );
652
653 doReturn( 1 ).when( mojo, "getEffectiveForkCount" );
654
655 return invokeMethod( mojo, "createStartupConfiguration",
656 providerInfo, false, null, null, testClassPath, null, null );
657 }
658
659 @Test
660 public void shouldCreateStartupConfigWithModularPath() throws Exception
661 {
662 String baseDir = System.getProperty( "user.dir" );
663
664 Mojo mojo = new Mojo();
665
666
667
668 LocationManager locationManager = mock( LocationManager.class );
669 ResolvePathsResult resolvePathsResult = mock( ResolvePathsResult.class );
670 when( locationManager.resolvePaths( any( ResolvePathsRequest.class ) ) ).thenReturn( resolvePathsResult );
671 when( resolvePathsResult.getPathExceptions() ).thenReturn( emptyMap() );
672 when( resolvePathsResult.getClasspathElements() ).thenReturn( emptyList() );
673 when( resolvePathsResult.getModulepathElements() ).thenReturn( emptyMap() );
674
675 mojo.setLogger( mock( Logger.class ) );
676 mojo.setUseModulePath( true );
677 setInternalState( mojo, "locationManager", locationManager );
678
679 File jdkHome = new File( System.getProperty( "java.home" ) );
680 File jvmExecutable = new File( jdkHome, IS_OS_WINDOWS ? "bin\\java.exe" : "bin/java" );
681 JdkAttributes jdkAttributes = new JdkAttributes( jvmExecutable, jdkHome, true );
682 Platform platform = new Platform().withJdkExecAttributesForTests( jdkAttributes );
683
684 File classesDirectory = new File( baseDir, "mock-dir" );
685 File testClassesDirectory = new File( baseDir, "mock-dir" );
686 mojo.setTestClassesDirectory( testClassesDirectory );
687 TestClassPath testClassPath = new TestClassPath( Collections.<Artifact>emptySet(),
688 classesDirectory, testClassesDirectory, new String[0] );
689
690 ProviderInfo providerInfo = mock( ProviderInfo.class );
691 when( providerInfo.getProviderName() ).thenReturn( "provider mock" );
692 when( providerInfo.getProviderClasspath() ).thenReturn( Collections.<Artifact>emptySet() );
693
694 DefaultScanResult defaultScanResult = mock( DefaultScanResult.class );
695 when( defaultScanResult.getClasses() ).thenReturn( Collections.<String>emptyList() );
696
697 Path pathToModularDescriptor =
698 Paths.get( baseDir, "src", "test", "resources", "org", "apache", "maven", "plugin", "surefire" );
699 mojo.setMainBuildPath( pathToModularDescriptor.toFile() );
700
701 Map<String, Artifact> artifacts = new HashMap<>();
702 Artifact dummyArtifact = mock( Artifact.class );
703 when( dummyArtifact.getFile() ).thenReturn( new File( baseDir, "mock-file" ) );
704 artifacts.put( "org.apache.maven.surefire:maven-surefire-common", dummyArtifact );
705 artifacts.put( "org.apache.maven.surefire:surefire-extensions-api", dummyArtifact );
706 artifacts.put( "org.apache.maven.surefire:surefire-api", dummyArtifact );
707 artifacts.put( "org.apache.maven.surefire:surefire-logger-api", dummyArtifact );
708 artifacts.put( "org.apache.maven.surefire:surefire-extensions-spi", dummyArtifact );
709 artifacts.put( "org.apache.maven.surefire:surefire-booter", dummyArtifact );
710 artifacts.put( "org.apache.maven.surefire:surefire-shared-utils", dummyArtifact );
711 mojo.setPluginArtifactMap( artifacts );
712
713 ResolvePathResult resolvePathResult = mock( ResolvePathResult.class );
714 JavaModuleDescriptor desc = mock( JavaModuleDescriptor.class );
715 when( desc.name() ).thenReturn( "" );
716 when( resolvePathResult.getModuleDescriptor() ).thenReturn( desc );
717 ResolvePathResultWrapper wrapper = new ResolvePathResultWrapper( resolvePathResult, true );
718
719
720 StartupConfiguration actualConfig = invokeMethod( mojo, "createStartupConfiguration",
721 new Class[] {ProviderInfo.class, boolean.class, ClassLoaderConfiguration.class, DefaultScanResult.class,
722 TestClassPath.class, Platform.class, ResolvePathResultWrapper.class},
723 providerInfo, true, mock( ClassLoaderConfiguration.class ), defaultScanResult,
724 testClassPath, platform, wrapper );
725
726 assertThat( actualConfig )
727 .isNotNull();
728
729 assertThat( actualConfig.getClasspathConfiguration() )
730 .isInstanceOf( ModularClasspathConfiguration.class );
731 }
732
733 @Test
734 public void shouldExistTmpDirectory() throws IOException
735 {
736 String systemTmpDir = System.getProperty( "java.io.tmpdir" );
737 String usrDir = new File( System.getProperty( "user.dir" ) ).getCanonicalPath();
738
739 String tmpDir = "surefireX" + System.currentTimeMillis();
740
741
742 new File( systemTmpDir, tmpDir ).delete();
743
744 File targetDir = new File( usrDir, "target" );
745
746 new File( targetDir, tmpDir ).delete();
747
748 AbstractSurefireMojo mojo = mock( AbstractSurefireMojo.class );
749 Logger logger = mock( Logger.class );
750 when( logger.isDebugEnabled() ).thenReturn( false );
751 when( logger.isErrorEnabled() ).thenReturn( false );
752 doNothing().when( logger ).debug( anyString() );
753 doNothing().when( logger ).error( anyString(), any( Throwable.class ) );
754 when( mojo.getConsoleLogger() ).thenReturn( new PluginConsoleLogger( logger ) );
755 when( mojo.getTempDir() ).thenReturn( tmpDir );
756 when( mojo.getProjectBuildDirectory() ).thenReturn( targetDir );
757 when( mojo.createSurefireBootDirectoryInTemp() ).thenCallRealMethod();
758 when( mojo.createSurefireBootDirectoryInBuild() ).thenCallRealMethod();
759 when( mojo.getSurefireTempDir() ).thenCallRealMethod();
760
761 File bootDir = mojo.createSurefireBootDirectoryInTemp();
762 assertThat( bootDir ).isNotNull();
763 assertThat( bootDir ).isDirectory();
764
765 assertThat( new File( systemTmpDir, bootDir.getName() ) ).isDirectory();
766 assertThat( bootDir.getName() )
767 .startsWith( tmpDir );
768
769 File buildTmp = mojo.createSurefireBootDirectoryInBuild();
770 assertThat( buildTmp ).isNotNull();
771 assertThat( buildTmp ).isDirectory();
772 assertThat( buildTmp.getParentFile().getCanonicalFile().getParent() ).isEqualTo( usrDir );
773 assertThat( buildTmp.getName() ).isEqualTo( tmpDir );
774
775 File tmp = mojo.getSurefireTempDir();
776 assertThat( tmp ).isNotNull();
777 assertThat( tmp ).isDirectory();
778 assertThat( IS_OS_WINDOWS ? new File( systemTmpDir, bootDir.getName() ) : new File( targetDir, tmpDir ) )
779 .isDirectory();
780 }
781
782 @Test
783 public void shouldSmartlyResolveJUnit5ProviderWithJUnit4() throws Exception
784 {
785 MavenProject mavenProject = new MavenProject();
786 mavenProject.setArtifact( new DefaultArtifact( "dummy", "pom", createFromVersion( "1.0.0" ),
787 null, "jar", null, mock( ArtifactHandler.class ) ) );
788 mojo.setProject( mavenProject );
789
790 final VersionRange surefireVersion = createFromVersion( "1" );
791
792 Artifact testClasspathJUnit = new DefaultArtifact( "junit", "junit",
793 createFromVersion( "4.12" ), null, "jar", null, mock( ArtifactHandler.class ) );
794
795 Artifact testClasspathHamcrest = new DefaultArtifact( "org.hamcrest", "hamcrest-core",
796 createFromVersion( "1.3" ), null, "jar", null, mock( ArtifactHandler.class ) );
797
798 setProjectDepedenciesToMojo( testClasspathJUnit, testClasspathHamcrest );
799
800 Collection<Artifact> testArtifacts = new ArrayList<>();
801 testArtifacts.add( testClasspathJUnit );
802 testArtifacts.add( testClasspathHamcrest );
803
804 File classesDirectory = new File( "target/classes" );
805
806 File testClassesDirectory = new File( "target/test-classes" );
807
808 TestClassPath testClasspathWrapper =
809 new TestClassPath( testArtifacts, classesDirectory, testClassesDirectory, null );
810
811 mojo.setRemoteRepositories( Collections.<ArtifactRepository>emptyList() );
812 mojo.setProjectRemoteRepositories( Collections.<ArtifactRepository>emptyList() );
813 RepositorySystem repositorySystem = mock( RepositorySystem.class );
814 final Artifact surefireProvider = new DefaultArtifact( "org.apache.maven.surefire",
815 "surefire-junit-platform", surefireVersion, null, "jar", null, mock( ArtifactHandler.class ) );
816 when( repositorySystem.createDependencyArtifact( any( Dependency.class ) ) ).thenAnswer( new Answer<Artifact>()
817 {
818 @Override
819 public Artifact answer( InvocationOnMock invocation )
820 {
821 Dependency provider = (Dependency) invocation.getArguments()[0];
822 assertThat( provider.getGroupId() ).isEqualTo( "org.apache.maven.surefire" );
823 assertThat( provider.getArtifactId() ).isEqualTo( "surefire-junit-platform" );
824 return surefireProvider;
825 }
826 } );
827 final ArtifactResolutionResult surefireProviderResolutionResult = mock( ArtifactResolutionResult.class );
828 when( repositorySystem.resolve( any( ArtifactResolutionRequest.class ) ) )
829 .thenAnswer( new Answer<ArtifactResolutionResult>()
830 {
831 @Override
832 public ArtifactResolutionResult answer( InvocationOnMock invocation )
833 {
834 ArtifactResolutionRequest req = (ArtifactResolutionRequest) invocation.getArguments()[0];
835 Artifact artifact = req.getArtifact();
836 if ( artifact == surefireProvider )
837 {
838 return surefireProviderResolutionResult;
839 }
840 else if ( "org.junit.platform".equals( artifact.getGroupId() )
841 && "junit-platform-launcher".equals( artifact.getArtifactId() )
842 && "1.4.0".equals( artifact.getVersion() ) )
843 {
844 return createExpectedJUnitPlatformLauncherResolutionResult();
845 }
846 else
847 {
848 fail( artifact.getGroupId() + ":" + artifact.getArtifactId() );
849 return null;
850 }
851 }
852 } );
853
854 Artifact java5 = new DefaultArtifact( "org.apache.maven.surefire", "common-java5",
855 surefireVersion, null, "jar", null, mock( ArtifactHandler.class ) );
856 Artifact launcher = new DefaultArtifact( "org.junit.platform", "junit-platform-launcher",
857 createFromVersion( "1.3.2" ), null, "jar", null, mock( ArtifactHandler.class ) );
858 Artifact apiguardian = new DefaultArtifact( "org.apiguardian", "apiguardian-api",
859 createFromVersion( "1.0.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
860 Artifact engine = new DefaultArtifact( "org.junit.platform", "junit-platform-engine",
861 createFromVersion( "1.3.2" ), null, "jar", null, mock( ArtifactHandler.class ) );
862 Artifact commons = new DefaultArtifact( "org.junit.platform", "junit-platform-commons",
863 createFromVersion( "1.3.2" ), null, "jar", null, mock( ArtifactHandler.class ) );
864 Artifact opentest4j = new DefaultArtifact( "org.opentest4j", "opentest4j",
865 createFromVersion( "1.1.1" ), null, "jar", null, mock( ArtifactHandler.class ) );
866 Set<Artifact> providerArtifacts = new HashSet<>();
867 providerArtifacts.add( surefireProvider );
868 providerArtifacts.add( java5 );
869 providerArtifacts.add( launcher );
870 providerArtifacts.add( apiguardian );
871 providerArtifacts.add( engine );
872 providerArtifacts.add( commons );
873 providerArtifacts.add( opentest4j );
874
875 when( surefireProviderResolutionResult.getArtifacts() )
876 .thenReturn( providerArtifacts );
877
878 final Artifact pluginDep1 = new DefaultArtifact( "org.junit.vintage", "junit-vintage-engine",
879 createFromVersion( "5.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
880
881 final Artifact pluginDep2 = new DefaultArtifact( "org.apiguardian", "apiguardian-api",
882 createFromVersion( "1.0.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
883
884 final Artifact pluginDep3 = new DefaultArtifact( "org.junit.platform", "junit-platform-engine",
885 createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
886
887 final Artifact pluginDep4 = new DefaultArtifact( "junit", "junit",
888 createFromVersion( "4.12" ), null, "jar", null, mock( ArtifactHandler.class ) );
889
890 final Artifact pluginDep5 = new DefaultArtifact( "org.hamcrest", "hamcrest-core",
891 createFromVersion( "1.3" ), null, "jar", null, mock( ArtifactHandler.class ) );
892
893 final Artifact pluginDep6 = new DefaultArtifact( "org.opentest4j", "opentest4j",
894 createFromVersion( "1.1.1" ), null, "jar", null, mock( ArtifactHandler.class ) );
895
896 final Artifact pluginDep7 = new DefaultArtifact( "org.junit.platform", "junit-platform-commons",
897 createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
898
899 addPluginDependencies( pluginDep1, pluginDep2, pluginDep3, pluginDep4, pluginDep5, pluginDep6, pluginDep7 );
900
901 mojo.setRepositorySystem( repositorySystem );
902 mojo.setLogger( mock( Logger.class ) );
903 mojo.setDependencyResolver( new DependencyResolverMock()
904 {
905 @Override
906 public Iterable<ArtifactResult> resolveDependencies( ProjectBuildingRequest buildingRequest,
907 Collection<Dependency> dependencies,
908 Collection<Dependency> managedDependencies,
909 TransformableFilter filter )
910 {
911 assertThat( dependencies ).hasSize( 1 );
912 Dependency pluginDependency = dependencies.iterator().next();
913 assertThat( pluginDependency.getGroupId() ).isEqualTo( "org.junit.vintage" );
914 assertThat( pluginDependency.getArtifactId() ).isEqualTo( "junit-vintage-engine" );
915 assertThat( pluginDependency.getVersion() ).isEqualTo( "5.4.0" );
916
917 Collection<ArtifactResult> it = new ArrayList<>();
918 it.add( toArtifactResult( pluginDep1 ) );
919 it.add( toArtifactResult( pluginDep2 ) );
920 it.add( toArtifactResult( pluginDep3 ) );
921 it.add( toArtifactResult( pluginDep4 ) );
922 it.add( toArtifactResult( pluginDep5 ) );
923 it.add( toArtifactResult( pluginDep6 ) );
924 it.add( toArtifactResult( pluginDep7 ) );
925 return it;
926 }
927 } );
928
929 invokeMethod( mojo, "setupStuff" );
930
931 when( mojo.getSession().getProjectBuildingRequest() )
932 .thenReturn( mock( ProjectBuildingRequest.class ) );
933
934 PluginDescriptor pluginDescriptor = mock( PluginDescriptor.class );
935 mojo.setPluginDescriptor( pluginDescriptor );
936 Plugin p = mock( Plugin.class );
937 when( pluginDescriptor.getPlugin() )
938 .thenReturn( p );
939 Artifact pluginDependency = new DefaultArtifact( "org.junit.vintage", "junit-vintage-engine",
940 createFromVersion( "5.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
941 when( p.getDependencies() )
942 .thenReturn( singletonList( toDependency( pluginDependency ) ) );
943
944 Artifact junitPlatformArtifact = invokeMethod( mojo, "getJUnit5Artifact" );
945 assertThat( junitPlatformArtifact.getGroupId() ).isEqualTo( "org.junit.platform" );
946 assertThat( junitPlatformArtifact.getArtifactId() ).isEqualTo( "junit-platform-engine" );
947 assertThat( junitPlatformArtifact.getVersion() ).isEqualTo( "1.4.0" );
948
949 JUnitPlatformProviderInfo prov =
950 mojo.createJUnitPlatformProviderInfo( junitPlatformArtifact, testClasspathWrapper );
951
952 assertThat( prov.isApplicable() )
953 .isTrue();
954
955 Artifact expectedProvider = new DefaultArtifact( "org.apache.maven.surefire", "surefire-junit-platform",
956 surefireVersion, null, "jar", null, mock( ArtifactHandler.class ) );
957 Artifact expectedCommonJava5 = new DefaultArtifact( "org.apache.maven.surefire", "common-java5",
958 surefireVersion, null, "jar", null, mock( ArtifactHandler.class ) );
959 Artifact expectedLauncher = new DefaultArtifact( "org.junit.platform", "junit-platform-launcher",
960 createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
961 Artifact expectedApiguardian = new DefaultArtifact( "org.apiguardian", "apiguardian-api",
962 createFromVersion( "1.0.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
963 Artifact expectedJUnit5Engine = new DefaultArtifact( "org.junit.platform", "junit-platform-engine",
964 createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
965 Artifact expectedOpentest4j = new DefaultArtifact( "org.opentest4j", "opentest4j",
966 createFromVersion( "1.1.1" ), null, "jar", null, mock( ArtifactHandler.class ) );
967 Artifact expectedPlatformCommons = new DefaultArtifact( "org.junit.platform", "junit-platform-commons",
968 createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
969 Artifact expectedEngine = new DefaultArtifact( "org.junit.vintage", "junit-vintage-engine",
970 createFromVersion( "5.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
971 assertThat( prov.getProviderClasspath() )
972 .hasSize( 8 )
973 .containsOnly( expectedProvider, expectedCommonJava5, expectedLauncher, expectedApiguardian,
974 expectedJUnit5Engine, expectedOpentest4j, expectedPlatformCommons, expectedEngine );
975
976 assertThat( testClasspathWrapper.getTestDependencies() )
977 .hasSize( 2 )
978 .includes( entry( "junit:junit", testClasspathJUnit ),
979 entry( "org.hamcrest:hamcrest-core", testClasspathHamcrest ) );
980 }
981
982 @Test
983 public void shouldSmartlyResolveJUnit5ProviderWithVintage() throws Exception
984 {
985 MavenProject mavenProject = new MavenProject();
986 mavenProject.setArtifact( new DefaultArtifact( "dummy", "pom", createFromVersion( "1.0.0" ),
987 null, "jar", null, mock( ArtifactHandler.class ) ) );
988 mojo.setProject( mavenProject );
989
990 VersionRange surefireVersion = createFromVersion( "1" );
991
992 Artifact testClasspathSomeTestArtifact = new DefaultArtifact( "third.party", "artifact",
993 createFromVersion( "1.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
994
995 Artifact testClasspathVintage = new DefaultArtifact( "org.junit.vintage", "junit-vintage-engine",
996 createFromVersion( "5.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
997
998 Artifact testClasspathApiguardian = new DefaultArtifact( "org.apiguardian", "apiguardian-api",
999 createFromVersion( "1.0.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1000
1001 Artifact testClasspathPlatformEng = new DefaultArtifact( "org.junit.platform", "junit-platform-engine",
1002 createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1003
1004 Artifact testClasspathJUnit4 = new DefaultArtifact( "junit", "junit",
1005 createFromVersion( "4.12" ), null, "jar", null, mock( ArtifactHandler.class ) );
1006
1007 Artifact testClasspathHamcrest = new DefaultArtifact( "org.hamcrest", "hamcrest-core",
1008 createFromVersion( "1.3" ), null, "jar", null, mock( ArtifactHandler.class ) );
1009
1010 Artifact testClasspathOpentest4j = new DefaultArtifact( "org.opentest4j", "opentest4j",
1011 createFromVersion( "1.1.1" ), null, "jar", null, mock( ArtifactHandler.class ) );
1012
1013 Artifact testClasspathCommons = new DefaultArtifact( "org.junit.platform", "junit-platform-commons",
1014 createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1015
1016 Collection<Artifact> testArtifacts = asList( testClasspathSomeTestArtifact, testClasspathVintage,
1017 testClasspathApiguardian, testClasspathPlatformEng, testClasspathJUnit4, testClasspathHamcrest,
1018 testClasspathOpentest4j, testClasspathCommons );
1019
1020 setProjectDepedenciesToMojo( testArtifacts.toArray( new Artifact[testArtifacts.size()] ) );
1021
1022 File classesDirectory = new File( "target/classes" );
1023
1024 File testClassesDirectory = new File( "target/test-classes" );
1025
1026 TestClassPath testClasspathWrapper =
1027 new TestClassPath( testArtifacts, classesDirectory, testClassesDirectory, null );
1028
1029 mojo.setRemoteRepositories( Collections.<ArtifactRepository>emptyList() );
1030 mojo.setProjectRemoteRepositories( Collections.<ArtifactRepository>emptyList() );
1031 RepositorySystem repositorySystem = mock( RepositorySystem.class );
1032 final Artifact surefireProvider = new DefaultArtifact( "org.apache.maven.surefire",
1033 "surefire-junit-platform", surefireVersion, null, "jar", null, mock( ArtifactHandler.class ) );
1034 when( repositorySystem.createDependencyArtifact( any( Dependency.class ) ) ).thenAnswer( new Answer<Artifact>()
1035 {
1036 @Override
1037 public Artifact answer( InvocationOnMock invocation )
1038 {
1039 Dependency provider = (Dependency) invocation.getArguments()[0];
1040 assertThat( provider.getGroupId() ).isEqualTo( "org.apache.maven.surefire" );
1041 assertThat( provider.getArtifactId() ).isEqualTo( "surefire-junit-platform" );
1042 return surefireProvider;
1043 }
1044 } );
1045 final ArtifactResolutionResult surefireProviderResolutionResult = mock( ArtifactResolutionResult.class );
1046 when( repositorySystem.resolve( any( ArtifactResolutionRequest.class ) ) )
1047 .thenAnswer( new Answer<ArtifactResolutionResult>()
1048 {
1049 @Override
1050 public ArtifactResolutionResult answer( InvocationOnMock invocation )
1051 {
1052 ArtifactResolutionRequest req = (ArtifactResolutionRequest) invocation.getArguments()[0];
1053 Artifact resolvable = req.getArtifact();
1054 if ( resolvable == surefireProvider )
1055 {
1056 return surefireProviderResolutionResult;
1057 }
1058 else if ( "org.junit.platform".equals( resolvable.getGroupId() )
1059 && "junit-platform-launcher".equals( resolvable.getArtifactId() )
1060 && "1.4.0".equals( resolvable.getVersion() ) )
1061 {
1062 return createExpectedJUnitPlatformLauncherResolutionResult();
1063 }
1064 else
1065 {
1066 fail( resolvable.getGroupId() + ":" + resolvable.getArtifactId() );
1067 return null;
1068 }
1069 }
1070 } );
1071
1072 Artifact java5 = new DefaultArtifact( "org.apache.maven.surefire", "common-java5",
1073 surefireVersion, null, "jar", null, mock( ArtifactHandler.class ) );
1074 Artifact launcher = new DefaultArtifact( "org.junit.platform", "junit-platform-launcher",
1075 createFromVersion( "1.3.2" ), null, "jar", null, mock( ArtifactHandler.class ) );
1076 Artifact apiguardian = new DefaultArtifact( "org.apiguardian", "apiguardian-api",
1077 createFromVersion( "1.0.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1078 Artifact engine = new DefaultArtifact( "org.junit.platform", "junit-platform-engine",
1079 createFromVersion( "1.3.2" ), null, "jar", null, mock( ArtifactHandler.class ) );
1080 Artifact commons = new DefaultArtifact( "org.junit.platform", "junit-platform-commons",
1081 createFromVersion( "1.3.2" ), null, "jar", null, mock( ArtifactHandler.class ) );
1082 Artifact opentest4j = new DefaultArtifact( "org.opentest4j", "opentest4j",
1083 createFromVersion( "1.1.1" ), null, "jar", null, mock( ArtifactHandler.class ) );
1084 Set<Artifact> providerArtifacts = new HashSet<>();
1085 providerArtifacts.add( surefireProvider );
1086 providerArtifacts.add( java5 );
1087 providerArtifacts.add( launcher );
1088 providerArtifacts.add( apiguardian );
1089 providerArtifacts.add( engine );
1090 providerArtifacts.add( commons );
1091 providerArtifacts.add( opentest4j );
1092 when( surefireProviderResolutionResult.getArtifacts() )
1093 .thenReturn( providerArtifacts );
1094
1095 mojo.setRepositorySystem( repositorySystem );
1096 mojo.setLogger( mock( Logger.class ) );
1097 mojo.setDependencyResolver( new DependencyResolverMock()
1098 {
1099 @Override
1100 public Iterable<ArtifactResult> resolveDependencies( ProjectBuildingRequest buildingRequest,
1101 Collection<Dependency> dependencies,
1102 Collection<Dependency> managedDependencies,
1103 TransformableFilter filter )
1104 {
1105 assertThat( dependencies ).isEmpty();
1106 return emptySet();
1107 }
1108 } );
1109
1110 invokeMethod( mojo, "setupStuff" );
1111
1112 PluginDescriptor pluginDescriptor = mock( PluginDescriptor.class );
1113 mojo.setPluginDescriptor( pluginDescriptor );
1114 Plugin p = mock( Plugin.class );
1115 when( pluginDescriptor.getPlugin() )
1116 .thenReturn( p );
1117 when( p.getDependencies() )
1118 .thenReturn( Collections.<Dependency>emptyList() );
1119
1120 Artifact junitPlatformArtifact = invokeMethod( mojo, "getJUnit5Artifact" );
1121 assertThat( junitPlatformArtifact.getGroupId() ).isEqualTo( "org.junit.platform" );
1122 assertThat( junitPlatformArtifact.getArtifactId() ).isEqualTo( "junit-platform-commons" );
1123 assertThat( junitPlatformArtifact.getVersion() ).isEqualTo( "1.4.0" );
1124
1125 JUnitPlatformProviderInfo prov =
1126 mojo.createJUnitPlatformProviderInfo( junitPlatformArtifact, testClasspathWrapper );
1127
1128 assertThat( prov.isApplicable() )
1129 .isTrue();
1130
1131 Artifact expectedProvider = new DefaultArtifact( "org.apache.maven.surefire", "surefire-junit-platform",
1132 surefireVersion, null, "jar", null, mock( ArtifactHandler.class ) );
1133 Artifact expectedCommonJava5 = new DefaultArtifact( "org.apache.maven.surefire", "common-java5",
1134 surefireVersion, null, "jar", null, mock( ArtifactHandler.class ) );
1135 Artifact expectedLauncher = new DefaultArtifact( "org.junit.platform", "junit-platform-launcher",
1136 createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1137 assertThat( prov.getProviderClasspath() )
1138 .hasSize( 3 )
1139 .containsOnly( expectedProvider, expectedCommonJava5, expectedLauncher );
1140
1141 assertThat( testClasspathWrapper.getTestDependencies() )
1142 .hasSize( 8 )
1143 .includes( entry( "third.party:artifact", testClasspathSomeTestArtifact ),
1144 entry( "org.junit.vintage:junit-vintage-engine", testClasspathVintage ),
1145 entry( "org.apiguardian:apiguardian-api", testClasspathApiguardian ),
1146 entry( "org.junit.platform:junit-platform-engine", testClasspathPlatformEng ),
1147 entry( "junit:junit", testClasspathJUnit4 ),
1148 entry( "org.hamcrest:hamcrest-core", testClasspathHamcrest ),
1149 entry( "org.opentest4j:opentest4j", testClasspathOpentest4j ),
1150 entry( "org.junit.platform:junit-platform-commons", testClasspathCommons ) );
1151 }
1152
1153 @Test
1154 public void shouldSmartlyResolveJUnit5ProviderWithJUnit5Commons() throws Exception
1155 {
1156 MavenProject mavenProject = new MavenProject();
1157 mavenProject.setArtifact( new DefaultArtifact( "dummy", "pom", createFromVersion( "1.0.0" ),
1158 null, "jar", null, mock( ArtifactHandler.class ) ) );
1159 mojo.setProject( mavenProject );
1160
1161 final VersionRange surefireVersion = createFromVersion( "1" );
1162
1163 Artifact testClasspathSomeTestArtifact = new DefaultArtifact( "third.party", "artifact",
1164 createFromVersion( "1.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1165
1166 Artifact testClasspathCommons = new DefaultArtifact( "org.junit.platform", "junit-platform-commons",
1167 createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1168
1169 Artifact testClasspathApiguardian = new DefaultArtifact( "org.apiguardian", "apiguardian-api",
1170 createFromVersion( "1.0.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1171
1172 Collection<Artifact> testArtifacts =
1173 asList( testClasspathSomeTestArtifact, testClasspathApiguardian, testClasspathCommons );
1174
1175 setProjectDepedenciesToMojo( testArtifacts.toArray( new Artifact[testArtifacts.size()] ) );
1176
1177 File classesDirectory = new File( "target/classes" );
1178
1179 File testClassesDirectory = new File( "target/test-classes" );
1180
1181 TestClassPath testClasspathWrapper =
1182 new TestClassPath( testArtifacts, classesDirectory, testClassesDirectory, null );
1183
1184 mojo.setRemoteRepositories( Collections.<ArtifactRepository>emptyList() );
1185 mojo.setProjectRemoteRepositories( Collections.<ArtifactRepository>emptyList() );
1186 RepositorySystem repositorySystem = mock( RepositorySystem.class );
1187 final Artifact surefireProvider = new DefaultArtifact( "org.apache.maven.surefire",
1188 "surefire-junit-platform", surefireVersion, null, "jar", null, mock( ArtifactHandler.class ) );
1189 when( repositorySystem.createDependencyArtifact( any( Dependency.class ) ) ).thenAnswer( new Answer<Artifact>()
1190 {
1191 @Override
1192 public Artifact answer( InvocationOnMock invocation )
1193 {
1194 Dependency provider = (Dependency) invocation.getArguments()[0];
1195 assertThat( provider.getGroupId() ).isEqualTo( "org.apache.maven.surefire" );
1196 assertThat( provider.getArtifactId() ).isEqualTo( "surefire-junit-platform" );
1197 return surefireProvider;
1198 }
1199 } );
1200
1201 when( repositorySystem.resolve( any( ArtifactResolutionRequest.class ) ) )
1202 .thenAnswer( new Answer<ArtifactResolutionResult>()
1203 {
1204 @Override
1205 public ArtifactResolutionResult answer( InvocationOnMock invocation )
1206 {
1207 ArtifactResolutionRequest req = (ArtifactResolutionRequest) invocation.getArguments()[0];
1208 Artifact resolvable = req.getArtifact();
1209 if ( resolvable == surefireProvider )
1210 {
1211 return createSurefireProviderResolutionResult( surefireVersion );
1212 }
1213 else if ( "org.junit.platform".equals( resolvable.getGroupId() )
1214 && "junit-platform-launcher".equals( resolvable.getArtifactId() )
1215 && "1.4.0".equals( resolvable.getVersion() ) )
1216 {
1217 return createExpectedJUnitPlatformLauncherResolutionResult();
1218 }
1219 else
1220 {
1221 fail( resolvable.getGroupId() + ":" + resolvable.getArtifactId() );
1222 return null;
1223 }
1224 }
1225 } );
1226
1227 mojo.setRepositorySystem( repositorySystem );
1228 mojo.setLogger( mock( Logger.class ) );
1229 mojo.setDependencyResolver( new DependencyResolverMock()
1230 {
1231 @Override
1232 public Iterable<ArtifactResult> resolveDependencies( ProjectBuildingRequest buildingRequest,
1233 Collection<Dependency> dependencies,
1234 Collection<Dependency> managedDependencies,
1235 TransformableFilter filter )
1236 {
1237 assertThat( dependencies ).isEmpty();
1238 return emptySet();
1239 }
1240 } );
1241
1242 invokeMethod( mojo, "setupStuff" );
1243
1244 when( mojo.getSession().getProjectBuildingRequest() )
1245 .thenReturn( mock( ProjectBuildingRequest.class ) );
1246
1247 PluginDescriptor pluginDescriptor = mock( PluginDescriptor.class );
1248 mojo.setPluginDescriptor( pluginDescriptor );
1249 Plugin p = mock( Plugin.class );
1250 when( pluginDescriptor.getPlugin() )
1251 .thenReturn( p );
1252 when( p.getDependencies() )
1253 .thenReturn( Collections.<Dependency>emptyList() );
1254
1255 Artifact junitPlatformArtifact = invokeMethod( mojo, "getJUnit5Artifact" );
1256 assertThat( junitPlatformArtifact.getGroupId() ).isEqualTo( "org.junit.platform" );
1257 assertThat( junitPlatformArtifact.getArtifactId() ).isEqualTo( "junit-platform-commons" );
1258 assertThat( junitPlatformArtifact.getVersion() ).isEqualTo( "1.4.0" );
1259
1260 JUnitPlatformProviderInfo prov =
1261 mojo.createJUnitPlatformProviderInfo( junitPlatformArtifact, testClasspathWrapper );
1262
1263 assertThat( prov.isApplicable() )
1264 .isTrue();
1265
1266 Set<Artifact> resolvedProviderArtifacts = prov.getProviderClasspath();
1267
1268 Artifact provider = new DefaultArtifact( "org.apache.maven.surefire", "surefire-junit-platform",
1269 surefireVersion, null, "jar", null, mock( ArtifactHandler.class ) );
1270 Artifact java5 = new DefaultArtifact( "org.apache.maven.surefire", "common-java5",
1271 surefireVersion, null, "jar", null, mock( ArtifactHandler.class ) );
1272 Artifact launcher = new DefaultArtifact( "org.junit.platform", "junit-platform-launcher",
1273 createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1274 Artifact engine = new DefaultArtifact( "org.junit.platform", "junit-platform-engine",
1275 createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1276 Artifact opentest4j = new DefaultArtifact( "org.opentest4j", "opentest4j",
1277 createFromVersion( "1.1.1" ), null, "jar", null, mock( ArtifactHandler.class ) );
1278 assertThat( resolvedProviderArtifacts )
1279 .hasSize( 5 )
1280 .containsOnly( provider, java5, launcher, engine, opentest4j );
1281
1282 assertThat( testClasspathWrapper.getTestDependencies() )
1283 .hasSize( 3 )
1284 .includes( entry( "third.party:artifact", testClasspathSomeTestArtifact ),
1285 entry( "org.junit.platform:junit-platform-commons", testClasspathCommons ),
1286 entry( "org.apiguardian:apiguardian-api", testClasspathApiguardian ) );
1287 }
1288
1289 @Test
1290 public void shouldSmartlyResolveJUnit5ProviderWithJUnit5Engine() throws Exception
1291 {
1292 MavenProject mavenProject = new MavenProject();
1293 mavenProject.setArtifact( new DefaultArtifact( "dummy", "pom", createFromVersion( "1.0.0" ),
1294 null, "jar", null, mock( ArtifactHandler.class ) ) );
1295 mojo.setProject( mavenProject );
1296
1297 final VersionRange surefireVersion = createFromVersion( "1" );
1298
1299 final Artifact testClasspathSomeTestArtifact = new DefaultArtifact( "third.party", "artifact",
1300 createFromVersion( "1.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1301
1302 final Artifact testClasspathJUnit5 = new DefaultArtifact( "org.junit.platform", "junit-platform-engine",
1303 createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1304
1305 final Artifact testClasspathApiguardian = new DefaultArtifact( "org.apiguardian", "apiguardian-api",
1306 createFromVersion( "1.0.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1307
1308 final Artifact testClasspathCommons = new DefaultArtifact( "org.junit.platform", "junit-platform-commons",
1309 createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1310
1311 final Artifact testClasspathOpentest4j = new DefaultArtifact( "org.opentest4j", "opentest4j",
1312 createFromVersion( "1.1.1" ), null, "jar", null, mock( ArtifactHandler.class ) );
1313
1314 Collection<Artifact> testArtifacts = asList( testClasspathSomeTestArtifact, testClasspathJUnit5,
1315 testClasspathApiguardian, testClasspathCommons, testClasspathOpentest4j );
1316
1317 setProjectDepedenciesToMojo( testArtifacts.toArray( new Artifact[testArtifacts.size()] ) );
1318
1319 File classesDirectory = new File( "target/classes" );
1320
1321 File testClassesDirectory = new File( "target/test-classes" );
1322
1323 TestClassPath testClasspathWrapper =
1324 new TestClassPath( testArtifacts, classesDirectory, testClassesDirectory, null );
1325
1326 mojo.setRemoteRepositories( Collections.<ArtifactRepository>emptyList() );
1327 mojo.setProjectRemoteRepositories( Collections.<ArtifactRepository>emptyList() );
1328 RepositorySystem repositorySystem = mock( RepositorySystem.class );
1329 final Artifact surefireProvider = new DefaultArtifact( "org.apache.maven.surefire",
1330 "surefire-junit-platform", surefireVersion, null, "jar", null, mock( ArtifactHandler.class ) );
1331 when( repositorySystem.createDependencyArtifact( any( Dependency.class ) ) ).thenAnswer( new Answer<Artifact>()
1332 {
1333 @Override
1334 public Artifact answer( InvocationOnMock invocation )
1335 {
1336 Dependency provider = (Dependency) invocation.getArguments()[0];
1337 assertThat( provider.getGroupId() ).isEqualTo( "org.apache.maven.surefire" );
1338 assertThat( provider.getArtifactId() ).isEqualTo( "surefire-junit-platform" );
1339 return surefireProvider;
1340 }
1341 } );
1342
1343 when( repositorySystem.resolve( any( ArtifactResolutionRequest.class ) ) )
1344 .thenAnswer( new Answer<ArtifactResolutionResult>()
1345 {
1346 @Override
1347 public ArtifactResolutionResult answer( InvocationOnMock invocation )
1348 {
1349 ArtifactResolutionRequest req = (ArtifactResolutionRequest) invocation.getArguments()[0];
1350 Artifact resolvable = req.getArtifact();
1351 if ( resolvable == surefireProvider )
1352 {
1353 return createSurefireProviderResolutionResult( surefireVersion );
1354 }
1355 else if ( "org.junit.platform".equals( resolvable.getGroupId() )
1356 && "junit-platform-launcher".equals( resolvable.getArtifactId() )
1357 && "1.4.0".equals( resolvable.getVersion() ) )
1358 {
1359 return createExpectedJUnitPlatformLauncherResolutionResult();
1360 }
1361 else
1362 {
1363 fail( resolvable.getGroupId() + ":" + resolvable.getArtifactId() );
1364 return null;
1365 }
1366 }
1367 } );
1368
1369 mojo.setRepositorySystem( repositorySystem );
1370 mojo.setLogger( mock( Logger.class ) );
1371 mojo.setDependencyResolver( new DependencyResolverMock()
1372 {
1373 @Override
1374 public Iterable<ArtifactResult> resolveDependencies( ProjectBuildingRequest buildingRequest,
1375 Collection<Dependency> dependencies,
1376 Collection<Dependency> managedDependencies,
1377 TransformableFilter filter )
1378 {
1379 assertThat( dependencies ).isEmpty();
1380 return emptySet();
1381 }
1382 } );
1383
1384 invokeMethod( mojo, "setupStuff" );
1385
1386 Artifact junitPlatformArtifact = invokeMethod( mojo, "getJUnit5Artifact" );
1387 assertThat( junitPlatformArtifact.getGroupId() ).isEqualTo( "org.junit.platform" );
1388 assertThat( junitPlatformArtifact.getArtifactId() ).isEqualTo( "junit-platform-commons" );
1389 assertThat( junitPlatformArtifact.getVersion() ).isEqualTo( "1.4.0" );
1390
1391 JUnitPlatformProviderInfo prov =
1392 mojo.createJUnitPlatformProviderInfo( junitPlatformArtifact, testClasspathWrapper );
1393
1394 assertThat( prov.isApplicable() )
1395 .isTrue();
1396
1397 Set<Artifact> resolvedProviderArtifacts = prov.getProviderClasspath();
1398
1399 Artifact java5 = new DefaultArtifact( "org.apache.maven.surefire", "common-java5",
1400 surefireVersion, null, "jar", null, mock( ArtifactHandler.class ) );
1401 Artifact launcher = new DefaultArtifact( "org.junit.platform", "junit-platform-launcher",
1402 createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1403 assertThat( resolvedProviderArtifacts )
1404 .hasSize( 3 )
1405 .containsOnly( surefireProvider, java5, launcher );
1406
1407 assertThat( testClasspathWrapper.getTestDependencies() )
1408 .hasSize( 5 )
1409 .includes( entry( "third.party:artifact", testClasspathSomeTestArtifact ),
1410 entry( "org.junit.platform:junit-platform-engine", testClasspathJUnit5 ),
1411 entry( "org.apiguardian:apiguardian-api", testClasspathApiguardian ),
1412 entry( "org.junit.platform:junit-platform-commons", testClasspathCommons ),
1413 entry( "org.opentest4j:opentest4j", testClasspathOpentest4j ) );
1414 }
1415
1416 @Test
1417 public void shouldSmartlyResolveJUnit5ProviderWithJupiterApi() throws Exception
1418 {
1419 MavenProject mavenProject = new MavenProject();
1420 mavenProject.setArtifact( new DefaultArtifact( "dummy", "pom", createFromVersion( "1.0.0" ),
1421 null, "jar", null, mock( ArtifactHandler.class ) ) );
1422 mojo.setProject( mavenProject );
1423
1424 final VersionRange surefireVersion = createFromVersion( "1" );
1425
1426 final Artifact testClasspathSomeTestArtifact = new DefaultArtifact( "third.party", "artifact",
1427 createFromVersion( "1.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1428
1429 final Artifact testClasspathJupiterApi = new DefaultArtifact( "org.junit.jupiter", "junit-jupiter-api",
1430 createFromVersion( "5.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1431
1432 final Artifact testClasspathApiguardian = new DefaultArtifact( "org.apiguardian", "apiguardian-api",
1433 createFromVersion( "1.0.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1434
1435 final Artifact testClasspathCommons = new DefaultArtifact( "org.junit.platform", "junit-platform-commons",
1436 createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1437
1438 final Artifact testClasspathOpentest4j = new DefaultArtifact( "org.opentest4j", "opentest4j",
1439 createFromVersion( "1.1.1" ), null, "jar", null, mock( ArtifactHandler.class ) );
1440
1441 Collection<Artifact> testArtifacts = asList( testClasspathSomeTestArtifact, testClasspathJupiterApi,
1442 testClasspathApiguardian, testClasspathCommons, testClasspathOpentest4j );
1443
1444 setProjectDepedenciesToMojo( testArtifacts.toArray( new Artifact[testArtifacts.size()] ) );
1445
1446 File classesDirectory = new File( "target/classes" );
1447
1448 File testClassesDirectory = new File( "target/test-classes" );
1449
1450 TestClassPath testClasspathWrapper =
1451 new TestClassPath( testArtifacts, classesDirectory, testClassesDirectory, null );
1452
1453 mojo.setRemoteRepositories( Collections.<ArtifactRepository>emptyList() );
1454 mojo.setProjectRemoteRepositories( Collections.<ArtifactRepository>emptyList() );
1455 RepositorySystem repositorySystem = mock( RepositorySystem.class );
1456 final Artifact surefireProvider = new DefaultArtifact( "org.apache.maven.surefire",
1457 "surefire-junit-platform", surefireVersion, null, "jar", null, mock( ArtifactHandler.class ) );
1458 when( repositorySystem.createDependencyArtifact( any( Dependency.class ) ) ).thenAnswer( new Answer<Artifact>()
1459 {
1460 @Override
1461 public Artifact answer( InvocationOnMock invocation )
1462 {
1463 Dependency provider = (Dependency) invocation.getArguments()[0];
1464 assertThat( provider.getGroupId() ).isEqualTo( "org.apache.maven.surefire" );
1465 assertThat( provider.getArtifactId() ).isEqualTo( "surefire-junit-platform" );
1466 return surefireProvider;
1467 }
1468 } );
1469
1470 when( repositorySystem.resolve( any( ArtifactResolutionRequest.class ) ) )
1471 .thenAnswer( new Answer<ArtifactResolutionResult>()
1472 {
1473 @Override
1474 public ArtifactResolutionResult answer( InvocationOnMock invocation )
1475 {
1476 ArtifactResolutionRequest req = (ArtifactResolutionRequest) invocation.getArguments()[0];
1477 Artifact resolvable = req.getArtifact();
1478 if ( resolvable == surefireProvider )
1479 {
1480 return createSurefireProviderResolutionResult( surefireVersion );
1481 }
1482 else if ( "org.junit.platform".equals( resolvable.getGroupId() )
1483 && "junit-platform-launcher".equals( resolvable.getArtifactId() )
1484 && "1.4.0".equals( resolvable.getVersion() ) )
1485 {
1486 return createExpectedJUnitPlatformLauncherResolutionResult();
1487 }
1488 else if ( "org.junit.jupiter".equals( resolvable.getGroupId() )
1489 && "junit-jupiter-engine".equals( resolvable.getArtifactId() )
1490 && "5.4.0".equals( resolvable.getVersion() ) )
1491 {
1492 return createJupiterEngineResolutionResult();
1493 }
1494 else
1495 {
1496 fail( resolvable.getGroupId() + ":" + resolvable.getArtifactId() );
1497 return null;
1498 }
1499 }
1500 } );
1501
1502 mojo.setRepositorySystem( repositorySystem );
1503 mojo.setLogger( mock( Logger.class ) );
1504 mojo.setDependencyResolver( new DependencyResolverMock()
1505 {
1506 @Override
1507 public Iterable<ArtifactResult> resolveDependencies( ProjectBuildingRequest buildingRequest,
1508 Collection<Dependency> dependencies,
1509 Collection<Dependency> managedDependencies,
1510 TransformableFilter filter )
1511 {
1512 assertThat( dependencies ).isEmpty();
1513 return emptySet();
1514 }
1515 } );
1516
1517 invokeMethod( mojo, "setupStuff" );
1518
1519 when( mojo.getSession().getProjectBuildingRequest() )
1520 .thenReturn( mock( ProjectBuildingRequest.class ) );
1521
1522 PluginDescriptor pluginDescriptor = mock( PluginDescriptor.class );
1523 mojo.setPluginDescriptor( pluginDescriptor );
1524 Plugin p = mock( Plugin.class );
1525 when( pluginDescriptor.getPlugin() )
1526 .thenReturn( p );
1527 when( p.getDependencies() )
1528 .thenReturn( Collections.<Dependency>emptyList() );
1529
1530 Artifact junitPlatformArtifact = invokeMethod( mojo, "getJUnit5Artifact" );
1531 assertThat( junitPlatformArtifact.getGroupId() ).isEqualTo( "org.junit.platform" );
1532 assertThat( junitPlatformArtifact.getArtifactId() ).isEqualTo( "junit-platform-commons" );
1533 assertThat( junitPlatformArtifact.getVersion() ).isEqualTo( "1.4.0" );
1534
1535 JUnitPlatformProviderInfo prov =
1536 mojo.createJUnitPlatformProviderInfo( junitPlatformArtifact, testClasspathWrapper );
1537
1538 assertThat( prov.isApplicable() )
1539 .isTrue();
1540
1541 Set<Artifact> resolvedProviderArtifacts = prov.getProviderClasspath();
1542
1543 Artifact java5 = new DefaultArtifact( "org.apache.maven.surefire", "common-java5",
1544 surefireVersion, null, "jar", null, mock( ArtifactHandler.class ) );
1545 Artifact launcher = new DefaultArtifact( "org.junit.platform", "junit-platform-launcher",
1546 createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1547 Artifact jupiterEngine = new DefaultArtifact( "org.junit.jupiter", "junit-jupiter-engine",
1548 createFromVersion( "5.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1549 Artifact platformEngine = new DefaultArtifact( "org.junit.platform", "junit-platform-engine",
1550 createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1551 assertThat( resolvedProviderArtifacts )
1552 .hasSize( 5 )
1553 .containsOnly( surefireProvider, java5, launcher, jupiterEngine, platformEngine );
1554
1555 assertThat( testClasspathWrapper.getTestDependencies() )
1556 .hasSize( 5 )
1557 .includes( entry( "third.party:artifact", testClasspathSomeTestArtifact ),
1558 entry( "org.junit.jupiter:junit-jupiter-api", testClasspathJupiterApi ),
1559 entry( "org.apiguardian:apiguardian-api", testClasspathApiguardian ),
1560 entry( "org.junit.platform:junit-platform-commons", testClasspathCommons ),
1561 entry( "org.opentest4j:opentest4j", testClasspathOpentest4j ) );
1562 }
1563
1564 @Test
1565 public void shouldSmartlyResolveJUnit5ProviderWithJupiterEngine() throws Exception
1566 {
1567 MavenProject mavenProject = new MavenProject();
1568 mavenProject.setArtifact( new DefaultArtifact( "dummy", "pom", createFromVersion( "1.0.0" ),
1569 null, "jar", null, mock( ArtifactHandler.class ) ) );
1570 mojo.setProject( mavenProject );
1571
1572 final VersionRange surefireVersion = createFromVersion( "1" );
1573
1574 final Artifact testClasspathSomeTestArtifact = new DefaultArtifact( "third.party", "artifact",
1575 createFromVersion( "1.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1576
1577 final Artifact testClasspathJupiterEngine = new DefaultArtifact( "org.junit.jupiter", "junit-jupiter-engine",
1578 createFromVersion( "5.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1579
1580 final Artifact testClasspathPlatformEngine = new DefaultArtifact( "org.junit.platform", "junit-platform-engine",
1581 createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1582
1583 final Artifact testClasspathJupiterApi = new DefaultArtifact( "org.junit.jupiter", "junit-jupiter-api",
1584 createFromVersion( "5.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1585
1586 final Artifact testClasspathApiguardian = new DefaultArtifact( "org.apiguardian", "apiguardian-api",
1587 createFromVersion( "1.0.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1588
1589 final Artifact testClasspathCommons = new DefaultArtifact( "org.junit.platform", "junit-platform-commons",
1590 createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1591
1592 final Artifact testClasspathOpentest4j = new DefaultArtifact( "org.opentest4j", "opentest4j",
1593 createFromVersion( "1.1.1" ), null, "jar", null, mock( ArtifactHandler.class ) );
1594
1595 Collection<Artifact> testArtifacts = asList( testClasspathSomeTestArtifact, testClasspathJupiterEngine,
1596 testClasspathPlatformEngine, testClasspathJupiterApi, testClasspathApiguardian, testClasspathCommons,
1597 testClasspathOpentest4j );
1598
1599 setProjectDepedenciesToMojo( testArtifacts.toArray( new Artifact[testArtifacts.size()] ) );
1600
1601 File classesDirectory = new File( "target/classes" );
1602
1603 File testClassesDirectory = new File( "target/test-classes" );
1604
1605 TestClassPath testClasspathWrapper =
1606 new TestClassPath( testArtifacts, classesDirectory, testClassesDirectory, null );
1607
1608 mojo.setRemoteRepositories( Collections.<ArtifactRepository>emptyList() );
1609 mojo.setProjectRemoteRepositories( Collections.<ArtifactRepository>emptyList() );
1610 RepositorySystem repositorySystem = mock( RepositorySystem.class );
1611 final Artifact surefireProvider = new DefaultArtifact( "org.apache.maven.surefire",
1612 "surefire-junit-platform", surefireVersion, null, "jar", null, mock( ArtifactHandler.class ) );
1613 when( repositorySystem.createDependencyArtifact( any( Dependency.class ) ) ).thenAnswer( new Answer<Artifact>()
1614 {
1615 @Override
1616 public Artifact answer( InvocationOnMock invocation )
1617 {
1618 Dependency provider = (Dependency) invocation.getArguments()[0];
1619 assertThat( provider.getGroupId() ).isEqualTo( "org.apache.maven.surefire" );
1620 assertThat( provider.getArtifactId() ).isEqualTo( "surefire-junit-platform" );
1621 return surefireProvider;
1622 }
1623 } );
1624
1625 when( repositorySystem.resolve( any( ArtifactResolutionRequest.class ) ) )
1626 .thenAnswer( new Answer<ArtifactResolutionResult>()
1627 {
1628 @Override
1629 public ArtifactResolutionResult answer( InvocationOnMock invocation )
1630 {
1631 ArtifactResolutionRequest req = (ArtifactResolutionRequest) invocation.getArguments()[0];
1632 Artifact resolvable = req.getArtifact();
1633 if ( resolvable == surefireProvider )
1634 {
1635 return createSurefireProviderResolutionResult( surefireVersion );
1636 }
1637 else if ( "org.junit.platform".equals( resolvable.getGroupId() )
1638 && "junit-platform-launcher".equals( resolvable.getArtifactId() )
1639 && "1.4.0".equals( resolvable.getVersion() ) )
1640 {
1641 return createExpectedJUnitPlatformLauncherResolutionResult();
1642 }
1643 else
1644 {
1645 fail( resolvable.getGroupId() + ":" + resolvable.getArtifactId() );
1646 return null;
1647 }
1648 }
1649 } );
1650
1651 mojo.setRepositorySystem( repositorySystem );
1652 mojo.setLogger( mock( Logger.class ) );
1653 mojo.setDependencyResolver( new DependencyResolverMock()
1654 {
1655 @Override
1656 public Iterable<ArtifactResult> resolveDependencies( ProjectBuildingRequest buildingRequest,
1657 Collection<Dependency> dependencies,
1658 Collection<Dependency> managedDependencies,
1659 TransformableFilter filter )
1660 {
1661 assertThat( dependencies ).isEmpty();
1662 return emptySet();
1663 }
1664 } );
1665
1666 invokeMethod( mojo, "setupStuff" );
1667
1668 Artifact junitPlatformArtifact = invokeMethod( mojo, "getJUnit5Artifact" );
1669 assertThat( junitPlatformArtifact.getGroupId() ).isEqualTo( "org.junit.platform" );
1670 assertThat( junitPlatformArtifact.getArtifactId() ).isEqualTo( "junit-platform-commons" );
1671 assertThat( junitPlatformArtifact.getVersion() ).isEqualTo( "1.4.0" );
1672
1673 JUnitPlatformProviderInfo prov =
1674 mojo.createJUnitPlatformProviderInfo( junitPlatformArtifact, testClasspathWrapper );
1675
1676 assertThat( prov.isApplicable() )
1677 .isTrue();
1678
1679 Set<Artifact> resolvedProviderArtifacts = prov.getProviderClasspath();
1680
1681 Artifact java5 = new DefaultArtifact( "org.apache.maven.surefire", "common-java5",
1682 surefireVersion, null, "jar", null, mock( ArtifactHandler.class ) );
1683 Artifact launcher = new DefaultArtifact( "org.junit.platform", "junit-platform-launcher",
1684 createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1685 assertThat( resolvedProviderArtifacts )
1686 .hasSize( 3 )
1687 .containsOnly( surefireProvider, java5, launcher );
1688
1689 assertThat( testClasspathWrapper.getTestDependencies() )
1690 .hasSize( 7 )
1691 .includes( entry( "third.party:artifact", testClasspathSomeTestArtifact ),
1692 entry( "org.junit.jupiter:junit-jupiter-engine", testClasspathJupiterEngine ),
1693 entry( "org.junit.platform:junit-platform-engine", testClasspathPlatformEngine ),
1694 entry( "org.junit.jupiter:junit-jupiter-api", testClasspathJupiterApi ),
1695 entry( "org.apiguardian:apiguardian-api", testClasspathApiguardian ),
1696 entry( "org.junit.platform:junit-platform-commons", testClasspathCommons ),
1697 entry( "org.opentest4j:opentest4j", testClasspathOpentest4j ) );
1698 }
1699
1700 @Test
1701 public void shouldSmartlyResolveJUnit5ProviderWithJupiterEngineInPluginDependencies() throws Exception
1702 {
1703 final VersionRange surefireVersion = createFromVersion( "1" );
1704
1705 final Artifact plugin = new DefaultArtifact( "org.apache.maven.surefire", "maven-surefire-plugin",
1706 surefireVersion, null, "jar", null, mock( ArtifactHandler.class ) );
1707
1708 final Artifact forkedBooter = new DefaultArtifact( "org.apache.maven.surefire",
1709 "surefire-booter", surefireVersion, null, "jar", null, mock( ArtifactHandler.class ) );
1710
1711 final Artifact pluginDepJupiterEngine = new DefaultArtifact( "org.junit.jupiter", "junit-jupiter-engine",
1712 createFromVersion( "5.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1713
1714 final Artifact pluginDepPlatformEngine = new DefaultArtifact( "org.junit.platform", "junit-platform-engine",
1715 createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1716
1717 final Artifact pluginDepJupiterApi = new DefaultArtifact( "org.junit.jupiter", "junit-jupiter-api",
1718 createFromVersion( "5.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1719
1720 final Artifact pluginDepApiguardian = new DefaultArtifact( "org.apiguardian", "apiguardian-api",
1721 createFromVersion( "1.0.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1722
1723 final Artifact pluginDepCommons = new DefaultArtifact( "org.junit.platform", "junit-platform-commons",
1724 createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1725
1726 final Artifact pluginDepOpentest4j = new DefaultArtifact( "org.opentest4j", "opentest4j",
1727 createFromVersion( "1.1.1" ), null, "jar", null, mock( ArtifactHandler.class ) );
1728
1729 mojo.getPluginArtifactMap().put( "org.apache.maven.surefire:maven-surefire-plugin", plugin );
1730 mojo.getPluginArtifactMap().put( "org.apache.maven.surefire:surefire-booter", forkedBooter );
1731 mojo.getPluginArtifactMap().put( "org.junit.jupiter:junit-jupiter-engine", pluginDepJupiterEngine );
1732 mojo.getPluginArtifactMap().put( "org.junit.platform:junit-platform-engine", pluginDepPlatformEngine );
1733 mojo.getPluginArtifactMap().put( "org.junit.jupiter:junit-jupiter-api", pluginDepJupiterApi );
1734 mojo.getPluginArtifactMap().put( "org.apiguardian:apiguardian-api", pluginDepApiguardian );
1735 mojo.getPluginArtifactMap().put( "org.junit.platform:junit-platform-commons", pluginDepCommons );
1736 mojo.getPluginArtifactMap().put( "org.opentest4j:opentest4j", pluginDepOpentest4j );
1737
1738 MavenProject mavenProject = new MavenProject();
1739 mavenProject.setArtifact( new DefaultArtifact( "dummy", "pom", createFromVersion( "1.0.0" ),
1740 null, "jar", null, mock( ArtifactHandler.class ) ) );
1741 mojo.setProject( mavenProject );
1742
1743 final Artifact testClasspathSomeTestArtifact = new DefaultArtifact( "third.party", "artifact",
1744 createFromVersion( "1.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1745
1746 final Artifact testClasspathJupiterApi = new DefaultArtifact( "org.junit.jupiter", "junit-jupiter-api",
1747 createFromVersion( "5.3.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1748
1749 final Artifact testClasspathApiguardian = new DefaultArtifact( "org.apiguardian", "apiguardian-api",
1750 createFromVersion( "1.0.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1751
1752 final Artifact testClasspathCommons = new DefaultArtifact( "org.junit.platform", "junit-platform-commons",
1753 createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1754
1755 final Artifact testClasspathOpentest4j = new DefaultArtifact( "org.opentest4j", "opentest4j",
1756 createFromVersion( "1.1.1" ), null, "jar", null, mock( ArtifactHandler.class ) );
1757
1758 Collection<Artifact> testArtifacts = asList( testClasspathSomeTestArtifact, testClasspathJupiterApi,
1759 testClasspathApiguardian, testClasspathCommons, testClasspathOpentest4j );
1760
1761 setProjectDepedenciesToMojo( testArtifacts.toArray( new Artifact[testArtifacts.size()] ) );
1762
1763 File classesDirectory = new File( "target/classes" );
1764
1765 File testClassesDirectory = new File( "target/test-classes" );
1766
1767 TestClassPath testClasspathWrapper =
1768 new TestClassPath( testArtifacts, classesDirectory, testClassesDirectory, null );
1769
1770 mojo.setRemoteRepositories( Collections.<ArtifactRepository>emptyList() );
1771 mojo.setProjectRemoteRepositories( Collections.<ArtifactRepository>emptyList() );
1772 RepositorySystem repositorySystem = mock( RepositorySystem.class );
1773 final Artifact surefireProvider = new DefaultArtifact( "org.apache.maven.surefire",
1774 "surefire-junit-platform", surefireVersion, null, "jar", null, mock( ArtifactHandler.class ) );
1775 when( repositorySystem.createDependencyArtifact( any( Dependency.class ) ) ).thenAnswer( new Answer<Artifact>()
1776 {
1777 @Override
1778 public Artifact answer( InvocationOnMock invocation )
1779 {
1780 Dependency provider = (Dependency) invocation.getArguments()[0];
1781 assertThat( provider.getGroupId() ).isEqualTo( "org.apache.maven.surefire" );
1782 assertThat( provider.getArtifactId() ).isEqualTo( "surefire-junit-platform" );
1783 return surefireProvider;
1784 }
1785 } );
1786
1787 when( repositorySystem.resolve( any( ArtifactResolutionRequest.class ) ) )
1788 .thenAnswer( new Answer<ArtifactResolutionResult>()
1789 {
1790 @Override
1791 public ArtifactResolutionResult answer( InvocationOnMock invocation )
1792 {
1793 ArtifactResolutionRequest req = (ArtifactResolutionRequest) invocation.getArguments()[0];
1794 Artifact resolvable = req.getArtifact();
1795 if ( resolvable == surefireProvider )
1796 {
1797 return createSurefireProviderResolutionResult( surefireVersion );
1798 }
1799 else if ( "org.junit.platform".equals( resolvable.getGroupId() )
1800 && "junit-platform-launcher".equals( resolvable.getArtifactId() )
1801 && "1.4.0".equals( resolvable.getVersion() ) )
1802 {
1803 return createExpectedJUnitPlatformLauncherResolutionResult();
1804 }
1805 else
1806 {
1807 fail( resolvable.getGroupId() + ":" + resolvable.getArtifactId() );
1808 return null;
1809 }
1810 }
1811 } );
1812
1813 mojo.setRepositorySystem( repositorySystem );
1814 mojo.setLogger( mock( Logger.class ) );
1815 mojo.setDependencyResolver( new DependencyResolverMock()
1816 {
1817 @Override
1818 public Iterable<ArtifactResult> resolveDependencies( ProjectBuildingRequest buildingRequest,
1819 Collection<Dependency> dependencies,
1820 Collection<Dependency> managedDependencies,
1821 TransformableFilter filter )
1822 {
1823 assertThat( dependencies ).hasSize( 1 );
1824 Dependency resolvable = dependencies.iterator().next();
1825 if ( "org.junit.jupiter".equals( resolvable.getGroupId() )
1826 && "junit-jupiter-engine".equals( resolvable.getArtifactId() )
1827 && "5.4.0".equals( resolvable.getVersion() ) )
1828 {
1829 Set<ArtifactResult> resolvedPluginDeps = new HashSet<>();
1830 resolvedPluginDeps.add( toArtifactResult( pluginDepJupiterEngine ) );
1831 resolvedPluginDeps.add( toArtifactResult( pluginDepPlatformEngine ) );
1832 resolvedPluginDeps.add( toArtifactResult( pluginDepJupiterApi ) );
1833 resolvedPluginDeps.add( toArtifactResult( pluginDepApiguardian ) );
1834 resolvedPluginDeps.add( toArtifactResult( pluginDepCommons ) );
1835 resolvedPluginDeps.add( toArtifactResult( pluginDepOpentest4j ) );
1836 return resolvedPluginDeps;
1837 }
1838 else
1839 {
1840 fail( resolvable.getGroupId() + ":" + resolvable.getArtifactId() );
1841 return null;
1842 }
1843 }
1844 } );
1845
1846 invokeMethod( mojo, "setupStuff" );
1847
1848 Artifact junitPlatformArtifact = invokeMethod( mojo, "getJUnit5Artifact" );
1849 assertThat( junitPlatformArtifact.getGroupId() ).isEqualTo( "org.junit.platform" );
1850 assertThat( junitPlatformArtifact.getArtifactId() ).isEqualTo( "junit-platform-engine" );
1851 assertThat( junitPlatformArtifact.getVersion() ).isEqualTo( "1.4.0" );
1852
1853 JUnitPlatformProviderInfo prov =
1854 mojo.createJUnitPlatformProviderInfo( junitPlatformArtifact, testClasspathWrapper );
1855
1856 assertThat( prov.isApplicable() )
1857 .isTrue();
1858
1859 when( mojo.getSession().getProjectBuildingRequest() )
1860 .thenReturn( mock( ProjectBuildingRequest.class ) );
1861
1862 PluginDescriptor pluginDescriptor = mock( PluginDescriptor.class );
1863 mojo.setPluginDescriptor( pluginDescriptor );
1864 Plugin p = mock( Plugin.class );
1865 when( pluginDescriptor.getPlugin() )
1866 .thenReturn( p );
1867 List<Dependency> directPluginDependencies = toDependencies( pluginDepJupiterEngine );
1868 when( p.getDependencies() )
1869 .thenReturn( directPluginDependencies );
1870
1871 Set<Artifact> resolvedProviderArtifacts = prov.getProviderClasspath();
1872
1873 Artifact java5 = new DefaultArtifact( "org.apache.maven.surefire", "common-java5",
1874 surefireVersion, null, "jar", null, mock( ArtifactHandler.class ) );
1875 Artifact launcher = new DefaultArtifact( "org.junit.platform", "junit-platform-launcher",
1876 createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1877 Artifact jupiterEngine = new DefaultArtifact( "org.junit.jupiter", "junit-jupiter-engine",
1878 createFromVersion( "5.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1879 Artifact platformEngine = new DefaultArtifact( "org.junit.platform", "junit-platform-engine",
1880 createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1881 assertThat( resolvedProviderArtifacts )
1882 .hasSize( 5 )
1883 .containsOnly( surefireProvider, java5, launcher, jupiterEngine, platformEngine );
1884
1885 assertThat( testClasspathWrapper.getTestDependencies() )
1886 .hasSize( 5 )
1887 .includes( entry( "third.party:artifact", testClasspathSomeTestArtifact ),
1888 entry( "org.junit.jupiter:junit-jupiter-api", testClasspathJupiterApi ),
1889 entry( "org.apiguardian:apiguardian-api", testClasspathApiguardian ),
1890 entry( "org.junit.platform:junit-platform-commons", testClasspathCommons ),
1891 entry( "org.opentest4j:opentest4j", testClasspathOpentest4j ) );
1892 }
1893
1894 private static ArtifactResolutionResult createJUnitPlatformLauncherResolutionResult(
1895 Artifact junit5Engine, Artifact apiguardian, Artifact commons, Artifact opentest4j )
1896 {
1897 ArtifactResolutionResult launcherResolutionResult = mock( ArtifactResolutionResult.class );
1898 Set<Artifact> resolvedLauncherArtifacts = new HashSet<>();
1899 Artifact launcher = new DefaultArtifact( "org.junit.platform", "junit-platform-launcher",
1900 commons.getVersionRange(), null, "jar", null, mock( ArtifactHandler.class ) );
1901 resolvedLauncherArtifacts.add( launcher );
1902 resolvedLauncherArtifacts.add( apiguardian );
1903 resolvedLauncherArtifacts.add( junit5Engine );
1904 resolvedLauncherArtifacts.add( commons );
1905 resolvedLauncherArtifacts.add( opentest4j );
1906 resolvedLauncherArtifacts.remove( null );
1907 when( launcherResolutionResult.getArtifacts() )
1908 .thenReturn( resolvedLauncherArtifacts );
1909 return launcherResolutionResult;
1910 }
1911
1912 private static ArtifactResolutionResult createJupiterEngineResolutionResult()
1913 {
1914 ArtifactResolutionResult launcherResolutionResult = mock( ArtifactResolutionResult.class );
1915 Set<Artifact> resolvedLauncherArtifacts = new HashSet<>();
1916 resolvedLauncherArtifacts.add( new DefaultArtifact( "org.junit.jupiter", "junit-jupiter-engine",
1917 createFromVersion( "5.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) ) );
1918 resolvedLauncherArtifacts.add( new DefaultArtifact( "org.junit.jupiter", "junit-jupiter-api",
1919 createFromVersion( "5.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) ) );
1920 resolvedLauncherArtifacts.add( new DefaultArtifact( "org.junit.platform", "junit-platform-engine",
1921 createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) ) );
1922 resolvedLauncherArtifacts.add( new DefaultArtifact( "org.apiguardian", "apiguardian-api",
1923 createFromVersion( "1.0.0" ), null, "jar", null, mock( ArtifactHandler.class ) ) );
1924 resolvedLauncherArtifacts.add( new DefaultArtifact( "org.opentest4j", "opentest4j",
1925 createFromVersion( "1.1.1" ), null, "jar", null, mock( ArtifactHandler.class ) ) );
1926 resolvedLauncherArtifacts.add( new DefaultArtifact( "org.junit.platform", "junit-platform-commons",
1927 createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) ) );
1928 when( launcherResolutionResult.getArtifacts() )
1929 .thenReturn( resolvedLauncherArtifacts );
1930 return launcherResolutionResult;
1931 }
1932
1933 private static ArtifactResolutionResult createExpectedJUnitPlatformLauncherResolutionResult()
1934 {
1935 Artifact engine = new DefaultArtifact( "org.junit.platform", "junit-platform-engine",
1936 createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1937 Artifact commons = new DefaultArtifact( "org.junit.platform", "junit-platform-commons",
1938 createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1939 Artifact apiguardian = new DefaultArtifact( "org.apiguardian", "apiguardian-api",
1940 createFromVersion( "1.0.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1941 Artifact opentest4j = new DefaultArtifact( "org.opentest4j", "opentest4j",
1942 createFromVersion( "1.1.1" ), null, "jar", null, mock( ArtifactHandler.class ) );
1943 return createJUnitPlatformLauncherResolutionResult( engine, apiguardian, commons, opentest4j );
1944 }
1945
1946 private static ArtifactResolutionResult createSurefireProviderResolutionResult( VersionRange surefireVersion )
1947 {
1948 ArtifactResolutionResult surefirePlatformResolutionResult = mock( ArtifactResolutionResult.class );
1949
1950 Artifact provider = new DefaultArtifact( "org.apache.maven.surefire", "surefire-junit-platform",
1951 surefireVersion, null, "jar", null, mock( ArtifactHandler.class ) );
1952 Artifact java5 = new DefaultArtifact( "org.apache.maven.surefire", "common-java5",
1953 surefireVersion, null, "jar", null, mock( ArtifactHandler.class ) );
1954 Artifact launcher = new DefaultArtifact( "org.junit.platform", "junit-platform-launcher",
1955 createFromVersion( "1.3.2" ), null, "jar", null, mock( ArtifactHandler.class ) );
1956 Artifact apiguardian = new DefaultArtifact( "org.apiguardian", "apiguardian-api",
1957 createFromVersion( "1.0.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
1958 Artifact engine = new DefaultArtifact( "org.junit.platform", "junit-platform-engine",
1959 createFromVersion( "1.3.2" ), null, "jar", null, mock( ArtifactHandler.class ) );
1960 Artifact commons = new DefaultArtifact( "org.junit.platform", "junit-platform-commons",
1961 createFromVersion( "1.3.2" ), null, "jar", null, mock( ArtifactHandler.class ) );
1962 Artifact opentest4j = new DefaultArtifact( "org.opentest4j", "opentest4j",
1963 createFromVersion( "1.1.1" ), null, "jar", null, mock( ArtifactHandler.class ) );
1964
1965 Set<Artifact> providerArtifacts = new HashSet<>();
1966 providerArtifacts.add( provider );
1967 providerArtifacts.add( java5 );
1968 providerArtifacts.add( launcher );
1969 providerArtifacts.add( apiguardian );
1970 providerArtifacts.add( engine );
1971 providerArtifacts.add( commons );
1972 providerArtifacts.add( opentest4j );
1973
1974 when( surefirePlatformResolutionResult.getArtifacts() )
1975 .thenReturn( providerArtifacts );
1976 return surefirePlatformResolutionResult;
1977 }
1978
1979 @Test
1980 public void shouldVerifyConfigParameters() throws Exception
1981 {
1982 Mojo mojo = new Mojo()
1983 {
1984 @Override
1985 public File getTestClassesDirectory()
1986 {
1987 return new File( System.getProperty( "user.dir" ), "target/test-classes" );
1988 }
1989
1990 @Override
1991 protected String getEnableProcessChecker()
1992 {
1993 return "fake";
1994 }
1995 };
1996
1997 e.expect( MojoFailureException.class );
1998 e.expectMessage( "Unexpected value 'fake' in the configuration parameter 'enableProcessChecker'." );
1999 mojo.verifyParameters();
2000 }
2001
2002 private void setProjectDepedenciesToMojo( Artifact... deps )
2003 {
2004 for ( Artifact dep : deps )
2005 {
2006 mojo.getProjectArtifactMap()
2007 .put( dep.getGroupId() + ":" + dep.getArtifactId(), dep );
2008 }
2009 }
2010
2011 private void addPluginDependencies( Artifact... deps )
2012 {
2013 for ( Artifact dep : deps )
2014 {
2015 mojo.getPluginArtifactMap()
2016 .put( dep.getGroupId() + ":" + dep.getArtifactId(), dep );
2017 }
2018 }
2019
2020
2021
2022
2023 public static class Mojo
2024 extends AbstractSurefireMojo implements SurefireReportParameters
2025 {
2026 private File mainBuildPath;
2027 private File testClassesDirectory;
2028 private boolean useModulePath;
2029
2030 private JUnitPlatformProviderInfo createJUnitPlatformProviderInfo( Artifact junitPlatformArtifact,
2031 TestClassPath testClasspathWrapper )
2032 {
2033 return new JUnitPlatformProviderInfo( junitPlatformArtifact, testClasspathWrapper );
2034 }
2035
2036 @Override
2037 protected void logDebugOrCliShowErrors( String s )
2038 {
2039
2040 }
2041
2042 @Override
2043 protected String getPluginName()
2044 {
2045 return null;
2046 }
2047
2048 @Override
2049 protected int getRerunFailingTestsCount()
2050 {
2051 return 0;
2052 }
2053
2054 @Override
2055 public boolean isSkipTests()
2056 {
2057 return false;
2058 }
2059
2060 @Override
2061 public void setSkipTests( boolean skipTests )
2062 {
2063
2064 }
2065
2066 @Override
2067 public boolean isSkipExec()
2068 {
2069 return false;
2070 }
2071
2072 @Override
2073 public void setSkipExec( boolean skipExec )
2074 {
2075
2076 }
2077
2078 @Override
2079 public boolean isSkip()
2080 {
2081 return false;
2082 }
2083
2084 @Override
2085 public void setSkip( boolean skip )
2086 {
2087
2088 }
2089
2090 @Override
2091 public boolean isTestFailureIgnore()
2092 {
2093 return false;
2094 }
2095
2096 @Override
2097 public void setTestFailureIgnore( boolean testFailureIgnore )
2098 {
2099
2100 }
2101
2102 @Override
2103 public File getBasedir()
2104 {
2105 return null;
2106 }
2107
2108 @Override
2109 public void setBasedir( File basedir )
2110 {
2111
2112 }
2113
2114 @Override
2115 public File getTestClassesDirectory()
2116 {
2117 return testClassesDirectory;
2118 }
2119
2120 @Override
2121 public void setTestClassesDirectory( File testClassesDirectory )
2122 {
2123 this.testClassesDirectory = testClassesDirectory;
2124 }
2125
2126 @Override
2127 public File getMainBuildPath()
2128 {
2129 return mainBuildPath;
2130 }
2131
2132 @Override
2133 public void setMainBuildPath( File mainBuildPath )
2134 {
2135 this.mainBuildPath = mainBuildPath;
2136 }
2137
2138 @Override
2139 public File getReportsDirectory()
2140 {
2141 return null;
2142 }
2143
2144 @Override
2145 public void setReportsDirectory( File reportsDirectory )
2146 {
2147
2148 }
2149
2150 @Override
2151 public String getTest()
2152 {
2153 return null;
2154 }
2155
2156 @Override
2157 public void setTest( String test )
2158 {
2159
2160 }
2161
2162 @Override
2163 public List<String> getIncludes()
2164 {
2165 return null;
2166 }
2167
2168 @Override
2169 public File getIncludesFile()
2170 {
2171 return null;
2172 }
2173
2174 @Override
2175 public void setIncludes( List<String> includes )
2176 {
2177
2178 }
2179
2180 @Override
2181 public boolean isPrintSummary()
2182 {
2183 return false;
2184 }
2185
2186 @Override
2187 public void setPrintSummary( boolean printSummary )
2188 {
2189
2190 }
2191
2192 @Override
2193 public String getReportFormat()
2194 {
2195 return null;
2196 }
2197
2198 @Override
2199 public void setReportFormat( String reportFormat )
2200 {
2201
2202 }
2203
2204 @Override
2205 public boolean isUseFile()
2206 {
2207 return false;
2208 }
2209
2210 @Override
2211 public void setUseFile( boolean useFile )
2212 {
2213
2214 }
2215
2216 @Override
2217 public String getDebugForkedProcess()
2218 {
2219 return null;
2220 }
2221
2222 @Override
2223 public void setDebugForkedProcess( String debugForkedProcess )
2224 {
2225
2226 }
2227
2228 @Override
2229 public int getForkedProcessTimeoutInSeconds()
2230 {
2231 return 0;
2232 }
2233
2234 @Override
2235 public void setForkedProcessTimeoutInSeconds( int forkedProcessTimeoutInSeconds )
2236 {
2237
2238 }
2239
2240 @Override
2241 public int getForkedProcessExitTimeoutInSeconds()
2242 {
2243 return 0;
2244 }
2245
2246 @Override
2247 public void setForkedProcessExitTimeoutInSeconds( int forkedProcessTerminationTimeoutInSeconds )
2248 {
2249
2250 }
2251
2252 @Override
2253 public double getParallelTestsTimeoutInSeconds()
2254 {
2255 return 0;
2256 }
2257
2258 @Override
2259 public void setParallelTestsTimeoutInSeconds( double parallelTestsTimeoutInSeconds )
2260 {
2261
2262 }
2263
2264 @Override
2265 public double getParallelTestsTimeoutForcedInSeconds()
2266 {
2267 return 0;
2268 }
2269
2270 @Override
2271 public void setParallelTestsTimeoutForcedInSeconds( double parallelTestsTimeoutForcedInSeconds )
2272 {
2273
2274 }
2275
2276 @Override
2277 public boolean isUseSystemClassLoader()
2278 {
2279 return false;
2280 }
2281
2282 @Override
2283 public void setUseSystemClassLoader( boolean useSystemClassLoader )
2284 {
2285
2286 }
2287
2288 @Override
2289 public boolean isUseManifestOnlyJar()
2290 {
2291 return false;
2292 }
2293
2294 @Override
2295 public void setUseManifestOnlyJar( boolean useManifestOnlyJar )
2296 {
2297
2298 }
2299
2300 @Override
2301 public String getEncoding()
2302 {
2303 return null;
2304 }
2305
2306 @Override
2307 public void setEncoding( String encoding )
2308 {
2309
2310 }
2311
2312 @Override
2313 public Boolean getFailIfNoSpecifiedTests()
2314 {
2315 return null;
2316 }
2317
2318 @Override
2319 public void setFailIfNoSpecifiedTests( boolean failIfNoSpecifiedTests )
2320 {
2321
2322 }
2323
2324 @Override
2325 public int getSkipAfterFailureCount()
2326 {
2327 return 0;
2328 }
2329
2330 @Override
2331 public String getShutdown()
2332 {
2333 return null;
2334 }
2335
2336 @Override
2337 public File getExcludesFile()
2338 {
2339 return null;
2340 }
2341
2342 @Override
2343 protected List<File> suiteXmlFiles()
2344 {
2345 return null;
2346 }
2347
2348 @Override
2349 protected boolean hasSuiteXmlFiles()
2350 {
2351 return false;
2352 }
2353
2354 @Override
2355 protected String[] getExcludedEnvironmentVariables()
2356 {
2357 return new String[0];
2358 }
2359
2360 @Override
2361 public File[] getSuiteXmlFiles()
2362 {
2363 return new File[0];
2364 }
2365
2366 @Override
2367 public void setSuiteXmlFiles( File[] suiteXmlFiles )
2368 {
2369
2370 }
2371
2372 @Override
2373 public String getRunOrder()
2374 {
2375 return null;
2376 }
2377
2378 @Override
2379 public void setRunOrder( String runOrder )
2380 {
2381
2382 }
2383
2384 @Override
2385 protected void handleSummary( RunResult summary, Exception firstForkException )
2386 {
2387
2388 }
2389
2390 @Override
2391 protected boolean isSkipExecution()
2392 {
2393 return false;
2394 }
2395
2396 @Override
2397 protected String[] getDefaultIncludes()
2398 {
2399 return new String[0];
2400 }
2401
2402 @Override
2403 protected String getReportSchemaLocation()
2404 {
2405 return null;
2406 }
2407
2408 @Override
2409 protected boolean useModulePath()
2410 {
2411 return useModulePath;
2412 }
2413
2414 @Override
2415 protected void setUseModulePath( boolean useModulePath )
2416 {
2417 this.useModulePath = useModulePath;
2418 }
2419
2420 @Override
2421 protected String getEnableProcessChecker()
2422 {
2423 return null;
2424 }
2425
2426 @Override
2427 protected ForkNodeFactory getForkNode()
2428 {
2429 return null;
2430 }
2431
2432 @Override
2433 protected Artifact getMojoArtifact()
2434 {
2435 return new DefaultArtifact( "org.apache.maven.surefire", "maven-surefire-plugin", createFromVersion( "1" ),
2436 null, "jar", null, mock( ArtifactHandler.class ) );
2437 }
2438
2439 @Override
2440 public File getSystemPropertiesFile()
2441 {
2442 return null;
2443 }
2444
2445 @Override
2446 public void setSystemPropertiesFile( File systemPropertiesFile )
2447 {
2448
2449 }
2450
2451 public void setToolchain( Toolchain toolchain )
2452 {
2453 setInternalState( this, "toolchain", toolchain );
2454 }
2455
2456 public void setJvm( String jvm )
2457 {
2458 setInternalState( this, "jvm", jvm );
2459 }
2460 }
2461
2462 private static File mockFile( String absolutePath )
2463 {
2464 File f = mock( File.class );
2465 when( f.getAbsolutePath() ).thenReturn( absolutePath );
2466 return f;
2467 }
2468
2469 private static Dependency toDependency( Artifact artifact )
2470 {
2471 Dependency dependency = new Dependency();
2472 dependency.setGroupId( artifact.getGroupId() );
2473 dependency.setArtifactId( artifact.getArtifactId() );
2474 dependency.setVersion( artifact.getBaseVersion() );
2475 dependency.setType( "jar" );
2476 return dependency;
2477 }
2478
2479 private static List<Dependency> toDependencies( Artifact... artifacts )
2480 {
2481 List<Dependency> dependencies = new ArrayList<>();
2482 for ( Artifact artifact : artifacts )
2483 {
2484 dependencies.add( toDependency( artifact ) );
2485 }
2486 return dependencies;
2487 }
2488
2489 private static ArtifactResult toArtifactResult( final Artifact artifact )
2490 {
2491 class AR implements ArtifactResult
2492 {
2493
2494 @Override
2495 public Artifact getArtifact()
2496 {
2497 return artifact;
2498 }
2499 }
2500 return new AR();
2501 }
2502
2503 private abstract static class DependencyResolverMock implements DependencyResolver
2504 {
2505 @Override
2506 public Iterable<ArtifactResult> resolveDependencies( ProjectBuildingRequest buildingRequest,
2507 DependableCoordinate coordinate,
2508 TransformableFilter filter )
2509 {
2510 fail( "unexpected call of DependencyResolver" );
2511 return null;
2512 }
2513
2514 @Override
2515 public Iterable<ArtifactResult> resolveDependencies( ProjectBuildingRequest buildingRequest, Model model,
2516 TransformableFilter filter )
2517 {
2518 fail( "unexpected call of DependencyResolver" );
2519 return null;
2520 }
2521 }
2522 }