test/langtools/jdk/jshell/UITesting.java
author rfield
Thu, 07 Dec 2017 13:23:18 -0800
changeset 48275 b2190c70a1ac
parent 47216 71c04702a3d5
child 48347 4f9683bf0923
permissions -rw-r--r--
8192863: jshell tool: /<id><tab> gives "No such command" Reviewed-by: jlahoda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
     1
/*
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
     4
 *
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
     8
 *
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    13
 * accompanied this code).
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    14
 *
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    18
 *
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    21
 * questions.
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    22
 */
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    23
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    24
import java.io.IOException;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    25
import java.io.InputStream;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    26
import java.io.OutputStream;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    27
import java.io.OutputStreamWriter;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    28
import java.io.PrintStream;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    29
import java.io.Writer;
44811
32c15978a3af 8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents: 44683
diff changeset
    30
import java.text.MessageFormat;
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    31
import java.util.HashMap;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    32
import java.util.Locale;
44811
32c15978a3af 8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents: 44683
diff changeset
    33
import java.util.ResourceBundle;
44683
610dc2b48954 8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents: 44570
diff changeset
    34
import java.util.logging.Level;
610dc2b48954 8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents: 44570
diff changeset
    35
import java.util.logging.Logger;
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    36
import java.util.regex.Matcher;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    37
import java.util.regex.Pattern;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    38
44811
32c15978a3af 8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents: 44683
diff changeset
    39
import jdk.jshell.JShell;
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    40
import jdk.jshell.tool.JavaShellToolBuilder;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    41
44570
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
    42
