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.factory.ArtifactFactory;
23 import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
24 import org.apache.maven.artifact.repository.ArtifactRepository;
25 import org.apache.maven.artifact.resolver.ArtifactResolver;
26 import org.apache.maven.execution.MavenSession;
27 import org.apache.maven.plugin.MojoExecutionException;
28 import org.apache.maven.plugin.MojoFailureException;
29 import org.apache.maven.plugin.surefire.booterclient.ForkConfiguration;
30 import org.apache.maven.plugin.surefire.booterclient.ForkStarter;
31 import org.apache.maven.project.MavenProject;
32 import org.apache.maven.surefire.booter.ClassLoaderConfiguration;
33 import org.apache.maven.surefire.booter.SurefireBooterForkException;
34 import org.apache.maven.surefire.booter.SurefireExecutionException;
35 import org.apache.maven.toolchain.ToolchainManager;
36
37 import java.io.File;
38 import java.util.HashMap;
39 import java.util.Iterator;
40 import java.util.List;
41 import java.util.Map;
42 import java.util.Properties;
43
44
45
46
47
48
49
50
51
52
53
54
55 public class SurefirePlugin
56 extends AbstractSurefireMojo
57 implements SurefireExecutionParameters, SurefireReportParameters
58 {
59
60
61
62
63
64
65
66
67 private boolean skipTests;
68
69
70
71
72
73
74
75
76 private boolean skipExec;
77
78
79
80
81
82
83
84
85 private boolean skip;
86
87
88
89
90
91
92
93 private boolean testFailureIgnore;
94
95
96
97
98
99
100
101 private File basedir;
102
103
104
105
106
107
108
109 private File testClassesDirectory;
110
111
112
113
114
115
116
117 private File classesDirectory;
118
119
120
121
122
123
124
125 private MavenProject project;
126
127
128
129
130
131
132
133
134
135 private List classpathDependencyExcludes;
136
137
138
139
140
141
142
143
144
145
146
147
148 private String classpathDependencyScopeExclude;
149
150
151
152
153
154
155
156 private List additionalClasspathElements;
157
158
159
160
161
162
163 private File reportsDirectory;
164
165
166
167
168
169
170
171
172 private File testSourceDirectory;
173
174
175
176
177
178
179
180
181
182
183 private String test;
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199 private List includes;
200
201
202
203
204
205
206
207
208
209
210
211
212
213 private List excludes;
214
215
216
217
218
219
220
221
222
223 private ArtifactRepository localRepository;
224
225
226
227
228
229
230
231 private Properties systemProperties;
232
233
234
235
236
237
238
239 private Map systemPropertyVariables;
240
241
242
243
244
245
246
247
248 private Properties properties;
249
250
251
252
253
254
255
256
257 private Map pluginArtifactMap;
258
259
260
261
262
263
264
265
266 private Map projectArtifactMap;
267
268
269
270
271
272
273 private boolean printSummary;
274
275
276
277
278
279
280 private String reportFormat;
281
282
283
284
285
286
287 private boolean useFile;
288
289
290
291
292
293
294
295
296 private boolean redirectTestOutputToFile;
297
298
299
300
301
302
303
304 private Boolean failIfNoTests;
305
306
307
308
309
310
311
312
313 private String forkMode;
314
315
316
317
318
319
320
321
322
323 private String jvm;
324
325
326
327
328
329
330
331 private String argLine;
332
333
334
335
336
337
338
339
340
341
342 private String debugForkedProcess;
343
344
345
346
347
348
349
350
351 private int forkedProcessTimeoutInSeconds;
352
353
354
355
356
357
358
359 private Map environmentVariables = new HashMap();
360
361
362
363
364
365
366
367 private File workingDirectory;
368
369
370
371
372
373
374
375
376
377 private boolean childDelegation;
378
379
380
381
382
383
384
385
386 private String groups;
387
388
389
390
391
392
393
394
395 private String excludedGroups;
396
397
398
399
400
401
402
403
404
405 private File[] suiteXmlFiles;
406
407
408
409
410
411
412
413 private String junitArtifactName;
414
415
416
417
418
419
420
421 private String testNGArtifactName;
422
423
424
425
426
427
428
429
430 private int threadCount;
431
432
433
434
435
436
437
438 private String perCoreThreadCount;
439
440
441
442
443
444
445
446
447 private String useUnlimitedThreads;
448
449
450
451
452
453
454
455
456
457
458
459 private String parallel;
460
461
462
463
464
465
466
467 private boolean trimStackTrace;
468
469
470
471
472
473
474 private ArtifactResolver artifactResolver;
475
476
477
478
479
480
481 private ArtifactFactory artifactFactory;
482
483
484
485
486
487
488
489 private List remoteRepositories;
490
491
492
493
494
495
496 private ArtifactMetadataSource metadataSource;
497
498 private Properties originalSystemProperties;
499
500
501
502
503 private Properties internalSystemProperties = new Properties();
504
505
506
507
508
509
510
511 private boolean disableXmlReport;
512
513
514
515
516
517
518
519
520
521 private Boolean useSystemClassLoader;
522
523
524
525
526
527
528
529
530
531
532
533
534
535 private boolean useManifestOnlyJar;
536
537
538
539
540
541
542
543
544 private boolean enableAssertions;
545
546
547
548
549
550
551
552
553 private MavenSession session;
554
555
556
557
558
559
560
561 private String objectFactory;
562
563
564
565
566
567
568
569 private Boolean parallelMavenExecution;
570
571
572
573
574
575
576
577
578
579
580
581 private String runOrder;
582
583
584
585
586 private ToolchainManager toolchainManager;
587
588 public void execute()
589 throws MojoExecutionException, MojoFailureException
590 {
591 if ( verifyParameters() )
592 {
593 logReportsDirectory();
594
595 final List providers = initialize();
596 Exception exception = null;
597 ForkConfiguration forkConfiguration = null;
598 int result = 0;
599 for ( Iterator iter = providers.iterator(); iter.hasNext(); )
600 {
601 ProviderInfo provider = (ProviderInfo) iter.next();
602 forkConfiguration = getForkConfiguration();
603 ClassLoaderConfiguration classLoaderConfiguration = getClassLoaderConfiguration( forkConfiguration );
604 ForkStarter forkStarter = createForkStarter( provider, forkConfiguration, classLoaderConfiguration );
605
606 try
607 {
608 result = forkStarter.run();
609 }
610 catch ( SurefireBooterForkException e )
611 {
612 exception = e;
613 }
614 catch ( SurefireExecutionException e )
615 {
616 exception = e;
617 }
618 }
619
620 if ( exception != null )
621 {
622 throw new MojoExecutionException( exception.getMessage(), exception );
623 }
624
625 if ( getOriginalSystemProperties() != null && forkConfiguration != null && !forkConfiguration.isForking() )
626 {
627
628 System.setProperties( getOriginalSystemProperties() );
629 }
630
631 SurefireHelper.reportExecution( this, result, getLog() );
632 }
633 }
634
635 protected boolean verifyParameters()
636 throws MojoFailureException
637 {
638 if ( isSkip() || isSkipTests() || isSkipExec() )
639 {
640 getLog().info( "Tests are skipped." );
641 return false;
642 }
643
644 if ( !getTestClassesDirectory().exists() )
645 {
646 if ( getFailIfNoTests() != null && getFailIfNoTests().booleanValue() )
647 {
648 throw new MojoFailureException( "No tests to run!" );
649 }
650 getLog().info( "No tests to run." );
651 return true;
652 }
653
654 ensureWorkingDirectoryExists();
655
656 ensureParallelRunningCompatibility();
657
658 warnIfUselessUseSystemClassLoaderParameter();
659
660 return true;
661 }
662
663 protected String getPluginName()
664 {
665 return "surefire";
666 }
667
668 protected String[] getDefaultIncludes()
669 {
670 return new String[]{ "**/Test*.java", "**/*Test.java", "**/*TestCase.java" };
671 }
672
673
674
675 public boolean isSkipTests()
676 {
677 return skipTests;
678 }
679
680
681 public void setSkipTests( boolean skipTests )
682 {
683 this.skipTests = skipTests;
684 }
685
686
687 public boolean isSkipExec()
688 {
689 return skipExec;
690 }
691
692
693 public void setSkipExec( boolean skipExec )
694 {
695 this.skipExec = skipExec;
696 }
697
698 public boolean isSkip()
699 {
700 return skip;
701 }
702
703 public void setSkip( boolean skip )
704 {
705 this.skip = skip;
706 }
707
708 public boolean isTestFailureIgnore()
709 {
710 return testFailureIgnore;
711 }
712
713 public void setTestFailureIgnore( boolean testFailureIgnore )
714 {
715 this.testFailureIgnore = testFailureIgnore;
716 }
717
718 public File getBasedir()
719 {
720 return basedir;
721 }
722
723 public void setBasedir( File basedir )
724 {
725 this.basedir = basedir;
726 }
727
728 public File getTestClassesDirectory()
729 {
730 return testClassesDirectory;
731 }
732
733 public void setTestClassesDirectory( File testClassesDirectory )
734 {
735 this.testClassesDirectory = testClassesDirectory;
736 }
737
738 public File getClassesDirectory()
739 {
740 return classesDirectory;
741 }
742
743 public void setClassesDirectory( File classesDirectory )
744 {
745 this.classesDirectory = classesDirectory;
746 }
747
748 public MavenProject getProject()
749 {
750 return project;
751 }
752
753 public void setProject( MavenProject project )
754 {
755 this.project = project;
756 }
757
758 public List getClasspathDependencyExcludes()
759 {
760 return classpathDependencyExcludes;
761 }
762
763 public void setClasspathDependencyExcludes( List classpathDependencyExcludes )
764 {
765 this.classpathDependencyExcludes = classpathDependencyExcludes;
766 }
767
768 public String getClasspathDependencyScopeExclude()
769 {
770 return classpathDependencyScopeExclude;
771 }
772
773 public void setClasspathDependencyScopeExclude( String classpathDependencyScopeExclude )
774 {
775 this.classpathDependencyScopeExclude = classpathDependencyScopeExclude;
776 }
777
778 public List getAdditionalClasspathElements()
779 {
780 return additionalClasspathElements;
781 }
782
783 public void setAdditionalClasspathElements( List additionalClasspathElements )
784 {
785 this.additionalClasspathElements = additionalClasspathElements;
786 }
787
788 public File getReportsDirectory()
789 {
790 return reportsDirectory;
791 }
792
793 public void setReportsDirectory( File reportsDirectory )
794 {
795 this.reportsDirectory = reportsDirectory;
796 }
797
798 public File getTestSourceDirectory()
799 {
800 return testSourceDirectory;
801 }
802
803 public void setTestSourceDirectory( File testSourceDirectory )
804 {
805 this.testSourceDirectory = testSourceDirectory;
806 }
807
808 public String getTest()
809 {
810 return test;
811 }
812
813 public void setTest( String test )
814 {
815 this.test = test;
816 }
817
818 public List getIncludes()
819 {
820 return includes;
821 }
822
823 public void setIncludes( List includes )
824 {
825 this.includes = includes;
826 }
827
828 public List getExcludes()
829 {
830 return excludes;
831 }
832
833 public void setExcludes( List excludes )
834 {
835 this.excludes = excludes;
836 }
837
838 public ArtifactRepository getLocalRepository()
839 {
840 return localRepository;
841 }
842
843 public void setLocalRepository( ArtifactRepository localRepository )
844 {
845 this.localRepository = localRepository;
846 }
847
848
849 public Properties getSystemProperties()
850 {
851 return systemProperties;
852 }
853
854
855 public void setSystemProperties( Properties systemProperties )
856 {
857 this.systemProperties = systemProperties;
858 }
859
860 public Map getSystemPropertyVariables()
861 {
862 return systemPropertyVariables;
863 }
864
865 public void setSystemPropertyVariables( Map systemPropertyVariables )
866 {
867 this.systemPropertyVariables = systemPropertyVariables;
868 }
869
870 public Properties getProperties()
871 {
872 return properties;
873 }
874
875 public void setProperties( Properties properties )
876 {
877 this.properties = properties;
878 }
879
880 public Map getPluginArtifactMap()
881 {
882 return pluginArtifactMap;
883 }
884
885 public void setPluginArtifactMap( Map pluginArtifactMap )
886 {
887 this.pluginArtifactMap = pluginArtifactMap;
888 }
889
890 public Map getProjectArtifactMap()
891 {
892 return projectArtifactMap;
893 }
894
895 public void setProjectArtifactMap( Map projectArtifactMap )
896 {
897 this.projectArtifactMap = projectArtifactMap;
898 }
899
900 public boolean isPrintSummary()
901 {
902 return printSummary;
903 }
904
905 public void setPrintSummary( boolean printSummary )
906 {
907 this.printSummary = printSummary;
908 }
909
910 public String getReportFormat()
911 {
912 return reportFormat;
913 }
914
915 public void setReportFormat( String reportFormat )
916 {
917 this.reportFormat = reportFormat;
918 }
919
920 public boolean isUseFile()
921 {
922 return useFile;
923 }
924
925 public void setUseFile( boolean useFile )
926 {
927 this.useFile = useFile;
928 }
929
930 public boolean isRedirectTestOutputToFile()
931 {
932 return redirectTestOutputToFile;
933 }
934
935 public void setRedirectTestOutputToFile( boolean redirectTestOutputToFile )
936 {
937 this.redirectTestOutputToFile = redirectTestOutputToFile;
938 }
939
940 public Boolean getFailIfNoTests()
941 {
942 return failIfNoTests;
943 }
944
945 public void setFailIfNoTests( Boolean failIfNoTests )
946 {
947 this.failIfNoTests = failIfNoTests;
948 }
949
950 public String getForkMode()
951 {
952 return forkMode;
953 }
954
955 public void setForkMode( String forkMode )
956 {
957 this.forkMode = forkMode;
958 }
959
960 public String getJvm()
961 {
962 return jvm;
963 }
964
965 public void setJvm( String jvm )
966 {
967 this.jvm = jvm;
968 }
969
970 public String getArgLine()
971 {
972 return argLine;
973 }
974
975 public void setArgLine( String argLine )
976 {
977 this.argLine = argLine;
978 }
979
980 public String getDebugForkedProcess()
981 {
982 return debugForkedProcess;
983 }
984
985 public void setDebugForkedProcess( String debugForkedProcess )
986 {
987 this.debugForkedProcess = debugForkedProcess;
988 }
989
990 public int getForkedProcessTimeoutInSeconds()
991 {
992 return forkedProcessTimeoutInSeconds;
993 }
994
995 public void setForkedProcessTimeoutInSeconds( int forkedProcessTimeoutInSeconds )
996 {
997 this.forkedProcessTimeoutInSeconds = forkedProcessTimeoutInSeconds;
998 }
999
1000 public Map getEnvironmentVariables()
1001 {
1002 return environmentVariables;
1003 }
1004
1005 public void setEnvironmentVariables( Map environmentVariables )
1006 {
1007 this.environmentVariables = environmentVariables;
1008 }
1009
1010 public File getWorkingDirectory()
1011 {
1012 return workingDirectory;
1013 }
1014
1015 public void setWorkingDirectory( File workingDirectory )
1016 {
1017 this.workingDirectory = workingDirectory;
1018 }
1019
1020 public boolean isChildDelegation()
1021 {
1022 return childDelegation;
1023 }
1024
1025 public void setChildDelegation( boolean childDelegation )
1026 {
1027 this.childDelegation = childDelegation;
1028 }
1029
1030 public String getGroups()
1031 {
1032 return groups;
1033 }
1034
1035 public void setGroups( String groups )
1036 {
1037 this.groups = groups;
1038 }
1039
1040 public String getExcludedGroups()
1041 {
1042 return excludedGroups;
1043 }
1044
1045 public void setExcludedGroups( String excludedGroups )
1046 {
1047 this.excludedGroups = excludedGroups;
1048 }
1049
1050 public File[] getSuiteXmlFiles()
1051 {
1052 return suiteXmlFiles;
1053 }
1054
1055 public void setSuiteXmlFiles( File[] suiteXmlFiles )
1056 {
1057 this.suiteXmlFiles = suiteXmlFiles;
1058 }
1059
1060 public String getJunitArtifactName()
1061 {
1062 return junitArtifactName;
1063 }
1064
1065 public void setJunitArtifactName( String junitArtifactName )
1066 {
1067 this.junitArtifactName = junitArtifactName;
1068 }
1069
1070 public String getTestNGArtifactName()
1071 {
1072 return testNGArtifactName;
1073 }
1074
1075 public void setTestNGArtifactName( String testNGArtifactName )
1076 {
1077 this.testNGArtifactName = testNGArtifactName;
1078 }
1079
1080 public int getThreadCount()
1081 {
1082 return threadCount;
1083 }
1084
1085 public void setThreadCount( int threadCount )
1086 {
1087 this.threadCount = threadCount;
1088 }
1089
1090 public String getPerCoreThreadCount()
1091 {
1092 return perCoreThreadCount;
1093 }
1094
1095 public void setPerCoreThreadCount( String perCoreThreadCount )
1096 {
1097 this.perCoreThreadCount = perCoreThreadCount;
1098 }
1099
1100 public String getUseUnlimitedThreads()
1101 {
1102 return useUnlimitedThreads;
1103 }
1104
1105 public void setUseUnlimitedThreads( String useUnlimitedThreads )
1106 {
1107 this.useUnlimitedThreads = useUnlimitedThreads;
1108 }
1109
1110 public String getParallel()
1111 {
1112 return parallel;
1113 }
1114
1115 public void setParallel( String parallel )
1116 {
1117 this.parallel = parallel;
1118 }
1119
1120 public boolean isTrimStackTrace()
1121 {
1122 return trimStackTrace;
1123 }
1124
1125 public void setTrimStackTrace( boolean trimStackTrace )
1126 {
1127 this.trimStackTrace = trimStackTrace;
1128 }
1129
1130 public ArtifactResolver getArtifactResolver()
1131 {
1132 return artifactResolver;
1133 }
1134
1135 public void setArtifactResolver( ArtifactResolver artifactResolver )
1136 {
1137 this.artifactResolver = artifactResolver;
1138 }
1139
1140 public ArtifactFactory getArtifactFactory()
1141 {
1142 return artifactFactory;
1143 }
1144
1145 public void setArtifactFactory( ArtifactFactory artifactFactory )
1146 {
1147 this.artifactFactory = artifactFactory;
1148 }
1149
1150 public List getRemoteRepositories()
1151 {
1152 return remoteRepositories;
1153 }
1154
1155 public void setRemoteRepositories( List remoteRepositories )
1156 {
1157 this.remoteRepositories = remoteRepositories;
1158 }
1159
1160 public ArtifactMetadataSource getMetadataSource()
1161 {
1162 return metadataSource;
1163 }
1164
1165 public void setMetadataSource( ArtifactMetadataSource metadataSource )
1166 {
1167 this.metadataSource = metadataSource;
1168 }
1169
1170 public Properties getOriginalSystemProperties()
1171 {
1172 return originalSystemProperties;
1173 }
1174
1175 public void setOriginalSystemProperties( Properties originalSystemProperties )
1176 {
1177 this.originalSystemProperties = originalSystemProperties;
1178 }
1179
1180 public Properties getInternalSystemProperties()
1181 {
1182 return internalSystemProperties;
1183 }
1184
1185 public void setInternalSystemProperties( Properties internalSystemProperties )
1186 {
1187 this.internalSystemProperties = internalSystemProperties;
1188 }
1189
1190 public boolean isDisableXmlReport()
1191 {
1192 return disableXmlReport;
1193 }
1194
1195 public void setDisableXmlReport( boolean disableXmlReport )
1196 {
1197 this.disableXmlReport = disableXmlReport;
1198 }
1199
1200 public Boolean getUseSystemClassLoader()
1201 {
1202 return useSystemClassLoader;
1203 }
1204
1205 public void setUseSystemClassLoader( Boolean useSystemClassLoader )
1206 {
1207 this.useSystemClassLoader = useSystemClassLoader;
1208 }
1209
1210 public boolean isUseManifestOnlyJar()
1211 {
1212 return useManifestOnlyJar;
1213 }
1214
1215 public void setUseManifestOnlyJar( boolean useManifestOnlyJar )
1216 {
1217 this.useManifestOnlyJar = useManifestOnlyJar;
1218 }
1219
1220 public boolean isEnableAssertions()
1221 {
1222 return enableAssertions;
1223 }
1224
1225 public void setEnableAssertions( boolean enableAssertions )
1226 {
1227 this.enableAssertions = enableAssertions;
1228 }
1229
1230 public MavenSession getSession()
1231 {
1232 return session;
1233 }
1234
1235 public void setSession( MavenSession session )
1236 {
1237 this.session = session;
1238 }
1239
1240 public String getObjectFactory()
1241 {
1242 return objectFactory;
1243 }
1244
1245 public void setObjectFactory( String objectFactory )
1246 {
1247 this.objectFactory = objectFactory;
1248 }
1249
1250 public ToolchainManager getToolchainManager()
1251 {
1252 return toolchainManager;
1253 }
1254
1255 public void setToolchainManager( ToolchainManager toolchainManager )
1256 {
1257 this.toolchainManager = toolchainManager;
1258 }
1259
1260 public boolean isMavenParallel()
1261 {
1262 return parallelMavenExecution != null && parallelMavenExecution.booleanValue();
1263 }
1264
1265 public String getRunOrder()
1266 {
1267 return runOrder;
1268 }
1269
1270 public void setRunOrder( String runOrder )
1271 {
1272 this.runOrder = runOrder;
1273 }
1274 }