ToolFormatTest.java revision 3409:3c09f576196a
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 8148316 8148317 8151755 8152246 8153551 8154812
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 ToolFormatTest
35 */
36import java.util.ArrayList;
37import java.util.List;
38import org.testng.annotations.Test;
39
40@Test
41public class ToolFormatTest extends ReplToolTesting {
42
43    public void testSetFormat() {
44        try {
45            test(
46                    (a) -> assertCommandOutputStartsWith(a, "/set newmode test -command", "|  Created new feedback mode: test"),
47                    (a) -> assertCommand(a, "/set format test pre '$ '", ""),
48                    (a) -> assertCommand(a, "/set format test post ''", ""),
49                    (a) -> assertCommand(a, "/set format test act 'ADD' added", ""),
50                    (a) -> assertCommand(a, "/set format test act 'MOD' modified", ""),
51                    (a) -> assertCommand(a, "/set format test act 'REP' replaced", ""),
52                    (a) -> assertCommand(a, "/set format test act 'OVR' overwrote", ""),
53                    (a) -> assertCommand(a, "/set format test act 'USE' used", ""),
54                    (a) -> assertCommand(a, "/set format test act 'DRP' dropped", ""),
55                    (a) -> assertCommand(a, "/set format test up 'UP-' update", ""),
56                    (a) -> assertCommand(a, "/set format test action '{up}{act} '", ""),
57                    (a) -> assertCommand(a, "/set format test resolve 'OK' ok", ""),
58                    (a) -> assertCommand(a, "/set format test resolve 'DEF' defined", ""),
59                    (a) -> assertCommand(a, "/set format test resolve 'NODEF' notdefined", ""),
60                    (a) -> assertCommand(a, "/set format test fname ':{name} ' ", ""),
61                    (a) -> assertCommand(a, "/set format test ftype '[{type}]' method,expression", ""),
62                    (a) -> assertCommand(a, "/set format test result '={value} ' expression", ""),
63                    (a) -> assertCommand(a, "/set format test display '{pre}{action}{ftype}{fname}{result}{resolve}'", ""),
64                    (a) -> assertCommand(a, "/set format test display '{pre}HI this is enum' enum", ""),
65                    (a) -> assertCommandOutputStartsWith(a, "/set feedback test", "$ Feedback mode: test"),
66                    (a) -> assertCommand(a, "class D {}", "$ ADD :D OK"),
67                    (a) -> assertCommand(a, "void m() {}", "$ ADD []:m OK"),
68                    (a) -> assertCommand(a, "interface EX extends EEX {}", "$ ADD :EX NODEF"),
69                    (a) -> assertCommand(a, "56", "$ ADD [int]:$4 =56 OK"),
70                    (a) -> assertCommand(a, "class D { int hh; }", "$ REP :D OK$ UP-OVR :D OK"),
71                    (a) -> assertCommand(a, "enum E {A,B}", "$ HI this is enum"),
72                    (a) -> assertCommand(a, "int z() { return f(); }", "$ ADD []:z DEF"),
73                    (a) -> assertCommand(a, "z()", "$ UP-USE []:z DEF"),
74                    (a) -> assertCommand(a, "/drop z", "$ DRP []:z OK"),
75                    (a) -> assertCommandOutputStartsWith(a, "/set feedback normal", "|  Feedback mode: normal")
76            );
77        } finally {
78            assertCommandCheckOutput(false, "/set feedback normal", s -> {
79            });
80        }
81    }
82
83    public void testSetFormatSelector() {
84        List<ReplTest> tests = new ArrayList<>();
85        tests.add((a) -> assertCommandOutputStartsWith(a, "/set newmode ate -quiet",
86                            "|  Created new feedback mode: ate"));
87        tests.add((a) -> assertCommand(a, "/set feedback ate", ""));
88        StringBuilder sb = new StringBuilder();
89        class KindList {
90            final String[] values;
91            final int matchIndex;
92            int current;
93            boolean match;
94            KindList(String[] values, int matchIndex) {
95                this.values = values;
96                this.matchIndex = matchIndex;
97                this.current = 1 << values.length;
98            }
99            boolean next() {
100                if (current <= 0) {
101                    return false;
102                }
103                --current;
104                return true;
105            }
106            boolean append(boolean ahead) {
107                boolean any = false;
108                match = false;
109                for (int i = values.length - 1; i >= 0 ; --i) {
110                    if ((current & (1 << i)) != 0) {
111                        match |= i == matchIndex;
112                        if (any) {
113                            sb.append(",");
114                        } else {
115                            if (ahead) {
116                                sb.append("-");
117                            }
118                        }
119                        sb.append(values[i]);
120                        any = true;
121                    }
122                }
123                match |= !any;
124                return ahead || any;
125            }
126        }
127        KindList klcase = new KindList(new String[] {"class", "method", "expression", "vardecl"}, 2);
128        while (klcase.next()) {
129            KindList klact  = new KindList(new String[] {"added", "modified", "replaced"}, 0);
130            while (klact.next()) {
131                KindList klwhen = new KindList(new String[] {"update", "primary"}, 1);
132                while (klwhen.next()) {
133                    sb.setLength(0);
134                    klwhen.append(
135                        klact.append(
136                            klcase.append(false)));
137                    boolean match = klcase.match && klact.match && klwhen.match;
138                    String select = sb.toString();
139                    String yes = "+++" + select + "+++";
140                    String no  = "---" + select + "---";
141                    String expect = match? yes : no;
142                    tests.add((a) -> assertCommand(a, "/set format ate display '" + no  + "'", ""));
143                    tests.add((a) -> assertCommand(a, "/set format ate display '" + yes + "' " + select, ""));
144                    tests.add((a) -> assertCommand(a, "\"" + select + "\"", expect));
145                }
146            }
147        }
148        tests.add((a) -> assertCommandOutputStartsWith(a, "/set feedback normal", "|  Feedback mode: normal"));
149
150        try {
151            test(tests.toArray(new ReplTest[tests.size()]));
152        } finally {
153            assertCommandCheckOutput(false, "/set feedback normal", s -> {
154            });
155        }
156    }
157
158    public void testSetTruncation() {
159        try {
160            test(
161                    (a) -> assertCommandOutputStartsWith(a, "/set feedback normal", ""),
162                    (a) -> assertCommand(a, "String s = java.util.stream.IntStream.range(65, 74)"+
163                            ".mapToObj(i -> \"\"+(char)i).reduce((a,b) -> a + b + a).get()",
164                            "s ==> \"ABACABADABACABAEABACABADABACABAFABACABADABACABAEABACABADABACABAGABACABADABA ..."),
165                    (a) -> assertCommandOutputStartsWith(a, "/set newmode test -quiet", ""),
166                    (a) -> assertCommandOutputStartsWith(a, "/set feedback test", ""),
167                    (a) -> assertCommand(a, "/set format test display '{type}:{value}' primary", ""),
168                    (a) -> assertCommand(a, "/set truncation test 20", ""),
169                    (a) -> assertCommand(a, "/set trunc test 10 varvalue", ""),
170                    (a) -> assertCommand(a, "/set trunc test 3 assignment", ""),
171                    (a) -> assertCommand(a, "String r = s", "String:\"ABACABADABACABA ..."),
172                    (a) -> assertCommand(a, "r", "String:\"ABACA ..."),
173                    (a) -> assertCommand(a, "r=s", "String:\"AB")
174            );
175        } finally {
176            assertCommandCheckOutput(false, "/set feedback normal", s -> {
177            });
178        }
179    }
180
181    public void testShowFeedbackModes() {
182        test(
183                (a) -> assertCommandOutputContains(a, "/set feedback", "normal")
184        );
185    }
186
187    public void testSetNewModeQuiet() {
188        try {
189            test(
190                    (a) -> assertCommandOutputStartsWith(a, "/set newmode nmq -quiet normal", "|  Created new feedback mode: nmq"),
191                    (a) -> assertCommand(a, "/set feedback nmq", ""),
192                    (a) -> assertCommand(a, "/se ne nmq2 -q nor", ""),
193                    (a) -> assertCommand(a, "/se fee nmq2", ""),
194                    (a) -> assertCommand(a, "/set newmode nmc -command normal", ""),
195                    (a) -> assertCommandOutputStartsWith(a, "/set feedback nmc", "|  Feedback mode: nmc"),
196                    (a) -> assertCommandOutputStartsWith(a, "/set newmode nm", "|  Created new feedback mode: nm"),
197                    (a) -> assertCommandOutputStartsWith(a, "/set feedback nm", "|  Feedback mode: nm"),
198                    (a) -> assertCommandOutputStartsWith(a, "/set feedback normal", "|  Feedback mode: normal")
199            );
200        } finally {
201            assertCommandCheckOutput(false, "/set feedback normal", s -> {
202            });
203        }
204    }
205
206    public void testSetError() {
207        try {
208            test(
209                    (a) -> assertCommandOutputStartsWith(a, "/set newmode tee -command foo",
210                            "|  Does not match any current feedback mode: foo"),
211                    (a) -> assertCommandOutputStartsWith(a, "/set newmode tee flurb",
212                            "|  Specify either '-command' or '-quiet'"),
213                    (a) -> assertCommandOutputStartsWith(a, "/set newmode te2",
214                            "|  Created new feedback mode: te2"),
215                    (a) -> assertCommandOutputStartsWith(a, "/set newmode te2 -command",
216                            "|  Expected a new feedback mode name. 'te2' is a known feedback mode"),
217                    (a) -> assertCommandOutputStartsWith(a, "/set newmode te -command normal",
218                            "|  Created new feedback mode: te"),
219                    (a) -> assertCommand(a, "/set format te errorpre 'ERROR: '", ""),
220                    (a) -> assertCommandOutputStartsWith(a, "/set feedback te",
221                            ""),
222                    (a) -> assertCommandOutputStartsWith(a, "/set ",
223                            "ERROR: The '/set' command requires a sub-command and arguments"),
224                    (a) -> assertCommandOutputStartsWith(a, "/set xyz",
225                            "ERROR: Invalid '/set' argument: xyz"),
226                    (a) -> assertCommandOutputStartsWith(a, "/set f",
227                            "ERROR: Ambiguous sub-command argument to '/set': f"),
228                    (a) -> assertCommandOutputStartsWith(a, "/set feedback",
229                            "ERROR: Expected a feedback mode"),
230                    (a) -> assertCommandOutputStartsWith(a, "/set feedback xyz",
231                            "ERROR: Does not match any current feedback mode"),
232                    (a) -> assertCommandOutputStartsWith(a, "/set format",
233                            "ERROR: Expected a feedback mode"),
234                    (a) -> assertCommandOutputStartsWith(a, "/set format xyz",
235                            "ERROR: Does not match any current feedback mode"),
236                    (a) -> assertCommandOutputStartsWith(a, "/set format t",
237                            "ERROR: Matches more then one current feedback mode: t"),
238                    (a) -> assertCommandOutputStartsWith(a, "/set format te",
239                            "ERROR: Expected field name missing"),
240                    (a) -> assertCommandOutputStartsWith(a, "/set format te fld",
241                            "ERROR: Expected format missing"),
242                    (a) -> assertCommandOutputStartsWith(a, "/set format te fld aaa",
243                            "ERROR: Format 'aaa' must be quoted"),
244                    (a) -> assertCommandOutputStartsWith(a, "/set format te fld 'aaa' frog",
245                            "ERROR: Not a valid selector"),
246                    (a) -> assertCommandOutputStartsWith(a, "/set format te fld 'aaa' import-frog",
247                            "ERROR: Not a valid selector"),
248                    (a) -> assertCommandOutputStartsWith(a, "/set format te fld 'aaa' import-import",
249                            "ERROR: Selector kind in multiple sections of"),
250                    (a) -> assertCommandOutputStartsWith(a, "/set format te fld 'aaa' import,added",
251                            "ERROR: Different selector kinds in same sections of"),
252                    (a) -> assertCommandOutputStartsWith(a, "/set trunc te 20x",
253                            "ERROR: Truncation length must be an integer: 20x"),
254                    (a) -> assertCommandOutputStartsWith(a, "/set trunc te",
255                            "ERROR: Expected truncation length"),
256                    (a) -> assertCommandOutputStartsWith(a, "/set truncation te 111 import,added",
257                            "ERROR: Different selector kinds in same sections of"),
258                    (a) -> assertCommandOutputStartsWith(a, "/set newmode",
259                            "ERROR: Expected new feedback mode"),
260                    (a) -> assertCommandOutputStartsWith(a, "/set newmode te",
261                            "ERROR: Expected a new feedback mode name"),
262                    (a) -> assertCommandOutputStartsWith(a, "/set newmode x xyz",
263                            "ERROR: Specify either '-command' or '-quiet'"),
264                    (a) -> assertCommandOutputStartsWith(a, "/set newmode x -quiet y",
265                            "ERROR: Does not match any current feedback mode"),
266                    (a) -> assertCommandOutputStartsWith(a, "/set prompt",
267                            "ERROR: Expected a feedback mode"),
268                    (a) -> assertCommandOutputStartsWith(a, "/set prompt te",
269                            "ERROR: Expected format missing"),
270                    (a) -> assertCommandOutputStartsWith(a, "/set prompt te aaa xyz",
271                            "ERROR: Format 'aaa' must be quoted"),
272                    (a) -> assertCommandOutputStartsWith(a, "/set prompt te 'aaa' xyz",
273                            "ERROR: Format 'xyz' must be quoted"),
274                    (a) -> assertCommandOutputStartsWith(a, "/set prompt",
275                            "ERROR: Expected a feedback mode"),
276                    (a) -> assertCommandOutputStartsWith(a, "/set prompt te",
277                            "ERROR: Expected format missing"),
278                    (a) -> assertCommandOutputStartsWith(a, "/set prompt te aaa",
279                            "ERROR: Format 'aaa' must be quoted"),
280                    (a) -> assertCommandOutputStartsWith(a, "/set prompt te 'aaa'",
281                            "ERROR: Expected format missing"),
282                    (a) -> assertCommandOutputStartsWith(a, "/set feedback normal",
283                            "|  Feedback mode: normal")
284            );
285        } finally {
286            assertCommandCheckOutput(false, "/set feedback normal", s -> {
287            });
288        }
289    }
290
291    public void testSetHelp() {
292        try {
293            test(
294                    (a) -> assertCommandOutputContains(a, "/help /set", "command to launch"),
295                    (a) -> assertCommandOutputContains(a, "/help /set format", "display"),
296                    (a) -> assertCommandOutputContains(a, "/hel /se for", "vardecl"),
297                    (a) -> assertCommandOutputContains(a, "/help /set editor", "temporary file")
298            );
299        } finally {
300            assertCommandCheckOutput(false, "/set feedback normal", s -> {
301            });
302        }
303    }
304}
305