File: FailOverDirectExecutionControlTest.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 (160 lines) | stat: -rw-r--r-- 6,107 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
/*
 * Copyright (c) 2016, 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
 * @bug 8168615
 * @summary Test that fail-over works for fail-over ExecutionControlProvider
 * with direct maps.
 * @modules jdk.compiler/com.sun.tools.javac.api
 *          jdk.compiler/com.sun.tools.javac.main
 *          jdk.jdeps/com.sun.tools.javap
 *          jdk.jshell/jdk.jshell.execution
 *          jdk.jshell/jdk.jshell.spi
 * @library /tools/lib
 * @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask
 * @build KullaTesting ExecutionControlTestBase Compiler
 * @run testng FailOverDirectExecutionControlTest
 */

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import jdk.jshell.execution.FailOverExecutionControlProvider;
import jdk.jshell.spi.ExecutionControlProvider;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;

@Test
public class FailOverDirectExecutionControlTest extends ExecutionControlTestBase {

    ClassLoader ccl;
    ExecutionControlProvider provider;
    LogTestHandler hndlr;
    Map<Level, List<String>> logged;

    private class LogTestHandler extends Handler {

        LogTestHandler() {
            setLevel(Level.ALL);
            setFilter(lr -> lr.getLoggerName().equals("jdk.jshell.execution"));
        }

        @Override
        public void publish(LogRecord lr) {
            List<String> l = logged.get(lr.getLevel());
            if (l == null) {
                l = new ArrayList<>();
                logged.put(lr.getLevel(), l);
            }
            l.add(lr.getMessage());
        }

        @Override
        public void flush() {
        }

        @Override
        public void close() throws SecurityException {
        }

    }

    @BeforeMethod
    @Override
    public void setUp() {
        Logger logger = Logger.getLogger("jdk.jshell.execution");
        logger.setLevel(Level.ALL);
        hndlr = new LogTestHandler();
        logger.addHandler(hndlr);
        logged = new HashMap<>();
        Compiler compiler = new Compiler();
        Path modDir = Paths.get("mod");
        compiler.compile(modDir,
                "package my.provide; import java.util.Map;\n" +
                "import jdk.jshell.spi.ExecutionControl;\n" +
                "import jdk.jshell.spi.ExecutionControlProvider;\n" +
                "import jdk.jshell.spi.ExecutionEnv;\n" +
                "public class AlwaysFailingProvider implements ExecutionControlProvider {\n" +
                "    @Override\n" +
                "    public String name() {\n" +
                "        return \"alwaysFailing\";\n" +
                "    }\n" +
                "    @Override\n" +
                "    public ExecutionControl generate(ExecutionEnv env, Map<String, String> parameters) throws Throwable {\n" +
                "        throw new UnsupportedOperationException(\"This operation intentionally broken.\");\n" +
                "    }\n" +
                "}\n",
                "module my.provide {\n" +
                "    requires transitive jdk.jshell;\n" +
                "    provides jdk.jshell.spi.ExecutionControlProvider\n" +
                "        with my.provide.AlwaysFailingProvider;\n" +
                " }");
        Path modPath = compiler.getPath(modDir);
        ccl = createAndRunFromModule("my.provide", modPath);

        provider = new FailOverExecutionControlProvider();
        Map<String, String> pm = provider.defaultParameters();
        pm.put("0", "alwaysFailing");
        pm.put("1", "alwaysFailing");
        pm.put("2", "jdi");
        setUp(builder -> builder.executionEngine(provider, pm));
    }

    @AfterMethod
    @Override
    public void tearDown() {
        super.tearDown();
        Logger logger = Logger.getLogger("jdk.jshell.execution");
        logger.removeHandler(hndlr);
        Thread.currentThread().setContextClassLoader(ccl);
    }

    @Override
    public void variables() {
        super.variables();
        assertEquals(logged.get(Level.FINEST).size(), 1);
        assertEquals(logged.get(Level.FINE).size(), 2);
        assertEquals(logged.get(Level.WARNING).size(), 2);
        assertNull(logged.get(Level.SEVERE));
        String log = logged.get(Level.WARNING).get(0);
        assertTrue(log.contains("Failure failover -- 0 = alwaysFailing"), log);
        assertTrue(log.contains("This operation intentionally broken"), log);
        log = logged.get(Level.WARNING).get(1);
        assertTrue(log.contains("Failure failover -- 1 = alwaysFailing"), log);
        assertTrue(log.contains("This operation intentionally broken"), log);
        log = logged.get(Level.FINEST).get(0);
        assertTrue(log.contains("Success failover -- 2 = jdi"), log);
    }
}