-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathJUnitCommandLineParseResult.java
186 lines (160 loc) · 6.22 KB
/
JUnitCommandLineParseResult.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package org.junit.runner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.junit.internal.Classes;
import org.junit.rules.TestRule;
import org.junit.runner.FilterFactory.FilterNotCreatedException;
import org.junit.runner.manipulation.Filter;
import org.junit.runner.manipulation.GlobalRuleRunner;
import org.junit.runners.model.InitializationError;
class JUnitCommandLineParseResult {
private final List<String> filterSpecs = new ArrayList<String>();
private final List<Class<?>> classes = new ArrayList<Class<?>>();
private final List<Throwable> parserErrors = new ArrayList<Throwable>();
private final List<Class<?>> globalRules = new ArrayList<Class<?>>();
/**
* Do not use. Testing purposes only.
*/
JUnitCommandLineParseResult() {}
/**
* Returns filter specs parsed from command line.
*/
public List<String> getFilterSpecs() {
return Collections.unmodifiableList(filterSpecs);
}
/**
* Returns test classes parsed from command line.
*/
public List<Class<?>> getClasses() {
return Collections.unmodifiableList(classes);
}
/**
* Returns global rules classes parsed from command line.
*/
public List<Class<?>> getGlobalRules() {
return Collections.unmodifiableList(globalRules);
}
/**
* Parses the arguments.
*
* @param args Arguments
*/
public static JUnitCommandLineParseResult parse(String[] args) {
JUnitCommandLineParseResult result = new JUnitCommandLineParseResult();
result.parseArgs(args);
return result;
}
private void parseArgs(String[] args) {
parseParameters(parseOptions(args));
}
String[] parseOptions(String... args) {
for (int i = 0; i != args.length; ++i) {
String arg = args[i];
if (arg.equals("--")) {
return copyArray(args, i + 1, args.length);
} else if (arg.startsWith("--")) {
if (arg.startsWith("--filter=") || arg.equals("--filter")) {
String filterSpec;
if (arg.equals("--filter")) {
++i;
if (i < args.length) {
filterSpec = args[i];
} else {
parserErrors.add(new CommandLineParserError(arg + " value not specified"));
break;
}
} else {
filterSpec = arg.substring(arg.indexOf('=') + 1);
}
filterSpecs.add(filterSpec);
} else if (arg.equals("--global-rule")) {
++i;
if (i < args.length) {
try {
Class<?> clazz = Classes.getClass(args[i]);
if (TestRule.class.isAssignableFrom(clazz)) {
globalRules.add(clazz);
} else {
parserErrors.add(new CommandLineParserError(args[i] + " does not implement TestRule"));
break;
}
} catch (ClassNotFoundException e) {
parserErrors.add(new CommandLineParserError(args[i] + " is not a valid value for global rule"));
break;
}
} else {
parserErrors.add(new CommandLineParserError(arg + " value not specified"));
break;
}
} else {
parserErrors.add(new CommandLineParserError("JUnit knows nothing about the " + arg + " option"));
}
} else {
return copyArray(args, i, args.length);
}
}
return new String[]{};
}
private String[] copyArray(String[] args, int from, int to) {
String[] result = new String[to - from];
for (int j = from; j != to; ++j) {
result[j - from] = args[j];
}
return result;
}
void parseParameters(String[] args) {
for (String arg : args) {
try {
classes.add(Classes.getClass(arg));
} catch (ClassNotFoundException e) {
parserErrors.add(new IllegalArgumentException("Could not find class [" + arg + "]", e));
}
}
}
private Request errorReport(Throwable cause) {
return Request.errorReport(JUnitCommandLineParseResult.class, cause);
}
/**
* Creates a {@link Request}.
*
* @param computer {@link Computer} to be used.
*/
public Request createRequest(Computer computer) {
if (parserErrors.isEmpty()) {
Request request = Request.classes(
computer, classes.toArray(new Class<?>[classes.size()]));
return applyFilterSpecs(applyGlobalRules(request));
} else {
return errorReport(new InitializationError(parserErrors));
}
}
private Request applyGlobalRules(Request request) {
if (!globalRules.isEmpty()) {
return request.withGlobalRules(new GlobalRuleRunner(globalRules));
} else {
return request;
}
}
private Request applyFilterSpecs(Request request) {
try {
for (String filterSpec : filterSpecs) {
Filter filter = FilterFactories.createFilterFromFilterSpec(
request, filterSpec);
request = request.filterWith(filter);
}
return request;
} catch (FilterNotCreatedException e) {
return errorReport(e);
}
}
/**
* Exception used if there's a problem parsing the command line.
*/
public static class CommandLineParserError extends Exception {
private static final long serialVersionUID= 1L;
public CommandLineParserError(String message) {
super(message);
}
}
}