ToolFormatTest.java revision 3746:46f2219faf5a
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 8157261 8163840 8166637 8161969
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.io.BufferedReader;
37import java.io.IOException;
38import java.io.StringReader;
39import java.util.ArrayList;
40import java.util.List;
41import java.util.logging.Level;
42import java.util.logging.Logger;
43import org.testng.annotations.Test;
44import static org.testng.Assert.assertEquals;
45import static org.testng.Assert.assertTrue;
46import static org.testng.Assert.fail;
47
48@Test
49public class ToolFormatTest extends ReplToolTesting {
50
51    public void testSetFormat() {
52        try {
53            test(
54                    (a) -> assertCommandOutputStartsWith(a, "/set mode test -command", "|  Created new feedback mode: test"),
55                    (a) -> assertCommand(a, "/set format test pre '$ '", ""),
56                    (a) -> assertCommand(a, "/set format test post ''", ""),
57                    (a) -> assertCommand(a, "/set format test act 'ADD' added", ""),
58                    (a) -> assertCommand(a, "/set format test act 'MOD' modified", ""),
59                    (a) -> assertCommand(a, "/set format test act 'REP' replaced", ""),
60                    (a) -> assertCommand(a, "/set format test act 'OVR' overwrote", ""),
61                    (a) -> assertCommand(a, "/set format test act 'USE' used", ""),
62                    (a) -> assertCommand(a, "/set format test act 'DRP' dropped", ""),
63                    (a) -> assertCommand(a, "/set format test up 'UP-' update", ""),
64                    (a) -> assertCommand(a, "/set format test action '{up}{act} '", ""),
65                    (a) -> assertCommand(a, "/set format test resolve 'OK' ok", ""),
66                    (a) -> assertCommand(a, "/set format test resolve 'DEF' defined", ""),
67                    (a) -> assertCommand(a, "/set format test resolve 'NODEF' notdefined", ""),
68                    (a) -> assertCommand(a, "/set format test fname ':{name} ' ", ""),
69                    (a) -> assertCommand(a, "/set format test ftype '[{type}]' method,expression", ""),
70                    (a) -> assertCommand(a, "/set format test result '={value} ' expression", ""),
71                    (a) -> assertCommand(a, "/set format test display '{pre}{action}{ftype}{fname}{result}{resolve}'", ""),
72                    (a) -> assertCommand(a, "/set format test display '{pre}HI this is enum' enum", ""),
73                    (a) -> assertCommandOutputStartsWith(a, "/set feedback test", "$ Feedback mode: test"),
74                    (a) -> assertCommand(a, "class D {}", "$ ADD :D OK"),
75                    (a) -> assertCommand(a, "void m() {}", "$ ADD []:m OK"),
76                    (a) -> assertCommand(a, "interface EX extends EEX {}", "$ ADD :EX NODEF"),
77                    (a) -> assertCommand(a, "56", "$ ADD [int]:$4 =56 OK"),
78                    (a) -> assertCommand(a, "class D { int hh; }", "$ REP :D OK$ UP-OVR :D OK"),
79                    (a) -> assertCommand(a, "enum E {A,B}", "$ HI this is enum"),
80                    (a) -> assertCommand(a, "int z() { return f(); }", "$ ADD []:z DEF"),
81                    (a) -> assertCommand(a, "z()", "$ UP-USE []:z DEF"),
82                    (a) -> assertCommand(a, "/drop z", "$ DRP []:z OK"),
83                    (a) -> assertCommandOutputStartsWith(a, "/set feedback normal", "|  Feedback mode: normal")
84            );
85        } finally {
86            assertCommandCheckOutput(false, "/set feedback normal", s -> {
87            });
88        }
89    }
90
91    public void testSetFormatOverride() {
92        test(
93                (a) -> assertCommand(a, "/set mode tm -c", "|  Created new feedback mode: tm"),
94                (a) -> assertCommand(a, "/se fo tm x \"aaa\"", ""),
95                (a) -> assertCommand(a, "/se fo tm x \"bbb\" class,method-added", ""),
96                (a) -> assertCommand(a, "/se fo tm x",
97                        "|  /set format tm x \"aaa\" \n" +
98                        "|  /set format tm x \"bbb\" class,method-added"),
99                (a) -> assertCommand(a, "/se fo tm x \"ccc\" class,method-added,modified", ""),
100                (a) -> assertCommand(a, "/se fo tm x \"ddd\" class,method-added", ""),
101                (a) -> assertCommand(a, "/se fo tm x \"eee\" method-added", ""),
102                (a) -> assertCommand(a, "/se fo tm x",
103                        "|  /set format tm x \"aaa\" \n" +
104                        "|  /set format tm x \"ccc\" class,method-added,modified\n" +
105                        "|  /set format tm x \"ddd\" class,method-added\n" +
106                        "|  /set format tm x \"eee\" method-added"),
107                (a) -> assertCommand(a, "/se fo tm x \"EEE\" method-added,replaced", ""),
108                (a) -> assertCommand(a, "/se fo tm x",
109                        "|  /set format tm x \"aaa\" \n" +
110                        "|  /set format tm x \"ccc\" class,method-added,modified\n" +
111                        "|  /set format tm x \"ddd\" class,method-added\n" +
112                        "|  /set format tm x \"EEE\" method-added,replaced"),
113                (a) -> assertCommand(a, "/se fo tm x \"fff\" method-added,replaced-ok", ""),
114                (a) -> assertCommand(a, "/se fo tm x",
115                        "|  /set format tm x \"aaa\" \n" +
116                        "|  /set format tm x \"ccc\" class,method-added,modified\n" +
117                        "|  /set format tm x \"ddd\" class,method-added\n" +
118                        "|  /set format tm x \"EEE\" method-added,replaced\n" +
119                        "|  /set format tm x \"fff\" method-added,replaced-ok"),
120                (a) -> assertCommand(a, "/se fo tm x \"ggg\" method-ok", ""),
121                (a) -> assertCommand(a, "/se fo tm x",
122                        "|  /set format tm x \"aaa\" \n" +
123                        "|  /set format tm x \"ccc\" class,method-added,modified\n" +
124                        "|  /set format tm x \"ddd\" class,method-added\n" +
125                        "|  /set format tm x \"EEE\" method-added,replaced\n" +
126                        "|  /set format tm x \"ggg\" method-ok"),
127                (a) -> assertCommand(a, "/se fo tm x \"hhh\" method", ""),
128                (a) -> assertCommand(a, "/se fo tm x",
129                        "|  /set format tm x \"aaa\" \n" +
130                        "|  /set format tm x \"ccc\" class,method-added,modified\n" +
131                        "|  /set format tm x \"ddd\" class,method-added\n" +
132                        "|  /set format tm x \"hhh\" method"),
133                (a) -> assertCommand(a, "/se fo tm x \"iii\" method,class", ""),
134                (a) -> assertCommand(a, "/se fo tm x",
135                        "|  /set format tm x \"aaa\" \n" +
136                        "|  /set format tm x \"iii\" class,method"),
137                (a) -> assertCommand(a, "/se fo tm x \"jjj\"", ""),
138                (a) -> assertCommand(a, "/se fo tm x",
139                        "|  /set format tm x \"jjj\"")
140        );
141    }
142
143    public void testSetFormatSelector() {
144        List<ReplTest> tests = new ArrayList<>();
145        tests.add((a) -> assertCommandOutputStartsWith(a, "/set mode ate -quiet",
146                            "|  Created new feedback mode: ate"));
147        tests.add((a) -> assertCommand(a, "/set feedback ate", ""));
148        StringBuilder sb = new StringBuilder();
149        class KindList {
150            final String[] values;
151            final int matchIndex;
152            int current;
153            boolean match;
154            KindList(String[] values, int matchIndex) {
155                this.values = values;
156                this.matchIndex = matchIndex;
157                this.current = 1 << values.length;
158            }
159            boolean next() {
160                if (current <= 0) {
161                    return false;
162                }
163                --current;
164                return true;
165            }
166            boolean append(boolean ahead) {
167                boolean any = false;
168                match = false;
169                for (int i = values.length - 1; i >= 0 ; --i) {
170                    if ((current & (1 << i)) != 0) {
171                        match |= i == matchIndex;
172                        if (any) {
173                            sb.append(",");
174                        } else {
175                            if (ahead) {
176                                sb.append("-");
177                            }
178                        }
179                        sb.append(values[i]);
180                        any = true;
181                    }
182                }
183                match |= !any;
184                return ahead || any;
185            }
186        }
187        KindList klcase = new KindList(new String[] {"class", "method", "expression", "vardecl"}, 2);
188        while (klcase.next()) {
189            KindList klact  = new KindList(new String[] {"added", "modified", "replaced"}, 0);
190            while (klact.next()) {
191                KindList klwhen = new KindList(new String[] {"update", "primary"}, 1);
192                while (klwhen.next()) {
193                    sb.setLength(0);
194                    klwhen.append(
195                        klact.append(
196                            klcase.append(false)));
197                    boolean match = klcase.match && klact.match && klwhen.match;
198                    String select = sb.toString();
199                    String yes = "+++" + select + "+++";
200                    String no  = "---" + select + "---";
201                    String expect = match? yes : no;
202                    tests.add((a) -> assertCommand(a, "/set format ate display '" + no  + "'", ""));
203                    tests.add((a) -> assertCommand(a, "/set format ate display '" + yes + "' " + select, ""));
204                    tests.add((a) -> assertCommand(a, "\"" + select + "\"", expect));
205                }
206            }
207        }
208        tests.add((a) -> assertCommandOutputStartsWith(a, "/set feedback normal", "|  Feedback mode: normal"));
209
210        try {
211            test(tests.toArray(new ReplTest[tests.size()]));
212        } finally {
213            assertCommandCheckOutput(false, "/set feedback normal", s -> {
214            });
215        }
216    }
217
218    public void testSetTruncation() {
219        try {
220            test(
221                    (a) -> assertCommandOutputStartsWith(a, "/set feedback normal", ""),
222                    (a) -> assertCommand(a, "String s = java.util.stream.IntStream.range(65, 74)"+
223                            ".mapToObj(i -> \"\"+(char)i).reduce((a,b) -> a + b + a).get() + \"XYZ\"",
224                            "s ==> \"ABACABADABACABAEABACABADABACABAFABACABADABACABAE ... BACABAEABACABADABACABAXYZ\""),
225                    (a) -> assertCommandOutputStartsWith(a, "/set mode test -quiet", ""),
226                    (a) -> assertCommandOutputStartsWith(a, "/set feedback test", ""),
227                    (a) -> assertCommand(a, "/set format test display '{type}:{value}' primary", ""),
228                    (a) -> assertCommand(a, "/set truncation test 20", ""),
229                    (a) -> assertCommand(a, "/set truncation test", "|  /set truncation test 20"),
230                    (a) -> assertCommandOutputContains(a, "/set truncation", "/set truncation test 20"),
231                    (a) -> assertCommand(a, "/set trunc test 10 varvalue", ""),
232                    (a) -> assertCommand(a, "/set trunc test 3 assignment", ""),
233                    (a) -> assertCommandOutputContains(a, "/set truncation",
234                            "/set truncation test 10 varvalue"),
235                    (a) -> assertCommandOutputContains(a, "/set truncation test",
236                            "/set truncation test 10 varvalue"),
237                    (a) -> assertCommand(a, "/var", "|    String s = \"ABACABADA"),
238                    (a) -> assertCommand(a, "String r = s", "String:\"ABACABAD ... BAXYZ\""),
239                    (a) -> assertCommand(a, "r", "String:\"ABACABADA"),
240                    (a) -> assertCommand(a, "r=s", "String:\"AB")
241            );
242        } finally {
243            assertCommandCheckOutput(false, "/set feedback normal", s -> {
244            });
245        }
246    }
247
248    public void testDefaultTruncation() {
249        test(
250                    (a) -> assertCommand(a, "char[] cs = new char[2000];", null),
251                    (a) -> assertCommand(a, "Arrays.fill(cs, 'A');", ""),
252                    (a) -> assertCommandCheckOutput(a, "String s = new String(cs)",
253                            (s) -> {
254                                assertTrue(s.length() < 120, "Result too long (" + s.length() + ") -- " + s);
255                                assertTrue(s.contains("AAAAAAAAAAAAAAAAAA"), "Bad value: " + s);
256                            }),
257                    (a) -> assertCommandCheckOutput(a, "s",
258                            (s) -> {
259                                assertTrue(s.length() > 300, "Result too short (" + s.length() + ") -- " + s);
260                                assertTrue(s.contains("AAAAAAAAAAAAAAAAAA"), "Bad value: " + s);
261                            }),
262                    (a) -> assertCommandCheckOutput(a, "\"X\" + s",
263                            (s) -> {
264                                assertTrue(s.length() > 300, "Result too short (" + s.length() + ") -- " + s);
265                                assertTrue(s.contains("XAAAAAAAAAAAAAAAAAA"), "Bad value: " + s);
266                            })
267        );
268    }
269
270    public void testPrompt() {
271        test(
272                (a) -> assertCommand(a, "/set mode tp -quiet", "|  Created new feedback mode: tp"),
273                (a) -> assertCommand(a, "/set prompt tp 'aaa' 'bbb'", ""),
274                (a) -> assertCommand(a, "/set prompt tp",
275                        "|  /set prompt tp \"aaa\" \"bbb\""),
276                (a) -> assertCommandOutputContains(a, "/set prompt",
277                        "|  /set prompt tp \"aaa\" \"bbb\""),
278                (a) -> assertCommand(a, "/set mode -retain tp", ""),
279                (a) -> assertCommand(a, "/set prompt tp 'ccc' 'ddd'", ""),
280                (a) -> assertCommand(a, "/set prompt tp",
281                        "|  /set prompt tp \"ccc\" \"ddd\""),
282                (a) -> assertCommandCheckOutput(a, "/set mode tp",
283                        (s) -> {
284                            try {
285                                BufferedReader rdr = new BufferedReader(new StringReader(s));
286                                assertEquals(rdr.readLine(), "|  /set mode tp -quiet",
287                                        "|  /set mode tp -quiet");
288                                assertEquals(rdr.readLine(), "|  /set prompt tp \"aaa\" \"bbb\"",
289                                        "|  /set prompt tp \"aaa\" \"bbb\"");
290                                String l = rdr.readLine();
291                                while (l.startsWith("|  /set format tp ")) {
292                                    l = rdr.readLine();
293                                }
294                                assertEquals(l, "|  /set mode -retain tp",
295                                        "|  /set mode -retain tp");
296                                assertEquals(rdr.readLine(), "|  ",
297                                        "|  ");
298                                assertEquals(rdr.readLine(), "|  /set mode tp -quiet",
299                                        "|  /set mode tp -quiet");
300                                assertEquals(rdr.readLine(), "|  /set prompt tp \"ccc\" \"ddd\"",
301                                        "|  /set prompt tp \"ccc\" \"ddd\"");
302                            } catch (IOException ex) {
303                                fail("threw " + ex);
304                            }
305                        })
306        );
307    }
308
309    public void testShowFeedbackModes() {
310        test(
311                (a) -> assertCommandOutputContains(a, "/set feedback", "normal")
312        );
313    }
314
315    public void testSetNewModeQuiet() {
316        try {
317            test(
318                    (a) -> assertCommandOutputStartsWith(a, "/set mode nmq -quiet normal", "|  Created new feedback mode: nmq"),
319                    (a) -> assertCommand(a, "/set feedback nmq", ""),
320                    (a) -> assertCommand(a, "/se mo nmq2 -q nor", ""),
321                    (a) -> assertCommand(a, "/se fee nmq2", ""),
322                    (a) -> assertCommand(a, "/set mode nmc -command normal", ""),
323                    (a) -> assertCommandOutputStartsWith(a, "/set feedback nmc", "|  Feedback mode: nmc"),
324                    (a) -> assertCommandOutputStartsWith(a, "/set mode nm -command",
325                            "|  Created new feedback mode: nm"),
326                    (a) -> assertCommandOutputStartsWith(a, "/set feedback nm", "|  Feedback mode: nm"),
327                    (a) -> assertCommandOutputStartsWith(a, "/set feedback normal", "|  Feedback mode: normal")
328            );
329        } finally {
330            assertCommandCheckOutput(false, "/set feedback normal", s -> {
331            });
332        }
333    }
334
335    public void testSetError() {
336        try {
337            test(
338                    (a) -> assertCommandOutputStartsWith(a, "/set mode tee -command foo",
339                            "|  Does not match any current feedback mode: foo"),
340                    (a) -> assertCommandOutputStartsWith(a, "/set mode tee -quiet flurb",
341                            "|  Does not match any current feedback mode: flurb"),
342                    (a) -> assertCommandOutputStartsWith(a, "/set mode -command tee",
343                            "|  Created new feedback mode: tee"),
344                    (a) -> assertCommandOutputStartsWith(a, "/set mode verbose -command",
345                            "|  Mode to be created already exists: verbose"),
346                    (a) -> assertCommandOutputStartsWith(a, "/set mode te -command normal",
347                            "|  Created new feedback mode: te"),
348                    (a) -> assertCommand(a, "/set format te errorpre 'ERROR: '", ""),
349                    (a) -> assertCommandOutputStartsWith(a, "/set feedback te",
350                            ""),
351                    (a) -> assertCommandOutputStartsWith(a, "/set xyz",
352                            "ERROR: Invalid '/set' argument: xyz"),
353                    (a) -> assertCommandOutputStartsWith(a, "/set f",
354                            "ERROR: Ambiguous sub-command argument to '/set': f"),
355                    (a) -> assertCommandOutputStartsWith(a, "/set feedback",
356                            "|  /set feedback te"),
357                    (a) -> assertCommandOutputStartsWith(a, "/set feedback xyz",
358                            "ERROR: Does not match any current feedback mode"),
359                    (a) -> assertCommandOutputStartsWith(a, "/set feed",
360                            "|  /set feedback te"),
361                    (a) -> assertCommandOutputStartsWith(a, "/set format xyz",
362                            "ERROR: Does not match any current feedback mode"),
363                    (a) -> assertCommandOutputStartsWith(a, "/set format t",
364                            "ERROR: Matches more then one current feedback mode: t"),
365                    (a) -> assertCommandOutputStartsWith(a, "/set format qqq",
366                            "ERROR: Does not match any current feedback mode: qqq"),
367                    (a) -> assertCommandOutputStartsWith(a, "/set format te fld",
368                            "ERROR: Expected a field name:"),
369                    (a) -> assertCommandOutputStartsWith(a, "/set format te fld aaa",
370                            "ERROR: Format 'aaa' must be quoted"),
371                    (a) -> assertCommandOutputStartsWith(a, "/set format te fld 'aaa' frog",
372                            "ERROR: Not a valid selector"),
373                    (a) -> assertCommandOutputStartsWith(a, "/set format te fld 'aaa' import-frog",
374                            "ERROR: Not a valid selector"),
375                    (a) -> assertCommandOutputStartsWith(a, "/set format te fld 'aaa' import-import",
376                            "ERROR: Selector kind in multiple sections of"),
377                    (a) -> assertCommandOutputStartsWith(a, "/set format te fld 'aaa' import,added",
378                            "ERROR: Different selector kinds in same sections of"),
379                    (a) -> assertCommandOutputStartsWith(a, "/set trunc te 20x",
380                            "ERROR: Truncation length must be an integer: 20x"),
381                    (a) -> assertCommandOutputStartsWith(a, "/set trunc qaz",
382                            "ERROR: Does not match any current feedback mode: qaz -- /set trunc qaz"),
383                    (a) -> assertCommandOutputStartsWith(a, "/set truncation te 111 import,added",
384                            "ERROR: Different selector kinds in same sections of"),
385                    (a) -> assertCommandOutputContains(a, "/set mode",
386                            "|  /set truncation verbose"),
387                    (a) -> assertCommandOutputStartsWith(a, "/set mode -command",
388                            "ERROR: Missing the feedback mode"),
389                    (a) -> assertCommandOutputStartsWith(a, "/set mode x -quiet y",
390                            "ERROR: Does not match any current feedback mode"),
391                    (a) -> assertCommandOutputStartsWith(a, "/set prompt",
392                            "|  /set prompt"),
393                    (a) -> assertCommandOutputStartsWith(a, "/set prompt te",
394                            "|  /set prompt te "),
395                    (a) -> assertCommandOutputStartsWith(a, "/set prompt te aaa xyz",
396                            "ERROR: Format 'aaa' must be quoted"),
397                    (a) -> assertCommandOutputStartsWith(a, "/set prompt te 'aaa' xyz",
398                            "ERROR: Format 'xyz' must be quoted"),
399                    (a) -> assertCommandOutputStartsWith(a, "/set prompt te aaa",
400                            "ERROR: Format 'aaa' must be quoted"),
401                    (a) -> assertCommandOutputStartsWith(a, "/set prompt te 'aaa'",
402                            "ERROR: Continuation prompt required"),
403                    (a) -> assertCommandOutputStartsWith(a, "/set feedback normal",
404                            "|  Feedback mode: normal")
405            );
406        } finally {
407            assertCommandCheckOutput(false, "/set feedback normal", s -> {
408            });
409        }
410    }
411
412    public void testSetHelp() {
413        try {
414            test(
415                    (a) -> assertCommandOutputContains(a, "/help /set", "command to launch"),
416                    (a) -> assertCommandOutputContains(a, "/help /set format", "display"),
417                    (a) -> assertCommandOutputContains(a, "/hel /se for", "vardecl"),
418                    (a) -> assertCommandOutputContains(a, "/help /set editor", "temporary file")
419            );
420        } finally {
421            assertCommandCheckOutput(false, "/set feedback normal", s -> {
422            });
423        }
424    }
425}
426