58416
|
1 |
/*
|
|
2 |
* Copyright (c) 2019, 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 |
|
|
24 |
package jdk.jpackage.test;
|
|
25 |
|
|
26 |
import java.lang.reflect.Method;
|
|
27 |
import java.nio.file.Files;
|
|
28 |
import java.util.Collections;
|
|
29 |
import java.util.List;
|
|
30 |
import java.util.stream.Collectors;
|
|
31 |
import java.util.stream.Stream;
|
|
32 |
import jdk.jpackage.test.Functional.ThrowingConsumer;
|
|
33 |
import jdk.jpackage.test.Functional.ThrowingFunction;
|
|
34 |
import jdk.jpackage.test.Functional.ThrowingRunnable;
|
|
35 |
import jdk.jpackage.test.Functional.ThrowingSupplier;
|
|
36 |
|
|
37 |
|
|
38 |
class TestInstance implements ThrowingRunnable {
|
|
39 |
|
|
40 |
static class TestDesc {
|
|
41 |
private TestDesc() {
|
|
42 |
}
|
|
43 |
|
|
44 |
String testFullName() {
|
|
45 |
StringBuilder sb = new StringBuilder();
|
|
46 |
sb.append(clazz.getSimpleName());
|
|
47 |
if (functionName != null) {
|
|
48 |
sb.append('.');
|
|
49 |
sb.append(functionName);
|
|
50 |
if (args != null) {
|
|
51 |
sb.append('(').append(args).append(')');
|
|
52 |
}
|
|
53 |
}
|
|
54 |
return sb.toString();
|
|
55 |
}
|
|
56 |
|
|
57 |
private Class clazz;
|
|
58 |
private String functionName;
|
|
59 |
private String args;
|
|
60 |
|
|
61 |
private static TestDesc create() {
|
|
62 |
TestDesc desc = new TestDesc();
|
|
63 |
desc.clazz = enclosingMainMethodClass();
|
|
64 |
return desc;
|
|
65 |
}
|
|
66 |
|
|
67 |
static TestDesc create(Method m, Object... args) {
|
|
68 |
TestDesc desc = new TestDesc();
|
|
69 |
desc.clazz = m.getDeclaringClass();
|
|
70 |
desc.functionName = m.getName();
|
|
71 |
if (args.length != 0) {
|
|
72 |
desc.args = Stream.of(args).map(Object::toString).collect(
|
|
73 |
Collectors.joining(","));
|
|
74 |
}
|
|
75 |
return desc;
|
|
76 |
}
|
|
77 |
}
|
|
78 |
|
|
79 |
TestInstance(ThrowingRunnable testBody) {
|
|
80 |
assertCount = 0;
|
|
81 |
this.testConstructor = (unused) -> null;
|
|
82 |
this.testBody = (unused) -> testBody.run();
|
|
83 |
this.beforeActions = Collections.emptyList();
|
|
84 |
this.afterActions = Collections.emptyList();
|
|
85 |
this.testDesc = TestDesc.create();
|
|
86 |
}
|
|
87 |
|
|
88 |
TestInstance(ThrowingFunction testConstructor, MethodCall testBody,
|
|
89 |
List<ThrowingConsumer> beforeActions,
|
|
90 |
List<ThrowingConsumer> afterActions) {
|
|
91 |
assertCount = 0;
|
|
92 |
this.testConstructor = testConstructor;
|
|
93 |
this.testBody = testBody;
|
|
94 |
this.beforeActions = beforeActions;
|
|
95 |
this.afterActions = afterActions;
|
|
96 |
this.testDesc = testBody.createDescription();
|
|
97 |
}
|
|
98 |
|
|
99 |
void notifyAssert() {
|
|
100 |
assertCount++;
|
|
101 |
}
|
|
102 |
|
|
103 |
boolean passed() {
|
|
104 |
return success;
|
|
105 |
}
|
|
106 |
|
|
107 |
String functionName() {
|
|
108 |
return testDesc.functionName;
|
|
109 |
}
|
|
110 |
|
|
111 |
String baseName() {
|
|
112 |
return testDesc.clazz.getSimpleName();
|
|
113 |
}
|
|
114 |
|
|
115 |
String fullName() {
|
|
116 |
return testDesc.testFullName();
|
|
117 |
}
|
|
118 |
|
|
119 |
@Override
|
|
120 |
public void run() throws Throwable {
|
|
121 |
final String fullName = testDesc.testFullName();
|
|
122 |
TKit.log(String.format("[ RUN ] %s", fullName));
|
|
123 |
try {
|
|
124 |
Object testInstance = testConstructor.apply(testBody);
|
|
125 |
beforeActions.forEach((a) -> ThrowingConsumer.toConsumer(a).accept(
|
|
126 |
testInstance));
|
|
127 |
Files.createDirectories(TKit.workDir());
|
|
128 |
try {
|
|
129 |
testBody.accept(testInstance);
|
|
130 |
} finally {
|
|
131 |
afterActions.forEach(a -> TKit.ignoreExceptions(() -> a.accept(
|
|
132 |
testInstance)));
|
|
133 |
}
|
|
134 |
success = true;
|
|
135 |
} finally {
|
|
136 |
TKit.log(String.format("%s %s; checks=%d",
|
|
137 |
success ? "[ OK ]" : "[ FAILED ]", fullName,
|
|
138 |
assertCount));
|
|
139 |
}
|
|
140 |
}
|
|
141 |
|
|
142 |
private static Class enclosingMainMethodClass() {
|
|
143 |
StackTraceElement st[] = Thread.currentThread().getStackTrace();
|
|
144 |
for (StackTraceElement ste : st) {
|
|
145 |
if ("main".equals(ste.getMethodName())) {
|
|
146 |
return Functional.ThrowingSupplier.toSupplier(() -> Class.forName(
|
|
147 |
ste.getClassName())).get();
|
|
148 |
}
|
|
149 |
}
|
|
150 |
return null;
|
|
151 |
}
|
|
152 |
|
|
153 |
private int assertCount;
|
|
154 |
private boolean success;
|
|
155 |
private final TestDesc testDesc;
|
|
156 |
private final ThrowingFunction testConstructor;
|
|
157 |
private final ThrowingConsumer testBody;
|
|
158 |
private final List<ThrowingConsumer> beforeActions;
|
|
159 |
private final List<ThrowingConsumer> afterActions;
|
|
160 |
}
|