test/langtools/jdk/jshell/PipeInputStreamTest.java
author dtitov
Tue, 08 Oct 2019 11:18:00 -0700
changeset 58506 dfe3d11378cc
parent 47216 71c04702a3d5
permissions -rw-r--r--
8199136: Dead code in src/jdk.jcmd/share/classes/sun/tools/common/ProcessArgumentMatcher.java Reviewed-by: dholmes, dtitov Contributed-by: mandrikov@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41628
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
     1
/*
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
     4
 *
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
     8
 *
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    13
 * accompanied this code).
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    14
 *
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    18
 *
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    21
 * questions.
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    22
 */
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    23
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    24
/*
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    25
 * @test 8167461
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    26
 * @summary Verify PipeInputStream works.
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    27
 * @modules jdk.compiler/com.sun.tools.javac.util
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41628
diff changeset
    28
 *          jdk.jshell/jdk.jshell.execution:open
41628
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    29
 * @run testng PipeInputStreamTest
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    30
 */
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    31
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    32
import java.io.InputStream;
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    33
import java.io.OutputStream;
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    34
import java.lang.reflect.Constructor;
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    35
import java.lang.reflect.Method;
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    36
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    37
import org.testng.annotations.Test;
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    38
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    39
import com.sun.tools.javac.util.Pair;
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    40
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    41
import static org.testng.Assert.*;
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    42
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    43
@Test
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    44
public class PipeInputStreamTest {
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    45
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    46
    public void testReadArrayNotBlocking() throws Exception {
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    47
        Pair<InputStream, OutputStream> streams = createPipeStream();
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    48
        InputStream in = streams.fst;
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    49
        OutputStream out = streams.snd;
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    50
        out.write('a');
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    51
        byte[] data = new byte[12];
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    52
        assertEquals(in.read(data), 1);
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    53
        assertEquals(data[0], 'a');
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    54
        out.write('a'); out.write('b'); out.write('c');
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    55
        assertEquals(in.read(data), 3);
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    56
        assertEquals(data[0], 'a');
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    57
        assertEquals(data[1], 'b');
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    58
        assertEquals(data[2], 'c');
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    59
    }
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    60
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    61
    private Pair<InputStream, OutputStream> createPipeStream() throws Exception {
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    62
        Class<?> pipeStreamClass = Class.forName("jdk.jshell.execution.PipeInputStream");
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    63
        Constructor<?> c = pipeStreamClass.getDeclaredConstructor();
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    64
        c.setAccessible(true);
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    65
        Object pipeStream = c.newInstance();
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    66
        Method createOutputStream = pipeStreamClass.getDeclaredMethod("createOutput");
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    67
        createOutputStream.setAccessible(true);
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    68
        return Pair.of((InputStream) pipeStream, (OutputStream) createOutputStream.invoke(pipeStream));
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    69
    }
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    70
664e7664343d 8167461: jshell tool: Scanner#next() hangs tool
jlahoda
parents:
diff changeset
    71
}