Path: blob/master/test/langtools/jdk/jshell/EditorTestBase.java
40931 views
/*1* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.util.concurrent.ExecutorService;24import java.util.concurrent.Executors;25import java.util.function.Consumer;2627import org.testng.annotations.Test;2829import static org.testng.Assert.assertEquals;30import static org.testng.Assert.assertTrue;3132public abstract class EditorTestBase extends ReplToolTesting {3334private static ExecutorService executor;3536public abstract void writeSource(String s);37public abstract String getSource();38public abstract void accept();39public abstract void exit();40public abstract void cancel();41public abstract void shutdownEditor();4243public void testEditor(ReplTest... tests) {44testEditor(false, new String[]{"--no-startup"}, tests);45}4647public void testEditor(boolean defaultStartup, String[] args, ReplTest... tests) {48test(defaultStartup, args, tests);49}5051abstract void assertEdit(boolean after, String cmd,52Consumer<String> checkInput, Consumer<String> checkOutput, Action action);5354void assertEditInput(boolean after, String cmd, Consumer<String> checkInput, Action action) {55assertEdit(after, cmd, checkInput, s -> {}, action);56}5758void assertEditOutput(boolean after, String cmd, Consumer<String> checkOutput, Action action) {59assertEdit(after, cmd, s -> {}, checkOutput, action);60}6162void assertEditInput(boolean after, String cmd, String input, Action action) {63assertEditInput(after, cmd, s -> assertEquals(s, input, "Input"), action);64}6566void assertEditOutput(boolean after, String cmd, String output, Action action) {67assertEditOutput(after, cmd, s -> assertEquals(s.trim(), output.trim(), "command"), action);68}6970@Test71public void testEditNegative() {72for (String edit : new String[] {"/ed", "/edit"}) {73test(new String[]{"--no-startup"},74a -> assertCommandOutputStartsWith(a, edit + " 1",75"| No snippet with ID: 1"),76a -> assertCommandOutputStartsWith(a, edit + " unknown",77"| No such snippet: unknown")78);79}80}8182@Test83public void testDoNothing() {84testEditor(85a -> assertVariable(a, "int", "a", "0", "0"),86a -> assertEditOutput(a, "/ed 1", "", this::exit),87a -> assertCommandCheckOutput(a, "/v", assertVariables())88);89}9091@Test92public void testEditVariable1() {93testEditor(94a -> assertVariable(a, "int", "a", "0", "0"),95a -> assertEditOutput(a, "/ed 1", "a ==> 10", () -> {96writeSource("\n\n\nint a = 10;\n\n\n");97exit();98loadVariable(true, "int", "a", "10", "10");99}),100a -> assertEditOutput(a, "/ed 1", "a ==> 15", () -> {101writeSource("int a = 15;");102exit();103loadVariable(true, "int", "a", "15", "15");104}),105a -> assertCommandCheckOutput(a, "/v", assertVariables())106);107}108109@Test110public void testEditVariable2() {111testEditor(112a -> assertVariable(a, "int", "a", "0", "0"),113a -> assertEditOutput(a, "/ed 1", "b ==> 10", () -> {114writeSource("int b = 10;");115exit();116loadVariable(true, "int", "b", "10", "10");117}),118a -> assertEditOutput(a, "/ed 1", "a ==> 15", () -> {119writeSource("int a = 15;");120exit();121loadVariable(true, "int", "a", "15", "15");122}),123a -> assertCommandCheckOutput(a, "/v", assertVariables())124);125}126127@Test128public void testWriteVariables() {129testEditor(130a -> assertEditOutput(a, "/edit",131"x ==> 1\n" +132"y ==> 2\n" +133"z ==> 3",134() -> {135writeSource(136"var x = 1;\n" +137"var y = 2;\n" +138"var z = 3;\n");139exit();140}),141a -> assertCommand(a, "z", "z ==> 3")142);143}144145public void testEditClass1() {146testEditor(147a -> assertClass(a, "class A {}", "class", "A"),148a -> assertEditOutput(a, "/ed 1", "", () -> {149writeSource("\n\n\nclass A {}\n\n\n");150exit();151loadClass(true, "class A {}", "class", "A");152}),153a -> assertEditOutput(a, "/ed 1",154"| replaced enum A", () -> {155writeSource("enum A {}");156exit();157loadClass(true, "enum A {}", "enum", "A");158}),159a -> assertCommandCheckOutput(a, "/types", assertClasses())160);161}162163@Test164public void testEditClass2() {165testEditor(166a -> assertClass(a, "class A {}", "class", "A"),167a -> assertEditOutput(a, "/ed 1", "| created class B", () -> {168writeSource("class B { }");169exit();170loadClass(true, "class B {}", "class", "B");171}),172a -> assertEditOutput(a, "/ed 1",173"| replaced enum A", () -> {174writeSource("enum A {}");175exit();176loadClass(true, "enum A {}", "enum", "A");177}),178a -> assertCommandCheckOutput(a, "/types", assertClasses())179);180}181182public void testEditMethod1() {183testEditor(184a -> assertMethod(a, "void f() {}", "()void", "f"),185a -> assertEditOutput(a, "/ed 1", "", () -> {186writeSource("\n\n\nvoid f() {}\n\n\n");187exit();188loadMethod(true, "void f() {}", "()void", "f");189}),190a -> assertEditOutput(a, "/ed 1",191"| replaced method f()", () -> {192writeSource("double f() { return 0; }");193exit();194loadMethod(true, "double f() { return 0; }", "()double", "f");195}),196a -> assertCommandCheckOutput(a, "/m", assertMethods())197);198}199200@Test201public void testEditMethod2() {202testEditor(203a -> assertMethod(a, "void f() {}", "()void", "f"),204a -> assertEditOutput(a, "/ed 1", "| created method g()", () -> {205writeSource("void g() {}");206exit();207loadMethod(true, "void g() {}", "()void", "g");208}),209a -> assertEditOutput(a, "/ed 1",210"| replaced method f()", () -> {211writeSource("double f() { return 0; }");212exit();213loadMethod(true, "double f() { return 0; }", "()double", "f");214}),215a -> assertCommandCheckOutput(a, "/m", assertMethods())216);217}218219@Test220public void testNoArguments() {221testEditor(222a -> assertVariable(a, "int", "a"),223a -> assertMethod(a, "void f() {}", "()void", "f"),224a -> assertClass(a, "class A {}", "class", "A"),225a -> assertEditInput(a, "/ed", s -> {226String[] ss = s.split("\n");227assertEquals(ss.length, 3, "Expected 3 lines: " + s);228assertEquals(ss[0], "int a;");229assertEquals(ss[1], "void f() {}");230assertEquals(ss[2], "class A {}");231}, this::exit)232);233}234235@Test236public void testStartup() {237testEditor(true, new String[0],238a -> assertEditInput(a, "/ed", s -> assertTrue(s.isEmpty(), "Checking of startup: " + s), this::cancel),239a -> assertEditInput(a, "/ed s1", assertStartsWith("import"), this::cancel));240}241242@Test243public void testCancel() {244testEditor(245a -> assertVariable(a, "int", "a"),246a -> assertEditOutput(a, "/ed a", "", () -> {247writeSource("int b = 10");248cancel();249})250);251}252253@Test254public void testAccept() {255testEditor(256a -> assertVariable(a, "int", "a"),257a -> assertEditOutput(a, "/ed a", "b ==> 10", () -> {258writeSource("int b = 10");259accept();260exit();261})262);263}264265@Test(enabled = false) // TODO JDK-8191875266public void testStatementMush() {267testEditor(268a -> assertCommand(a, "System.out.println(\"Hello\")",269"", "", null, "Hello\n", ""),270a -> assertEditOutput(a, "/ed", "b ==> 10", () -> {271writeSource(getSource() + "\nint b = 10");272exit();273}),274275//TODO: this is a work-around to JDK-8170369276a -> assertCommand(a, "1234",277null, "", null, null, "")278);279}280281public static ExecutorService getExecutor() {282if (executor == null) {283executor = Executors.newSingleThreadExecutor();284}285return executor;286}287288public static void executorShutdown() {289if (executor != null) {290executor.shutdown();291executor = null;292}293}294295interface Action {296void accept();297}298}299300301