1/*
2 * Copyright (c) 2015, 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 * @summary Shutdown tests
27 * @build KullaTesting TestingInputStream
28 * @run testng ShutdownTest
29 */
30
31import java.util.function.Consumer;
32
33import jdk.jshell.JShell;
34import jdk.jshell.JShell.Subscription;
35import org.testng.annotations.Test;
36
37import static org.testng.Assert.assertEquals;
38
39@Test
40public class ShutdownTest extends KullaTesting {
41
42    int shutdownCount;
43
44    void shutdownCounter(JShell state) {
45        ++shutdownCount;
46    }
47
48    @Test(enabled = false) //TODO 8139873
49    public void testExit() {
50        shutdownCount = 0;
51        getState().onShutdown(this::shutdownCounter);
52        assertEval("System.exit(1);");
53        assertEquals(shutdownCount, 1);
54    }
55
56    public void testCloseCallback() {
57        shutdownCount = 0;
58        getState().onShutdown(this::shutdownCounter);
59        getState().close();
60        assertEquals(shutdownCount, 1);
61    }
62
63    public void testCloseUnsubscribe() {
64        shutdownCount = 0;
65        Subscription token = getState().onShutdown(this::shutdownCounter);
66        getState().unsubscribe(token);
67        getState().close();
68        assertEquals(shutdownCount, 0);
69    }
70
71    public void testTwoShutdownListeners() {
72        ShutdownListener listener1 = new ShutdownListener();
73        ShutdownListener listener2 = new ShutdownListener();
74        Subscription subscription1 = getState().onShutdown(listener1);
75        Subscription subscription2 = getState().onShutdown(listener2);
76        getState().unsubscribe(subscription1);
77        getState().close();
78
79        assertEquals(listener1.getEvents(), 0, "Checking got events");
80        assertEquals(listener2.getEvents(), 1, "Checking got events");
81
82        getState().close();
83
84        assertEquals(listener1.getEvents(), 0, "Checking got events");
85        assertEquals(listener2.getEvents(), 1, "Checking got events");
86
87        getState().unsubscribe(subscription2);
88    }
89
90    @Test(expectedExceptions = IllegalStateException.class)
91    public void testCloseException() {
92        getState().close();
93        getState().eval("45");
94    }
95
96    @Test(expectedExceptions = IllegalStateException.class,
97          enabled = false) //TODO 8139873
98    public void testShutdownException() {
99        assertEval("System.exit(0);");
100        getState().eval("45");
101    }
102
103    @Test(expectedExceptions = NullPointerException.class)
104    public void testNullCallback() {
105        getState().onShutdown(null);
106    }
107
108    @Test(expectedExceptions = IllegalStateException.class)
109    public void testSubscriptionAfterClose() {
110        getState().close();
111        getState().onShutdown(e -> {});
112    }
113
114    @Test(expectedExceptions = IllegalStateException.class,
115          enabled = false) //TODO 8139873
116    public void testSubscriptionAfterShutdown() {
117        assertEval("System.exit(0);");
118        getState().onShutdown(e -> {});
119    }
120
121    private static class ShutdownListener implements Consumer<JShell> {
122        private int count;
123
124        @Override
125        public void accept(JShell shell) {
126            ++count;
127        }
128
129        public int getEvents() {
130            return count;
131        }
132    }
133}
134