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
26 * @bug 8147515
27 * @summary Tests for output customization
28 * @library /tools/lib
29 * @modules jdk.compiler/com.sun.tools.javac.api
30 *          jdk.compiler/com.sun.tools.javac.main
31 *          jdk.jdeps/com.sun.tools.javap
32 *          jdk.jshell/jdk.internal.jshell.tool
33 * @build KullaTesting TestingInputStream toolbox.ToolBox Compiler
34 * @run testng ToolLocaleMessageTest
35 * @key intermittent
36 */
37
38import java.util.Locale;
39import org.testng.annotations.Test;
40import static org.testng.Assert.assertFalse;
41import static org.testng.Assert.assertTrue;
42
43@Test
44public class ToolLocaleMessageTest extends ReplToolTesting {
45
46    void testLocale(ReplTest... tests) {
47        test(Locale.getDefault(), false, new String[]{"--no-startup"}, "", tests);
48    }
49
50    void assertCommandOK(boolean after, String cmd, String... contains) {
51        assertCommandCheckOutput(after, cmd, s -> {
52            assertFalse(s.contains("Exception"), "Output of '" + cmd + "' has Exception: " + s);
53            assertFalse(s.contains("ERROR:"), "Output of '" + cmd + "' has error: " + s);
54            for (String m : contains) {
55                assertTrue(s.contains(m), "Expected to find '" + m + "' in output of '" + cmd + "' -- output: " + s);
56            }
57        });
58    }
59
60    void assertCommandFail(boolean after, String cmd, String... contains) {
61        assertCommandCheckOutput(after, cmd, s -> {
62            assertFalse(s.contains("Exception"), "Output of '" + cmd + "' has Exception: " + s);
63            assertTrue(s.contains("ERROR:"), "Expected to find error in output of '" + cmd + "' has error: " + s);
64            for (String m : contains) {
65                assertTrue(s.contains(m), "Expected to find '" + m + "' in output of '" + cmd + "' -- output: " + s);
66            }
67        });
68    }
69
70    public void testTerminate() {
71        testLocale(
72                (a) -> assertCommandOK(a, "System.exit(1)", "/reload")
73        );
74    }
75
76    public void testSample() {
77        try {
78            testLocale(
79                    (a) -> assertCommandOK(a, "/set mode test -command normal", "test"),
80                    (a) -> assertCommandOK(a, "/set format test errorpre 'ERROR: '"),
81                    (a) -> assertCommandOK(a, "/set feedback test", "test"),
82
83                    (a) -> assertCommandFail(a, "/turkey", "/turkey"),
84                    (a) -> assertCommandFail(a, "/s", "/set"),
85                    (a) -> assertCommandOK(a, "void m() { blah(); }", "blah"),
86                    (a) -> assertCommandOK(a, "void m() {}"),
87                    (a) -> assertCommandOK(a, "class C {}"),
88                    (a) -> assertCommandOK(a, "47"),
89                    (a) -> assertCommandOK(a, "double d"),
90                    (a) -> assertCommandOK(a, "/drop m", "m"),
91                    (a) -> assertCommandOK(a, "void dup() {}"),
92                    (a) -> assertCommandOK(a, "int dup"),
93
94                    (a) -> assertCommandOK(a, "/set feedback normal", "normal")
95            );
96        } finally {
97            assertCommandOK(false, "/set feedback normal");
98        }
99    }
100
101    public void testCommand() {
102        try {
103            testLocale(
104                    (a) -> assertCommandOK(a, "/set mode test -command normal", "test"),
105                    (a) -> assertCommandOK(a, "/set format test errorpre 'ERROR: '"),
106                    (a) -> assertCommandOK(a, "/set feedback test", "test"),
107
108                    (a) -> assertCommandFail(a, "/list zebra"),
109                    (a) -> assertCommandFail(a, "/set editor -rot", "/set editor -rot"),
110                    (a) -> assertCommandFail(a, "/set snowball", "/set", "snowball"),
111                    (a) -> assertCommandOK(a, "/set", "|  /set feedback test", "verbose"),
112                    (a) -> assertCommandFail(a, "/set f", "/set"),
113                    (a) -> assertCommandOK(a, "/set fe", "|  /set feedback test"),
114                    (a) -> assertCommandFail(a, "/classpath", "/classpath"),
115                    (a) -> assertCommandFail(a, "/help rabbits", "rabbits"),
116                    (a) -> assertCommandFail(a, "/drop"),
117                    (a) -> assertCommandFail(a, "/drop rats"),
118                    (a) -> assertCommandOK(a, "void dup() {}"),
119                    (a) -> assertCommandOK(a, "int dup"),
120                    (a) -> assertCommandFail(a, "/drop dup"),
121                    (a) -> assertCommandFail(a, "/edit zebra", "zebra"),
122                    (a) -> assertCommandFail(a, "/list zebra", "zebra", "No such snippet: zebra"),
123                    (a) -> assertCommandFail(a, "/open", "/open"),
124                    (a) -> assertCommandFail(a, "/open zebra", "zebra", "/open"),
125                    (a) -> assertCommandFail(a, "/reload zebra", "zebra", "Unexpected arguments at end of command"),
126                    (a) -> assertCommandFail(a, "/save", "/save"),
127                    (a) -> assertCommandFail(a, "/-99"),
128
129                    (a) -> assertCommandOK(a, "/set feedback normal", "normal")
130            );
131        } finally {
132            assertCommandOK(false, "/set feedback normal");
133        }
134    }
135
136    public void testHelp() {
137        testLocale(
138                (a) -> assertCommandOK(a, "/help", "/list", "/save", "/set", "[-restore]"),
139                (a) -> assertCommandOK(a, "/help /list", "-start", "-all"),
140                (a) -> assertCommandOK(a, "/help /edit", "/set editor"),
141                (a) -> assertCommandOK(a, "/help /drop", "/drop"),
142                (a) -> assertCommandOK(a, "/help /save", "-all", "-start"),
143                (a) -> assertCommandOK(a, "/help /open", "/open"),
144                (a) -> assertCommandOK(a, "/help /reload", "-restore"),
145                (a) -> assertCommandOK(a, "/help /help", "intro"),
146                (a) -> assertCommandOK(a, "/help /set", "mode"),
147                (a) -> assertCommandOK(a, "/help /?", "intro"),
148                (a) -> assertCommandOK(a, "/help intro", "/help"),
149                (a) -> assertCommandOK(a, "/help /set format", "import", "case", "{value}", "added"),
150                (a) -> assertCommandOK(a, "/help /set feedback", "mode"),
151                (a) -> assertCommandOK(a, "/help /set mode", "feedback"),
152                (a) -> assertCommandOK(a, "/help /set prompt", "/set prompt"),
153                (a) -> assertCommandOK(a, "/help /set editor", "/edit")
154        );
155    }
156
157    public void testFeedbackError() {
158        try {
159            testLocale(
160                    (a) -> assertCommandOK(a, "/set mode te2", "te2"),
161                    (a) -> assertCommandOK(a, "/set mode te3 -command", "te3"),
162                    (a) -> assertCommandOK(a, "/set mode te2 -command"),
163                    (a) -> assertCommandOK(a, "/set mode te -command normal", "te"),
164                    (a) -> assertCommandOK(a, "/set mode te"),
165                    (a) -> assertCommandOK(a, "/set format te errorpre 'ERROR: '"),
166                    (a) -> assertCommandOK(a, "/set feedback te"),
167
168                    (a) -> assertCommandFail(a, "/set xyz", "xyz"),
169                    (a) -> assertCommandFail(a, "/set f", "/set", "f"),
170                    (a) -> assertCommandFail(a, "/set feedback xyz"),
171                    (a) -> assertCommandFail(a, "/set format xyz"),
172                    (a) -> assertCommandFail(a, "/set format t"),
173                    (a) -> assertCommandFail(a, "/set format te fld"),
174                    (a) -> assertCommandFail(a, "/set format te fld aaa", "aaa"),
175                    (a) -> assertCommandFail(a, "/set format te fld 'aaa' frog"),
176                    (a) -> assertCommandFail(a, "/set format te fld 'aaa' import-frog"),
177                    (a) -> assertCommandFail(a, "/set format te fld 'aaa' import-import"),
178                    (a) -> assertCommandFail(a, "/set format te fld 'aaa' import,added"),
179                    (a) -> assertCommandFail(a, "/set mode x xyz"),
180                    (a) -> assertCommandFail(a, "/set mode x -quiet y"),
181                    (a) -> assertCommandFail(a, "/set mode tee -command foo", "foo"),
182                    (a) -> assertCommandFail(a, "/set prompt te aaa xyz", "aaa"),
183                    (a) -> assertCommandFail(a, "/set prompt te 'aaa' xyz", "xyz"),
184                    (a) -> assertCommandFail(a, "/set prompt te aaa"),
185                    (a) -> assertCommandFail(a, "/set prompt te 'aaa'"),
186
187                    (a) -> assertCommandOK(a, "/set feedback normal")
188            );
189        } finally {
190            assertCommandOK(false, "/set feedback normal");
191        }
192    }
193
194
195}
196