/
AbstractSurefireMojoTest.java
2689 lines (2206 loc) · 120 KB
/
AbstractSurefireMojoTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package org.apache.maven.plugin.surefire;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.apache.commons.io.FileUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.artifact.handler.ArtifactHandler;
import org.apache.maven.artifact.handler.DefaultArtifactHandler;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.plugin.surefire.AbstractSurefireMojo.JUnitPlatformProviderInfo;
import org.apache.maven.plugin.surefire.booterclient.Platform;
import org.apache.maven.plugin.surefire.log.PluginConsoleLogger;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.repository.RepositorySystem;
import org.apache.maven.surefire.api.suite.RunResult;
import org.apache.maven.surefire.api.util.DefaultScanResult;
import org.apache.maven.surefire.booter.ClassLoaderConfiguration;
import org.apache.maven.surefire.booter.Classpath;
import org.apache.maven.surefire.booter.ModularClasspathConfiguration;
import org.apache.maven.surefire.booter.StartupConfiguration;
import org.apache.maven.surefire.extensions.ForkNodeFactory;
import org.apache.maven.surefire.providerapi.ProviderInfo;
import org.apache.maven.toolchain.Toolchain;
import org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor;
import org.codehaus.plexus.languages.java.jpms.LocationManager;
import org.codehaus.plexus.languages.java.jpms.ResolvePathRequest;
import org.codehaus.plexus.languages.java.jpms.ResolvePathResult;
import org.codehaus.plexus.languages.java.jpms.ResolvePathsRequest;
import org.codehaus.plexus.languages.java.jpms.ResolvePathsResult;
import org.codehaus.plexus.logging.Logger;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static java.io.File.separatorChar;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.nio.file.Files.write;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static java.util.Collections.singleton;
import static java.util.Collections.singletonList;
import static org.apache.maven.artifact.versioning.VersionRange.createFromVersion;
import static org.apache.maven.artifact.versioning.VersionRange.createFromVersionSpec;
import static org.apache.maven.surefire.shared.lang3.JavaVersion.JAVA_9;
import static org.apache.maven.surefire.shared.lang3.JavaVersion.JAVA_RECENT;
import static org.apache.maven.surefire.shared.lang3.SystemUtils.IS_OS_WINDOWS;
import static org.assertj.core.api.Assertions.assertThat;
import static org.codehaus.plexus.languages.java.jpms.ModuleNameSource.MODULEDESCRIPTOR;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.doNothing;
import static org.powermock.api.mockito.PowerMockito.doReturn;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.spy;
import static org.powermock.api.mockito.PowerMockito.verifyPrivate;
import static org.powermock.reflect.Whitebox.invokeMethod;
import static org.powermock.reflect.Whitebox.setInternalState;
/**
* Test for {@link AbstractSurefireMojo}.
*/
@RunWith( PowerMockRunner.class )
@PrepareForTest( AbstractSurefireMojo.class )
@PowerMockIgnore( {"org.jacoco.agent.rt.*", "com.vladium.emma.rt.*"} )
public class AbstractSurefireMojoTest
{
@Rule
public final ExpectedException e = ExpectedException.none();
@Rule
public final TemporaryFolder tempFolder = new TemporaryFolder();
@Mock
private ArtifactHandler handler;
private final Mojo mojo = new Mojo();
@Before
public void setupMojo()
{
Artifact mojoArtifact = mojo.getMojoArtifact();
mojo.setPluginArtifactMap( new LinkedHashMap<String, Artifact>() );
mojo.getPluginArtifactMap().put( mojoArtifact.getGroupId() + ":" + mojoArtifact.getArtifactId(), mojoArtifact );
Artifact forkedBooter = new DefaultArtifact( "org.apache.maven.surefire", "surefire-booter",
mojoArtifact.getVersion(), null, "jar", null, mock( ArtifactHandler.class ) );
mojo.getPluginArtifactMap().put( "org.apache.maven.surefire:surefire-booter", forkedBooter );
mojo.setProjectArtifactMap( new LinkedHashMap<String, Artifact>() );
MavenSession session = mock( MavenSession.class );
mojo.setSession( session );
PluginDescriptor pluginDescriptor = mock( PluginDescriptor.class );
Plugin plugin = new Plugin();
plugin.setGroupId( mojoArtifact.getGroupId() );
plugin.setArtifactId( mojoArtifact.getArtifactId() );
plugin.setVersion( mojoArtifact.getVersion() );
when( pluginDescriptor.getPlugin() ).thenReturn( plugin );
mojo.setPluginDescriptor( pluginDescriptor );
}
@Test
public void noModuleDescriptorFile() throws Exception
{
AbstractSurefireMojo mojo = spy( new Mojo() );
mojo.setMainBuildPath( tempFolder.newFolder() );
File testClassesDir = tempFolder.newFolder();
mojo.setTestClassesDirectory( testClassesDir );
File jdkHome = new File( System.getProperty( "java.home" ) );
ResolvePathResultWrapper wrapper = invokeMethod( mojo, "findModuleDescriptor", jdkHome );
assertThat( wrapper )
.isNotNull();
assertThat( wrapper.getResolvePathResult() )
.isNull();
assertThat( (boolean) invokeMethod( mojo, "existsModuleDescriptor", wrapper ) )
.isEqualTo( false );
when( mojo.useModulePath() ).thenReturn( true );
File jvmExecutable = new File( jdkHome, IS_OS_WINDOWS ? "bin\\java.exe" : "bin/java" );
JdkAttributes jdkAttributes = new JdkAttributes( jvmExecutable, jdkHome, true );
Platform platform = new Platform().withJdkExecAttributesForTests( jdkAttributes );
assertThat( (boolean) invokeMethod( mojo, "canExecuteProviderWithModularPath", platform, wrapper ) )
.isEqualTo( false );
}
@Test
public void correctModuleDescriptor() throws Exception
{
AbstractSurefireMojo mojo = spy( new Mojo() );
LocationManager locationManager = mock( LocationManager.class );
ResolvePathResult result = mock( ResolvePathResult.class );
when( result.getModuleNameSource() ).thenReturn( MODULEDESCRIPTOR );
JavaModuleDescriptor descriptor = mock( JavaModuleDescriptor.class );
when( result.getModuleDescriptor() ).thenReturn( descriptor );
when( locationManager.resolvePath( any( ResolvePathRequest.class ) ) ).thenReturn( result );
doReturn( locationManager )
.when( mojo, "getLocationManager" );
File classesDir = tempFolder.newFolder();
mojo.setMainBuildPath( classesDir );
File testClassesDir = tempFolder.newFolder();
mojo.setTestClassesDirectory( testClassesDir );
File descriptorFile = new File( classesDir, "module-info.class" );
assertThat( descriptorFile.createNewFile() ).isTrue();
File jdkHome = new File( System.getProperty( "java.home" ) );
ResolvePathResultWrapper wrapper = invokeMethod( mojo, "findModuleDescriptor", jdkHome );
assertThat( wrapper )
.isNotNull();
assertThat( wrapper.getResolvePathResult() )
.isSameAs( result );
assertThat( wrapper.getResolvePathResult().getModuleNameSource() )
.isSameAs( MODULEDESCRIPTOR );
assertThat( wrapper.getResolvePathResult().getModuleDescriptor() )
.isSameAs( descriptor );
assertThat( (boolean) invokeMethod( mojo, "existsModuleDescriptor", wrapper ) )
.isEqualTo( true );
when( mojo.useModulePath() ).thenReturn( true );
File jvmExecutable = new File( jdkHome, IS_OS_WINDOWS ? "bin\\java.exe" : "bin/java" );
JdkAttributes jdkAttributes = new JdkAttributes( jvmExecutable, jdkHome, true );
Platform platform = new Platform().withJdkExecAttributesForTests( jdkAttributes );
assertThat( (boolean) invokeMethod( mojo, "canExecuteProviderWithModularPath", platform, wrapper ) )
.isEqualTo( true );
jdkAttributes = new JdkAttributes( jvmExecutable, jdkHome, false );
platform = new Platform().withJdkExecAttributesForTests( jdkAttributes );
assertThat( (boolean) invokeMethod( mojo, "canExecuteProviderWithModularPath", platform, wrapper ) )
.isEqualTo( false );
when( mojo.useModulePath() ).thenReturn( false );
jdkAttributes = new JdkAttributes( jvmExecutable, jdkHome, true );
platform = new Platform().withJdkExecAttributesForTests( jdkAttributes );
assertThat( (boolean) invokeMethod( mojo, "canExecuteProviderWithModularPath", platform, wrapper ) )
.isEqualTo( false );
}
@Test
@SuppressWarnings( "checkstyle:magicnumber" )
public void corruptedModuleDescriptor() throws Exception
{
if ( !JAVA_RECENT.atLeast( JAVA_9 ) )
{
return;
}
AbstractSurefireMojo mojo = spy( new Mojo() );
doReturn( new LocationManager() )
.when( mojo, "getLocationManager" );
File classesDir = tempFolder.newFolder();
mojo.setMainBuildPath( classesDir );
File testClassesDir = tempFolder.newFolder();
mojo.setTestClassesDirectory( testClassesDir );
File descriptorFile = new File( classesDir, "module-info.class" );
assertThat( descriptorFile.createNewFile() ).isTrue();
write( descriptorFile.toPath(), new byte[] {(byte) 0xCA, (byte) 0xFE, (byte) 0xBA, (byte) 0xBE} );
File jdkHome = new File( System.getProperty( "java.home" ) );
ResolvePathResultWrapper wrapper = invokeMethod( mojo, "findModuleDescriptor", jdkHome );
assertThat( wrapper )
.isNotNull();
assertThat( wrapper.getResolvePathResult() )
.isNull();
assertThat( (boolean) invokeMethod( mojo, "existsModuleDescriptor", wrapper ) )
.isEqualTo( false );
}
@Test
public void shouldShowArray() throws Exception
{
Logger logger = mock( Logger.class );
when( logger.isDebugEnabled() ).thenReturn( true );
doNothing().when( logger ).debug( anyString() );
AbstractSurefireMojo mojo = spy( this.mojo );
when( mojo.getConsoleLogger() ).thenReturn( new PluginConsoleLogger( logger ) );
Object[] array = {"ABC", "XYZ"};
invokeMethod( mojo, "showArray", array, "prefix" );
ArgumentCaptor<String> argument = ArgumentCaptor.forClass( String.class );
verify( logger, times( 2 ) ).debug( argument.capture() );
assertThat( argument.getAllValues() )
.containsExactly( "Setting prefix [ABC]", "Setting prefix [XYZ]" );
}
@Test
public void shouldShowMap() throws Exception
{
Logger logger = mock( Logger.class );
when( logger.isDebugEnabled() ).thenReturn( true );
doNothing().when( logger ).debug( anyString() );
AbstractSurefireMojo mojo = spy( this.mojo );
when( mojo.getConsoleLogger() ).thenReturn( new PluginConsoleLogger( logger ) );
Map<String, String> map = new LinkedHashMap<>();
map.put( "ABC", "123" );
map.put( "XYZ", "987" );
invokeMethod( mojo, "showMap", map, "prefix" );
ArgumentCaptor<String> argument = ArgumentCaptor.forClass( String.class );
verify( logger, times( 2 ) ).debug( argument.capture() );
assertThat( argument.getAllValues() )
.containsExactly( "Setting prefix [ABC]=[123]", "Setting prefix [XYZ]=[987]" );
}
@Test
public void shouldRetainInPluginArtifacts() throws Exception
{
Artifact provider = new DefaultArtifact( "g", "a", createFromVersionSpec( "1" ), "compile", "jar", "", null );
Artifact common = new DefaultArtifact( "g", "c", createFromVersionSpec( "1" ), "compile", "jar", "", null );
Artifact api = new DefaultArtifact( "g", "a", createFromVersionSpec( "1" ), "compile", "jar", "", null );
Set<Artifact> providerArtifacts = singleton( provider );
Artifact[] inPluginArtifacts = {common, api};
Set<Artifact> inPluginClasspath = invokeMethod( AbstractSurefireMojo.class,
"retainInProcArtifactsUnique", providerArtifacts, inPluginArtifacts );
assertThat( inPluginClasspath )
.containsOnly( common );
}
@Test
public void shouldRetainInProcArtifactsUnique() throws Exception
{
Artifact provider = new DefaultArtifact( "g", "p", createFromVersionSpec( "1" ), "compile", "jar", "", null );
Artifact common = new DefaultArtifact( "g", "c", createFromVersionSpec( "1" ), "compile", "jar", "", null );
Artifact api = new DefaultArtifact( "g", "a", createFromVersionSpec( "1" ), "compile", "jar", "", null );
Set<Artifact> providerArtifacts = singleton( provider );
Artifact[] inPluginArtifacts = {common, api};
Set<Artifact> inPluginClasspath = invokeMethod( AbstractSurefireMojo.class,
"retainInProcArtifactsUnique", providerArtifacts, inPluginArtifacts );
assertThat( inPluginClasspath )
.containsOnly( common, api );
}
@Test
public void shouldCreateInProcClasspath() throws Exception
{
Artifact provider = new DefaultArtifact( "g", "p", createFromVersionSpec( "1" ), "compile", "jar", "", null );
provider.setFile( mockFile( "provider.jar" ) );
Artifact common = new DefaultArtifact( "g", "c", createFromVersionSpec( "1" ), "compile", "jar", "", null );
common.setFile( mockFile( "maven-surefire-common.jar" ) );
Artifact api = new DefaultArtifact( "g", "a", createFromVersionSpec( "1" ), "compile", "jar", "", null );
api.setFile( mockFile( "surefire-api.jar" ) );
Set<Artifact> newArtifacts = new LinkedHashSet<>();
newArtifacts.add( common );
newArtifacts.add( api );
Classpath providerClasspath = new Classpath( singleton( provider.getFile().getAbsolutePath() ) );
Classpath inPluginClasspath = invokeMethod( AbstractSurefireMojo.class,
"createInProcClasspath", providerClasspath, newArtifacts );
Classpath expectedClasspath =
new Classpath( asList( provider.getFile().getAbsolutePath(),
common.getFile().getAbsolutePath(),
api.getFile().getAbsolutePath() ) );
assertThat( (Object) inPluginClasspath )
.isEqualTo( expectedClasspath );
}
@Test
public void shouldGenerateTestClasspath() throws Exception
{
AbstractSurefireMojo mojo = spy( this.mojo );
when( mojo.getMainBuildPath() ).thenReturn( new File( "target" + separatorChar + "classes" ) );
when( mojo.getTestClassesDirectory() ).thenReturn( new File( "target" + separatorChar + "test-classes" ) );
when( mojo.getClasspathDependencyScopeExclude() ).thenReturn( "runtime" );
when( mojo.getClasspathDependencyExcludes() ).thenReturn( new String[] {"g3:a3"} );
doReturn( mock( Artifact.class ) ).when( mojo, "getTestNgArtifact" );
Set<Artifact> artifacts = new HashSet<>();
Artifact a1 = mock( Artifact.class );
when( a1.getGroupId() ).thenReturn( "g1" );
when( a1.getArtifactId() ).thenReturn( "a1" );
when( a1.getVersion() ).thenReturn( "1" );
when( a1.getScope() ).thenReturn( "runtime" );
when( a1.getDependencyConflictId() ).thenReturn( "g1:a1:jar" );
when( a1.getId() ).thenReturn( "g1:a1:jar:1" );
artifacts.add( a1 );
ArtifactHandler artifactHandler = mock( ArtifactHandler.class );
when( artifactHandler.isAddedToClasspath() ).thenReturn( true );
Artifact a2 = mock( Artifact.class );
when( a2.getGroupId() ).thenReturn( "g2" );
when( a2.getArtifactId() ).thenReturn( "a2" );
when( a2.getVersion() ).thenReturn( "2" );
when( a2.getScope() ).thenReturn( "test" );
when( a2.getDependencyConflictId() ).thenReturn( "g2:a2:jar" );
when( a2.getId() ).thenReturn( "g2:a2:jar:2" );
when( a2.getFile() ).thenReturn( new File( "a2-2.jar" ) );
when( a2.getArtifactHandler() ).thenReturn( artifactHandler );
artifacts.add( a2 );
Artifact a3 = mock( Artifact.class );
when( a3.getGroupId() ).thenReturn( "g3" );
when( a3.getArtifactId() ).thenReturn( "a3" );
when( a3.getVersion() ).thenReturn( "3" );
when( a3.getScope() ).thenReturn( "test" );
when( a3.getDependencyConflictId() ).thenReturn( "g3:a3:jar" );
when( a3.getId() ).thenReturn( "g3:a3:jar:3" );
when( a3.getFile() ).thenReturn( new File( "a3-3.jar" ) );
when( a3.getArtifactHandler() ).thenReturn( artifactHandler );
artifacts.add( a3 );
MavenProject project = mock( MavenProject.class );
when( project.getArtifacts() ).thenReturn( artifacts );
when( mojo.getProject() ).thenReturn( project );
TestClassPath cp = invokeMethod( mojo, "generateTestClasspath" );
verifyPrivate( mojo, times( 1 ) ).invoke( "generateTestClasspath" );
verify( mojo, times( 1 ) ).getMainBuildPath();
verify( mojo, times( 1 ) ).getTestClassesDirectory();
verify( mojo, times( 3 ) ).getClasspathDependencyScopeExclude();
verify( mojo, times( 2 ) ).getClasspathDependencyExcludes();
verify( mojo, times( 1 ) ).getAdditionalClasspathElements();
assertThat( cp.toClasspath().getClassPath() ).hasSize( 3 );
assertThat( cp.toClasspath().getClassPath().get( 0 ) ).endsWith( "test-classes" );
assertThat( cp.toClasspath().getClassPath().get( 1 ) ).endsWith( "classes" );
assertThat( cp.toClasspath().getClassPath().get( 2 ) ).endsWith( "a2-2.jar" );
}
@Test
@SuppressWarnings( "checkstyle:linelength" )
public void shouldHaveStartupConfigForNonModularClasspath()
throws Exception
{
AbstractSurefireMojo mojo = spy( this.mojo );
Artifact common = new DefaultArtifact( "org.apache.maven.surefire", "maven-surefire-common",
createFromVersion( "1" ), "runtime", "jar", "", handler );
common.setFile( mockFile( "maven-surefire-common.jar" ) );
Artifact ext = new DefaultArtifact( "org.apache.maven.surefire", "surefire-extensions-api",
createFromVersion( "1" ), "runtime", "jar", "", handler );
ext.setFile( mockFile( "surefire-extensions-api.jar" ) );
Artifact api = new DefaultArtifact( "org.apache.maven.surefire", "surefire-api",
createFromVersion( "1" ), "runtime", "jar", "", handler );
api.setFile( mockFile( "surefire-api.jar" ) );
Artifact loggerApi = new DefaultArtifact( "org.apache.maven.surefire", "surefire-logger-api",
createFromVersion( "1" ), "runtime", "jar", "", handler );
loggerApi.setFile( mockFile( "surefire-logger-api.jar" ) );
Artifact spi = new DefaultArtifact( "org.apache.maven.surefire", "surefire-extensions-spi",
createFromVersion( "1" ), "runtime", "jar", "", handler );
spi.setFile( mockFile( "surefire-extensions-spi.jar" ) );
Artifact booter = new DefaultArtifact( "org.apache.maven.surefire", "surefire-booter",
createFromVersion( "1" ), "runtime", "jar", "", handler );
booter.setFile( mockFile( "surefire-booter.jar" ) );
Artifact utils = new DefaultArtifact( "org.apache.maven.surefire", "surefire-shared-utils",
createFromVersion( "1" ), "runtime", "jar", "", handler );
utils.setFile( mockFile( "surefire-shared-utils.jar" ) );
Map<String, Artifact> providerArtifactsMap = new HashMap<>();
providerArtifactsMap.put( "org.apache.maven.surefire:maven-surefire-common", common );
providerArtifactsMap.put( "org.apache.maven.surefire:surefire-extensions-api", ext );
providerArtifactsMap.put( "org.apache.maven.surefire:surefire-api", api );
providerArtifactsMap.put( "org.apache.maven.surefire:surefire-logger-api", loggerApi );
providerArtifactsMap.put( "org.apache.maven.surefire:surefire-extensions-spi", spi );
providerArtifactsMap.put( "org.apache.maven.surefire:surefire-booter", booter );
providerArtifactsMap.put( "org.apache.maven.surefire:surefire-shared-utils", utils );
when( mojo.getPluginArtifactMap() )
.thenReturn( providerArtifactsMap );
when( handler.isAddedToClasspath() ).thenReturn( true );
VersionRange v1 = createFromVersion( "4.12" );
Artifact junit = new DefaultArtifact( "junit", "junit", v1, "test", "jar", "", handler );
junit.setFile( mockFile( "junit.jar" ) );
VersionRange v2 = createFromVersion( "1.3.0" );
Artifact hamcrest = new DefaultArtifact( "org.hamcrest", "hamcrest-core", v2, "test", "jar", "", handler );
hamcrest.setFile( mockFile( "hamcrest.jar" ) );
File classesDir = mockFile( "classes" );
File testClassesDir = mockFile( "test-classes" );
TestClassPath testClasspath =
new TestClassPath( asList( junit, hamcrest ), classesDir, testClassesDir, null );
doReturn( testClasspath ).when( mojo, "generateTestClasspath" );
doReturn( 1 ).when( mojo, "getEffectiveForkCount" );
doReturn( true ).when( mojo, "effectiveIsEnableAssertions" );
when( mojo.isChildDelegation() ).thenReturn( false );
ClassLoaderConfiguration classLoaderConfiguration = new ClassLoaderConfiguration( false, true );
VersionRange v3 = createFromVersion( "1" );
Artifact provider = new DefaultArtifact( "x", "surefire-provider", v3, "runtime", "jar", "", handler );
provider.setFile( mockFile( "surefire-provider.jar" ) );
Set<Artifact> providerArtifacts = singleton( provider );
Logger logger = mock( Logger.class );
when( logger.isDebugEnabled() ).thenReturn( true );
doNothing().when( logger ).debug( anyString() );
when( mojo.getConsoleLogger() ).thenReturn( new PluginConsoleLogger( logger ) );
ProviderInfo providerInfo = mock( ProviderInfo.class );
when( providerInfo.getProviderName() ).thenReturn( "org.asf.Provider" );
when( providerInfo.getProviderClasspath() ).thenReturn( providerArtifacts );
StartupConfiguration conf = invokeMethod( mojo, "newStartupConfigWithClasspath",
classLoaderConfiguration, providerInfo, testClasspath );
verify( mojo, times( 1 ) ).effectiveIsEnableAssertions();
verify( mojo, times( 1 ) ).isChildDelegation();
ArgumentCaptor<String> argument = ArgumentCaptor.forClass( String.class );
verify( logger, times( 6 ) ).debug( argument.capture() );
assertThat( argument.getAllValues() )
.containsExactly( "test classpath: test-classes classes junit.jar hamcrest.jar",
"provider classpath: surefire-provider.jar",
"test(compact) classpath: test-classes classes junit.jar hamcrest.jar",
"provider(compact) classpath: surefire-provider.jar",
"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",
"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"
);
assertThat( conf.getClassLoaderConfiguration() )
.isSameAs( classLoaderConfiguration );
assertThat( (Object) conf.getClasspathConfiguration().getTestClasspath() )
.isEqualTo( testClasspath.toClasspath() );
Collection<String> files = new ArrayList<>();
for ( Artifact providerArtifact : providerArtifacts )
{
files.add( providerArtifact.getFile().getAbsolutePath() );
}
assertThat( (Object) conf.getClasspathConfiguration().getProviderClasspath() )
.isEqualTo( new Classpath( files ) );
assertThat( (Object) conf.getClasspathConfiguration().isClassPathConfig() )
.isEqualTo( true );
assertThat( (Object) conf.getClasspathConfiguration().isModularPathConfig() )
.isEqualTo( false );
assertThat( (Object) conf.getClasspathConfiguration().isEnableAssertions() )
.isEqualTo( true );
assertThat( conf.getProviderClassName() )
.isEqualTo( "org.asf.Provider" );
}
@Test
public void providerClasspathCachingIsNotSharedAcrossMojoInstances() throws Exception
{
ProviderInfo providerInfo = mock( ProviderInfo.class );
when( providerInfo.getProviderName() ).thenReturn( "test-provider" );
Artifact provider = new DefaultArtifact( "com.example", "provider", createFromVersion( "1" ), "runtime",
"jar", "", handler );
provider.setFile( mockFile( "original-test-provider.jar" ) );
Set<Artifact> providerClasspath = singleton( provider );
when( providerInfo.getProviderClasspath() ).thenReturn( providerClasspath );
StartupConfiguration startupConfiguration = startupConfigurationForProvider( providerInfo );
assertThat( startupConfiguration.getClasspathConfiguration().getProviderClasspath().getClassPath() )
.containsExactly( "original-test-provider.jar" );
provider.setFile( mockFile( "modified-test-provider.jar" ) );
startupConfiguration = startupConfigurationForProvider( providerInfo );
assertThat( startupConfiguration.getClasspathConfiguration().getProviderClasspath().getClassPath() )
.containsExactly( "modified-test-provider.jar" );
}
private StartupConfiguration startupConfigurationForProvider( ProviderInfo providerInfo ) throws Exception
{
AbstractSurefireMojo mojo = spy( new Mojo() );
Logger logger = mock( Logger.class );
when( logger.isDebugEnabled() ).thenReturn( true );
doNothing().when( logger ).debug( anyString() );
when( mojo.getConsoleLogger() ).thenReturn( new PluginConsoleLogger( logger ) );
File classesDir = mockFile( "classes" );
File testClassesDir = mockFile( "test-classes" );
TestClassPath testClassPath =
new TestClassPath( new ArrayList<Artifact>(), classesDir, testClassesDir, new String[0] );
Artifact common = new DefaultArtifact( "org.apache.maven.surefire", "maven-surefire-common",
createFromVersion( "1" ), "runtime", "jar", "", handler );
common.setFile( mockFile( "maven-surefire-common.jar" ) );
Artifact ext = new DefaultArtifact( "org.apache.maven.surefire", "surefire-extensions-api",
createFromVersion( "1" ), "runtime", "jar", "", handler );
ext.setFile( mockFile( "surefire-extensions-api.jar" ) );
Artifact api = new DefaultArtifact( "org.apache.maven.surefire", "surefire-api",
createFromVersion( "1" ), "runtime", "jar", "", handler );
api.setFile( mockFile( "surefire-api.jar" ) );
Artifact loggerApi = new DefaultArtifact( "org.apache.maven.surefire", "surefire-logger-api",
createFromVersion( "1" ), "runtime", "jar", "", handler );
loggerApi.setFile( mockFile( "surefire-logger-api.jar" ) );
Artifact spi = new DefaultArtifact( "org.apache.maven.surefire", "surefire-extensions-spi",
createFromVersion( "1" ), "runtime", "jar", "", handler );
spi.setFile( mockFile( "surefire-extensions-spi.jar" ) );
Artifact booter = new DefaultArtifact( "org.apache.maven.surefire", "surefire-booter",
createFromVersion( "1" ), "runtime", "jar", "", handler );
booter.setFile( mockFile( "surefire-booter.jar" ) );
Artifact utils = new DefaultArtifact( "org.apache.maven.surefire", "surefire-shared-utils",
createFromVersion( "1" ), "runtime", "jar", "", handler );
utils.setFile( mockFile( "surefire-shared-utils.jar" ) );
Map<String, Artifact> providerArtifactsMap = new HashMap<>();
providerArtifactsMap.put( "org.apache.maven.surefire:maven-surefire-common", common );
providerArtifactsMap.put( "org.apache.maven.surefire:surefire-extensions-api", ext );
providerArtifactsMap.put( "org.apache.maven.surefire:surefire-api", api );
providerArtifactsMap.put( "org.apache.maven.surefire:surefire-logger-api", loggerApi );
providerArtifactsMap.put( "org.apache.maven.surefire:surefire-extensions-spi", spi );
providerArtifactsMap.put( "org.apache.maven.surefire:surefire-booter", booter );
providerArtifactsMap.put( "org.apache.maven.surefire:surefire-shared-utils", utils );
when( mojo.getPluginArtifactMap() ).thenReturn( providerArtifactsMap );
doReturn( 1 ).when( mojo, "getEffectiveForkCount" );
return invokeMethod( mojo, "createStartupConfiguration",
providerInfo, false, null, null, testClassPath, null, null );
}
@Test
public void shouldCreateStartupConfigWithModularPath() throws Exception
{
String baseDir = System.getProperty( "user.dir" );
Mojo mojo = new Mojo();
// ### BEGIN
// we cannot mock private method newStartupConfigWithModularPath() - mocking the data to prevent from errors
LocationManager locationManager = mock( LocationManager.class );
ResolvePathsResult resolvePathsResult = mock( ResolvePathsResult.class );
when( locationManager.resolvePaths( any( ResolvePathsRequest.class ) ) ).thenReturn( resolvePathsResult );
when( resolvePathsResult.getPathExceptions() ).thenReturn( emptyMap() );
when( resolvePathsResult.getClasspathElements() ).thenReturn( emptyList() );
when( resolvePathsResult.getModulepathElements() ).thenReturn( emptyMap() );
mojo.setLogger( mock( Logger.class ) );
mojo.setUseModulePath( true );
setInternalState( mojo, "locationManager", locationManager );
File jdkHome = new File( System.getProperty( "java.home" ) );
File jvmExecutable = new File( jdkHome, IS_OS_WINDOWS ? "bin\\java.exe" : "bin/java" );
JdkAttributes jdkAttributes = new JdkAttributes( jvmExecutable, jdkHome, true );
Platform platform = new Platform().withJdkExecAttributesForTests( jdkAttributes );
File classesDirectory = new File( baseDir, "mock-dir" );
File testClassesDirectory = new File( baseDir, "mock-dir" );
mojo.setTestClassesDirectory( testClassesDirectory );
TestClassPath testClassPath = new TestClassPath( Collections.<Artifact>emptySet(),
classesDirectory, testClassesDirectory, new String[0] );
ProviderInfo providerInfo = mock( ProviderInfo.class );
when( providerInfo.getProviderName() ).thenReturn( "provider mock" );
when( providerInfo.getProviderClasspath() ).thenReturn( Collections.<Artifact>emptySet() );
DefaultScanResult defaultScanResult = mock( DefaultScanResult.class );
when( defaultScanResult.getClasses() ).thenReturn( Collections.<String>emptyList() );
Path pathToModularDescriptor =
Paths.get( baseDir, "src", "test", "resources", "org", "apache", "maven", "plugin", "surefire" );
mojo.setMainBuildPath( pathToModularDescriptor.toFile() );
Map<String, Artifact> artifacts = new HashMap<>();
Artifact dummyArtifact = mock( Artifact.class );
when( dummyArtifact.getFile() ).thenReturn( new File( baseDir, "mock-file" ) );
artifacts.put( "org.apache.maven.surefire:maven-surefire-common", dummyArtifact );
artifacts.put( "org.apache.maven.surefire:surefire-extensions-api", dummyArtifact );
artifacts.put( "org.apache.maven.surefire:surefire-api", dummyArtifact );
artifacts.put( "org.apache.maven.surefire:surefire-logger-api", dummyArtifact );
artifacts.put( "org.apache.maven.surefire:surefire-extensions-spi", dummyArtifact );
artifacts.put( "org.apache.maven.surefire:surefire-booter", dummyArtifact );
artifacts.put( "org.apache.maven.surefire:surefire-shared-utils", dummyArtifact );
mojo.setPluginArtifactMap( artifacts );
ResolvePathResult resolvePathResult = mock( ResolvePathResult.class );
JavaModuleDescriptor desc = mock( JavaModuleDescriptor.class );
when( desc.name() ).thenReturn( "" );
when( resolvePathResult.getModuleDescriptor() ).thenReturn( desc );
ResolvePathResultWrapper wrapper = new ResolvePathResultWrapper( resolvePathResult, true );
// ### END
StartupConfiguration actualConfig = invokeMethod( mojo, "createStartupConfiguration",
new Class[] {ProviderInfo.class, boolean.class, ClassLoaderConfiguration.class, DefaultScanResult.class,
TestClassPath.class, Platform.class, ResolvePathResultWrapper.class},
providerInfo, true, mock( ClassLoaderConfiguration.class ), defaultScanResult,
testClassPath, platform, wrapper );
assertThat( actualConfig )
.isNotNull();
assertThat( actualConfig.getClasspathConfiguration() )
.isInstanceOf( ModularClasspathConfiguration.class );
}
@Test
public void shouldExistTmpDirectory() throws IOException
{
String systemTmpDir = System.getProperty( "java.io.tmpdir" );
String usrDir = new File( System.getProperty( "user.dir" ) ).getCanonicalPath();
String tmpDir = "surefireX" + System.currentTimeMillis();
//noinspection ResultOfMethodCallIgnored
new File( systemTmpDir, tmpDir ).delete();
File targetDir = new File( usrDir, "target" );
//noinspection ResultOfMethodCallIgnored
new File( targetDir, tmpDir ).delete();
AbstractSurefireMojo mojo = mock( AbstractSurefireMojo.class );
Logger logger = mock( Logger.class );
when( logger.isDebugEnabled() ).thenReturn( false );
when( logger.isErrorEnabled() ).thenReturn( false );
doNothing().when( logger ).debug( anyString() );
doNothing().when( logger ).error( anyString(), any( Throwable.class ) );
when( mojo.getConsoleLogger() ).thenReturn( new PluginConsoleLogger( logger ) );
when( mojo.getTempDir() ).thenReturn( tmpDir );
when( mojo.getProjectBuildDirectory() ).thenReturn( targetDir );
when( mojo.createSurefireBootDirectoryInTemp() ).thenCallRealMethod();
when( mojo.createSurefireBootDirectoryInBuild() ).thenCallRealMethod();
when( mojo.getSurefireTempDir() ).thenCallRealMethod();
File bootDir = mojo.createSurefireBootDirectoryInTemp();
assertThat( bootDir ).isNotNull();
assertThat( bootDir ).isDirectory();
assertThat( new File( systemTmpDir, bootDir.getName() ) ).isDirectory();
assertThat( bootDir.getName() )
.startsWith( tmpDir );
File buildTmp = mojo.createSurefireBootDirectoryInBuild();
assertThat( buildTmp ).isNotNull();
assertThat( buildTmp ).isDirectory();
assertThat( buildTmp.getParentFile().getCanonicalFile().getParent() ).isEqualTo( usrDir );
assertThat( buildTmp.getName() ).isEqualTo( tmpDir );
File tmp = mojo.getSurefireTempDir();
assertThat( tmp ).isNotNull();
assertThat( tmp ).isDirectory();
assertThat( IS_OS_WINDOWS ? new File( systemTmpDir, bootDir.getName() ) : new File( targetDir, tmpDir ) )
.isDirectory();
}
@Test
public void shouldSmartlyResolveJUnit5ProviderWithJUnit4() throws Exception
{
MavenProject mavenProject = new MavenProject();
mavenProject.setArtifact( new DefaultArtifact( "dummy", "pom", createFromVersion( "1.0.0" ),
null, "jar", null, mock( ArtifactHandler.class ) ) );
mojo.setProject( mavenProject );
final VersionRange surefireVersion = createFromVersion( "1" );
Artifact testClasspathJUnit = new DefaultArtifact( "junit", "junit",
createFromVersion( "4.12" ), null, "jar", null, mock( ArtifactHandler.class ) );
Artifact testClasspathHamcrest = new DefaultArtifact( "org.hamcrest", "hamcrest-core",
createFromVersion( "1.3" ), null, "jar", null, mock( ArtifactHandler.class ) );
setProjectDepedenciesToMojo( testClasspathJUnit, testClasspathHamcrest );
Collection<Artifact> testArtifacts = new ArrayList<>();
testArtifacts.add( testClasspathJUnit );
testArtifacts.add( testClasspathHamcrest );
File classesDirectory = new File( "target/classes" );
File testClassesDirectory = new File( "target/test-classes" );
TestClassPath testClasspathWrapper =
new TestClassPath( testArtifacts, classesDirectory, testClassesDirectory, null );
mojo.setRemoteRepositories( Collections.<ArtifactRepository>emptyList() );
mojo.setProjectRemoteRepositories( Collections.<ArtifactRepository>emptyList() );
RepositorySystem repositorySystem = mock( RepositorySystem.class );
final Artifact surefireProvider = new DefaultArtifact( "org.apache.maven.surefire",
"surefire-junit-platform", surefireVersion, null, "jar", null, mock( ArtifactHandler.class ) );
final Artifact pluginDependency = new DefaultArtifact( "org.junit.vintage", "junit-vintage-engine",
createFromVersion( "5.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
when( repositorySystem.createDependencyArtifact( any( Dependency.class ) ) ).thenAnswer( new Answer<Artifact>()
{
@Override
public Artifact answer( InvocationOnMock invocation )
{
Dependency dependency = (Dependency) invocation.getArguments()[0];
if ( dependency.getArtifactId().equals( "surefire-junit-platform" ) )
{
return surefireProvider;
}
else if ( dependency.getArtifactId().equals( "junit-vintage-engine" ) )
{
return pluginDependency;
}
else
{
fail( dependency.getGroupId() + ":" + dependency.getArtifactId() );
return null;
}
}
} );
final ArtifactResolutionResult surefireProviderResolutionResult = mock( ArtifactResolutionResult.class );
final ArtifactResolutionResult pluginDependencyResolutionResult = mock( ArtifactResolutionResult.class );
when( repositorySystem.resolve( any( ArtifactResolutionRequest.class ) ) )
.thenAnswer( new Answer<ArtifactResolutionResult>()
{
@Override
public ArtifactResolutionResult answer( InvocationOnMock invocation )
{
ArtifactResolutionRequest req = (ArtifactResolutionRequest) invocation.getArguments()[0];
Artifact artifact = req.getArtifact();
if ( artifact == surefireProvider )
{
return surefireProviderResolutionResult;
}
else if ( artifact == pluginDependency )
{
return pluginDependencyResolutionResult;
}
else if ( "org.junit.platform".equals( artifact.getGroupId() )
&& "junit-platform-launcher".equals( artifact.getArtifactId() )
&& "1.4.0".equals( artifact.getVersion() ) )
{
return createExpectedJUnitPlatformLauncherResolutionResult();
}
else
{
fail( artifact.getGroupId() + ":" + artifact.getArtifactId() );
return null;
}
}
} );
Artifact java5 = new DefaultArtifact( "org.apache.maven.surefire", "common-java5",
surefireVersion, null, "jar", null, mock( ArtifactHandler.class ) );
Artifact launcher = new DefaultArtifact( "org.junit.platform", "junit-platform-launcher",
createFromVersion( "1.3.2" ), null, "jar", null, mock( ArtifactHandler.class ) );
Artifact apiguardian = new DefaultArtifact( "org.apiguardian", "apiguardian-api",
createFromVersion( "1.0.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
Artifact engine = new DefaultArtifact( "org.junit.platform", "junit-platform-engine",
createFromVersion( "1.3.2" ), null, "jar", null, mock( ArtifactHandler.class ) );
Artifact commons = new DefaultArtifact( "org.junit.platform", "junit-platform-commons",
createFromVersion( "1.3.2" ), null, "jar", null, mock( ArtifactHandler.class ) );
Artifact opentest4j = new DefaultArtifact( "org.opentest4j", "opentest4j",
createFromVersion( "1.1.1" ), null, "jar", null, mock( ArtifactHandler.class ) );
Set<Artifact> providerArtifacts = new HashSet<>();
providerArtifacts.add( surefireProvider );
providerArtifacts.add( java5 );
providerArtifacts.add( launcher );
providerArtifacts.add( apiguardian );
providerArtifacts.add( engine );
providerArtifacts.add( commons );
providerArtifacts.add( opentest4j );
when( surefireProviderResolutionResult.getArtifacts() )
.thenReturn( providerArtifacts );
final Artifact pluginDep1 = new DefaultArtifact( "org.junit.vintage", "junit-vintage-engine",
createFromVersion( "5.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
final Artifact pluginDep2 = new DefaultArtifact( "org.apiguardian", "apiguardian-api",
createFromVersion( "1.0.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
final Artifact pluginDep3 = new DefaultArtifact( "org.junit.platform", "junit-platform-engine",
createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
final Artifact pluginDep4 = new DefaultArtifact( "junit", "junit",
createFromVersion( "4.12" ), null, "jar", null, mock( ArtifactHandler.class ) );
final Artifact pluginDep5 = new DefaultArtifact( "org.hamcrest", "hamcrest-core",
createFromVersion( "1.3" ), null, "jar", null, mock( ArtifactHandler.class ) );
final Artifact pluginDep6 = new DefaultArtifact( "org.opentest4j", "opentest4j",
createFromVersion( "1.1.1" ), null, "jar", null, mock( ArtifactHandler.class ) );
final Artifact pluginDep7 = new DefaultArtifact( "org.junit.platform", "junit-platform-commons",
createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
addPluginDependencies( pluginDep1, pluginDep2, pluginDep3, pluginDep4, pluginDep5, pluginDep6, pluginDep7 );
mojo.setRepositorySystem( repositorySystem );
mojo.setLogger( mock( Logger.class ) );
Set<Artifact> pluginDependencyArtifacts = new HashSet<>();
pluginDependencyArtifacts.add( pluginDep1 );
pluginDependencyArtifacts.add( pluginDep2 );
pluginDependencyArtifacts.add( pluginDep3 );
pluginDependencyArtifacts.add( pluginDep4 );
pluginDependencyArtifacts.add( pluginDep5 );
pluginDependencyArtifacts.add( pluginDep6 );
pluginDependencyArtifacts.add( pluginDep7 );
when( pluginDependencyResolutionResult.getArtifacts() ).thenReturn( pluginDependencyArtifacts );
invokeMethod( mojo, "setupStuff" );
when( mojo.getSession().getProjectBuildingRequest() )
.thenReturn( mock( ProjectBuildingRequest.class ) );
PluginDescriptor pluginDescriptor = mock( PluginDescriptor.class );
mojo.setPluginDescriptor( pluginDescriptor );
Plugin p = mock( Plugin.class );
when( pluginDescriptor.getPlugin() )
.thenReturn( p );
when( p.getDependencies() )
.thenReturn( singletonList( toDependency( pluginDependency ) ) );
Artifact junitPlatformArtifact = invokeMethod( mojo, "getJUnit5Artifact" );
assertThat( junitPlatformArtifact.getGroupId() ).isEqualTo( "org.junit.platform" );
assertThat( junitPlatformArtifact.getArtifactId() ).isEqualTo( "junit-platform-engine" );
assertThat( junitPlatformArtifact.getVersion() ).isEqualTo( "1.4.0" );
JUnitPlatformProviderInfo prov =
mojo.createJUnitPlatformProviderInfo( junitPlatformArtifact, testClasspathWrapper );
assertThat( prov.isApplicable() )
.isTrue();
Artifact expectedProvider = new DefaultArtifact( "org.apache.maven.surefire", "surefire-junit-platform",
surefireVersion, null, "jar", null, mock( ArtifactHandler.class ) );
Artifact expectedCommonJava5 = new DefaultArtifact( "org.apache.maven.surefire", "common-java5",
surefireVersion, null, "jar", null, mock( ArtifactHandler.class ) );
Artifact expectedLauncher = new DefaultArtifact( "org.junit.platform", "junit-platform-launcher",
createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
Artifact expectedApiguardian = new DefaultArtifact( "org.apiguardian", "apiguardian-api",
createFromVersion( "1.0.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
Artifact expectedJUnit5Engine = new DefaultArtifact( "org.junit.platform", "junit-platform-engine",
createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
Artifact expectedOpentest4j = new DefaultArtifact( "org.opentest4j", "opentest4j",
createFromVersion( "1.1.1" ), null, "jar", null, mock( ArtifactHandler.class ) );
Artifact expectedPlatformCommons = new DefaultArtifact( "org.junit.platform", "junit-platform-commons",
createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
Artifact expectedEngine = new DefaultArtifact( "org.junit.vintage", "junit-vintage-engine",
createFromVersion( "5.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
assertThat( prov.getProviderClasspath() )
.hasSize( 8 )
.containsOnly( expectedProvider, expectedCommonJava5, expectedLauncher, expectedApiguardian,
expectedJUnit5Engine, expectedOpentest4j, expectedPlatformCommons, expectedEngine );
assertThat( testClasspathWrapper.getTestDependencies() )
.hasSize( 2 )
.containsEntry( "junit:junit", testClasspathJUnit )
.containsEntry( "org.hamcrest:hamcrest-core", testClasspathHamcrest );
}
@Test
public void shouldSmartlyResolveJUnit5ProviderWithVintage() throws Exception
{
MavenProject mavenProject = new MavenProject();
mavenProject.setArtifact( new DefaultArtifact( "dummy", "pom", createFromVersion( "1.0.0" ),
null, "jar", null, mock( ArtifactHandler.class ) ) );
mojo.setProject( mavenProject );
VersionRange surefireVersion = createFromVersion( "1" );
Artifact testClasspathSomeTestArtifact = new DefaultArtifact( "third.party", "artifact",
createFromVersion( "1.0" ), null, "jar", null, mock( ArtifactHandler.class ) );
Artifact testClasspathVintage = new DefaultArtifact( "org.junit.vintage", "junit-vintage-engine",
createFromVersion( "5.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );