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