FailOverDirectExecutionControlTest.java revision 3846:605b0823d19b
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 that fail-over works for fail-over ExecutionControlProvider 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 ExecutionControlTestBase Compiler 37 * @run testng FailOverDirectExecutionControlTest 38 */ 39 40import 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; 58 59@Test 60public class FailOverDirectExecutionControlTest extends ExecutionControlTestBase { 61 62 ClassLoader ccl; 63 ExecutionControlProvider provider; 64 Map<Level, List<String>> logged = new HashMap<>(); 65 66 private class LogTestHandler extends Handler { 67 68 LogTestHandler() { 69 setLevel(Level.ALL); 70 setFilter(lr -> lr.getLoggerName().equals("jdk.jshell.execution")); 71 } 72 73 @Override 74 public void publish(LogRecord lr) { 75 List<String> l = logged.get(lr.getLevel()); 76 if (l == null) { 77 l = new ArrayList<>(); 78 logged.put(lr.getLevel(), l); 79 } 80 l.add(lr.getMessage()); 81 } 82 83 @Override 84 public void flush() { 85 } 86 87 @Override 88 public void close() throws SecurityException { 89 } 90 91 } 92 93 @BeforeMethod 94 @Override 95 public void setUp() { 96 Logger logger = Logger.getLogger("jdk.jshell.execution"); 97 logger.setLevel(Level.ALL); 98 logger.addHandler(new LogTestHandler()); 99 Compiler compiler = new Compiler(); 100 Path modDir = Paths.get("mod"); 101 compiler.compile(modDir, 102 "package my.provide; import java.util.Map;\n" + 103 "import jdk.jshell.spi.ExecutionControl;\n" + 104 "import jdk.jshell.spi.ExecutionControlProvider;\n" + 105 "import jdk.jshell.spi.ExecutionEnv;\n" + 106 "public class AlwaysFailingProvider implements ExecutionControlProvider {\n" + 107 " @Override\n" + 108 " public String name() {\n" + 109 " return \"alwaysFailing\";\n" + 110 " }\n" + 111 " @Override\n" + 112 " public ExecutionControl generate(ExecutionEnv env, Map<String, String> parameters) throws Throwable {\n" + 113 " throw new UnsupportedOperationException(\"This operation intentionally broken.\");\n" + 114 " }\n" + 115 "}\n", 116 "module my.provide {\n" + 117 " requires transitive jdk.jshell;\n" + 118 " provides jdk.jshell.spi.ExecutionControlProvider\n" + 119 " with my.provide.AlwaysFailingProvider;\n" + 120 " }"); 121 Path modPath = compiler.getPath(modDir); 122 ccl = createAndRunFromModule("my.provide", modPath); 123 124 provider = new FailOverExecutionControlProvider(); 125 Map<String, String> pm = provider.defaultParameters(); 126 pm.put("0", "alwaysFailing"); 127 pm.put("1", "alwaysFailing"); 128 pm.put("2", "jdi"); 129 setUp(builder -> builder.executionEngine(provider, pm)); 130 } 131 132 @AfterMethod 133 @Override 134 public void tearDown() { 135 super.tearDown(); 136 Thread.currentThread().setContextClassLoader(ccl); 137 } 138 139 @Override 140 public void variables() { 141 super.variables(); 142 assertEquals(logged.get(Level.FINEST).size(), 1); 143 assertEquals(logged.get(Level.FINE).size(), 2); 144 assertEquals(logged.get(Level.WARNING).size(), 2); 145 assertNull(logged.get(Level.SEVERE)); 146 String log = logged.get(Level.WARNING).get(0); 147 assertTrue(log.contains("Failure failover -- 0 = alwaysFailing"), log); 148 assertTrue(log.contains("This operation intentionally broken"), log); 149 log = logged.get(Level.WARNING).get(1); 150 assertTrue(log.contains("Failure failover -- 1 = alwaysFailing"), log); 151 assertTrue(log.contains("This operation intentionally broken"), log); 152 log = logged.get(Level.FINEST).get(0); 153 assertTrue(log.contains("Success failover -- 2 = jdi"), log); 154 } 155} 156