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 
  38 import java.util.Locale;
  39 import org.testng.annotations.Test;
  40 import static org.testng.Assert.assertFalse;
  41 import static org.testng.Assert.assertTrue;
  42 
  43 @Test
  44 public 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", "/set editor"),
 110                     (a) -> assertCommandFail(a, "/set snowball", "/set", "snowball"),
 111                     (a) -> assertCommandFail(a, "/set", "/set", "/help"),
 112                     (a) -> assertCommandFail(a, "/set f", "feedback"),
 113                     (a) -> assertCommandFail(a, "/classpath", "/classpath"),
 114                     (a) -> assertCommandFail(a, "/help rabbits", "rabbits"),
 115                     (a) -> assertCommandFail(a, "/drop"),
 116                     (a) -> assertCommandFail(a, "/drop rats"),
 117                     (a) -> assertCommandOK(a, "void dup() {}"),
 118                     (a) -> assertCommandOK(a, "int dup"),
 119                     (a) -> assertCommandFail(a, "/drop dup"),
 120                     (a) -> assertCommandFail(a, "/edit zebra", "zebra"),
 121                     (a) -> assertCommandFail(a, "/list zebra", "zebra", "No such snippet: zebra"),
 122                     (a) -> assertCommandFail(a, "/open", "/open"),
 123                     (a) -> assertCommandFail(a, "/open zebra", "zebra", "/open"),
 124                     (a) -> assertCommandFail(a, "/reload zebra", "zebra", "Unexpected arguments at end of command"),
 125                     (a) -> assertCommandFail(a, "/save", "/save"),
 126                     (a) -> assertCommandFail(a, "/-99"),
 127 
 128                     (a) -> assertCommandOK(a, "/set feedback normal", "normal")
 129             );
 130         } finally {
 131             assertCommandOK(false, "/set feedback normal");
 132         }
 133     }
 134 
 135     public void testHelp() {
 136         testLocale(
 137                 (a) -> assertCommandOK(a, "/help", "/list", "/save", "/set", "[-restore]"),
 138                 (a) -> assertCommandOK(a, "/help /list", "-start", "-all"),
 139                 (a) -> assertCommandOK(a, "/help /edit", "/set editor"),
 140                 (a) -> assertCommandOK(a, "/help /drop", "/drop"),
 141                 (a) -> assertCommandOK(a, "/help /save", "-all", "-start"),
 142                 (a) -> assertCommandOK(a, "/help /open", "/open"),
 143                 (a) -> assertCommandOK(a, "/help /reload", "-restore"),
 144                 (a) -> assertCommandOK(a, "/help /help", "intro"),
 145                 (a) -> assertCommandOK(a, "/help /set", "mode"),
 146                 (a) -> assertCommandOK(a, "/help /?", "intro"),
 147                 (a) -> assertCommandOK(a, "/help intro", "/help"),
 148                 (a) -> assertCommandOK(a, "/help /set format", "import", "case", "{value}", "added"),
 149                 (a) -> assertCommandOK(a, "/help /set feedback", "mode"),
 150                 (a) -> assertCommandOK(a, "/help /set mode", "feedback"),
 151                 (a) -> assertCommandOK(a, "/help /set prompt", "/set prompt"),
 152                 (a) -> assertCommandOK(a, "/help /set editor", "/edit")
 153         );
 154     }
 155 
 156     public void testFeedbackError() {
 157         try {
 158             testLocale(
 159                     (a) -> assertCommandOK(a, "/set mode te2", "te2"),
 160                     (a) -> assertCommandOK(a, "/set mode te3 -command", "te3"),
 161                     (a) -> assertCommandOK(a, "/set mode te2 -command"),
 162                     (a) -> assertCommandOK(a, "/set mode te -command normal", "te"),
 163                     (a) -> assertCommandOK(a, "/set mode te"),
 164                     (a) -> assertCommandOK(a, "/set format te errorpre 'ERROR: '"),
 165                     (a) -> assertCommandOK(a, "/set feedback te"),
 166 
 167                     (a) -> assertCommandFail(a, "/set "),
 168                     (a) -> assertCommandFail(a, "/set xyz", "xyz"),
 169                     (a) -> assertCommandFail(a, "/set f", "/set", "f"),
 170                     (a) -> assertCommandFail(a, "/set feedback"),
 171                     (a) -> assertCommandFail(a, "/set feedback xyz"),
 172                     (a) -> assertCommandFail(a, "/set format"),
 173                     (a) -> assertCommandFail(a, "/set format xyz"),
 174                     (a) -> assertCommandFail(a, "/set format t"),
 175                     (a) -> assertCommandFail(a, "/set format te"),
 176                     (a) -> assertCommandFail(a, "/set format te fld"),
 177                     (a) -> assertCommandFail(a, "/set format te fld aaa", "aaa"),
 178                     (a) -> assertCommandFail(a, "/set format te fld 'aaa' frog"),
 179                     (a) -> assertCommandFail(a, "/set format te fld 'aaa' import-frog"),
 180                     (a) -> assertCommandFail(a, "/set format te fld 'aaa' import-import"),
 181                     (a) -> assertCommandFail(a, "/set format te fld 'aaa' import,added"),
 182                     (a) -> assertCommandFail(a, "/set mode"),
 183                     (a) -> assertCommandFail(a, "/set mode x xyz"),
 184                     (a) -> assertCommandFail(a, "/set mode x -quiet y"),
 185                     (a) -> assertCommandFail(a, "/set mode tee -command foo", "foo"),
 186                     (a) -> assertCommandFail(a, "/set prompt"),
 187                     (a) -> assertCommandFail(a, "/set prompt te"),
 188                     (a) -> assertCommandFail(a, "/set prompt te aaa xyz", "aaa"),
 189                     (a) -> assertCommandFail(a, "/set prompt te 'aaa' xyz", "xyz"),
 190                     (a) -> assertCommandFail(a, "/set prompt te aaa"),
 191                     (a) -> assertCommandFail(a, "/set prompt te 'aaa'"),
 192 
 193                     (a) -> assertCommandOK(a, "/set feedback normal")
 194             );
 195         } finally {
 196             assertCommandOK(false, "/set feedback normal");
 197         }
 198     }
 199 
 200 
 201 }