author | herrick |
Wed, 16 Oct 2019 10:32:08 -0400 | |
branch | JDK-8200758-branch |
changeset 58648 | 3bf53ffa9ae7 |
parent 58464 | d82489644b15 |
child 58696 | 61c44899b4eb |
permissions | -rw-r--r-- |
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 |
||
58648 | 26 |
import java.lang.reflect.Array; |
58416 | 27 |
import java.lang.reflect.Method; |
28 |
import java.nio.file.Files; |
|
58648 | 29 |
import java.nio.file.Path; |
30 |
import java.util.*; |
|
31 |
import java.util.function.Predicate; |
|
32 |
import java.util.function.Supplier; |
|
58416 | 33 |
import java.util.stream.Collectors; |
34 |
import java.util.stream.Stream; |
|
35 |
import jdk.jpackage.test.Functional.ThrowingConsumer; |
|
36 |
import jdk.jpackage.test.Functional.ThrowingFunction; |
|
37 |
import jdk.jpackage.test.Functional.ThrowingRunnable; |
|
38 |
||
58648 | 39 |
final class TestInstance implements ThrowingRunnable { |
58416 | 40 |
|
41 |
static class TestDesc { |
|
42 |
private TestDesc() { |
|
43 |
} |
|
44 |
||
45 |
String testFullName() { |
|
46 |
StringBuilder sb = new StringBuilder(); |
|
47 |
sb.append(clazz.getSimpleName()); |
|
58648 | 48 |
if (instanceArgs != null) { |
49 |
sb.append('(').append(instanceArgs).append(')'); |
|
50 |
} |
|
58416 | 51 |
if (functionName != null) { |
52 |
sb.append('.'); |
|
53 |
sb.append(functionName); |
|
58648 | 54 |
if (functionArgs != null) { |
55 |
sb.append('(').append(functionArgs).append(')'); |
|
58416 | 56 |
} |
57 |
} |
|
58 |
return sb.toString(); |
|
59 |
} |
|
60 |
||
58648 | 61 |
static Builder createBuilder() { |
62 |
return new Builder(); |
|
63 |
} |
|
64 |
||
65 |
static final class Builder implements Supplier<TestDesc> { |
|
66 |
private Builder() { |
|
67 |
} |
|
68 |
||
69 |
Builder method(Method v) { |
|
70 |
method = v; |
|
71 |
return this; |
|
72 |
} |
|
73 |
||
74 |
Builder ctorArgs(Object... v) { |
|
75 |
ctorArgs = ofNullable(v); |
|
76 |
return this; |
|
77 |
} |
|
78 |
||
79 |
Builder methodArgs(Object... v) { |
|
80 |
methodArgs = ofNullable(v); |
|
81 |
return this; |
|
82 |
} |
|
58416 | 83 |
|
58648 | 84 |
@Override |
85 |
public TestDesc get() { |
|
86 |
TestDesc desc = new TestDesc(); |
|
87 |
if (method == null) { |
|
88 |
desc.clazz = enclosingMainMethodClass(); |
|
89 |
} else { |
|
90 |
desc.clazz = method.getDeclaringClass(); |
|
91 |
desc.functionName = method.getName(); |
|
92 |
desc.functionArgs = formatArgs(methodArgs); |
|
93 |
desc.instanceArgs = formatArgs(ctorArgs); |
|
94 |
} |
|
95 |
return desc; |
|
96 |
} |
|
97 |
||
98 |
private static String formatArgs(List<Object> values) { |
|
99 |
if (values == null) { |
|
100 |
return null; |
|
101 |
} |
|
102 |
return values.stream().map(v -> { |
|
103 |
if (v != null && v.getClass().isArray()) { |
|
104 |
return String.format("%s(length=%d)", |
|
105 |
Arrays.deepToString((Object[]) v), |
|
106 |
Array.getLength(v)); |
|
107 |
} |
|
108 |
return String.format("%s", v); |
|
109 |
}).collect(Collectors.joining(", ")); |
|
110 |
} |
|
111 |
||
112 |
private static List<Object> ofNullable(Object... values) { |
|
113 |
List<Object> result = new ArrayList(); |
|
114 |
for (var v: values) { |
|
115 |
result.add(v); |
|
116 |
} |
|
117 |
return result; |
|
118 |
} |
|
119 |
||
120 |
private List<Object> ctorArgs; |
|
121 |
private List<Object> methodArgs; |
|
122 |
private Method method; |
|
58416 | 123 |
} |
124 |
||
125 |
static TestDesc create(Method m, Object... args) { |
|
126 |
TestDesc desc = new TestDesc(); |
|
127 |
desc.clazz = m.getDeclaringClass(); |
|
128 |
desc.functionName = m.getName(); |
|
129 |
if (args.length != 0) { |
|
58648 | 130 |
desc.functionArgs = Stream.of(args).map(v -> { |
131 |
if (v.getClass().isArray()) { |
|
132 |
return String.format("%s(length=%d)", |
|
133 |
Arrays.deepToString((Object[]) v), |
|
134 |
Array.getLength(v)); |
|
135 |
} |
|
136 |
return String.format("%s", v); |
|
137 |
}).collect(Collectors.joining(", ")); |
|
58416 | 138 |
} |
139 |
return desc; |
|
140 |
} |
|
58648 | 141 |
|
142 |
private Class clazz; |
|
143 |
private String functionName; |
|
144 |
private String functionArgs; |
|
145 |
private String instanceArgs; |
|
58416 | 146 |
} |
147 |
||
148 |
TestInstance(ThrowingRunnable testBody) { |
|
149 |
assertCount = 0; |
|
150 |
this.testConstructor = (unused) -> null; |
|
151 |
this.testBody = (unused) -> testBody.run(); |
|
152 |
this.beforeActions = Collections.emptyList(); |
|
153 |
this.afterActions = Collections.emptyList(); |
|
58648 | 154 |
this.testDesc = TestDesc.createBuilder().get(); |
155 |
this.dryRun = false; |
|
58416 | 156 |
} |
157 |
||
58648 | 158 |
TestInstance(MethodCall testBody, List<ThrowingConsumer> beforeActions, |
159 |
List<ThrowingConsumer> afterActions, boolean dryRun) { |
|
58416 | 160 |
assertCount = 0; |
58648 | 161 |
this.testConstructor = v -> ((MethodCall)v).newInstance(); |
58416 | 162 |
this.testBody = testBody; |
163 |
this.beforeActions = beforeActions; |
|
164 |
this.afterActions = afterActions; |
|
165 |
this.testDesc = testBody.createDescription(); |
|
58648 | 166 |
this.dryRun = dryRun; |
58416 | 167 |
} |
168 |
||
169 |
void notifyAssert() { |
|
170 |
assertCount++; |
|
171 |
} |
|
172 |
||
58464
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
173 |
void notifySkipped(RuntimeException ex) { |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
174 |
skippedTestException = ex; |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
175 |
} |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
176 |
|
58416 | 177 |
boolean passed() { |
58464
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
178 |
return status == Status.Passed; |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
179 |
} |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
180 |
|
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
181 |
boolean skipped() { |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
182 |
return status == Status.Skipped; |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
183 |
} |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
184 |
|
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
185 |
boolean failed() { |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
186 |
return status == Status.Failed; |
58416 | 187 |
} |
188 |
||
189 |
String functionName() { |
|
190 |
return testDesc.functionName; |
|
191 |
} |
|
192 |
||
193 |
String baseName() { |
|
194 |
return testDesc.clazz.getSimpleName(); |
|
195 |
} |
|
196 |
||
197 |
String fullName() { |
|
198 |
return testDesc.testFullName(); |
|
199 |
} |
|
200 |
||
58464
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
201 |
void rethrowIfSkipped() { |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
202 |
if (skippedTestException != null) { |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
203 |
throw skippedTestException; |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
204 |
} |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
205 |
} |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
206 |
|
58648 | 207 |
Path workDir() { |
208 |
Path result = Path.of("."); |
|
209 |
List<String> components = new ArrayList<>(); |
|
210 |
||
211 |
String testFunctionName = functionName(); |
|
212 |
if (testFunctionName != null) { |
|
213 |
components.add(testFunctionName); |
|
214 |
} |
|
215 |
||
216 |
if (isPrametrized()) { |
|
217 |
components.add(String.format("%08x", fullName().hashCode())); |
|
218 |
} |
|
219 |
||
220 |
if (!components.isEmpty()) { |
|
221 |
result = result.resolve(String.join(".", components)); |
|
222 |
} |
|
223 |
||
224 |
return result; |
|
225 |
} |
|
226 |
||
227 |
boolean isPrametrized() { |
|
228 |
return Stream.of(testDesc.functionArgs, testDesc.instanceArgs).anyMatch( |
|
229 |
Objects::nonNull); |
|
230 |
} |
|
231 |
||
58416 | 232 |
@Override |
233 |
public void run() throws Throwable { |
|
58648 | 234 |
final String fullName = fullName(); |
58416 | 235 |
TKit.log(String.format("[ RUN ] %s", fullName)); |
236 |
try { |
|
237 |
Object testInstance = testConstructor.apply(testBody); |
|
58648 | 238 |
beforeActions.forEach(a -> ThrowingConsumer.toConsumer(a).accept( |
58416 | 239 |
testInstance)); |
240 |
try { |
|
58648 | 241 |
if (!dryRun) { |
242 |
Files.createDirectories(workDir()); |
|
243 |
testBody.accept(testInstance); |
|
244 |
} |
|
58416 | 245 |
} finally { |
246 |
afterActions.forEach(a -> TKit.ignoreExceptions(() -> a.accept( |
|
247 |
testInstance))); |
|
248 |
} |
|
58464
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
249 |
status = Status.Passed; |
58416 | 250 |
} finally { |
58464
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
251 |
if (skippedTestException != null) { |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
252 |
status = Status.Skipped; |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
253 |
} else if (status == null) { |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
254 |
status = Status.Failed; |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
255 |
} |
58648 | 256 |
|
257 |
if (!KEEP_WORK_DIR.contains(status)) { |
|
258 |
TKit.deleteDirectoryRecursive(workDir()); |
|
259 |
} |
|
260 |
||
58464
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
261 |
TKit.log(String.format("%s %s; checks=%d", status, fullName, |
58416 | 262 |
assertCount)); |
263 |
} |
|
264 |
} |
|
265 |
||
266 |
private static Class enclosingMainMethodClass() { |
|
267 |
StackTraceElement st[] = Thread.currentThread().getStackTrace(); |
|
268 |
for (StackTraceElement ste : st) { |
|
269 |
if ("main".equals(ste.getMethodName())) { |
|
270 |
return Functional.ThrowingSupplier.toSupplier(() -> Class.forName( |
|
271 |
ste.getClassName())).get(); |
|
272 |
} |
|
273 |
} |
|
274 |
return null; |
|
275 |
} |
|
276 |
||
58464
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
277 |
private enum Status { |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
278 |
Passed("[ OK ]"), |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
279 |
Failed("[ FAILED ]"), |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
280 |
Skipped("[ SKIPPED ]"); |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
281 |
|
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
282 |
Status(String msg) { |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
283 |
this.msg = msg; |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
284 |
} |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
285 |
|
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
286 |
@Override |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
287 |
public String toString() { |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
288 |
return msg; |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
289 |
} |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
290 |
|
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
291 |
private final String msg; |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
292 |
} |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
293 |
|
58416 | 294 |
private int assertCount; |
58464
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
295 |
private Status status; |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
296 |
private RuntimeException skippedTestException; |
58416 | 297 |
private final TestDesc testDesc; |
298 |
private final ThrowingFunction testConstructor; |
|
299 |
private final ThrowingConsumer testBody; |
|
300 |
private final List<ThrowingConsumer> beforeActions; |
|
301 |
private final List<ThrowingConsumer> afterActions; |
|
58648 | 302 |
private final boolean dryRun; |
303 |
||
304 |
private final static Set<Status> KEEP_WORK_DIR = Functional.identity( |
|
305 |
() -> { |
|
306 |
final String propertyName = "keep-work-dir"; |
|
307 |
Set<String> keepWorkDir = TKit.tokenizeConfigProperty( |
|
308 |
propertyName); |
|
309 |
if (keepWorkDir == null) { |
|
310 |
return Set.of(Status.Failed); |
|
311 |
} |
|
312 |
||
313 |
Predicate<Set<String>> isOneOf = options -> { |
|
314 |
return !Collections.disjoint(keepWorkDir, options); |
|
315 |
}; |
|
316 |
||
317 |
Set<Status> result = new HashSet<>(); |
|
318 |
if (isOneOf.test(Set.of("pass", "p"))) { |
|
319 |
result.add(Status.Passed); |
|
320 |
} |
|
321 |
if (isOneOf.test(Set.of("fail", "f"))) { |
|
322 |
result.add(Status.Failed); |
|
323 |
} |
|
324 |
||
325 |
return Collections.unmodifiableSet(result); |
|
326 |
}).get(); |
|
327 |
||
58416 | 328 |
} |