File: StartOptionTest.java

package info (click to toggle)
libnb-javaparser-java 9%2B2018-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 65,320 kB
  • sloc: java: 440,096; xml: 6,359; sh: 865; makefile: 314
file content (266 lines) | stat: -rw-r--r-- 11,229 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
 * Copyright (c) 2015, 2017, 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 8151754 8080883 8160089 8170162 8166581 8172102 8171343 8178023
 * @summary Testing start-up options.
 * @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
 * @library /tools/lib
 * @build Compiler toolbox.ToolBox
 * @run testng StartOptionTest
 */

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Locale;
import java.util.function.Consumer;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import jdk.jshell.tool.JavaShellToolBuilder;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

@Test
public class StartOptionTest {

    private ByteArrayOutputStream cmdout;
    private ByteArrayOutputStream cmderr;
    private ByteArrayOutputStream console;
    private ByteArrayOutputStream userout;
    private ByteArrayOutputStream usererr;
    private InputStream cmdInStream;

    private JavaShellToolBuilder builder() {
        // turn on logging of launch failures
        Logger.getLogger("jdk.jshell.execution").setLevel(Level.ALL);
        return JavaShellToolBuilder
                    .builder()
                    .out(new PrintStream(cmdout), new PrintStream(console), new PrintStream(userout))
                    .err(new PrintStream(cmderr), new PrintStream(usererr))
                    .in(cmdInStream, null)
                    .persistence(new HashMap<>())
                    .env(new HashMap<>())
                    .locale(Locale.ROOT);
    }

    private void runShell(String... args) {
        try {
            builder()
                    .run(args);
        } catch (Exception ex) {
            fail("Repl tool died with exception", ex);
        }
    }

    protected void check(ByteArrayOutputStream str, Consumer<String> checkOut, String label) {
        byte[] bytes = str.toByteArray();
        str.reset();
        String out =  new String(bytes, StandardCharsets.UTF_8);
        if (checkOut != null) {
            checkOut.accept(out);
        } else {
            assertEquals("", out, label + ": Expected empty -- ");
        }
    }

    protected void start(Consumer<String> checkCmdOutput,
            Consumer<String> checkUserOutput, Consumer<String> checkError,
            String... args) throws Exception {
        runShell(args);
        check(cmdout, checkCmdOutput, "cmdout");
        check(cmderr, checkError, "cmderr");
        check(console, null, "console");
        check(userout, checkUserOutput, "userout");
        check(usererr, null, "usererr");
    }

    protected void start(String expectedCmdOutput, String expectedError, String... args) throws Exception {
        startWithUserOutput(expectedCmdOutput, "",  expectedError, args);
    }

    private void startWithUserOutput(String expectedCmdOutput, String expectedUserOutput,
            String expectedError, String... args) throws Exception {
        start(
                s -> assertEquals(s.trim(), expectedCmdOutput, "cmdout: "),
                s -> assertEquals(s.trim(), expectedUserOutput, "userout: "),
                s -> assertEquals(s.trim(), expectedError, "cmderr: "),
                args);
    }

    @BeforeMethod
    public void setUp() {
        cmdout  = new ByteArrayOutputStream();
        cmderr  = new ByteArrayOutputStream();
        console = new ByteArrayOutputStream();
        userout = new ByteArrayOutputStream();
        usererr = new ByteArrayOutputStream();
        cmdInStream = new ByteArrayInputStream("/exit\n".getBytes());
    }

    protected String writeToFile(String stuff) throws Exception {
        Compiler compiler = new Compiler();
        Path p = compiler.getPath("doit.repl");
        compiler.writeToFile(p, stuff);
        return p.toString();
    }

    public void testCommandFile() throws Exception {
        String fn = writeToFile("String str = \"Hello \"\n/list\nSystem.out.println(str + str)\n/exit\n");
        startWithUserOutput("1 : String str = \"Hello \";", "Hello Hello", "", "--no-startup", fn, "-s");
    }

    public void testUsage() throws Exception {
        for (String opt : new String[]{"-h", "--help"}) {
            start(s -> {
                assertTrue(s.split("\n").length >= 7, "Not enough usage lines: " + s);
                assertTrue(s.startsWith("Usage:   jshell <options>"), "Unexpect usage start: " + s);
                assertTrue(s.contains("--show-version"), "Expected help: " + s);
                assertFalse(s.contains("Welcome"), "Unexpected start: " + s);
            }, null, null, opt);
        }
    }

