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 8168615
27  * @summary Test ExecutionControlProvider specs can load user ExecutionControlProviders
28  * with direct maps.
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.jshell.execution
33  *          jdk.jshell/jdk.jshell.spi
34  * @library /tools/lib
35  * @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask
36  * @build KullaTesting Compiler
37  * @run testng ExecutionControlSpecTest
38  */
39 
40 import java.nio.file.Path;
41 import java.nio.file.Paths;
42 import org.testng.annotations.AfterMethod;
43 import org.testng.annotations.Test;
44 import org.testng.annotations.BeforeMethod;
45 
46 public class ExecutionControlSpecTest extends KullaTesting {
47 
48     ClassLoader ccl;
49 
50     @BeforeMethod
51     @Override
setUp()52     public void setUp() {
53         String mod = "my.ec";
54         String pkg = "package my.ec;\n";
55         Compiler compiler = new Compiler();
56         Path modDir = Paths.get("mod");
57         compiler.compile(modDir,
58                 pkg +
59                 "public class PrefixingExecutionControl extends jdk.jshell.execution.LocalExecutionControl {\n" +
60                 "    @Override\n" +
61                 "    public String invoke(String className, String methodName)\n" +
62                 "            throws RunException, InternalException, EngineTerminationException {\n" +
63                 "        return \"Blah:\" + super.invoke(className, methodName);\n" +
64                 "    }\n" +
65                 "}\n",
66                 pkg +
67                 "public class PrefixingExecutionControlProvider implements jdk.jshell.spi.ExecutionControlProvider {\n" +
68                 "    @Override\n" +
69                 "    public String name() {\n" +
70                 "        return \"prefixing\";\n" +
71                 "    }\n" +
72                 "    @Override\n" +
73                 "    public jdk.jshell.spi.ExecutionControl generate(jdk.jshell.spi.ExecutionEnv env,\n" +
74                 "            java.util.Map<String, String> parameters) {\n" +
75                 "        return new PrefixingExecutionControl();\n" +
76                 "    }\n" +
77                 "}\n",
78                 "module my.ec {\n" +
79                 "    requires transitive jdk.jshell;\n" +
80                 "    provides jdk.jshell.spi.ExecutionControlProvider\n" +
81                 "        with my.ec.PrefixingExecutionControlProvider;\n" +
82                 " }");
83         Path modPath = compiler.getPath(modDir);
84         ccl = createAndRunFromModule(mod, modPath);
85 
86         setUp(builder -> builder.executionEngine("prefixing"));
87     }
88 
89     @AfterMethod
90     @Override
tearDown()91     public void tearDown() {
92         super.tearDown();
93         Thread.currentThread().setContextClassLoader(ccl);
94     }
95 
96     @Test
testPrefix()97     public void testPrefix() {
98         assertEval("43;", "Blah:43");
99     }
100 
101 }
102