-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
ExecuteTestsTask.java
114 lines (95 loc) · 3.78 KB
/
ExecuteTestsTask.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* Copyright 2015-2016 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.junit.platform.console.tasks;
import static org.junit.platform.commons.meta.API.Usage.Internal;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;
import org.junit.platform.commons.JUnitException;
import org.junit.platform.commons.meta.API;
import org.junit.platform.commons.util.ReflectionUtils;
import org.junit.platform.console.options.CommandLineOptions;
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.launcher.core.LauncherFactory;
import org.junit.platform.launcher.listeners.SummaryGeneratingListener;
import org.junit.platform.launcher.listeners.TestExecutionSummary;
/**
* @since 1.0
*/
@API(Internal)
public class ExecuteTestsTask implements ConsoleTask {
private final CommandLineOptions options;
private final Supplier<Launcher> launcherSupplier;
public ExecuteTestsTask(CommandLineOptions options) {
this(options, LauncherFactory::create);
}
// for tests only
ExecuteTestsTask(CommandLineOptions options, Supplier<Launcher> launcherSupplier) {
this.options = options;
this.launcherSupplier = launcherSupplier;
}
@Override
public int execute(PrintWriter out) throws Exception {
return new CustomContextClassLoaderExecutor(createCustomClassLoader()).invoke(() -> executeTests(out));
}
private int executeTests(PrintWriter out) {
Launcher launcher = launcherSupplier.get();
SummaryGeneratingListener summaryListener = registerListeners(out, launcher);
LauncherDiscoveryRequest discoveryRequest = new DiscoveryRequestCreator().toDiscoveryRequest(options);
launcher.execute(discoveryRequest);
TestExecutionSummary summary = summaryListener.getSummary();
printSummary(summary, out);
return computeExitCode(summary);
}
private Optional<ClassLoader> createCustomClassLoader() {
List<Path> additionalClasspathEntries = options.getAdditionalClasspathEntries();
if (!additionalClasspathEntries.isEmpty()) {
URL[] urls = additionalClasspathEntries.stream().map(this::toURL).toArray(URL[]::new);
ClassLoader parentClassLoader = ReflectionUtils.getDefaultClassLoader();
ClassLoader customClassLoader = URLClassLoader.newInstance(urls, parentClassLoader);
return Optional.of(customClassLoader);
}
return Optional.empty();
}
private URL toURL(Path path) {
try {
return path.toUri().toURL();
}
catch (Exception ex) {
throw new JUnitException("Invalid classpath entry: " + path, ex);
}
}
private SummaryGeneratingListener registerListeners(PrintWriter out, Launcher launcher) {
SummaryGeneratingListener summaryListener = new SummaryGeneratingListener();
launcher.registerTestExecutionListeners(summaryListener);
if (!options.isHideDetails()) {
launcher.registerTestExecutionListeners(
new ColoredPrintingTestListener(out, options.isAnsiColorOutputDisabled()));
}
if (options.getReportsDir().isPresent()) {
launcher.registerTestExecutionListeners(new XmlReportsWritingListener(options.getReportsDir().get(), out));
}
return summaryListener;
}
private void printSummary(TestExecutionSummary summary, PrintWriter out) {
if (options.isHideDetails()) { // Otherwise the failures have already been printed
summary.printFailuresTo(out);
}
summary.printTo(out);
}
private int computeExitCode(TestExecutionSummary summary) {
return (summary.getTotalFailureCount() == 0 ? SUCCESS : TESTS_FAILED);
}
}