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