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