langtools/test/jdk/jshell/UserInputTest.java
author rfield
Mon, 09 Jan 2017 18:04:16 -0800
changeset 43134 006808ae5f6e
parent 43031 2d0b2f38225a
permissions -rw-r--r--
8171981: JShell: Fails compilation: new Object().getClass().getSuperclass() Reviewed-by: mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
     1
/*
43031
2d0b2f38225a 8172220: Mark UserInputTest.java as intermittently failing and problem list it
darcy
parents: 41628
diff changeset
     2
 * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
40767
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
43031
2d0b2f38225a 8172220: Mark UserInputTest.java as intermittently failing and problem list it
darcy
parents: 41628
diff changeset
    30
 * @key intermittent
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    31
 */
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    32
41628
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    33
import java.io.IOException;
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    34
import java.io.InputStream;
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    35
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    36
import org.testng.annotations.Test;
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    37
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    38
@Test
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    39
public class UserInputTest extends KullaTesting {
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    40
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    41
    public void testReadInput() {
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    42
        setInput("AB\n");
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    43
        assertEval("System.in.read()", "65");
41628
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    44
        setInput("CD\n");
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    45
        assertEval("System.in.read()", "67");
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
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    48
    public void testScanner() {
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    49
        assertEval("import java.util.Scanner;");
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    50
        assertEval("Scanner s = new Scanner(System.in);");
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    51
        setInput("12\n");
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    52
        assertEval("s.nextInt();", "12");
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    53
    }
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
    54
41628
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    55
    public void testClose() {
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    56
        setInput(new InputStream() {
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    57
            private final byte[] data = new byte[] {0, 1, 2};
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    58
            private int cursor;
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    59
            @Override public int read() throws IOException {
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    60
                if (cursor < data.length) {
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    61
                    return data[cursor++];
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    62
                } else {
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    63
                    return -1;
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
        });
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    67
        assertEval("int read;", "0");
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    68
        assertEval("System.in.read();", "0");
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    69
        assertEval("System.in.read();", "1");
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    70
        assertEval("System.in.read();", "2");
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
        assertEval("System.in.read();", "-1");
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
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    76
    public void testException() {
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    77
        setInput(new InputStream() {
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    78
            private final int[] data = new int[] {0, 1, -2, 2};
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    79
            private int cursor;
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    80
            @Override public int read() throws IOException {
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    81
                if (cursor < data.length) {
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    82
                    int d = data[cursor++];
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    83
                    if (d == (-2)) {
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    84
                        throw new IOException("Crashed");
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    85
                    }
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    86
                    return d;
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    87
                } else {
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    88
                    return -1;
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
        });
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    92
        assertEval("int read;", "0");
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    93
        assertEval("System.in.read();", "0");
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    94
        assertEval("System.in.read();", "1");
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    95
        assertEval("java.io.IOException e;");
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    96
        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
    97
        assertEval("e", "java.io.IOException: Crashed");
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    98
        assertEval("System.in.read();", "2");
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
    99
        assertEval("System.in.read();", "-1");
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents: 40767
diff changeset
   100
    }
40767
c7908e8c786b 8131023: JShell: System.in does not work
jlahoda
parents:
diff changeset
   101
}