1 /* |
1 /* |
2 * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
24 /* @test |
24 /* @test |
25 * @bug 4776783 4778091 4778099 |
25 * @bug 4776783 4778091 4778099 |
26 * @summary Check various properties of key and selected-key sets |
26 * @summary Check various properties of key and selected-key sets |
27 * |
27 * |
28 * @run main KeySets |
28 * @run main KeySets |
29 * @run main/othervm -Dsun.nio.ch.bugLevel=1.4 KeySets |
|
30 */ |
29 */ |
31 |
30 |
32 import java.io.*; |
31 import java.io.*; |
33 import java.nio.channels.*; |
32 import java.nio.channels.*; |
34 import java.util.*; |
33 import java.util.*; |
35 |
34 |
36 |
|
37 public class KeySets { |
35 public class KeySets { |
38 |
|
39 static boolean compat; |
|
40 |
36 |
41 static abstract class Catch { |
37 static abstract class Catch { |
42 abstract void go() throws Exception; |
38 abstract void go() throws Exception; |
43 Catch(Class xc) throws Exception { |
39 Catch(Class xc) throws Exception { |
44 try { |
40 try { |
45 go(); |
41 go(); |
46 } catch (Exception x) { |
42 } catch (Exception x) { |
47 if (compat) |
|
48 throw new Exception("Exception thrown", x); |
|
49 if (xc.isInstance(x)) |
43 if (xc.isInstance(x)) |
50 return; |
44 return; |
51 throw new Exception("Wrong exception", x); |
45 throw new Exception("Wrong exception", x); |
52 } |
46 } |
53 if (compat) |
47 throw new Exception("Not thrown as expected: " + xc.getName()); |
54 return; |
|
55 throw new Exception("Not thrown as expected: " |
|
56 + xc.getName()); |
|
57 } |
48 } |
58 } |
49 } |
59 |
50 |
60 // 4776783: Closing a selector should make key sets inaccessible |
51 // 4776783: Closing a selector should make key sets inaccessible |
61 static void testClose() throws Exception { |
52 static void testClose() throws Exception { |
72 |
63 |
73 new Catch(ClosedSelectorException.class) { |
64 new Catch(ClosedSelectorException.class) { |
74 void go() throws Exception { |
65 void go() throws Exception { |
75 sel.selectedKeys(); |
66 sel.selectedKeys(); |
76 }}; |
67 }}; |
77 |
|
78 } |
68 } |
79 |
69 |
80 static void testNoAddition(final Set s) throws Exception { |
70 static void testNoAddition(final Set s) throws Exception { |
81 new Catch(UnsupportedOperationException.class) { |
71 new Catch(UnsupportedOperationException.class) { |
82 void go() throws Exception { |
72 void go() throws Exception { |
172 hs.add(reg(sel)); |
162 hs.add(reg(sel)); |
173 sel.select(); |
163 sel.select(); |
174 sel.selectedKeys().clear(); |
164 sel.selectedKeys().clear(); |
175 if (!sel.selectedKeys().isEmpty()) |
165 if (!sel.selectedKeys().isEmpty()) |
176 throw new Exception("clear failed"); |
166 throw new Exception("clear failed"); |
177 |
|
178 } |
167 } |
179 |
168 |
180 public static void main(String[] args) throws Exception { |
169 public static void main(String[] args) throws Exception { |
181 String bl = System.getProperty("sun.nio.ch.bugLevel"); |
|
182 compat = (bl != null) && bl.equals("1.4"); |
|
183 testClose(); |
170 testClose(); |
184 testMutability(); |
171 testMutability(); |
185 } |
172 } |
186 |
|
187 } |
173 } |