Path: blob/master/test/langtools/jdk/jshell/FailOverDirectExecutionControlTest.java
40931 views
/*1* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 816861526* @summary Test that fail-over works for fail-over ExecutionControlProvider27* with direct maps.28* @modules jdk.compiler/com.sun.tools.javac.api29* jdk.compiler/com.sun.tools.javac.main30* jdk.jdeps/com.sun.tools.javap31* jdk.jshell/jdk.jshell.execution32* jdk.jshell/jdk.jshell.spi33* @library /tools/lib34* @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask35* @build KullaTesting ExecutionControlTestBase Compiler36* @run testng FailOverDirectExecutionControlTest37* @key intermittent38*/3940import java.nio.file.Path;41import java.nio.file.Paths;42import java.util.ArrayList;43import java.util.HashMap;44import java.util.List;45import java.util.Map;46import java.util.logging.Handler;47import java.util.logging.Level;48import java.util.logging.LogRecord;49import java.util.logging.Logger;50import org.testng.annotations.AfterMethod;51import org.testng.annotations.Test;52import org.testng.annotations.BeforeMethod;53import jdk.jshell.execution.FailOverExecutionControlProvider;54import jdk.jshell.spi.ExecutionControlProvider;55import static org.testng.Assert.assertEquals;56import static org.testng.Assert.assertNull;57import static org.testng.Assert.assertTrue;5859@Test60public class FailOverDirectExecutionControlTest extends ExecutionControlTestBase {6162ClassLoader ccl;63ExecutionControlProvider provider;64LogTestHandler hndlr;65Map<Level, List<String>> logged;6667private class LogTestHandler extends Handler {6869LogTestHandler() {70setLevel(Level.ALL);71setFilter(lr -> lr.getLoggerName().equals("jdk.jshell.execution"));72}7374@Override75public void publish(LogRecord lr) {76List<String> l = logged.get(lr.getLevel());77if (l == null) {78l = new ArrayList<>();79logged.put(lr.getLevel(), l);80}81l.add(lr.getMessage());82}8384@Override85public void flush() {86}8788@Override89public void close() throws SecurityException {90}9192}9394@BeforeMethod95@Override96public void setUp() {97Logger logger = Logger.getLogger("jdk.jshell.execution");98logger.setLevel(Level.ALL);99hndlr = new LogTestHandler();100logger.addHandler(hndlr);101logged = new HashMap<>();102Compiler compiler = new Compiler();103Path modDir = Paths.get("mod");104compiler.compile(modDir,105"package my.provide; import java.util.Map;\n" +106"import jdk.jshell.spi.ExecutionControl;\n" +107"import jdk.jshell.spi.ExecutionControlProvider;\n" +108"import jdk.jshell.spi.ExecutionEnv;\n" +109"public class AlwaysFailingProvider implements ExecutionControlProvider {\n" +110" @Override\n" +111" public String name() {\n" +112" return \"alwaysFailing\";\n" +113" }\n" +114" @Override\n" +115" public ExecutionControl generate(ExecutionEnv env, Map<String, String> parameters) throws Throwable {\n" +116" throw new UnsupportedOperationException(\"This operation intentionally broken.\");\n" +117" }\n" +118"}\n",119"module my.provide {\n" +120" requires transitive jdk.jshell;\n" +121" provides jdk.jshell.spi.ExecutionControlProvider\n" +122" with my.provide.AlwaysFailingProvider;\n" +123" }");124Path modPath = compiler.getPath(modDir);125ccl = createAndRunFromModule("my.provide", modPath);126127provider = new FailOverExecutionControlProvider();128Map<String, String> pm = provider.defaultParameters();129pm.put("0", "alwaysFailing");130pm.put("1", "alwaysFailing");131pm.put("2", standardListenSpec());132pm.put("3", standardLaunchSpec());133pm.put("4", standardJdiSpec());134setUp(builder -> builder.executionEngine(provider, pm));135}136137@AfterMethod138@Override139public void tearDown() {140super.tearDown();141Logger logger = Logger.getLogger("jdk.jshell.execution");142logger.removeHandler(hndlr);143Thread.currentThread().setContextClassLoader(ccl);144}145146@Override147public void variables() {148super.variables();149assertEquals(logged.get(Level.FINEST).size(), 1);150assertEquals(logged.get(Level.FINE).size(), 2);151assertEquals(logged.get(Level.WARNING).size(), 2);152assertNull(logged.get(Level.SEVERE));153String log = logged.get(Level.WARNING).get(0);154assertTrue(log.contains("Failure failover -- 0 = alwaysFailing"), log);155assertTrue(log.contains("This operation intentionally broken"), log);156log = logged.get(Level.WARNING).get(1);157assertTrue(log.contains("Failure failover -- 1 = alwaysFailing"), log);158assertTrue(log.contains("This operation intentionally broken"), log);159log = logged.get(Level.FINEST).get(0);160assertTrue(161log.contains("Success failover -- 2 = " + standardListenSpec())162|| log.contains("Success failover -- 3 = " + standardLaunchSpec())163|| log.contains("Success failover -- 4 = " + standardJdiSpec()),164log);165}166}167168169