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