test/langtools/jdk/jshell/ExternalEditorTest.java
author rfield
Mon, 11 Dec 2017 21:56:34 -0800
changeset 48286 745ea7d5039a
parent 48272 128de6a38f8e
child 52313 f300b4ca2637
permissions -rw-r--r--
8162989: jshell tool: /edit with external editor leaks files in /tmp Reviewed-by: jlahoda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     1
/*
48286
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 48272
diff changeset
     2
 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     4
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     8
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    13
 * accompanied this code).
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    14
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    18
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    21
 * questions.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    22
 */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    23
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    24
/*
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    25
 * @test
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    26
 * @summary Testing external editor.
48286
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 48272
diff changeset
    27
 * @bug 8143955 8080843 8163816 8143006 8169828 8171130 8162989
37389
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 36494
diff changeset
    28
 * @modules jdk.jshell/jdk.internal.jshell.tool
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    29
 * @build ReplToolTesting CustomEditor EditorTestBase
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    30
 * @run testng ExternalEditorTest
46077
08de9d5386a9 8184894: Mark ExternalEditorTest.java as intermittently failing, demote to tier 2
amlu
parents: 43264
diff changeset
    31
 * @key intermittent
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    32
 */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    33
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    34
import java.io.BufferedWriter;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    35
import java.io.DataInputStream;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    36
import java.io.DataOutputStream;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    37
import java.io.IOException;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    38
import java.io.UncheckedIOException;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    39
import java.net.ServerSocket;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    40
import java.net.Socket;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    41
import java.net.SocketTimeoutException;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    42
import java.nio.charset.StandardCharsets;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    43
import java.nio.file.Files;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    44
import java.nio.file.Path;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    45
import java.nio.file.Paths;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    46
import java.util.concurrent.ExecutionException;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    47
import java.util.concurrent.Future;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    48
import java.util.function.Consumer;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    49
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    50
import org.testng.annotations.AfterClass;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    51
import org.testng.annotations.BeforeClass;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    52
import org.testng.annotations.Test;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    53
43264
7b06e19184de 8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents: 42412
diff changeset
    54
import static org.testng.Assert.assertEquals;
48286
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 48272
diff changeset
    55
import static org.testng.Assert.assertFalse;
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 48272
diff changeset
    56
import static org.testng.Assert.assertTrue;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    57
import static org.testng.Assert.fail;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    58
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    59
public class ExternalEditorTest extends EditorTestBase {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    60
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    61
    private static Path executionScript;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    62
    private static ServerSocket listener;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    63
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    64
    private DataInputStream inputStream;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    65
    private DataOutputStream outputStream;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    66
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    67
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    68
    public void writeSource(String s) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    69
        try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    70
            outputStream.writeInt(CustomEditor.SOURCE_CODE);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    71
            byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    72
            outputStream.writeInt(bytes.length);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    73
            outputStream.write(bytes);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    74
        } catch (IOException e) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    75
            throw new UncheckedIOException(e);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    76
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    77
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    78
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    79
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    80
    public String getSource() {
48286
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 48272
diff changeset
    81
        return readString(CustomEditor.GET_SOURCE_CODE);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    82
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    83
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    84
    private void sendCode(int code) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    85
        try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    86
            outputStream.writeInt(code);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    87
        } catch (IOException e) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    88
            throw new UncheckedIOException(e);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    89
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    90
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    91
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    92
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    93
    public void accept() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    94
        sendCode(CustomEditor.ACCEPT_CODE);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    95
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    96
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    97
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    98
    public void exit() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    99
        sendCode(CustomEditor.EXIT_CODE);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   100
        inputStream = null;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   101
        outputStream = null;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   102
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   103
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   104
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   105
    public void cancel() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   106
        sendCode(CustomEditor.CANCEL_CODE);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   107
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   108
48286
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 48272
diff changeset
   109
    protected String getFilename() {
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 48272
diff changeset
   110
        return readString(CustomEditor.GET_FILENAME);
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 48272
diff changeset
   111
    }
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 48272
diff changeset
   112
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 48272
diff changeset
   113
    private String readString(int code) {
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 48272
diff changeset
   114
        try {
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 48272
diff changeset
   115
            outputStream.writeInt(code);
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 48272
diff changeset
   116
            int length = inputStream.readInt();
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 48272
diff changeset
   117
            byte[] bytes = new byte[length];
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 48272
diff changeset
   118
            inputStream.readFully(bytes);
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 48272
diff changeset
   119
            return new String(bytes, StandardCharsets.UTF_8);
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 48272
diff changeset
   120
        } catch (IOException e) {
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 48272
diff changeset
   121
            throw new UncheckedIOException(e);
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 48272
diff changeset
   122
        }
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 48272
diff changeset
   123
    }
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 48272
diff changeset
   124
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   125
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   126
    public void testEditor(boolean defaultStartup, String[] args, ReplTest... tests) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   127
        ReplTest[] t = new ReplTest[tests.length + 1];
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 33362
diff changeset
   128
        t[0] = a -> assertCommandCheckOutput(a, "/set editor " + executionScript,
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   129
                assertStartsWith("|  Editor set to: " + executionScript));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   130
        System.arraycopy(tests, 0, t, 1, tests.length);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   131
        super.testEditor(defaultStartup, args, t);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   132
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   133
43264
7b06e19184de 8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents: 42412
diff changeset
   134
    @Test
