langtools/test/jdk/jshell/ToolLocaleMessageTest.java
author rfield
Sat, 09 Apr 2016 11:03:48 -0700
changeset 37007 6023a9a9d58a
parent 36990 ec0b843a7af5
child 37856 443bdc8a8b7c
permissions -rw-r--r--
8153716: JShell tool: should warn when failed to launch editor Summary: Catch launch exceptions. Split ToolBasicTest into two to provide place for regression test. Reviewed-by: rfield Contributed-by: kubota.yuji@gmail.com

/*
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 8147515
 * @summary Tests for output customization
 * @library /tools/lib
 * @modules jdk.compiler/com.sun.tools.javac.api
 *          jdk.compiler/com.sun.tools.javac.main
 *          jdk.jdeps/com.sun.tools.javap
 *          jdk.jshell/jdk.internal.jshell.tool
 * @build KullaTesting TestingInputStream toolbox.ToolBox Compiler
 * @run testng ToolLocaleMessageTest
 */

import java.util.Locale;
import org.testng.annotations.Test;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;

@Test
public class ToolLocaleMessageTest extends ReplToolTesting {

    void testLocale(ReplTest... tests) {
        test(Locale.getDefault(), false, new String[]{"-nostartup"}, "", tests);
    }

    void assertCommandOK(boolean after, String cmd, String... contains) {
        assertCommandCheckOutput(after, cmd, s -> {
            assertFalse(s.contains("Exception"), "Output of '" + cmd + "' has Exception: " + s);
            assertFalse(s.contains("ERROR:"), "Output of '" + cmd + "' has error: " + s);
            for (String m : contains) {
                assertTrue(s.contains(m), "Expected to find '" + m + "' in output of '" + cmd + "' -- output: " + s);
            }
        });
    }

    void assertCommandFail(boolean after, String cmd, String... contains) {
        assertCommandCheckOutput(after, cmd, s -> {
            assertFalse(s.contains("Exception"), "Output of '" + cmd + "' has Exception: " + s);
            assertTrue(s.contains("ERROR:"), "Expected to find error in output of '" + cmd + "' has error: " + s);
            for (String m : contains) {
                assertTrue(s.contains(m), "Expected to find '" + m + "' in output of '" + cmd + "' -- output: " + s);
            }
        });
    }

    public void testTerminate() {
        testLocale(
                (a) -> assertCommandOK(a, "System.exit(1)", "/reload")
        );
    }

    public void testSample() {
        try {
            testLocale(
                    (a) -> assertCommandOK(a, "/set newmode test command normal", "test"),
                    (a) -> assertCommandOK(a, "/set format test errorpre 'ERROR: '"),
                    (a) -> assertCommandOK(a, "/set feedback test", "test"),

                    (a) -> assertCommandFail(a, "/turkey", "/turkey"),
                    (a) -> assertCommandFail(a, "/s", "/set"),
                    (a) -> assertCommandOK(a, "void m() { blah(); }", "blah"),
                    (a) -> assertCommandOK(a, "void m() {}"),
                    (a) -> assertCommandOK(a, "class C {}"),
                    (a) -> assertCommandOK(a, "47"),
                    (a) -> assertCommandOK(a, "double d"),
                    (a) -> assertCommandOK(a, "/drop m", "m"),
                    (a) -> assertCommandOK(a, "void dup() {}"),
                    (a) -> assertCommandOK(a, "int dup"),

                    (a) -> assertCommandOK(a, "/set feedback normal", "normal")
            );
        } finally {
            assertCommandOK(false, "/set feedback normal");
        }
    }

    public void testCommand() {
        try {
            testLocale(
                    (a) -> assertCommandOK(a, "/set newmode test command normal", "test"),
                    (a) -> assertCommandOK(a, "/set format test errorpre 'ERROR: '"),
                    (a) -> assertCommandOK(a, "/set feedback test", "test"),

                    (a) -> assertCommandFail(a, "/list zebra"),
                    (a) -> assertCommandFail(a, "/set editor", "/set editor"),
                    (a) -> assertCommandFail(a, "/set snowball", "/set", "snowball"),
                    (a) -> assertCommandFail(a, "/set", "/set", "/help"),
                    (a) -> assertCommandFail(a, "/set f", "feedback"),
                    (a) -> assertCommandFail(a, "/classpath", "/classpath"),
                    (a) -> assertCommandFail(a, "/help rabbits", "rabbits"),
                    (a) -> assertCommandFail(a, "/drop"),
                    (a) -> assertCommandFail(a, "/drop rats"),
                    (a) -> assertCommandOK(a, "void dup() {}"),
                    (a) -> assertCommandOK(a, "int dup"),
                    (a) -> assertCommandFail(a, "/drop dup"),
                    (a) -> assertCommandFail(a, "/edit zebra", "zebra"),
                    (a) -> assertCommandFail(a, "/list zebra", "zebra", "/list"),
                    (a) -> assertCommandFail(a, "/open", "/open"),
                    (a) -> assertCommandFail(a, "/open zebra", "zebra", "/open"),
                    (a) -> assertCommandFail(a, "/reload zebra", "zebra", "/reload"),
                    (a) -> assertCommandFail(a, "/save", "/save"),
                    (a) -> assertCommandFail(a, "/-99"),

                    (a) -> assertCommandOK(a, "/set feedback normal", "normal")
            );
        } finally {
            assertCommandOK(false, "/set feedback normal");
        }
    }

