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