7b06e19184de 8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents: 42412
diff changeset
   135
    public void testStatementSemicolonAddition() {
7b06e19184de 8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents: 42412
diff changeset
   136
        testEditor(
7b06e19184de 8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents: 42412
diff changeset
   137
                a -> assertCommand(a, "if (true) {}", ""),
7b06e19184de 8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents: 42412
diff changeset
   138
                a -> assertCommand(a, "if (true) {} else {}", ""),
7b06e19184de 8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents: 42412
diff changeset
   139
                a -> assertCommand(a, "Object o", "o ==> null"),
7b06e19184de 8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents: 42412
diff changeset
   140
                a -> assertCommand(a, "if (true) o = new Object() { int x; }", ""),
7b06e19184de 8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents: 42412
diff changeset
   141
                a -> assertCommand(a, "if (true) o = new Object() { int y; }", ""),
7b06e19184de 8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents: 42412
diff changeset
   142
                a -> assertCommand(a, "System.err.flush()", ""), // test still ; for expression statement
7b06e19184de 8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents: 42412
diff changeset
   143
                a -> assertEditOutput(a, "/ed", "", () -> {
7b06e19184de 8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents: 42412
diff changeset
   144
                    assertEquals(getSource(),
7b06e19184de 8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents: 42412
diff changeset
   145
                            "if (true) {}\n" +
7b06e19184de 8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents: 42412
diff changeset
   146
                            "if (true) {} else {}\n" +
7b06e19184de 8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents: 42412
diff changeset
   147
                            "Object o;\n" +
7b06e19184de 8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents: 42412
diff changeset
   148
                            "if (true) o = new Object() { int x; };\n" +
7b06e19184de 8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents: 42412
diff changeset
   149
                            "if (true) o = new Object() { int y; };\n" +
7b06e19184de 8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents: 42412
diff changeset
   150
                            "System.err.flush();\n");
7b06e19184de 8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents: 42412
diff changeset
   151
                    exit();
7b06e19184de 8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents: 42412
diff changeset
   152
                })
7b06e19184de 8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents: 42412
diff changeset
   153
        );
7b06e19184de 8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents: 42412
diff changeset
   154
    }
7b06e19184de 8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents: 42412
diff changeset
   155
48286
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 48272
diff changeset
   156
    @Test
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 48272
diff changeset
   157
    public void testTempFileDeleted() {
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 48272
diff changeset
   158
        String[] fna = new String[1];
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 48272
diff changeset
   159
        testEditor(
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 48272
diff changeset
   160
                a -> assertVariable(a, "int", "a", "0", "0"),
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 48272
diff changeset
   161
                a -> assertEditOutput(a, "/ed 1", "a ==> 10", () -> {
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 48272
diff changeset
   162
                    fna[0] = getFilename();
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 48272
diff changeset
   163
                    assertTrue(Files.exists(Paths.get(fna[0])), "Test set-up failed: " + fna[0]);
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 48272
diff changeset
   164
                    writeSource("\n\n\nint a = 10;\n\n\n");
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 48272
diff changeset
   165
                    exit();
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 48272
diff changeset
   166
                }),
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 48272
diff changeset
   167
               a -> assertCommand(a, "if (true) {} else {}", "")
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 48272
diff changeset
   168
        );
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 48272
diff changeset
   169
        assertFalse(Files.exists(Paths.get(fna[0])), "File not deleted: " + fna[0]);
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 48272
diff changeset
   170
    }
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 48272
diff changeset
   171
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   172
    private static boolean isWindows() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   173
        return System.getProperty("os.name").startsWith("Windows");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   174
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   175
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   176
    @BeforeClass
