test/langtools/jdk/jshell/UITesting.java
author coleenp
Wed, 15 Nov 2017 08:14:31 -0500
changeset 47894 352b17f62ff7
parent 47216 71c04702a3d5
child 48275 b2190c70a1ac
permissions -rw-r--r--
8191315: Add serviceability/sa/TestRevPtrsForInvokeDynamic.java to ProblemList.txt Reviewed-by: dholmes, sspitsyn
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
45757
1017cd5d6506 8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents: 44821
diff changeset
    44
    private final boolean laxLineEndings;
1017cd5d6506 8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents: 44821
diff changeset
    45
1017cd5d6506 8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents: 44821
diff changeset
    46
    public UITesting() {
1017cd5d6506 8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents: 44821
diff changeset
    47
        this(false);
1017cd5d6506 8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents: 44821
diff changeset
    48
    }
1017cd5d6506 8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents: 44821
diff changeset
    49
1017cd5d6506 8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents: 44821
diff changeset
    50
    public UITesting(boolean laxLineEndings) {
1017cd5d6506 8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents: 44821
diff changeset
    51
        this.laxLineEndings = laxLineEndings;
1017cd5d6506 8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents: 44821
diff changeset
    52
    }
1017cd5d6506 8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents: 44821
diff changeset
    53
44570
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
    54
    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
    55
        // 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
    56
        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
    57
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    58
        PipeInputStream input = new PipeInputStream();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    59
        StringBuilder out = new StringBuilder();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    60
        PrintStream outS = new PrintStream(new OutputStream() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    61
            @Override public void write(int b) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    62
                synchronized (out) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    63
                    System.out.print((char) b);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    64
                    out.append((char) b);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    65
                    out.notifyAll();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    66
                }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    67
            }
44821
c436e60e7af8 8179002: jdk/jshell/MergedTabShiftTabExpressionTest.java fails intermittently
jlahoda
parents: 44811
diff changeset
    68
            @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
    69
                synchronized (out) {
c436e60e7af8 8179002: jdk/jshell/MergedTabShiftTabExpressionTest.java fails intermittently
jlahoda
parents: 44811
diff changeset
    70
                    String data = new String(b, off, len);
c436e60e7af8 8179002: jdk/jshell/MergedTabShiftTabExpressionTest.java fails intermittently
jlahoda
parents: 44811
diff changeset
    71
                    System.out.print(data);
c436e60e7af8 8179002: jdk/jshell/MergedTabShiftTabExpressionTest.java fails intermittently
jlahoda
parents: 44811
diff changeset
    72
                    out.append(data);
c436e60e7af8 8179002: jdk/jshell/MergedTabShiftTabExpressionTest.java fails intermittently
jlahoda
parents: 44811
diff changeset
    73
                    out.notifyAll();
c436e60e7af8 8179002: jdk/jshell/MergedTabShiftTabExpressionTest.java fails intermittently
jlahoda
parents: 44811
diff changeset
    74
                }