    public void testHelpExtra() throws Exception {
        for (String opt : new String[]{"-X", "--help-extra"}) {
            start(s -> {
                assertTrue(s.split("\n").length >= 5, "Not enough help-extra lines: " + s);
                assertTrue(s.contains("--add-exports"), "Expected --add-exports: " + s);
                assertTrue(s.contains("--execution"), "Expected --add-exports: " + s);
                assertFalse(s.contains("Welcome"), "Unexpected start: " + s);
            }, null, null, opt);
        }
    }

    public void testUnknown() throws Exception {
        start(null, null,
              s -> assertEquals(s.trim(), "Unknown option: u"), "-unknown");
        start(null, null,
              s -> assertEquals(s.trim(), "Unknown option: unknown"), "--unknown");
    }

    public void testStartup() throws Exception {
        Compiler compiler = new Compiler();
        Path p = compiler.getPath("file.txt");
        compiler.writeToFile(p);
        start("", "Argument to startup missing.", "--startup");
        start("", "Conflicting options: both --startup and --no-startup were used.", "--no-startup", "--startup", p.toString());
        start("", "Conflicting options: both --startup and --no-startup were used.", "--startup", p.toString(), "--no-startup");
        start("", "Argument to startup missing.", "--no-startup", "--startup");
    }

    public void testStartupFailedOption() throws Exception {
        start(
                s -> assertEquals(s.trim(), "", "cmdout: "),
                s -> assertEquals(s.trim(), "", "userout: "),
                s -> assertTrue(s.contains("Unrecognized option: -hoge-foo-bar"), "cmderr: " + s),
                "-R-hoge-foo-bar");
    }

    public void testStartupUnknown() throws Exception {
        start("", "File 'UNKNOWN' for '--startup' is not found.", "--startup", "UNKNOWN");
        start("", "File 'UNKNOWN' for '--startup' is not found.", "--startup", "DEFAULT", "--startup", "UNKNOWN");
    }

    public void testClasspath() throws Exception {
        for (String cp : new String[] {"--class-path"}) {
            start("", "Only one --class-path option may be used.", cp, ".", "--class-path", ".");
            start("", "Argument to class-path missing.", cp);
        }
    }

    public void testUnknownModule() throws Exception {
        start(
                s -> assertEquals(s.trim(), "", "cmdout: "),
                s -> assertEquals(s.trim(), "", "userout: "),
                s -> assertTrue(s.contains("rror") && s.contains("unKnown"), "cmderr: " + s),
                "--add-modules", "unKnown");
    }

    public void testFeedbackOptionConflict() throws Exception {
        start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.",
                "--feedback", "concise", "--feedback", "verbose");
        start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "--feedback", "concise", "-s");
        start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "--feedback", "verbose", "-q");
        start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "--feedback", "concise", "-v");
        start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-v", "--feedback", "concise");
        start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-q", "-v");
        start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-s", "-v");
        start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-v", "-q");
        start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-q", "-s");
    }

    public void testNegFeedbackOption() throws Exception {
        start("", "Argument to feedback missing.", "--feedback");
        start("", "Does not match any current feedback mode: blorp -- --feedback blorp", "--feedback", "blorp");
    }

    public void testVersion() throws Exception {
        start(
                s -> {
                    assertTrue(s.startsWith("jshell"), "unexpected version: " + s);
                    assertFalse(s.contains("Welcome"), "Unexpected start: " + s);
                },
                null, null,
                "--version");
    }

    public void testShowVersion() throws Exception {
        runShell("--show-version");
        check(cmdout,
                s -> {
                    assertTrue(s.startsWith("jshell"), "unexpected version: " + s);
                    assertTrue(s.contains("Welcome"), "Expected start (but got no welcome): " + s);
                },
                "cmdout");
        check(cmderr, null, "cmderr");
        check(console,
                s -> assertTrue(s.trim().startsWith("jshell>"), "Expected prompt, got: " + s),
                "console");
        check(userout, null, "userout");
        check(usererr, null, "usererr");
    }

    @AfterMethod
    public void tearDown() {
        cmdout  = null;
        cmderr  = null;
        console = null;
        userout = null;
        usererr = null;
        cmdInStream = null;
    }
}