-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
ConsoleLauncher.java
106 lines (92 loc) · 3.45 KB
/
ConsoleLauncher.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
/*
* Copyright 2015-2020 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 v2.0 which
* accompanies this distribution and is available at
*
* https://www.eclipse.org/legal/epl-v20.html
*/
package org.junit.platform.console;
import static org.apiguardian.api.API.Status.INTERNAL;
import static org.apiguardian.api.API.Status.MAINTAINED;
import java.io.PrintStream;
import java.io.PrintWriter;
import org.apiguardian.api.API;
import org.junit.platform.commons.JUnitException;
import org.junit.platform.console.options.CommandLineOptions;
import org.junit.platform.console.options.CommandLineOptionsParser;
import org.junit.platform.console.options.PicocliCommandLineOptionsParser;
import org.junit.platform.console.tasks.ConsoleTestExecutor;
import org.junit.platform.launcher.listeners.TestExecutionSummary;
/**
* The {@code ConsoleLauncher} is a stand-alone application for launching the
* JUnit Platform from the console.
*
* @since 1.0
*/
@API(status = MAINTAINED, since = "1.0")
public class ConsoleLauncher {
public static void main(String... args) {
int exitCode = execute(System.out, System.err, args).getExitCode();
System.exit(exitCode);
}
@API(status = INTERNAL, since = "1.0")
public static ConsoleLauncherExecutionResult execute(PrintStream out, PrintStream err, String... args) {
return execute(new PrintWriter(out), new PrintWriter(err), args);
}
@API(status = INTERNAL, since = "1.0")
public static ConsoleLauncherExecutionResult execute(PrintWriter out, PrintWriter err, String... args) {
CommandLineOptionsParser parser = new PicocliCommandLineOptionsParser();
ConsoleLauncher consoleLauncher = new ConsoleLauncher(parser, out, err);
return consoleLauncher.execute(args);
}
private final CommandLineOptionsParser commandLineOptionsParser;
private final PrintWriter out;
private final PrintWriter err;
ConsoleLauncher(CommandLineOptionsParser commandLineOptionsParser, PrintWriter out, PrintWriter err) {
this.commandLineOptionsParser = commandLineOptionsParser;
this.out = out;
this.err = err;
}
ConsoleLauncherExecutionResult execute(String... args) {
try {
CommandLineOptions options = commandLineOptionsParser.parse(args);
if (!options.isBannerDisabled()) {
displayBanner(out);
}
if (options.isDisplayHelp()) {
commandLineOptionsParser.printHelp(out, options.isAnsiColorOutputDisabled());
return ConsoleLauncherExecutionResult.success();
}
return executeTests(options, out);
}
catch (JUnitException ex) {
err.println(ex.getMessage());
err.println();
commandLineOptionsParser.printHelp(err, false);
return ConsoleLauncherExecutionResult.failed();
}
finally {
out.flush();
err.flush();
}
}
void displayBanner(PrintWriter out) {
out.println();
out.println("Thanks for using JUnit! Support its development at https://junit.org/sponsoring");
out.println();
}
private ConsoleLauncherExecutionResult executeTests(CommandLineOptions options, PrintWriter out) {
try {
TestExecutionSummary testExecutionSummary = new ConsoleTestExecutor(options).execute(out);
return ConsoleLauncherExecutionResult.forSummary(testExecutionSummary, options);
}
catch (Exception exception) {
exception.printStackTrace(err);
err.println();
commandLineOptionsParser.printHelp(out, options.isAnsiColorOutputDisabled());
return ConsoleLauncherExecutionResult.failed();
}
}
}