1 /* 2 * Copyright (c) 2003, 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 /* @test 25 * @bug 4776783 4778091 4778099 26 * @summary Check various properties of key and selected-key sets 27 * 28 * @run main KeySets 29 * @run main/othervm -Dsun.nio.ch.bugLevel=1.4 KeySets 30 */ 31 32 import java.io.*; 33 import java.nio.channels.*; 34 import java.util.*; 35 36 37 public class KeySets { 38 39 static boolean compat; 40 41 static abstract class Catch { go()42 abstract void go() throws Exception; Catch(Class xc)43 Catch(Class xc) throws Exception { 44 try { 45 go(); 46 } catch (Exception x) { 47 if (compat) 48 throw new Exception("Exception thrown", x); 49 if (xc.isInstance(x)) 50 return; 51 throw new Exception("Wrong exception", x); 52 } 53 if (compat) 54 return; 55 throw new Exception("Not thrown as expected: " 56 + xc.getName()); 57 } 58 } 59 60 // 4776783: Closing a selector should make key sets inaccessible testClose()61 static void testClose() throws Exception { 62 63 final Selector sel = Selector.open(); 64 sel.keys(); 65 sel.selectedKeys(); 66 sel.close(); 67 68 new Catch(ClosedSelectorException.class) { 69 void go() throws Exception { 70 sel.keys(); 71 }}; 72 73 new Catch(ClosedSelectorException.class) { 74 void go() throws Exception { 75 sel.selectedKeys(); 76 }}; 77 78 } 79 testNoAddition(final Set s)80 static void testNoAddition(final Set s) throws Exception { 81 new Catch(UnsupportedOperationException.class) { 82 void go() throws Exception { 83 s.add(new Object()); 84 }}; 85 new Catch(UnsupportedOperationException.class) { 86 void go() throws Exception { 87 ArrayList al = new ArrayList(); 88 al.add(new Object()); 89 s.addAll(al); 90 }}; 91 } 92 93 static interface Adder { add()94 void add() throws IOException; 95 } 96 testNoRemoval(final Set s, final Adder adder)97 static void testNoRemoval(final Set s, final Adder adder) 98 throws Exception 99 { 100 new Catch(UnsupportedOperationException.class) { 101 void go() throws Exception { 102 adder.add(); 103 s.clear(); 104 }}; 105 new Catch(UnsupportedOperationException.class) { 106 void go() throws Exception { 107 adder.add(); 108 Iterator i = s.iterator(); 109 i.next(); 110 i.remove(); 111 }}; 112 new Catch(UnsupportedOperationException.class) { 113 void go() throws Exception { 114 adder.add(); 115 s.remove(s.iterator().next()); 116 }}; 117 new Catch(UnsupportedOperationException.class) { 118 void go() throws Exception { 119 adder.add(); 120 HashSet hs = new HashSet(); 121 hs.addAll(s); 122 s.removeAll(hs); 123 }}; 124 new Catch(UnsupportedOperationException.class) { 125 void go() throws Exception { 126 adder.add(); 127 s.retainAll(Collections.EMPTY_SET); 128 }}; 129 } 130 reg(Selector sel)131 static SelectionKey reg(Selector sel) throws IOException { 132 DatagramChannel dc = DatagramChannel.open(); 133 dc.configureBlocking(false); 134 return dc.register(sel, SelectionKey.OP_WRITE); 135 } 136 testMutability()137 static void testMutability() throws Exception { 138 139 final Selector sel = Selector.open(); 140 141 // 4778091: Selector.keys() should be immutable 142 143 testNoRemoval(sel.keys(), new Adder() { 144 public void add() throws IOException { 145 reg(sel); 146 } 147 }); 148 testNoAddition(sel.keys()); 149 150 // 4778099: Selector.selectedKeys() should allow removal but not addition 151 152 sel.select(); 153 testNoAddition(sel.selectedKeys()); 154 SelectionKey sk = reg(sel); 155 sel.select(); 156 int n = sel.selectedKeys().size(); 157 sel.selectedKeys().remove(sk); 158 if (sel.selectedKeys().size() != n - 1) 159 throw new Exception("remove failed"); 160 161 HashSet hs = new HashSet(); 162 hs.add(reg(sel)); 163 sel.select(); 164 sel.selectedKeys().retainAll(hs); 165 if (sel.selectedKeys().isEmpty()) 166 throw new Exception("retainAll failed"); 167 sel.selectedKeys().removeAll(hs); 168 if (!sel.selectedKeys().isEmpty()) 169 throw new Exception("removeAll failed"); 170 171 hs.clear(); 172 hs.add(reg(sel)); 173 sel.select(); 174 sel.selectedKeys().clear(); 175 if (!sel.selectedKeys().isEmpty()) 176 throw new Exception("clear failed"); 177 178 } 179 main(String[] args)180 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(); 184 testMutability(); 185 } 186 187 } 188