37389
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 36494
diff changeset
   177
    public static void setUpExternalEditorTest() throws IOException {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   178
        listener = new ServerSocket(0);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   179
        listener.setSoTimeout(30000);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   180
        int localPort = listener.getLocalPort();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   181
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   182
        executionScript = Paths.get(isWindows() ? "editor.bat" : "editor.sh").toAbsolutePath();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   183
        Path java = Paths.get(System.getProperty("java.home")).resolve("bin").resolve("java");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   184
        try (BufferedWriter writer = Files.newBufferedWriter(executionScript)) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   185
            if(!isWindows()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   186
                writer.append(java.toString()).append(" ")
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   187
                        .append(" -cp ").append(System.getProperty("java.class.path"))
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   188
                        .append(" CustomEditor ").append(Integer.toString(localPort)).append(" $@");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   189
                executionScript.toFile().setExecutable(true);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   190
            } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   191
                writer.append(java.toString()).append(" ")
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   192
                        .append(" -cp ").append(System.getProperty("java.class.path"))
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   193
                        .append(" CustomEditor ").append(Integer.toString(localPort)).append(" %*");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   194
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   195
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   196
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   197
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   198
    private Future<?> task;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   199
    @Override
48272
128de6a38f8e 8191455: jdk/jshell/StartOptionTest.java and jdk/jshell/ToolProviderTest.java failed after changeset e0f08a
rfield
parents: 47216
diff changeset
   200
    void assertEdit(boolean after, String cmd,
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   201
                           Consumer<String> checkInput, Consumer<String> checkOutput, Action action) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   202
        if (!after) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   203
            setCommandInput(cmd + "\n");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   204
            task = getExecutor().submit(() -> {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   205
                try (Socket socket = listener.accept()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   206
                    inputStream = new DataInputStream(socket.getInputStream());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   207
                    outputStream = new DataOutputStream(socket.getOutputStream());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   208
                    checkInput.accept(getSource());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   209
                    action.accept();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   210
                } catch (SocketTimeoutException e) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   211
                    fail("Socket timeout exception.\n Output: " + getCommandOutput() +
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   212
                            "\n, error: " + getCommandErrorOutput());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   213
                } catch (Throwable e) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   214
                    shutdownEditor();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   215
                    if (e instanceof AssertionError) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   216
                        throw (AssertionError) e;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   217
                    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   218
                    throw new RuntimeException(e);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   219
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   220
            });
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   221
        } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   222
            try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   223
                task.get();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   224
                checkOutput.accept(getCommandOutput());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   225
            } catch (ExecutionException e) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   226
                if (e.getCause() instanceof AssertionError) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   227
                    throw (AssertionError) e.getCause();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   228
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   229
                throw new RuntimeException(e);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   230
            } catch (Exception e) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   231
                throw new RuntimeException(e);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   232
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   233
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   234
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   235
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   236
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   237
    public void shutdownEditor() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   238
        if (outputStream != null) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   239
            exit();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   240
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   241
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   242
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   243
    @Test
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   244
    public void setUnknownEditor() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   245
        test(
37389
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 36494
diff changeset
   246
                a -> assertCommand(a, "/set editor UNKNOWN", "|  Editor set to: UNKNOWN"),
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   247
                a -> assertCommand(a, "int a;", null),
38828
520da69917b5 8080843: JShell tool: invalid key error occurs when external editor is used
rfield
parents: 37389
diff changeset
   248
                a -> assertCommandOutputStartsWith(a, "/ed 1",
520da69917b5 8080843: JShell tool: invalid key error occurs when external editor is used
rfield
parents: 37389
diff changeset
   249
                        "|  Edit Error:")
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   250
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   251
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   252
40307
3ad1ad0e7bba 8163817: JShell tests: disable minor failing editor tool cases: 8161276, 8163816, 8159229
rfield
parents: 38828
diff changeset
   253
    @Test(enabled = false) // TODO 8159229
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   254
    public void testRemoveTempFile() {
40588
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40307
diff changeset
   255
        test(new String[]{"--no-startup"},
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 33362
diff changeset
   256
                a -> assertCommandCheckOutput(a, "/set editor " + executionScript,
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   257
                        assertStartsWith("|  Editor set to: " + executionScript)),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   258
                a -> assertVariable(a, "int", "a", "0", "0"),
38828
520da69917b5 8080843: JShell tool: invalid key error occurs when external editor is used
rfield
parents: 37389
diff changeset
   259
                a -> assertEditOutput(a, "/ed 1", assertStartsWith("|  Edit Error: Failure in read edit file:"), () -> {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   260
                    sendCode(CustomEditor.REMOVE_CODE);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   261
                    exit();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   262
                }),
38828
520da69917b5 8080843: JShell tool: invalid key error occurs when external editor is used
rfield
parents: 37389
diff changeset
   263
                a -> assertCommandCheckOutput(a, "/vars", assertVariables())
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   264
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   265
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   266
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   267
    @AfterClass
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   268
    public static void shutdown() throws IOException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   269
        executorShutdown();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   270
        if (listener != null) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   271
            listener.close();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   272
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   273
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   274
}