langtools/test/jdk/jshell/UserInputTest.java
changeset 41628 664e7664343d
parent 40767 c7908e8c786b
child 43031 2d0b2f38225a
--- a/langtools/test/jdk/jshell/UserInputTest.java	Wed Jul 05 22:21:06 2017 +0200
+++ b/langtools/test/jdk/jshell/UserInputTest.java	Tue Oct 18 16:00:32 2016 +0200
@@ -23,12 +23,15 @@
 
 /*
  * @test
- * @bug 8131023
+ * @bug 8131023 8167461
  * @summary Verify that the user's code can read System.in
  * @build KullaTesting TestingInputStream
  * @run testng UserInputTest
  */
 
+import java.io.IOException;
+import java.io.InputStream;
+
 import org.testng.annotations.Test;
 
 @Test
@@ -37,8 +40,61 @@
     public void testReadInput() {
         setInput("AB\n");
         assertEval("System.in.read()", "65");
-        setInput("BC\n");
-        assertEval("System.in.read()", "66");
+        setInput("CD\n");
+        assertEval("System.in.read()", "67");
+    }
+
+    public void testScanner() {
+        assertEval("import java.util.Scanner;");
+        assertEval("Scanner s = new Scanner(System.in);");
+        setInput("12\n");
+        assertEval("s.nextInt();", "12");
     }
 
+    public void testClose() {
+        setInput(new InputStream() {
+            private final byte[] data = new byte[] {0, 1, 2};
+            private int cursor;
+            @Override public int read() throws IOException {
+                if (cursor < data.length) {
+                    return data[cursor++];
+                } else {
+                    return -1;
+                }
+            }
+        });
+        assertEval("int read;", "0");
+        assertEval("System.in.read();", "0");
+        assertEval("System.in.read();", "1");
+        assertEval("System.in.read();", "2");
+        assertEval("System.in.read();", "-1");
+        assertEval("System.in.read();", "-1");
+        assertEval("System.in.read();", "-1");
+    }
+
+    public void testException() {
+        setInput(new InputStream() {
+            private final int[] data = new int[] {0, 1, -2, 2};
+            private int cursor;
+            @Override public int read() throws IOException {
+                if (cursor < data.length) {
+                    int d = data[cursor++];
+                    if (d == (-2)) {
+                        throw new IOException("Crashed");
+                    }
+                    return d;
+                } else {
+                    return -1;
+                }
+            }
+        });
+        assertEval("int read;", "0");
+        assertEval("System.in.read();", "0");
+        assertEval("System.in.read();", "1");
+        assertEval("java.io.IOException e;");
+        assertEval("try { System.in.read(); } catch (java.io.IOException exc) { e = exc; }");
+        assertEval("e", "java.io.IOException: Crashed");
+        assertEval("System.in.read();", "2");
+        assertEval("System.in.read();", "-1");
+    }
 }