Path: blob/master/test/jdk/java/nio/channels/Selector/KeySets.java
41153 views
/*1* Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 4776783 4778091 477809925* @summary Check various properties of key and selected-key sets26*27* @run main KeySets28*/2930import java.io.*;31import java.nio.channels.*;32import java.util.*;3334public class KeySets {3536static abstract class Catch {37abstract void go() throws Exception;38Catch(Class xc) throws Exception {39try {40go();41} catch (Exception x) {42if (xc.isInstance(x))43return;44throw new Exception("Wrong exception", x);45}46throw new Exception("Not thrown as expected: " + xc.getName());47}48}4950// 4776783: Closing a selector should make key sets inaccessible51static void testClose() throws Exception {5253final Selector sel = Selector.open();54sel.keys();55sel.selectedKeys();56sel.close();5758new Catch(ClosedSelectorException.class) {59void go() throws Exception {60sel.keys();61}};6263new Catch(ClosedSelectorException.class) {64void go() throws Exception {65sel.selectedKeys();66}};67}6869static void testNoAddition(final Set s) throws Exception {70new Catch(UnsupportedOperationException.class) {71void go() throws Exception {72s.add(new Object());73}};74new Catch(UnsupportedOperationException.class) {75void go() throws Exception {76ArrayList al = new ArrayList();77al.add(new Object());78s.addAll(al);79}};80}8182static interface Adder {83void add() throws IOException;84}8586static void testNoRemoval(final Set s, final Adder adder)87throws Exception88{89new Catch(UnsupportedOperationException.class) {90void go() throws Exception {91adder.add();92s.clear();93}};94new Catch(UnsupportedOperationException.class) {95void go() throws Exception {96adder.add();97Iterator i = s.iterator();98i.next();99i.remove();100}};101new Catch(UnsupportedOperationException.class) {102void go() throws Exception {103adder.add();104s.remove(s.iterator().next());105}};106new Catch(UnsupportedOperationException.class) {107void go() throws Exception {108adder.add();109HashSet hs = new HashSet();110hs.addAll(s);111s.removeAll(hs);112}};113new Catch(UnsupportedOperationException.class) {114void go() throws Exception {115adder.add();116s.retainAll(Collections.EMPTY_SET);117}};118}119120static SelectionKey reg(Selector sel) throws IOException {121DatagramChannel dc = DatagramChannel.open();122dc.configureBlocking(false);123return dc.register(sel, SelectionKey.OP_WRITE);124}125126static void testMutability() throws Exception {127128final Selector sel = Selector.open();129130// 4778091: Selector.keys() should be immutable131132testNoRemoval(sel.keys(), new Adder() {133public void add() throws IOException {134reg(sel);135}136});137testNoAddition(sel.keys());138139// 4778099: Selector.selectedKeys() should allow removal but not addition140141sel.select();142testNoAddition(sel.selectedKeys());143SelectionKey sk = reg(sel);144sel.select();145int n = sel.selectedKeys().size();146sel.selectedKeys().remove(sk);147if (sel.selectedKeys().size() != n - 1)148throw new Exception("remove failed");149150HashSet hs = new HashSet();151hs.add(reg(sel));152sel.select();153sel.selectedKeys().retainAll(hs);154if (sel.selectedKeys().isEmpty())155throw new Exception("retainAll failed");156sel.selectedKeys().removeAll(hs);157if (!sel.selectedKeys().isEmpty())158throw new Exception("removeAll failed");159160hs.clear();161hs.add(reg(sel));162sel.select();163sel.selectedKeys().clear();164if (!sel.selectedKeys().isEmpty())165throw new Exception("clear failed");166}167168public static void main(String[] args) throws Exception {169testClose();170testMutability();171}172}173174175