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 8167461
26  * @summary Verify PipeInputStream works.
27  * @modules jdk.compiler/com.sun.tools.javac.util
28  *          jdk.jshell/jdk.jshell.execution:open
29  * @run testng PipeInputStreamTest
30  */
31 
32 import java.io.InputStream;
33 import java.io.OutputStream;
34 import java.lang.reflect.Constructor;
35 import java.lang.reflect.Method;
36 
37 import org.testng.annotations.Test;
38 
39 import com.sun.tools.javac.util.Pair;
40 
41 import static org.testng.Assert.*;
42 
43 @Test
44 public class PipeInputStreamTest {
45 
testReadArrayNotBlocking()46     public void testReadArrayNotBlocking() throws Exception {
47         Pair<InputStream, OutputStream> streams = createPipeStream();
48         InputStream in = streams.fst;
49         OutputStream out = streams.snd;
50         out.write('a');
51         byte[] data = new byte[12];
52         assertEquals(in.read(data), 1);
53         assertEquals(data[0], 'a');
54         out.write('a'); out.write('b'); out.write('c');
55         assertEquals(in.read(data), 3);
56         assertEquals(data[0], 'a');
57         assertEquals(data[1], 'b');
58         assertEquals(data[2], 'c');
59     }
60 
createPipeStream()61     private Pair<InputStream, OutputStream> createPipeStream() throws Exception {
62         Class<?> pipeStreamClass = Class.forName("jdk.jshell.execution.PipeInputStream");
63         Constructor<?> c = pipeStreamClass.getDeclaredConstructor();
64         c.setAccessible(true);
65         Object pipeStream = c.newInstance();
66         Method createOutputStream = pipeStreamClass.getDeclaredMethod("createOutput");
67         createOutputStream.setAccessible(true);
68         return Pair.of((InputStream) pipeStream, (OutputStream) createOutputStream.invoke(pipeStream));
69     }
70 
71 }
72