|
1 | 1 | /* |
2 | | - * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
23 | 23 |
|
24 | 24 | /** |
25 | 25 | * @test |
26 | | - * @bug 8331681 |
| 26 | + * @bug 8331681 8351435 |
27 | 27 | * @summary Verify the java.base's console provider handles the prompt correctly. |
28 | 28 | * @library /test/lib |
29 | | - * @run main/othervm --limit-modules java.base ConsolePromptTest |
30 | | - * @run main/othervm -Djdk.console=java.base ConsolePromptTest |
31 | 29 | */ |
32 | 30 |
|
| 31 | +import java.lang.reflect.InvocationTargetException; |
33 | 32 | import java.lang.reflect.Method; |
34 | | -import java.util.Objects; |
| 33 | +import java.nio.file.Files; |
| 34 | +import java.nio.file.Paths; |
| 35 | +import java.util.ArrayList; |
| 36 | +import java.util.List; |
35 | 37 |
|
36 | 38 | import jdk.test.lib.process.OutputAnalyzer; |
37 | 39 | import jdk.test.lib.process.ProcessTools; |
| 40 | +import jtreg.SkippedException; |
38 | 41 |
|
39 | 42 | public class ConsolePromptTest { |
40 | 43 |
|
| 44 | + private static final List<List<String>> VARIANTS = List.of( |
| 45 | + List.of("--limit-modules", "java.base"), |
| 46 | + List.of("-Djdk.console=java.base") |
| 47 | + ); |
| 48 | + |
41 | 49 | public static void main(String... args) throws Throwable { |
42 | 50 | for (Method m : ConsolePromptTest.class.getDeclaredMethods()) { |
43 | 51 | if (m.getName().startsWith("test")) { |
44 | | - m.invoke(new ConsolePromptTest()); |
| 52 | + for (List<String> variant : VARIANTS) { |
| 53 | + try { |
| 54 | + m.invoke(new ConsolePromptTest(variant)); |
| 55 | + } catch (InvocationTargetException e) { |
| 56 | + if (e.getCause() instanceof SkippedException se) { |
| 57 | + throw se; |
| 58 | + } else { |
| 59 | + throw e; |
| 60 | + } |
| 61 | + } |
| 62 | + } |
45 | 63 | } |
46 | 64 | } |
47 | 65 | } |
48 | 66 |
|
| 67 | + private final List<String> extraParams; |
| 68 | + |
| 69 | + public ConsolePromptTest(List<String> extraParams) { |
| 70 | + this.extraParams = extraParams; |
| 71 | + } |
| 72 | + |
49 | 73 | void testCorrectOutputReadLine() throws Exception { |
50 | | - doRunConsoleTest("testCorrectOutputReadLine", "inp", "%s"); |
| 74 | + doRunConsoleTest("testCorrectOutputReadLine"); |
51 | 75 | } |
52 | 76 |
|
53 | 77 | void testCorrectOutputReadPassword() throws Exception { |
54 | | - doRunConsoleTest("testCorrectOutputReadPassword", "inp", "%s"); |
| 78 | + doRunConsoleTest("testCorrectOutputReadPassword"); |
55 | 79 | } |
56 | 80 |
|
57 | | - void doRunConsoleTest(String testName, |
58 | | - String input, |
59 | | - String expectedOut) throws Exception { |
60 | | - ProcessBuilder builder = |
61 | | - ProcessTools.createTestJavaProcessBuilder(ConsoleTest.class.getName(), |
62 | | - testName); |
63 | | - OutputAnalyzer output = ProcessTools.executeProcess(builder, input); |
64 | | - |
65 | | - output.waitFor(); |
66 | | - |
67 | | - if (output.getExitValue() != 0) { |
68 | | - throw new AssertionError("Unexpected return value: " + output.getExitValue() + |
69 | | - ", actualOut: " + output.getStdout() + |
70 | | - ", actualErr: " + output.getStderr()); |
| 81 | + void doRunConsoleTest(String testName) throws Exception { |
| 82 | + // check "expect" command availability |
| 83 | + var expect = Paths.get("/usr/bin/expect"); |
| 84 | + if (!Files.exists(expect) || !Files.isExecutable(expect)) { |
| 85 | + throw new SkippedException("'expect' command not found. Test ignored."); |
71 | 86 | } |
72 | 87 |
|
73 | | - String actualOut = output.getStdout(); |
74 | | - |
75 | | - if (!Objects.equals(expectedOut, actualOut)) { |
76 | | - throw new AssertionError("Unexpected stdout content. " + |
77 | | - "Expected: '" + expectedOut + "'" + |
78 | | - ", got: '" + actualOut + "'"); |
79 | | - } |
80 | | - |
81 | | - String expectedErr = ""; |
82 | | - String actualErr = output.getStderr(); |
83 | | - |
84 | | - if (!Objects.equals(expectedErr, actualErr)) { |
85 | | - throw new AssertionError("Unexpected stderr content. " + |
86 | | - "Expected: '" + expectedErr + "'" + |
87 | | - ", got: '" + actualErr + "'"); |
| 88 | + // invoking "expect" command |
| 89 | + var testSrc = System.getProperty("test.src", "."); |
| 90 | + var jdkDir = System.getProperty("test.jdk"); |
| 91 | + |
| 92 | + List<String> command = new ArrayList<>(); |
| 93 | + |
| 94 | + command.add("expect"); |
| 95 | + command.add("-n"); |
| 96 | + command.add(testSrc + "/consolePrompt.exp"); |
| 97 | + command.add("%s"); |
| 98 | + command.add(jdkDir + "/bin/java"); |
| 99 | + command.addAll(extraParams); |
| 100 | + command.add("-cp"); |
| 101 | + command.add(System.getProperty("java.class.path")); |
| 102 | + command.add(ConsoleTest.class.getName()); |
| 103 | + command.add(testName); |
| 104 | + |
| 105 | + OutputAnalyzer output = ProcessTools.executeProcess(command.toArray(String[]::new)); |
| 106 | + output.reportDiagnosticSummary(); |
| 107 | + var eval = output.getExitValue(); |
| 108 | + if (eval != 0) { |
| 109 | + throw new RuntimeException("Test failed. Exit value from 'expect' command: " + eval); |
88 | 110 | } |
89 | 111 | } |
90 | 112 |
|
|
0 commit comments