ToolProviderTest.java revision 4111:256d9fce6c53
11558Srgrimes/*
21558Srgrimes * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
31558Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
41558Srgrimes *
51558Srgrimes * This code is free software; you can redistribute it and/or modify it
61558Srgrimes * under the terms of the GNU General Public License version 2 only, as
71558Srgrimes * published by the Free Software Foundation.
81558Srgrimes *
91558Srgrimes * This code is distributed in the hope that it will be useful, but WITHOUT
101558Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
111558Srgrimes * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
121558Srgrimes * version 2 for more details (a copy is included in the LICENSE file that
131558Srgrimes * accompanied this code).
141558Srgrimes *
151558Srgrimes * You should have received a copy of the GNU General Public License version
161558Srgrimes * 2 along with this work; if not, write to the Free Software Foundation,
171558Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
181558Srgrimes *
191558Srgrimes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
201558Srgrimes * or visit www.oracle.com if you need additional information or have any
211558Srgrimes * questions.
221558Srgrimes */
231558Srgrimes
241558Srgrimesimport java.io.ByteArrayInputStream;
251558Srgrimesimport java.io.ByteArrayOutputStream;
261558Srgrimesimport java.io.InputStream;
271558Srgrimesimport java.util.ServiceLoader;
281558Srgrimesimport java.util.function.Consumer;
291558Srgrimesimport javax.tools.Tool;
301558Srgrimesimport org.testng.annotations.BeforeMethod;
311558Srgrimesimport org.testng.annotations.Test;
321558Srgrimesimport static org.testng.Assert.assertTrue;
331558Srgrimesimport static org.testng.Assert.fail;
341558Srgrimes
351558Srgrimes/*
3637906Scharnier * @test
3723685Speter * @bug 8170044 8171343
3837906Scharnier * @summary Test ServiceLoader launching of jshell tool
391558Srgrimes * @modules jdk.compiler/com.sun.tools.javac.api
401558Srgrimes *          jdk.compiler/com.sun.tools.javac.main
41146754Scharnier *          jdk.jdeps/com.sun.tools.javap
42146754Scharnier *          jdk.jshell/jdk.internal.jshell.tool
43146754Scharnier * @library /tools/lib
441558Srgrimes * @build Compiler toolbox.ToolBox
451558Srgrimes * @run testng ToolProviderTest
461558Srgrimes */
471558Srgrimes@Test
4866907Swollmanpublic class ToolProviderTest extends StartOptionTest {
491558Srgrimes
501558Srgrimes    private ByteArrayOutputStream cmdout;
511558Srgrimes    private ByteArrayOutputStream cmderr;
521558Srgrimes    private InputStream cmdInStream;
531558Srgrimes
54103949Smike    @BeforeMethod
5573986Sobrien    @Override
561558Srgrimes    public void setUp() {
57164911Sdwmalone        cmdout = new ByteArrayOutputStream();
581558Srgrimes        cmderr = new ByteArrayOutputStream();
591558Srgrimes        cmdInStream = new ByteArrayInputStream("/exit\n".getBytes());
601558Srgrimes    }
6166907Swollman
62129665Sstefanf    @Override
631558Srgrimes    protected void start(Consumer<String> checkCmdOutput,
641558Srgrimes            Consumer<String> checkUserOutput, Consumer<String> checkError,
651558Srgrimes            String... args) throws Exception {
661558Srgrimes        if (runShellServiceLoader(args) != 0) {
671558Srgrimes            fail("Repl tool failed");
681558Srgrimes        }
691558Srgrimes        check(cmdout, checkCmdOutput, "cmdout");
701558Srgrimes        check(cmderr, checkError, "cmderr");
71128175Sgreen    }
72128175Sgreen
7321174Sguido    private int runShellServiceLoader(String... args) {
741558Srgrimes        ServiceLoader<Tool> sl = ServiceLoader.load(Tool.class);
751558Srgrimes        for (Tool provider : sl) {
761558Srgrimes            if (provider.name().equals("jshell")) {
771558Srgrimes                return provider.run(cmdInStream, cmdout, cmderr, args);
78164911Sdwmalone            }
791558Srgrimes        }
8098542Smckusick        throw new AssertionError("Repl tool not found by ServiceLoader: " + sl);
811558Srgrimes    }
821558Srgrimes
831558Srgrimes    @Override
841558Srgrimes    public void testCommandFile() throws Exception {
8598542Smckusick        String fn = writeToFile("String str = \"Hello \"\n/list\nSystem.out.println(str + str)\n/exit\n");
861558Srgrimes        start("1 : String str = \"Hello \";" + "\n" + "Hello Hello", "", "--no-startup", fn, "-s");
871558Srgrimes    }
881558Srgrimes
891558Srgrimes    @Override
901558Srgrimes    public void testShowVersion() throws Exception {
911558Srgrimes        start(
9298542Smckusick                s -> {
93144099Simp                    assertTrue(s.startsWith("jshell "), "unexpected version: " + s);
941558Srgrimes                    assertTrue(s.contains("Welcome"), "Expected start (but got no welcome): " + s);
951558Srgrimes                    assertTrue(s.trim().contains("jshell>"), "Expected prompt, got: " + s);
961558Srgrimes                },
9792837Simp                null, null,
9892837Simp                "--show-version");
9992837Simp    }
10092837Simp}
10192837Simp