author | herrick |
Wed, 23 Oct 2019 10:37:54 -0400 | |
branch | JDK-8200758-branch |
changeset 58762 | 0fe62353385b |
parent 58696 | 61c44899b4eb |
child 58888 | d802578912f3 |
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 |
package jdk.jpackage.test; |
|
24 |
||
25 |
import java.io.FileOutputStream; |
|
26 |
import java.io.IOException; |
|
27 |
import java.io.PrintStream; |
|
28 |
import java.lang.reflect.InvocationTargetException; |
|
58648 | 29 |
import java.nio.file.*; |
58416 | 30 |
import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE; |
31 |
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY; |
|
32 |
import java.util.*; |
|
33 |
import java.util.concurrent.TimeUnit; |
|
34 |
import java.util.concurrent.atomic.AtomicInteger; |
|
58648 | 35 |
import java.util.function.BiPredicate; |
36 |
import java.util.function.Consumer; |
|
37 |
import java.util.function.Predicate; |
|
38 |
import java.util.function.Supplier; |
|
58416 | 39 |
import java.util.stream.Collectors; |
40 |
import java.util.stream.Stream; |
|
41 |
import jdk.jpackage.test.Functional.ExceptionBox; |
|
42 |
import jdk.jpackage.test.Functional.ThrowingConsumer; |
|
43 |
import jdk.jpackage.test.Functional.ThrowingRunnable; |
|
44 |
import jdk.jpackage.test.Functional.ThrowingSupplier; |
|
45 |
||
46 |
final public class TKit { |
|
47 |
||
58696
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58648
diff
changeset
|
48 |
private static final String OS = System.getProperty("os.name").toLowerCase(); |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58648
diff
changeset
|
49 |
|
58416 | 50 |
public static final Path TEST_SRC_ROOT = Functional.identity(() -> { |
51 |
Path root = Path.of(System.getProperty("test.src")); |
|
52 |
||
53 |
for (int i = 0; i != 10; ++i) { |
|
54 |
if (root.resolve("apps").toFile().isDirectory()) { |
|
55 |
return root.toAbsolutePath(); |
|
56 |
} |
|
57 |
root = root.resolve(".."); |
|
58 |
} |
|
59 |
||
60 |
throw new RuntimeException("Failed to locate apps directory"); |
|
61 |
}).get(); |
|
62 |
||
58696
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58648
diff
changeset
|
63 |
public final static String ICON_SUFFIX = Functional.identity(() -> { |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58648
diff
changeset
|
64 |
if (isOSX()) { |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58648
diff
changeset
|
65 |
return ".icns"; |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58648
diff
changeset
|
66 |
} |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58648
diff
changeset
|
67 |
|
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58648
diff
changeset
|
68 |
if (isLinux()) { |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58648
diff
changeset
|
69 |
return ".png"; |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58648
diff
changeset
|
70 |
} |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58648
diff
changeset
|
71 |
|
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58648
diff
changeset
|
72 |
if (isWindows()) { |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58648
diff
changeset
|
73 |
return ".ico"; |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58648
diff
changeset
|
74 |
} |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58648
diff
changeset
|
75 |
|
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58648
diff
changeset
|
76 |
throw throwUnknownPlatformError(); |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58648
diff
changeset
|
77 |
}).get(); |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58648
diff
changeset
|
78 |
|
58416 | 79 |
public static void run(String args[], ThrowingRunnable testBody) { |
80 |
if (currentTest != null) { |
|
81 |
throw new IllegalStateException( |
|
82 |
"Unexpeced nested or concurrent Test.run() call"); |
|
83 |
} |
|
84 |
||
85 |
TestInstance test = new TestInstance(testBody); |
|
86 |
ThrowingRunnable.toRunnable(() -> runTests(List.of(test))).run(); |
|
58464
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
87 |
test.rethrowIfSkipped(); |
58416 | 88 |
if (!test.passed()) { |
89 |
throw new RuntimeException(); |
|
90 |
} |
|
91 |
} |
|
92 |
||
58648 | 93 |
static void withExtraLogStream(ThrowingRunnable action) { |
94 |
if (extraLogStream != null) { |
|
95 |
ThrowingRunnable.toRunnable(action).run(); |
|
96 |
} else { |
|
97 |
try (PrintStream logStream = openLogStream()) { |
|
98 |
extraLogStream = logStream; |
|
99 |
ThrowingRunnable.toRunnable(action).run(); |
|
100 |
} finally { |
|
101 |
extraLogStream = null; |
|
102 |
} |
|
103 |
} |
|
104 |
} |
|
105 |
||
58416 | 106 |
static void runTests(List<TestInstance> tests) { |
107 |
if (currentTest != null) { |
|
108 |
throw new IllegalStateException( |
|
109 |
"Unexpeced nested or concurrent Test.run() call"); |
|
110 |
} |
|
111 |
||
58648 | 112 |
withExtraLogStream(() -> { |
58416 | 113 |
tests.stream().forEach(test -> { |
114 |
currentTest = test; |
|
115 |
try { |
|
116 |
ignoreExceptions(test).run(); |
|
117 |
} finally { |
|
118 |
currentTest = null; |
|
119 |
if (extraLogStream != null) { |
|
120 |
extraLogStream.flush(); |
|
121 |
} |
|
122 |
} |
|
123 |
}); |
|
58648 | 124 |
}); |
58416 | 125 |
} |
126 |
||
127 |
static Runnable ignoreExceptions(ThrowingRunnable action) { |
|
128 |
return () -> { |
|
129 |
try { |
|
130 |
try { |
|
131 |
action.run(); |
|
132 |
} catch (Throwable ex) { |
|
133 |
unbox(ex); |
|
134 |
} |
|
135 |
} catch (Throwable throwable) { |
|
58648 | 136 |
printStackTrace(throwable); |
58416 | 137 |
} |
138 |
}; |
|
139 |
} |
|
140 |
||
141 |
static void unbox(Throwable throwable) throws Throwable { |
|
142 |
try { |
|
143 |
throw throwable; |
|
144 |
} catch (ExceptionBox | InvocationTargetException ex) { |
|
145 |
unbox(ex.getCause()); |
|
146 |
} |
|
147 |
} |
|
148 |
||
149 |
public static Path workDir() { |
|
58648 | 150 |
return currentTest.workDir(); |
58416 | 151 |
} |
152 |
||
153 |
static Path defaultInputDir() { |
|
154 |
return workDir().resolve("input"); |
|
155 |
} |
|
156 |
||
157 |
static Path defaultOutputDir() { |
|
158 |
return workDir().resolve("output"); |
|
159 |
} |
|
160 |
||
161 |
static String getCurrentDefaultAppName() { |
|
162 |
// Construct app name from swapping and joining test base name |
|
163 |
// and test function name. |
|
164 |
// Say the test name is `FooTest.testBasic`. Then app name would be `BasicFooTest`. |
|
165 |
String appNamePrefix = currentTest.functionName(); |
|
166 |
if (appNamePrefix != null && appNamePrefix.startsWith("test")) { |
|
167 |
appNamePrefix = appNamePrefix.substring("test".length()); |
|
168 |
} |
|
169 |
return Stream.of(appNamePrefix, currentTest.baseName()).filter( |
|
170 |
v -> v != null && !v.isEmpty()).collect(Collectors.joining()); |
|
171 |
} |
|
172 |
||
173 |
public static boolean isWindows() { |
|
174 |
return (OS.contains("win")); |
|
175 |
} |
|
176 |
||
177 |
public static boolean isOSX() { |
|
178 |
return (OS.contains("mac")); |
|
179 |
} |
|
180 |
||
181 |
public static boolean isLinux() { |
|
182 |
return ((OS.contains("nix") || OS.contains("nux"))); |
|
183 |
} |
|
184 |
||
185 |
static void log(String v) { |
|
186 |
System.out.println(v); |
|
187 |
if (extraLogStream != null) { |
|
188 |
extraLogStream.println(v); |
|
189 |
} |
|
190 |
} |
|
191 |
||
192 |
public static void createTextFile(Path propsFilename, Collection<String> lines) { |
|
58648 | 193 |
createTextFile(propsFilename, lines.stream()); |
194 |
} |
|
195 |
||
196 |
public static void createTextFile(Path propsFilename, Stream<String> lines) { |
|
58416 | 197 |
trace(String.format("Create [%s] text file...", |
198 |
propsFilename.toAbsolutePath().normalize())); |
|
199 |
ThrowingRunnable.toRunnable(() -> Files.write(propsFilename, |
|
58648 | 200 |
lines.peek(TKit::trace).collect(Collectors.toList()))).run(); |
58416 | 201 |
trace("Done"); |
202 |
} |
|
203 |
||
204 |
public static void createPropertiesFile(Path propsFilename, |
|
205 |
Collection<Map.Entry<String, String>> props) { |
|
206 |
trace(String.format("Create [%s] properties file...", |
|
207 |
propsFilename.toAbsolutePath().normalize())); |
|
208 |
ThrowingRunnable.toRunnable(() -> Files.write(propsFilename, |
|
209 |
props.stream().map(e -> String.join("=", e.getKey(), |
|
210 |
e.getValue())).peek(TKit::trace).collect(Collectors.toList()))).run(); |
|
211 |
trace("Done"); |
|
212 |
} |
|
213 |
||
214 |
public static void createPropertiesFile(Path propsFilename, |
|
215 |
Map.Entry<String, String>... props) { |
|
216 |
createPropertiesFile(propsFilename, List.of(props)); |
|
217 |
} |
|
218 |
||
219 |
public static void createPropertiesFile(Path propsFilename, |
|
220 |
Map<String, String> props) { |
|
221 |
createPropertiesFile(propsFilename, props.entrySet()); |
|
222 |
} |
|
223 |
||
224 |
public static void trace(String v) { |
|
225 |
if (TRACE) { |
|
226 |
log("TRACE: " + v); |
|
227 |
} |
|
228 |
} |
|
229 |
||
230 |
private static void traceAssert(String v) { |
|
231 |
if (TRACE_ASSERTS) { |
|
232 |
log("TRACE: " + v); |
|
233 |
} |
|
234 |
} |
|
235 |
||
236 |
public static void error(String v) { |
|
237 |
log("ERROR: " + v); |
|
238 |
throw new AssertionError(v); |
|
239 |
} |
|
240 |
||
241 |
private final static String TEMP_FILE_PREFIX = null; |
|
242 |
||
243 |
private static Path createUniqueFileName(String defaultName) { |
|
244 |
final String[] nameComponents; |
|
245 |
||
246 |
int separatorIdx = defaultName.lastIndexOf('.'); |
|
247 |
final String baseName; |
|
248 |
if (separatorIdx == -1) { |
|
249 |
baseName = defaultName; |
|
250 |
nameComponents = new String[]{baseName}; |
|
251 |
} else { |
|
252 |
baseName = defaultName.substring(0, separatorIdx); |
|
253 |
nameComponents = new String[]{baseName, defaultName.substring( |
|
254 |
separatorIdx + 1)}; |
|
255 |
} |
|
256 |
||
257 |
final Path basedir = workDir(); |
|
258 |
int i = 0; |
|
259 |
for (; i < 100; ++i) { |
|
260 |
Path path = basedir.resolve(String.join(".", nameComponents)); |
|
261 |
if (!path.toFile().exists()) { |
|
262 |
return path; |
|
263 |
} |
|
264 |
nameComponents[0] = String.format("%s.%d", baseName, i); |
|
265 |
} |
|
266 |
throw new IllegalStateException(String.format( |
|
267 |
"Failed to create unique file name from [%s] basename after %d attempts", |
|
268 |
baseName, i)); |
|
269 |
} |
|
270 |
||
271 |
public static Path createTempDirectory(String role) throws IOException { |
|
272 |
if (role == null) { |
|
273 |
return Files.createTempDirectory(workDir(), TEMP_FILE_PREFIX); |
|
274 |
} |
|
275 |
return Files.createDirectory(createUniqueFileName(role)); |
|
276 |
} |
|
277 |
||
278 |
public static Path createTempFile(String role, String suffix) throws |
|
279 |
IOException { |
|
280 |
if (role == null) { |
|
281 |
return Files.createTempFile(workDir(), TEMP_FILE_PREFIX, suffix); |
|
282 |
} |
|
283 |
return Files.createFile(createUniqueFileName(role)); |
|
284 |
} |
|
285 |
||
58648 | 286 |
public static Path withTempFile(String role, String suffix, |
58416 | 287 |
ThrowingConsumer<Path> action) { |
288 |
final Path tempFile = ThrowingSupplier.toSupplier(() -> createTempFile( |
|
289 |
role, suffix)).get(); |
|
290 |
boolean keepIt = true; |
|
291 |
try { |
|
292 |
ThrowingConsumer.toConsumer(action).accept(tempFile); |
|
293 |
keepIt = false; |
|
58648 | 294 |
return tempFile; |
58416 | 295 |
} finally { |
296 |
if (tempFile != null && !keepIt) { |
|
297 |
ThrowingRunnable.toRunnable(() -> Files.deleteIfExists(tempFile)).run(); |
|
298 |
} |
|
299 |
} |
|
300 |
} |
|
301 |
||
58648 | 302 |
public static Path withTempDirectory(String role, |
58416 | 303 |
ThrowingConsumer<Path> action) { |
304 |
final Path tempDir = ThrowingSupplier.toSupplier( |
|
305 |
() -> createTempDirectory(role)).get(); |
|
306 |
boolean keepIt = true; |
|
307 |
try { |
|
308 |
ThrowingConsumer.toConsumer(action).accept(tempDir); |
|
309 |
keepIt = false; |
|
58648 | 310 |
return tempDir; |
58416 | 311 |
} finally { |
312 |
if (tempDir != null && tempDir.toFile().isDirectory() && !keepIt) { |
|
313 |
deleteDirectoryRecursive(tempDir, ""); |
|
314 |
} |
|
315 |
} |
|
316 |
} |
|
317 |
||
58648 | 318 |
private static class DirectoryCleaner implements Consumer<Path> { |
319 |
DirectoryCleaner traceMessage(String v) { |
|
320 |
msg = v; |
|
321 |
return this; |
|
322 |
} |
|
323 |
||
324 |
DirectoryCleaner contentsOnly(boolean v) { |
|
325 |
contentsOnly = v; |
|
326 |
return this; |
|
327 |
} |
|
328 |
||
329 |
@Override |
|
330 |
public void accept(Path root) { |
|
331 |
if (msg == null) { |
|
332 |
if (contentsOnly) { |
|
333 |
msg = String.format("Cleaning [%s] directory recursively", |
|
334 |
root); |
|
335 |
} else { |
|
336 |
msg = String.format("Deleting [%s] directory recursively", |
|
337 |
root); |
|
338 |
} |
|
339 |
} |
|
340 |
||
341 |
if (!msg.isEmpty()) { |
|
342 |
trace(msg); |
|
343 |
} |
|
344 |
||
345 |
List<Throwable> errors = new ArrayList<>(); |
|
346 |
try { |
|
347 |
final List<Path> paths; |
|
348 |
if (contentsOnly) { |
|
349 |
try (var pathStream = Files.walk(root, 0)) { |
|
350 |
paths = pathStream.collect(Collectors.toList()); |
|
351 |
} |
|
352 |
} else { |
|
353 |
paths = List.of(root); |
|
354 |
} |
|
355 |
||
356 |
for (var path : paths) { |
|
357 |
try (var pathStream = Files.walk(path)) { |
|
358 |
pathStream |
|
359 |
.sorted(Comparator.reverseOrder()) |
|
360 |
.sequential() |
|
361 |
.forEachOrdered(file -> { |
|
362 |
try { |
|
363 |
if (isWindows()) { |
|
364 |
Files.setAttribute(file, "dos:readonly", false); |
|
365 |
} |
|
366 |
Files.delete(file); |
|
367 |
} catch (IOException ex) { |
|
368 |
errors.add(ex); |
|
369 |
} |
|
370 |
}); |
|
371 |
} |
|
372 |
} |
|
373 |
||
374 |
} catch (IOException ex) { |
|
375 |
errors.add(ex); |
|
376 |
} |
|
377 |
errors.forEach(error -> trace(error.toString())); |
|
378 |
} |
|
379 |
||
380 |
private String msg; |
|
381 |
private boolean contentsOnly; |
|
382 |
} |
|
383 |
||
58416 | 384 |
/** |
385 |
* Deletes contents of the given directory recursively. Shortcut for |
|
386 |
* <code>deleteDirectoryContentsRecursive(path, null)</code> |
|
387 |
* |
|
388 |
* @param path path to directory to clean |
|
389 |
*/ |
|
390 |
public static void deleteDirectoryContentsRecursive(Path path) { |
|
391 |
deleteDirectoryContentsRecursive(path, null); |
|
392 |
} |
|
393 |
||
394 |
/** |
|
395 |
* Deletes contents of the given directory recursively. If <code>path<code> is not a |
|
396 |
* directory, request is silently ignored. |
|
397 |
* |
|
398 |
* @param path path to directory to clean |
|
399 |
* @param msg log message. If null, the default log message is used. If |
|
400 |
* empty string, no log message will be saved. |
|
401 |
*/ |
|
402 |
public static void deleteDirectoryContentsRecursive(Path path, String msg) { |
|
403 |
if (path.toFile().isDirectory()) { |
|
58648 | 404 |
new DirectoryCleaner().contentsOnly(true).traceMessage(msg).accept( |
405 |
path); |
|
58416 | 406 |
} |
407 |
} |
|
408 |
||
409 |
/** |
|
410 |
* Deletes the given directory recursively. Shortcut for |
|
411 |
* <code>deleteDirectoryRecursive(path, null)</code> |
|
412 |
* |
|
413 |
* @param path path to directory to delete |
|
414 |
*/ |
|
415 |
public static void deleteDirectoryRecursive(Path path) { |
|
416 |
deleteDirectoryRecursive(path, null); |
|
417 |
} |
|
418 |
||
419 |
/** |
|
420 |
* Deletes the given directory recursively. If <code>path<code> is not a |
|
421 |
* directory, request is silently ignored. |
|
422 |
* |
|
423 |
* @param path path to directory to delete |
|
424 |
* @param msg log message. If null, the default log message is used. If |
|
425 |
* empty string, no log message will be saved. |
|
426 |
*/ |
|
427 |
public static void deleteDirectoryRecursive(Path path, String msg) { |
|
428 |
if (path.toFile().isDirectory()) { |
|
58648 | 429 |
new DirectoryCleaner().traceMessage(msg).accept(path); |
58416 | 430 |
} |
431 |
} |
|
432 |
||
433 |
public static RuntimeException throwUnknownPlatformError() { |
|
434 |
if (isWindows() || isLinux() || isOSX()) { |
|
435 |
throw new IllegalStateException( |
|
436 |
"Platform is known. throwUnknownPlatformError() called by mistake"); |
|
437 |
} |
|
438 |
throw new IllegalStateException("Unknown platform"); |
|
439 |
} |
|
440 |
||
58464
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
441 |
public static RuntimeException throwSkippedException(String reason) { |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
442 |
trace("Skip the test: " + reason); |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
443 |
RuntimeException ex = ThrowingSupplier.toSupplier( |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
444 |
() -> (RuntimeException) Class.forName("jtreg.SkippedException").getConstructor( |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
445 |
String.class).newInstance(reason)).get(); |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
446 |
|
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
447 |
currentTest.notifySkipped(ex); |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
448 |
throw ex; |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
449 |
} |
d82489644b15
8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
58416
diff
changeset
|
450 |
|
58648 | 451 |
public static Path createRelativePathCopy(final Path file) { |
452 |
Path fileCopy = workDir().resolve(file.getFileName()).toAbsolutePath().normalize(); |
|
453 |
||
454 |
ThrowingRunnable.toRunnable(() -> Files.copy(file, fileCopy, |
|
455 |
StandardCopyOption.REPLACE_EXISTING)).run(); |
|
456 |
||
457 |
final Path basePath = Path.of(".").toAbsolutePath().normalize(); |
|
458 |
try { |
|
459 |
return basePath.relativize(fileCopy); |
|
460 |
} catch (IllegalArgumentException ex) { |
|
461 |
// May happen on Windows: java.lang.IllegalArgumentException: 'other' has different root |
|
462 |
trace(String.format("Failed to relativize [%s] at [%s]", fileCopy, |
|
463 |
basePath)); |
|
464 |
printStackTrace(ex); |
|
465 |
} |
|
466 |
return file; |
|
467 |
} |
|
468 |
||
58416 | 469 |
static void waitForFileCreated(Path fileToWaitFor, |
470 |
long timeoutSeconds) throws IOException { |
|
471 |
||
472 |
trace(String.format("Wait for file [%s] to be available", fileToWaitFor)); |
|
473 |
||
474 |
WatchService ws = FileSystems.getDefault().newWatchService(); |
|
475 |
||
476 |
Path watchDirectory = fileToWaitFor.toAbsolutePath().getParent(); |
|
477 |
watchDirectory.register(ws, ENTRY_CREATE, ENTRY_MODIFY); |
|
478 |
||
479 |
long waitUntil = System.currentTimeMillis() + timeoutSeconds * 1000; |
|
480 |
for (;;) { |
|
481 |
long timeout = waitUntil - System.currentTimeMillis(); |
|
482 |
assertTrue(timeout > 0, String.format( |
|
483 |
"Check timeout value %d is positive", timeout)); |
|
484 |
||
485 |
WatchKey key = ThrowingSupplier.toSupplier(() -> ws.poll(timeout, |
|
486 |
TimeUnit.MILLISECONDS)).get(); |
|
487 |
if (key == null) { |
|
488 |
if (fileToWaitFor.toFile().exists()) { |
|
489 |
trace(String.format( |
|
490 |
"File [%s] is available after poll timeout expired", |
|
491 |
fileToWaitFor)); |
|
492 |
return; |
|
493 |
} |
|
494 |
assertUnexpected(String.format("Timeout expired", timeout)); |
|
495 |
} |
|
496 |
||
497 |
for (WatchEvent<?> event : key.pollEvents()) { |
|
498 |
if (event.kind() == StandardWatchEventKinds.OVERFLOW) { |
|
499 |
continue; |
|
500 |
} |
|
501 |
Path contextPath = (Path) event.context(); |
|
502 |
if (Files.isSameFile(watchDirectory.resolve(contextPath), |
|
503 |
fileToWaitFor)) { |
|
504 |
trace(String.format("File [%s] is available", fileToWaitFor)); |
|
505 |
return; |
|
506 |
} |
|
507 |
} |
|
508 |
||
509 |
if (!key.reset()) { |
|
510 |
assertUnexpected("Watch key invalidated"); |
|
511 |
} |
|
512 |
} |
|
513 |
} |
|
514 |
||
58648 | 515 |
static void printStackTrace(Throwable throwable) { |
516 |
if (extraLogStream != null) { |
|
517 |
throwable.printStackTrace(extraLogStream); |
|
518 |
} |
|
519 |
throwable.printStackTrace(); |
|
520 |
} |
|
521 |
||
58416 | 522 |
private static String concatMessages(String msg, String msg2) { |
523 |
if (msg2 != null && !msg2.isBlank()) { |
|
524 |
return msg + ": " + msg2; |
|
525 |
} |
|
526 |
return msg; |
|
527 |
} |
|
528 |
||
529 |
public static void assertEquals(long expected, long actual, String msg) { |
|
530 |
currentTest.notifyAssert(); |
|
531 |
if (expected != actual) { |
|
532 |
error(concatMessages(String.format( |
|
533 |
"Expected [%d]. Actual [%d]", expected, actual), |
|
534 |
msg)); |
|
535 |
} |
|
536 |
||
537 |
traceAssert(String.format("assertEquals(%d): %s", expected, msg)); |
|
538 |
} |
|
539 |
||
540 |
public static void assertNotEquals(long expected, long actual, String msg) { |
|
541 |
currentTest.notifyAssert(); |
|
542 |
if (expected == actual) { |
|
543 |
error(concatMessages(String.format("Unexpected [%d] value", actual), |
|
544 |
msg)); |
|
545 |
} |
|
546 |
||
547 |
traceAssert(String.format("assertNotEquals(%d, %d): %s", expected, |
|
548 |
actual, msg)); |
|
549 |
} |
|
550 |
||
551 |
public static void assertEquals(String expected, String actual, String msg) { |
|
552 |
currentTest.notifyAssert(); |
|
553 |
if ((actual != null && !actual.equals(expected)) |
|
554 |
|| (expected != null && !expected.equals(actual))) { |
|
555 |
error(concatMessages(String.format( |
|
556 |
"Expected [%s]. Actual [%s]", expected, actual), |
|
557 |
msg)); |
|
558 |
} |
|
559 |
||
560 |
traceAssert(String.format("assertEquals(%s): %s", expected, msg)); |
|
561 |
} |
|
562 |
||
563 |
public static void assertNotEquals(String expected, String actual, String msg) { |
|
564 |
currentTest.notifyAssert(); |
|
565 |
if ((actual != null && !actual.equals(expected)) |
|
566 |
|| (expected != null && !expected.equals(actual))) { |
|
567 |
||
568 |
traceAssert(String.format("assertNotEquals(%s, %s): %s", expected, |
|
569 |
actual, msg)); |
|
570 |
return; |
|
571 |
} |
|
572 |
||
573 |
error(concatMessages(String.format("Unexpected [%s] value", actual), msg)); |
|
574 |
} |
|
575 |
||
576 |
public static void assertNull(Object value, String msg) { |
|
577 |
currentTest.notifyAssert(); |
|
578 |
if (value != null) { |
|
579 |
error(concatMessages(String.format("Unexpected not null value [%s]", |
|
580 |
value), msg)); |
|
581 |
} |
|
582 |
||
583 |
traceAssert(String.format("assertNull(): %s", msg)); |
|
584 |
} |
|
585 |
||
586 |
public static void assertNotNull(Object value, String msg) { |
|
587 |
currentTest.notifyAssert(); |
|
588 |
if (value == null) { |
|
589 |
error(concatMessages("Unexpected null value", msg)); |
|
590 |
} |
|
591 |
||
592 |
traceAssert(String.format("assertNotNull(%s): %s", value, msg)); |
|
593 |
} |
|
594 |
||
595 |
public static void assertTrue(boolean actual, String msg) { |
|
58762
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
596 |
assertTrue(actual, msg, null); |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
597 |
} |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
598 |
|
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
599 |
public static void assertFalse(boolean actual, String msg) { |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
600 |
assertFalse(actual, msg, null); |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
601 |
} |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
602 |
|
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
603 |
public static void assertTrue(boolean actual, String msg, Runnable onFail) { |
58416 | 604 |
currentTest.notifyAssert(); |
605 |
if (!actual) { |
|
58762
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
606 |
if (onFail != null) { |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
607 |
onFail.run(); |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
608 |
} |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
609 |
error(concatMessages("Failed", msg)); |
58416 | 610 |
} |
611 |
||
612 |
traceAssert(String.format("assertTrue(): %s", msg)); |
|
613 |
} |
|
614 |
||
58762
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
615 |
public static void assertFalse(boolean actual, String msg, Runnable onFail) { |
58416 | 616 |
currentTest.notifyAssert(); |
617 |
if (actual) { |
|
58762
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
618 |
if (onFail != null) { |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
619 |
onFail.run(); |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
620 |
} |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
621 |
error(concatMessages("Failed", msg)); |
58416 | 622 |
} |
623 |
||
624 |
traceAssert(String.format("assertFalse(): %s", msg)); |
|
625 |
} |
|
626 |
||
627 |
public static void assertPathExists(Path path, boolean exists) { |
|
628 |
if (exists) { |
|
629 |
assertTrue(path.toFile().exists(), String.format( |
|
630 |
"Check [%s] path exists", path)); |
|
631 |
} else { |
|
632 |
assertFalse(path.toFile().exists(), String.format( |
|
633 |
"Check [%s] path doesn't exist", path)); |
|
634 |
} |
|
635 |
} |
|
636 |
||
637 |
public static void assertDirectoryExists(Path path) { |
|
638 |
assertPathExists(path, true); |
|
639 |
assertTrue(path.toFile().isDirectory(), String.format( |
|
640 |
"Check [%s] is a directory", path)); |
|
641 |
} |
|
642 |
||
643 |
public static void assertFileExists(Path path) { |
|
644 |
assertPathExists(path, true); |
|
645 |
assertTrue(path.toFile().isFile(), String.format("Check [%s] is a file", |
|
646 |
path)); |
|
647 |
} |
|
648 |
||
649 |
public static void assertExecutableFileExists(Path path) { |
|
650 |
assertFileExists(path); |
|
651 |
assertTrue(path.toFile().canExecute(), String.format( |
|
652 |
"Check [%s] file is executable", path)); |
|
653 |
} |
|
654 |
||
655 |
public static void assertReadableFileExists(Path path) { |
|
656 |
assertFileExists(path); |
|
657 |
assertTrue(path.toFile().canRead(), String.format( |
|
658 |
"Check [%s] file is readable", path)); |
|
659 |
} |
|
660 |
||
661 |
public static void assertUnexpected(String msg) { |
|
662 |
currentTest.notifyAssert(); |
|
663 |
error(concatMessages("Unexpected", msg)); |
|
664 |
} |
|
665 |
||
666 |
public static void assertStringListEquals(List<String> expected, |
|
667 |
List<String> actual, String msg) { |
|
668 |
currentTest.notifyAssert(); |
|
669 |
||
670 |
traceAssert(String.format("assertStringListEquals(): %s", msg)); |
|
671 |
||
672 |
String idxFieldFormat = Functional.identity(() -> { |
|
673 |
int listSize = expected.size(); |
|
674 |
int width = 0; |
|
675 |
while (listSize != 0) { |
|
676 |
listSize = listSize / 10; |
|
677 |
width++; |
|
678 |
} |
|
679 |
return "%" + width + "d"; |
|
680 |
}).get(); |
|
681 |
||
682 |
AtomicInteger counter = new AtomicInteger(0); |
|
683 |
Iterator<String> actualIt = actual.iterator(); |
|
684 |
expected.stream().sequential().filter(expectedStr -> actualIt.hasNext()).forEach(expectedStr -> { |
|
685 |
int idx = counter.incrementAndGet(); |
|
686 |
String actualStr = actualIt.next(); |
|
687 |
||
688 |
if ((actualStr != null && !actualStr.equals(expectedStr)) |
|
689 |
|| (expectedStr != null && !expectedStr.equals(actualStr))) { |
|
690 |
error(concatMessages(String.format( |
|
691 |
"(" + idxFieldFormat + ") Expected [%s]. Actual [%s]", |
|
692 |
idx, expectedStr, actualStr), msg)); |
|
693 |
} |
|
694 |
||
695 |
traceAssert(String.format( |
|
696 |
"assertStringListEquals(" + idxFieldFormat + ", %s)", idx, |
|
697 |
expectedStr)); |
|
698 |
}); |
|
699 |
||
700 |
if (expected.size() < actual.size()) { |
|
701 |
// Actual string list is longer than expected |
|
702 |
error(concatMessages(String.format( |
|
703 |
"Actual list is longer than expected by %d elements", |
|
704 |
actual.size() - expected.size()), msg)); |
|
705 |
} |
|
706 |
||
707 |
if (actual.size() < expected.size()) { |
|
708 |
// Actual string list is shorter than expected |
|
709 |
error(concatMessages(String.format( |
|
710 |
"Actual list is longer than expected by %d elements", |
|
711 |
expected.size() - actual.size()), msg)); |
|
712 |
} |
|
713 |
} |
|
714 |
||
58648 | 715 |
public final static class TextStreamAsserter { |
716 |
TextStreamAsserter(String value) { |
|
717 |
this.value = value; |
|
718 |
predicate(String::contains); |
|
719 |
} |
|
720 |
||
721 |
public TextStreamAsserter label(String v) { |
|
722 |
label = v; |
|
723 |
return this; |
|
724 |
} |
|
725 |
||
726 |
public TextStreamAsserter predicate(BiPredicate<String, String> v) { |
|
727 |
predicate = v; |
|
728 |
return this; |
|
729 |
} |
|
730 |
||
731 |
public TextStreamAsserter negate() { |
|
732 |
negate = true; |
|
733 |
return this; |
|
734 |
} |
|
735 |
||
736 |
public TextStreamAsserter orElseThrow(RuntimeException v) { |
|
737 |
return orElseThrow(() -> v); |
|
738 |
} |
|
739 |
||
740 |
public TextStreamAsserter orElseThrow(Supplier<RuntimeException> v) { |
|
741 |
createException = v; |
|
742 |
return this; |
|
743 |
} |
|
744 |
||
745 |
public void apply(Stream<String> lines) { |
|
746 |
String matchedStr = lines.filter(line -> predicate.test(line, value)).findFirst().orElse( |
|
747 |
null); |
|
748 |
String labelStr = Optional.ofNullable(label).orElse("output"); |
|
749 |
if (negate) { |
|
750 |
String msg = String.format( |
|
751 |
"Check %s doesn't contain [%s] string", labelStr, value); |
|
752 |
if (createException == null) { |
|
753 |
assertNull(matchedStr, msg); |
|
754 |
} else { |
|
755 |
trace(msg); |
|
756 |
if (matchedStr != null) { |
|
757 |
throw createException.get(); |
|
758 |
} |
|
759 |
} |
|
760 |
} else { |
|
761 |
String msg = String.format("Check %s contains [%s] string", |
|
762 |
labelStr, value); |
|
763 |
if (createException == null) { |
|
764 |
assertNotNull(matchedStr, msg); |
|
765 |
} else { |
|
766 |
trace(msg); |
|
767 |
if (matchedStr == null) { |
|
768 |
throw createException.get(); |
|
769 |
} |
|
770 |
} |
|
771 |
} |
|
772 |
} |
|
773 |
||
774 |
private BiPredicate<String, String> predicate; |
|
775 |
private String label; |
|
776 |
private boolean negate; |
|
777 |
private Supplier<RuntimeException> createException; |
|
778 |
final private String value; |
|
779 |
} |
|
780 |
||
781 |
public static TextStreamAsserter assertTextStream(String what) { |
|
782 |
return new TextStreamAsserter(what); |
|
783 |
} |
|
784 |
||
58416 | 785 |
private static PrintStream openLogStream() { |
786 |
if (LOG_FILE == null) { |
|
787 |
return null; |
|
788 |
} |
|
789 |
||
790 |
return ThrowingSupplier.toSupplier(() -> new PrintStream( |
|
791 |
new FileOutputStream(LOG_FILE.toFile(), true))).get(); |
|
792 |
} |
|
793 |
||
794 |
private static TestInstance currentTest; |
|
795 |
private static PrintStream extraLogStream; |
|
796 |
||
797 |
private static final boolean TRACE; |
|
798 |
private static final boolean TRACE_ASSERTS; |
|
799 |
||
800 |
static final boolean VERBOSE_JPACKAGE; |
|
801 |
static final boolean VERBOSE_TEST_SETUP; |
|
802 |
||
803 |
static String getConfigProperty(String propertyName) { |
|
804 |
return System.getProperty(getConfigPropertyName(propertyName)); |
|
805 |
} |
|
806 |
||
807 |
static String getConfigPropertyName(String propertyName) { |
|
808 |
return "jpackage.test." + propertyName; |
|
809 |
} |
|
810 |
||
58648 | 811 |
static Set<String> tokenizeConfigProperty(String propertyName) { |
812 |
final String val = TKit.getConfigProperty(propertyName); |
|
813 |
if (val == null) { |
|
814 |
return null; |
|
815 |
} |
|
816 |
return Stream.of(val.toLowerCase().split(",")).map(String::strip).filter( |
|
817 |
Predicate.not(String::isEmpty)).collect(Collectors.toSet()); |
|
818 |
} |
|
819 |
||
58416 | 820 |
static final Path LOG_FILE = Functional.identity(() -> { |
821 |
String val = getConfigProperty("logfile"); |
|
822 |
if (val == null) { |
|
823 |
return null; |
|
824 |
} |
|
825 |
return Path.of(val); |
|
826 |
}).get(); |
|
827 |
||
828 |
static { |
|
58648 | 829 |
Set<String> logOptions = tokenizeConfigProperty("suppress-logging"); |
830 |
if (logOptions == null) { |
|
58416 | 831 |
TRACE = true; |
832 |
TRACE_ASSERTS = true; |
|
833 |
VERBOSE_JPACKAGE = true; |
|
834 |
VERBOSE_TEST_SETUP = true; |
|
58648 | 835 |
} else if (logOptions.contains("all")) { |
58416 | 836 |
TRACE = false; |
837 |
TRACE_ASSERTS = false; |
|
838 |
VERBOSE_JPACKAGE = false; |
|
839 |
VERBOSE_TEST_SETUP = false; |
|
840 |
} else { |
|
58648 | 841 |
Predicate<Set<String>> isNonOf = options -> { |
842 |
return Collections.disjoint(logOptions, options); |
|
843 |
}; |
|
844 |
||
845 |
TRACE = isNonOf.test(Set.of("trace", "t")); |
|
846 |
TRACE_ASSERTS = isNonOf.test(Set.of("assert", "a")); |
|
847 |
VERBOSE_JPACKAGE = isNonOf.test(Set.of("jpackage", "jp")); |
|
848 |
VERBOSE_TEST_SETUP = isNonOf.test(Set.of("init", "i")); |
|
58416 | 849 |
} |
850 |
} |
|
851 |
} |