c436e60e7af8 8179002: jdk/jshell/MergedTabShiftTabExpressionTest.java fails intermittently
jlahoda
parents: 44811
diff changeset
    75
            }
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    76
        });
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    77
        Thread runner = new Thread(() -> {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    78
            try {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    79
                JavaShellToolBuilder.builder()
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    80
                        .in(input, input)
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    81
                        .out(outS)
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    82
                        .err(outS)
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    83
                        .promptCapture(true)
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    84
                        .persistence(new HashMap<>())
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    85
                        .locale(Locale.US)
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    86
                        .run("--no-startup");
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    87
            } catch (Exception ex) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    88
                throw new IllegalStateException(ex);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    89
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    90
        });
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    91
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    92
        Writer inputSink = new OutputStreamWriter(input.createOutput()) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    93
            @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    94
            public void write(String str) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    95
                super.write(str);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    96
                flush();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    97
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    98
        };
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
    99
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   100
        runner.start();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   101
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   102
        try {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   103
            waitOutput(out, "\u0005");
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   104
            test.test(inputSink, out);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   105
        } finally {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   106
            inputSink.write("\003\003/exit");
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   107
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   108
            runner.join(1000);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   109
            if (runner.isAlive()) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   110
                runner.stop();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   111
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   112
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   113
    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   114
44570
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
   115
    protected interface Test {
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   116
        public void test(Writer inputSink, StringBuilder out) throws Exception;
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
    private static final long TIMEOUT;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   120
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   121
    static {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   122
        long factor;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   123
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   124
        try {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   125
            factor = (long) Double.parseDouble(System.getProperty("test.timeout.factor", "1"));
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   126
        } catch (NumberFormatException ex) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   127
            factor = 1;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   128
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   129
        TIMEOUT = 60_000 * factor;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   130
    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   131
44570
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
   132
    protected void waitOutput(StringBuilder out, String expected) {
45757
1017cd5d6506 8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents: 44821
diff changeset
   133
        expected = expected.replaceAll("\n", laxLineEndings ? "\r?\n" : System.getProperty("line.separator"));
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   134
        Pattern expectedPattern = Pattern.compile(expected, Pattern.DOTALL);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   135
        synchronized (out) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   136
            long s = System.currentTimeMillis();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   137
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   138
            while (true) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   139
                Matcher m = expectedPattern.matcher(out);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   140
                if (m.find()) {
46923
0172e4350f65 8182297: jshell tool: pasting multiple lines of code truncated
jlahoda
parents: 45757
diff changeset
   141
                    out.delete(0, m.end());
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   142
                    return ;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   143
                }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   144
                long e =  System.currentTimeMillis();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   145
                if ((e - s) > TIMEOUT) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   146
                    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
   147
                }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   148
                try {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   149
                    out.wait(TIMEOUT);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   150
                } catch (InterruptedException ex) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   151
                    ex.printStackTrace();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   152
                }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   153
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   154
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   155
    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   156
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   157
    private String quote(String original) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   158
        StringBuilder output = new StringBuilder();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   159
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   160
        for (char c : original.toCharArray()) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   161
            if (c < 32) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   162
                output.append(String.format("\\u%04X", (int) c));
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   163
            } else {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   164
                output.append(c);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   165
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   166
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   167
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   168
        return output.toString();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   169
    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   170
44570
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
   171
    protected String clearOut(String what) {
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
   172
        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
   173
    }
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
   174
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
   175
    protected String backspace(int n) {
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
   176
        return fill(n, '\010');
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
   177
    }
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
    protected String space(int n) {
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
   180
        return fill(n, ' ');
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
   181
    }
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
    private String fill(int n, char c) {
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
   184
        StringBuilder result = new StringBuilder(n);
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
   185
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
   186
        while (n-- > 0)
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
   187
            result.append(c);
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
   188
7fec4e13a5cf 8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents: 44459
diff changeset
   189
        return result.toString();
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
44811
32c15978a3af 8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents: 44683
diff changeset
   192
    private final ResourceBundle resources;
32c15978a3af 8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents: 44683
diff changeset
   193
    {
32c15978a3af 8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents: 44683
diff changeset
   194
        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
   195
    }
32c15978a3af 8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents: 44683
diff changeset
   196
32c15978a3af 8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents: 44683
diff changeset
   197
    protected String getResource(String key) {
32c15978a3af 8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents: 44683
diff changeset
   198
        return resources.getString(key);
32c15978a3af 8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents: 44683
diff changeset
   199
    }
32c15978a3af 8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents: 44683
diff changeset
   200
32c15978a3af 8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents: 44683
diff changeset
   201
    protected String getMessage(String key, Object... args) {
32c15978a3af 8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents: 44683
diff changeset
   202
        return MessageFormat.format(resources.getString(key), args);
32c15978a3af 8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents: 44683
diff changeset
   203
    }
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   204
    private static class PipeInputStream extends InputStream {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   205
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   206
        private static final int INITIAL_SIZE = 128;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   207
        private int[] buffer = new int[INITIAL_SIZE];
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   208
        private int start;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   209
        private int end;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   210
        private boolean closed;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   211
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   212
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   213
        public synchronized int read() throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   214
            if (start == end && !closed) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   215
                inputNeeded();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   216
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   217
            while (start == end) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   218
                if (closed) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   219
                    return -1;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   220
                }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   221
                try {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   222
                    wait();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   223
                } catch (InterruptedException ex) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   224
                    //ignore
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   225
                }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   226
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   227
            try {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   228
                return buffer[start];
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   229
            } finally {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   230
                start = (start + 1) % buffer.length;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   231
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   232
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   233
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   234
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   235
        public synchronized int read(byte[] b, int off, int len) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   236
            if (b == null) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   237
                throw new NullPointerException();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   238
            } else if (off < 0 || len < 0 || len > b.length - off) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   239
                throw new IndexOutOfBoundsException();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   240
            } else if (len == 0) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   241
                return 0;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   242
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   243
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   244
            int c = read();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   245
            if (c == -1) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   246
                return -1;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   247
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   248
            b[off] = (byte)c;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   249
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   250
            int totalRead = 1;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   251
            while (totalRead < len && start != end) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   252
                int r = read();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   253
                if (r == (-1))
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   254
                    break;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   255
                b[off + totalRead++] = (byte) r;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   256
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   257
            return totalRead;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   258
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   259
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   260
        protected void inputNeeded() throws IOException {}
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   261
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   262
        private synchronized void write(int b) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   263
            if (closed) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   264
                throw new IllegalStateException("Already closed.");
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   265
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   266
            int newEnd = (end + 1) % buffer.length;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   267
            if (newEnd == start) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   268
                //overflow:
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   269
                int[] newBuffer = new int[buffer.length * 2];
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   270
                int rightPart = (end > start ? end : buffer.length) - start;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   271
                int leftPart = end > start ? 0 : start - 1;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   272
                System.arraycopy(buffer, start, newBuffer, 0, rightPart);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   273
                System.arraycopy(buffer, 0, newBuffer, rightPart, leftPart);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   274
                buffer = newBuffer;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   275
                start = 0;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   276
                end = rightPart + leftPart;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   277
                newEnd = end + 1;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   278
            }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   279
            buffer[end] = b;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   280
            end = newEnd;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   281
            notifyAll();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   282
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   283
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   284
        @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   285
        public synchronized void close() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   286
            closed = true;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   287
            notifyAll();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   288
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   289
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   290
        public OutputStream createOutput() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   291
            return new OutputStream() {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   292
                @Override public void write(int b) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   293
                    PipeInputStream.this.write(b);
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   294
                }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   295
                @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   296
                public void write(byte[] b, int off, int len) throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   297
                    for (int i = 0 ; i < len ; i++) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   298
                        write(Byte.toUnsignedInt(b[off + i]));
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   299
                    }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   300
                }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   301
                @Override
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   302
                public void close() throws IOException {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   303
                    PipeInputStream.this.close();
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   304
                }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   305
            };
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   306
        }
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents:
diff changeset
   307
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
}