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