|
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 Subscribe tests |
|
27 * @build KullaTesting TestingInputStream |
|
28 * @run testng SnippetStatusListenerTest |
|
29 */ |
|
30 |
|
31 import java.util.ArrayList; |
|
32 import java.util.Collections; |
|
33 import java.util.List; |
|
34 import java.util.function.Consumer; |
|
35 |
|
36 import jdk.jshell.DeclarationSnippet; |
|
37 import jdk.jshell.JShell.Subscription; |
|
38 import jdk.jshell.SnippetEvent; |
|
39 import jdk.jshell.TypeDeclSnippet; |
|
40 import org.testng.annotations.Test; |
|
41 |
|
42 import static jdk.jshell.Snippet.Status.*; |
|
43 import static org.testng.Assert.assertEquals; |
|
44 |
|
45 @Test |
|
46 public class SnippetStatusListenerTest extends KullaTesting { |
|
47 |
|
48 public void testTwoSnippetEventListeners() { |
|
49 SnippetListener listener1 = new SnippetListener(); |
|
50 SnippetListener listener2 = new SnippetListener(); |
|
51 |
|
52 Subscription subscription1 = getState().onSnippetEvent(listener1); |
|
53 getState().onSnippetEvent(listener2); |
|
54 |
|
55 TypeDeclSnippet a = classKey(assertEval("class A {}")); |
|
56 assertEval("interface A {}", |
|
57 ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
58 ste(a, VALID, OVERWRITTEN, false, null)); |
|
59 DeclarationSnippet f = (DeclarationSnippet) assertDeclareFail("double f() { }", "compiler.err.missing.ret.stmt"); |
|
60 assertEvalException("throw new RuntimeException();"); |
|
61 assertEval("int a = 0;"); |
|
62 |
|
63 List<SnippetEvent> events1 = Collections.unmodifiableList(listener1.getEvents()); |
|
64 assertEquals(events1, listener2.getEvents(), "Checking got events"); |
|
65 getState().unsubscribe(subscription1); |
|
66 |
|
67 assertDrop(f, DiagCheck.DIAG_IGNORE, DiagCheck.DIAG_IGNORE, ste(f, REJECTED, DROPPED, false, null)); |
|
68 assertEval("void f() { }", added(VALID)); |
|
69 assertEvalException("throw new RuntimeException();"); |
|
70 assertEquals(listener1.getEvents(), events1, "Checking that unsubscribed listener does not get events"); |
|
71 |
|
72 List<SnippetEvent> events2 = new ArrayList<>(listener2.getEvents()); |
|
73 events2.removeAll(events1); |
|
74 |
|
75 assertEquals(events2.size(), 3, "The second listener got events"); |
|
76 } |
|
77 |
|
78 @Test(expectedExceptions = NullPointerException.class) |
|
79 public void testNullCallback() { |
|
80 getState().onSnippetEvent(null); |
|
81 } |
|
82 |
|
83 @Test(expectedExceptions = IllegalStateException.class) |
|
84 public void testSubscriptionAfterClose() { |
|
85 getState().close(); |
|
86 getState().onSnippetEvent(e -> {}); |
|
87 } |
|
88 |
|
89 @Test(expectedExceptions = IllegalStateException.class, |
|
90 enabled = false) //TODO 8139873 |
|
91 public void testSubscriptionAfterShutdown() { |
|
92 assertEval("System.exit(0);"); |
|
93 getState().onSnippetEvent(e -> {}); |
|
94 } |
|
95 |
|
96 public void testSubscriptionToAnotherState() { |
|
97 SnippetListener listener = new SnippetListener(); |
|
98 Subscription subscription = getState().onSnippetEvent(listener); |
|
99 tearDown(); |
|
100 setUp(); |
|
101 assertEval("int x;"); |
|
102 assertEquals(Collections.emptyList(), listener.getEvents(), "No events"); |
|
103 getState().unsubscribe(subscription); |
|
104 } |
|
105 |
|
106 private static class SnippetListener implements Consumer<SnippetEvent> { |
|
107 private final List<SnippetEvent> events = new ArrayList<>(); |
|
108 |
|
109 @Override |
|
110 public void accept(SnippetEvent event) { |
|
111 events.add(event); |
|
112 } |
|
113 |
|
114 public List<SnippetEvent> getEvents() { |
|
115 return events; |
|
116 } |
|
117 } |
|
118 } |