50908
|
1 |
/*
|
|
2 |
* Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
package com.oracle.mxtool.junit;
|
|
24 |
|
|
25 |
import java.lang.reflect.Modifier;
|
|
26 |
import java.util.ArrayList;
|
|
27 |
import java.util.LinkedHashSet;
|
|
28 |
import java.util.List;
|
|
29 |
import java.util.Set;
|
|
30 |
import org.junit.runner.Description;
|
|
31 |
import org.junit.runner.Request;
|
|
32 |
import org.junit.runner.notification.Failure;
|
|
33 |
|
|
34 |
public final class MxJUnitRequest {
|
|
35 |
|
|
36 |
private final Request request;
|
|
37 |
|
|
38 |
final Set<Class<?>> classes;
|
|
39 |
final String methodName;
|
|
40 |
|
|
41 |
final List<Failure> missingClasses;
|
|
42 |
|
|
43 |
private MxJUnitRequest(Request request, Set<Class<?>> classes, String methodName, List<Failure> missingClasses) {
|
|
44 |
this.request = request;
|
|
45 |
this.classes = classes;
|
|
46 |
this.methodName = methodName;
|
|
47 |
this.missingClasses = missingClasses;
|
|
48 |
}
|
|
49 |
|
|
50 |
public Request getRequest() {
|
|
51 |
return request;
|
|
52 |
}
|
|
53 |
|
|
54 |
public static final class BuilderException extends Exception {
|
|
55 |
|
|
56 |
private static final long serialVersionUID = 1L;
|
|
57 |
|
|
58 |
private BuilderException(String msg) {
|
|
59 |
super(msg);
|
|
60 |
}
|
|
61 |
}
|
|
62 |
|
|
63 |
public static class Builder {
|
|
64 |
|
|
65 |
private final Set<Class<?>> classes = new LinkedHashSet<>();
|
|
66 |
private String methodName = null;
|
|
67 |
private final List<Failure> missingClasses = new ArrayList<>();
|
|
68 |
|
|
69 |
protected Class<?> resolveClass(String name) throws ClassNotFoundException {
|
|
70 |
return Class.forName(name, false, Builder.class.getClassLoader());
|
|
71 |
}
|
|
72 |
|
|
73 |
public void addTestSpec(String arg) throws BuilderException {
|
|
74 |
String className;
|
|
75 |
/*
|
|
76 |
* Entries of the form class#method are handled specially. Only one can be specified on
|
|
77 |
* the command line as there's no obvious way to build a runner for multiple ones.
|
|
78 |
*/
|
|
79 |
if (methodName != null) {
|
|
80 |
throw new BuilderException("Only a single class and method can be specified: " + arg);
|
|
81 |
} else if (arg.contains("#")) {
|
|
82 |
String[] pair = arg.split("#");
|
|
83 |
if (pair.length != 2) {
|
|
84 |
throw new BuilderException("Malformed class and method request: " + arg);
|
|
85 |
} else if (!classes.isEmpty()) {
|
|
86 |
throw new BuilderException("Only a single class and method can be specified: " + arg);
|
|
87 |
} else {
|
|
88 |
methodName = pair[1];
|
|
89 |
className = pair[0];
|
|
90 |
}
|
|
91 |
} else {
|
|
92 |
className = arg;
|
|
93 |
}
|
|
94 |
try {
|
|
95 |
Class<?> cls = resolveClass(className);
|
|
96 |
if ((cls.getModifiers() & Modifier.ABSTRACT) == 0) {
|
|
97 |
classes.add(cls);
|
|
98 |
}
|
|
99 |
} catch (ClassNotFoundException e) {
|
|
100 |
Description description = Description.createSuiteDescription(className);
|
|
101 |
Failure failure = new Failure(description, e);
|
|
102 |
missingClasses.add(failure);
|
|
103 |
}
|
|
104 |
}
|
|
105 |
|
|
106 |
public MxJUnitRequest build() {
|
|
107 |
Request request;
|
|
108 |
if (methodName == null) {
|
|
109 |
request = Request.classes(classes.toArray(new Class<?>[0]));
|
|
110 |
} else {
|
|
111 |
request = Request.method(classes.iterator().next(), methodName);
|
|
112 |
}
|
|
113 |
return new MxJUnitRequest(request, classes, methodName, missingClasses);
|
|
114 |
}
|
|
115 |
}
|
|
116 |
}
|