public class UITesting {
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    43
48275
b2190c70a1ac 8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents: 47216
diff changeset
    44
    protected static final String TAB = "\011";
b2190c70a1ac 8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents: 47216
diff changeset
    45
    protected static final String INTERRUPT = "\u0003";
b2190c70a1ac 8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents: 47216
diff changeset
    46
    protected static final String BELL = "\u0007";
b2190c70a1ac 8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents: 47216
diff changeset
    47
    protected static final String PROMPT = "\u0005";
b2190c70a1ac 8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents: 47216
diff changeset
    48
    protected static final String REDRAW_PROMPT = "\n\r" + PROMPT;
45757
1017cd5d6506 8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents: 44821
diff changeset
    49
    private final boolean laxLineEndings;
1017cd5d6506 8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents: 44821
diff changeset
    50
1017cd5d6506 8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents: 44821
diff changeset
    51
    public UITesting() {
1017cd5d6506 8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents: 44821
diff changeset
    52
        this(false);
1017cd5d6506 8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents: 44821
diff changeset
    53
    }
1017cd5d6506 8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents: 44821
diff changeset
    54
1017cd5d6506 8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents: 44821
diff changeset
    55
    public UITesting(boolean laxLineEndings) {
1017cd5d6506 8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents: 44821
diff changeset
    56
        this.laxLineEndings = laxLineEndings;
1017cd5d6506 8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents: 44821
diff changeset
    57
    }
1017cd5d6506 8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents: 44821
diff changeset
    58
44570
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
    59
    protected void doRunTest(Test test) throws Exception {
44683
610dc2b48954 8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents: 44570
diff changeset
    60
        // turn on logging of launch failures
610dc2b48954 8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents: 44570
diff changeset
    61
        Logger.getLogger("jdk.jshell.execution").setLevel(Level.ALL);
610dc2b48954 8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents: 44570
diff changeset
    62
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    63
        PipeInputStream input = new PipeInputStream();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    64
        StringBuilder out = new StringBuilder();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    65
        PrintStream outS = new PrintStream(new OutputStream() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    66
            @Override public void write(int b) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    67
                synchronized (out) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    68
                    System.out.print((char) b);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    69
                    out.append((char) b);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    70
                    out.notifyAll();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    71
                }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    72
            }
44821
c436e60e7af8 8179002: jdk/jshell/MergedTabShiftTabExpressionTest.java fails intermittently
jlahoda
parents: 44811
diff changeset
    73
            @Override public void write(byte[] b, int off, int len) throws IOException {
c436e60e7af8 8179002: jdk/jshell/MergedTabShiftTabExpressionTest.java fails intermittently
jlahoda
parents: 44811
diff changeset
    74
                synchronized (out) {
c436e60e7af8 8179002: jdk/jshell/MergedTabShiftTabExpressionTest.java fails intermittently
jlahoda
parents: 44811
diff changeset
    75
                    String data = new String(b, off, len);
c436e60e7af8 8179002: jdk/jshell/MergedTabShiftTabExpressionTest.java fails intermittently
jlahoda
parents: 44811
diff changeset
    76
                    System.out.print(data);
c436e60e7af8 8179002: jdk/jshell/MergedTabShiftTabExpressionTest.java fails intermittently
jlahoda
parents: 44811
diff changeset
    77
                    out.append(data);
c436e60e7af8 8179002: jdk/jshell/MergedTabShiftTabExpressionTest.java fails intermittently
jlahoda
parents: 44811
diff changeset
    78
                    out.notifyAll();
c436e60e7af8 8179002: jdk/jshell/MergedTabShiftTabExpressionTest.java fails intermittently
jlahoda
parents: 44811
diff changeset
    79
                }
c436e60e7af8 8179002: jdk/jshell/MergedTabShiftTabExpressionTest.java fails intermittently
jlahoda
parents: 44811
diff changeset
    80
            }
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    81
        });
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    82
        Thread runner = new Thread(() -> {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    83
            try {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    84
                JavaShellToolBuilder.builder()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    85
                        .in(input, input)
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    86
                        .out(outS)
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    87
                        .err(outS)
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    88
                        .promptCapture(true)
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    89
                        .persistence(new HashMap<>())
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    90
                        .locale(Locale.US)
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    91
                        .run("--no-startup");
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    92
            } catch (Exception ex) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    93
                throw new IllegalStateException(ex);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    94
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    95
        });
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    96
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    97
        Writer inputSink = new OutputStreamWriter(input.createOutput()) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    98
            @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    99
            public void write(String str) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   100
                super.write(str);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   101
                flush();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   102
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   103
        };
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   104
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   105
        runner.start();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   106
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   107
        try {
48275
b2190c70a1ac 8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents: 47216
diff changeset
   108
            waitOutput(out, PROMPT);
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   109
            test.test(inputSink, out);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   110
        } finally {
48275
b2190c70a1ac 8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents: 47216
diff changeset
   111
            inputSink.write(INTERRUPT + INTERRUPT + "/exit");
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   112
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   113
            runner.join(1000);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   114
            if (runner.isAlive()) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   115
                runner.stop();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   116
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   117
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   118
    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   119
44570
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
   120
    protected interface Test {
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   121
        public void test(Writer inputSink, StringBuilder out) throws Exception;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   122
    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   123
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   124
    private static final long TIMEOUT;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   125
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   126
    static {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   127
        long factor;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   128
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   129
        try {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   130
            factor = (long) Double.parseDouble(System.getProperty("test.timeout.factor", "1"));
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   131
        } catch (NumberFormatException ex) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   132
            factor = 1;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   133
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   134
        TIMEOUT = 60_000 * factor;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   135
    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   136
44570
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
   137
    protected void waitOutput(StringBuilder out, String expected) {
45757
1017cd5d6506 8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents: 44821
diff changeset
   138
        expected = expected.replaceAll("\n", laxLineEndings ? "\r?\n" : System.getProperty("line.separator"));
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   139
        Pattern expectedPattern = Pattern.compile(expected, Pattern.DOTALL);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   140
        synchronized (out) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   141
            long s = System.currentTimeMillis();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   142
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   143
            while (true) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   144
                Matcher m = expectedPattern.matcher(out);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   145
                if (m.find()) {
46923
0172e4350f65 8182297: jshell tool: pasting multiple lines of code truncated
jlahoda
parents: 45757
diff changeset
   146
                    out.delete(0, m.end());
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   147
                    return ;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   148
                }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   149
                long e =  System.currentTimeMillis();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   150
                if ((e - s) > TIMEOUT) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   151
                    throw new IllegalStateException("Timeout waiting for: " + quote(expected) + ", actual output so far: " + quote(out.toString()));
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   152
                }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   153
                try {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   154
                    out.wait(TIMEOUT);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   155
                } catch (InterruptedException ex) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   156
                    ex.printStackTrace();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   157
                }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   158
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   159
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   160
    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   161
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   162
    private String quote(String original) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   163
        StringBuilder output = new StringBuilder();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   164
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   165
        for (char c : original.toCharArray()) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   166
            if (c < 32) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   167
                output.append(String.format("\\u%04X", (int) c));
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   168
            } else {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   169
                output.append(c);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   170
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   171
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   172
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   173
        return output.toString();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   174
    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   175
44570
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
   176
    protected String clearOut(String what) {
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
   177
        return backspace(what.length()) + space(what.length()) + backspace(what.length());
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
   178
    }
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
   179
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
   180
    protected String backspace(int n) {
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
   181
        return fill(n, '\010');
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
   182
    }
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
   183
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
   184
    protected String space(int n) {
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
   185
        return fill(n, ' ');
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
   186
    }
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
   187
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
   188
    private String fill(int n, char c) {
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
   189
        StringBuilder result = new StringBuilder(n);
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
   190
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
   191
        while (n-- > 0)
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
   192
            result.append(c);
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
   193
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
   194
        return result.toString();
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
   195
    }
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
   196
44811
32c15978a3af 8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents: 44683
diff changeset
   197
    private final ResourceBundle resources;
32c15978a3af 8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents: 44683
diff changeset
   198
    {
32c15978a3af 8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents: 44683
diff changeset
   199
        resources = ResourceBundle.getBundle("jdk.internal.jshell.tool.resources.l10n", Locale.US, JShell.class.getModule());
32c15978a3af 8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents: 44683
diff changeset
   200
    }
32c15978a3af 8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents: 44683
diff changeset
   201
32c15978a3af 8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents: 44683
diff changeset
   202
    protected String getResource(String key) {
32c15978a3af 8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents: 44683
diff changeset
   203
        return resources.getString(key);
32c15978a3af 8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents: 44683
diff changeset
   204
    }
32c15978a3af 8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents: 44683
diff changeset
   205
48275
b2190c70a1ac 8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents: 47216
diff changeset
   206
    protected String resource(String key) {
b2190c70a1ac 8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents: 47216
diff changeset
   207
        return Pattern.quote(getResource(key).replaceAll("\t", "    "));
b2190c70a1ac 8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents: 47216
diff changeset
   208
    }
b2190c70a1ac 8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents: 47216
diff changeset
   209
44811
32c15978a3af 8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents: 44683
diff changeset
   210
    protected String getMessage(String key, Object... args) {
32c15978a3af 8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents: 44683
diff changeset
   211
        return MessageFormat.format(resources.getString(key), args);
32c15978a3af 8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents: 44683
diff changeset
   212
    }
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   213
    private static class PipeInputStream extends InputStream {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   214
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   215
        private static final int INITIAL_SIZE = 128;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   216
        private int[] buffer = new int[INITIAL_SIZE];
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   217
        private int start;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   218
        private int end;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   219
        private boolean closed;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   220
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   221
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   222
        public synchronized int read() throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   223
            if (start == end && !closed) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   224
                inputNeeded();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   225
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   226
            while (start == end) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   227
                if (closed) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   228
                    return -1;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   229
                }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   230
                try {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   231
                    wait();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   232
                } catch (InterruptedException ex) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   233
                    //ignore
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   234
                }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   235
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   236
            try {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   237
                return buffer[start];
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   238
            } finally {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   239
                start = (start + 1) % buffer.length;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   240
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   241
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   242
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   243
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   244
        public synchronized int read(byte[] b, int off, int len) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   245
            if (b == null) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   246
                throw new NullPointerException();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   247
            } else if (off < 0 || len < 0 || len > b.length - off) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   248
                throw new IndexOutOfBoundsException();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   249
            } else if (len == 0) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   250
                return 0;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   251
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   252
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   253
            int c = read();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   254
            if (c == -1) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   255
                return -1;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   256
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   257
            b[off] = (byte)c;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   258
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   259
            int totalRead = 1;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   260
            while (totalRead < len && start != end) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   261
                int r = read();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   262
                if (r == (-1))
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   263
                    break;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   264
                b[off + totalRead++] = (byte) r;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   265
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   266
            return totalRead;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   267
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   268
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   269
        protected void inputNeeded() throws IOException {}
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   270
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   271
        private synchronized void write(int b) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   272
            if (closed) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   273
                throw new IllegalStateException("Already closed.");
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   274
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   275
            int newEnd = (end + 1) % buffer.length;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   276
            if (newEnd == start) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   277
                //overflow:
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   278
                int[] newBuffer = new int[buffer.length * 2];
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   279
                int rightPart = (end > start ? end : buffer.length) - start;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   280
                int leftPart = end > start ? 0 : start - 1;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   281
                System.arraycopy(buffer, start, newBuffer, 0, rightPart);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   282
                System.arraycopy(buffer, 0, newBuffer, rightPart, leftPart);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   283
                buffer = newBuffer;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   284
                start = 0;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   285
                end = rightPart + leftPart;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   286
                newEnd = end + 1;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   287
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   288
            buffer[end] = b;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   289
            end = newEnd;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   290
            notifyAll();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   291
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   292
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   293
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   294
        public synchronized void close() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   295
            closed = true;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   296
            notifyAll();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   297
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   298
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   299
        public OutputStream createOutput() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   300
            return new OutputStream() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   301
                @Override public void write(int b) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   302
                    PipeInputStream.this.write(b);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   303
                }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   304
                @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   305
                public void write(byte[] b, int off, int len) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   306
                    for (int i = 0 ; i < len ; i++) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   307
                        write(Byte.toUnsignedInt(b[off + i]));
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   308
                    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   309
                }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   310
                @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   311
                public void close() throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   312
                    PipeInputStream.this.close();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   313
                }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   314
            };
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   315
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   316
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   317
    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   318
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   319
}