langtools/test/jdk/jshell/UserInputTest.java
author jlahoda
Tue, 18 Oct 2016 16:00:32 +0200
changeset 41628 664e7664343d
parent 40767 c7908e8c786b
child 43031 2d0b2f38225a
permissions -rw-r--r--
8167461: jshell tool: Scanner#next() hangs tool Summary: PipeInputStream.read(byte[]...) should only read available bytes; properly resending exceptions for snippet's System.in and properly closing it; more reliable way to cancel user input while waiting in System.in. Reviewed-by: rfield
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
     1
/*
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
     4
 *
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
     8
 *
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    13
 * accompanied this code).
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    14
 *
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    18
 *
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    21
 * questions.
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    22
 */
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    23
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    24
/*
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    25
 * @test
41628
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    26
 * @bug 8131023 8167461
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    27
 * @summary Verify that the user's code can read System.in
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    28
 * @build KullaTesting TestingInputStream
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    29
 * @run testng UserInputTest
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    30
 */
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    31
41628
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    32
import java.io.IOException;
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    33
import java.io.InputStream;
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    34
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    35
import org.testng.annotations.Test;
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    36
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    37
@Test
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    38
public class UserInputTest extends KullaTesting {
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    39
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    40
    public void testReadInput() {
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    41
        setInput("AB\n");
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    42
        assertEval("System.in.read()", "65");
41628
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    43
        setInput("CD\n");
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    44
        assertEval("System.in.read()", "67");
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    45
    }
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    46
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    47
    public void testScanner() {
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    48
        assertEval("import java.util.Scanner;");
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    49
        assertEval("Scanner s = new Scanner(System.in);");
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    50
        setInput("12\n");
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    51
        assertEval("s.nextInt();", "12");
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    52
    }
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    53
41628
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    54
    public void testClose() {
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    55
        setInput(new InputStream() {
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    56
            private final byte[] data = new byte[] {0, 1, 2};
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    57
            private int cursor;
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    58
            @Override public int read() throws IOException {
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    59
                if (cursor < data.length) {
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    60
                    return data[cursor++];
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    61
                } else {
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    62
                    return -1;
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    63
                }
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    64
            }
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    65
        });
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    66
        assertEval("int read;", "0");
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    67
        assertEval("System.in.read();", "0");
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    68
        assertEval("System.in.read();", "1");
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    69
        assertEval("System.in.read();", "2");
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    70
        assertEval("System.in.read();", "-1");
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    71
        assertEval("System.in.read();", "-1");
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    72
        assertEval("System.in.read();", "-1");
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    73
    }
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    74
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    75
    public void testException() {
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    76
        setInput(new InputStream() {
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    77
            private final int[] data = new int[] {0, 1, -2, 2};
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    78
            private int cursor;
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    79
            @Override public int read() throws IOException {
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    80
                if (cursor < data.length) {
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    81
                    int d = data[cursor++];
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    82
                    if (d == (-2)) {
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    83
                        throw new IOException("Crashed");
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    84
                    }
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    85
                    return d;
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    86
                } else {
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    87
                    return -1;
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    88
                }
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    89
            }
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    90
        });
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    91
        assertEval("int read;", "0");
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    92
        assertEval("System.in.read();", "0");
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    93
        assertEval("System.in.read();", "1");
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    94
        assertEval("java.io.IOException e;");
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    95
        assertEval("try { System.in.read(); } catch (java.io.IOException exc) { e = exc; }");
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    96
        assertEval("e", "java.io.IOException: Crashed");
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    97
        assertEval("System.in.read();", "2");
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    98
        assertEval("System.in.read();", "-1");
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    99
    }
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
   100
}