    public void testHelp() {
        testLocale(
                (a) -> assertCommandOK(a, "/help", "/list", "/save", "/set", "[restore]"),
                (a) -> assertCommandOK(a, "/help /list", "start", "all"),
                (a) -> assertCommandOK(a, "/help /edit", "/set editor"),
                (a) -> assertCommandOK(a, "/help /drop", "/drop"),
                (a) -> assertCommandOK(a, "/help /save", "all", "start"),
                (a) -> assertCommandOK(a, "/help /open", "/open"),
                (a) -> assertCommandOK(a, "/help /reload", "restore"),
                (a) -> assertCommandOK(a, "/help /help", "intro"),
                (a) -> assertCommandOK(a, "/help /set", "newmode"),
                (a) -> assertCommandOK(a, "/help /?", "intro"),
                (a) -> assertCommandOK(a, "/help intro", "/help"),
                (a) -> assertCommandOK(a, "/help /set format", "import", "case", "{value}", "added"),
                (a) -> assertCommandOK(a, "/help /set feedback", "newmode"),
                (a) -> assertCommandOK(a, "/help /set newmode", "feedback"),
                (a) -> assertCommandOK(a, "/help /set prompt", "/set prompt"),
                (a) -> assertCommandOK(a, "/help /set editor", "/edit")
        );
    }

    public void testFeedbackError() {
        try {
            testLocale(
                    (a) -> assertCommandOK(a, "/set newmode tee command foo", "foo"),
                    (a) -> assertCommandOK(a, "/set newmode tee flurb", "command", "quiet"),
                    (a) -> assertCommandOK(a, "/set newmode te2", "te2"),
                    (a) -> assertCommandOK(a, "/set newmode te2 command", "te2"),
                    (a) -> assertCommandOK(a, "/set newmode te command normal", "te"),
                    (a) -> assertCommandOK(a, "/set format te errorpre 'ERROR: '"),
                    (a) -> assertCommandOK(a, "/set feedback te"),

                    (a) -> assertCommandFail(a, "/set "),
                    (a) -> assertCommandFail(a, "/set xyz", "xyz"),
                    (a) -> assertCommandFail(a, "/set f", "/set", "f"),
                    (a) -> assertCommandFail(a, "/set feedback"),
                    (a) -> assertCommandFail(a, "/set feedback xyz"),
                    (a) -> assertCommandFail(a, "/set format"),
                    (a) -> assertCommandFail(a, "/set format xyz"),
                    (a) -> assertCommandFail(a, "/set format t"),
                    (a) -> assertCommandFail(a, "/set format te"),
                    (a) -> assertCommandFail(a, "/set format te fld"),
                    (a) -> assertCommandFail(a, "/set format te fld aaa", "aaa"),
                    (a) -> assertCommandFail(a, "/set format te fld 'aaa' frog"),
                    (a) -> assertCommandFail(a, "/set format te fld 'aaa' import-frog"),
                    (a) -> assertCommandFail(a, "/set format te fld 'aaa' import-import"),
                    (a) -> assertCommandFail(a, "/set format te fld 'aaa' import,added"),
                    (a) -> assertCommandFail(a, "/set newmode"),
                    (a) -> assertCommandFail(a, "/set newmode te"),
                    (a) -> assertCommandFail(a, "/set newmode x xyz"),
                    (a) -> assertCommandFail(a, "/set newmode x quiet y"),
                    (a) -> assertCommandFail(a, "/set prompt"),
                    (a) -> assertCommandFail(a, "/set prompt te"),
                    (a) -> assertCommandFail(a, "/set prompt te aaa xyz", "aaa"),
                    (a) -> assertCommandFail(a, "/set prompt te 'aaa' xyz", "xyz"),
                    (a) -> assertCommandFail(a, "/set prompt"),
                    (a) -> assertCommandFail(a, "/set prompt te"),
                    (a) -> assertCommandFail(a, "/set prompt te aaa"),
                    (a) -> assertCommandFail(a, "/set prompt te 'aaa'"),

                    (a) -> assertCommandOK(a, "/set feedback normal")
            );
        } finally {
            assertCommandOK(false, "/set feedback normal");
        }
    }


}