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