40767
|
1 |
/*
|
|
2 |
* Copyright (c) 2016, 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
|
41628
|
26 |
* @bug 8131023 8167461
|
40767
|
27 |
* @summary Verify that the user's code can read System.in
|
|
28 |
* @build KullaTesting TestingInputStream
|
|
29 |
* @run testng UserInputTest
|
|
30 |
*/
|
|
31 |
|
41628
|
32 |
import java.io.IOException;
|
|
33 |
import java.io.InputStream;
|
|
34 |
|
40767
|
35 |
import org.testng.annotations.Test;
|
|
36 |
|
|
37 |
@Test
|
|
38 |
public class UserInputTest extends KullaTesting {
|
|
39 |
|
|
40 |
public void testReadInput() {
|
|
41 |
setInput("AB\n");
|
|
42 |
assertEval("System.in.read()", "65");
|
41628
|
43 |
setInput("CD\n");
|
|
44 |
assertEval("System.in.read()", "67");
|
|
45 |
}
|
|
46 |
|
|
47 |
public void testScanner() {
|
|
48 |
assertEval("import java.util.Scanner;");
|
|
49 |
assertEval("Scanner s = new Scanner(System.in);");
|
|
50 |
setInput("12\n");
|
|
51 |
assertEval("s.nextInt();", "12");
|
40767
|
52 |
}
|
|
53 |
|
41628
|
54 |
public void testClose() {
|
|
55 |
setInput(new InputStream() {
|
|
56 |
private final byte[] data = new byte[] {0, 1, 2};
|
|
57 |
private int cursor;
|
|
58 |
@Override public int read() throws IOException {
|
|
59 |
if (cursor < data.length) {
|
|
60 |
return data[cursor++];
|
|
61 |
} else {
|
|
62 |
return -1;
|
|
63 |
}
|
|
64 |
}
|
|
65 |
});
|
|
66 |
assertEval("int read;", "0");
|
|
67 |
assertEval("System.in.read();", "0");
|
|
68 |
assertEval("System.in.read();", "1");
|
|
69 |
assertEval("System.in.read();", "2");
|
|
70 |
assertEval("System.in.read();", "-1");
|
|
71 |
assertEval("System.in.read();", "-1");
|
|
72 |
assertEval("System.in.read();", "-1");
|
|
73 |
}
|
|
74 |
|
|
75 |
public void testException() {
|
|
76 |
setInput(new InputStream() {
|
|
77 |
private final int[] data = new int[] {0, 1, -2, 2};
|
|
78 |
private int cursor;
|
|
79 |
@Override public int read() throws IOException {
|
|
80 |
if (cursor < data.length) {
|
|
81 |
int d = data[cursor++];
|
|
82 |
if (d == (-2)) {
|
|
83 |
throw new IOException("Crashed");
|
|
84 |
}
|
|
85 |
return d;
|
|
86 |
} else {
|
|
87 |
return -1;
|
|
88 |
}
|
|
89 |
}
|
|
90 |
});
|
|
91 |
assertEval("int read;", "0");
|
|
92 |
assertEval("System.in.read();", "0");
|
|
93 |
assertEval("System.in.read();", "1");
|
|
94 |
assertEval("java.io.IOException e;");
|
|
95 |
assertEval("try { System.in.read(); } catch (java.io.IOException exc) { e = exc; }");
|
|
96 |
assertEval("e", "java.io.IOException: Crashed");
|
|
97 |
assertEval("System.in.read();", "2");
|
|
98 |
assertEval("System.in.read();", "-1");
|
|
99 |
}
|
40767
|
100 |
}
|