Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/Selector/KeySets.java
38828 views
/*1* Copyright (c) 2003, 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* @run main/othervm -Dsun.nio.ch.bugLevel=1.4 KeySets29*/3031import java.io.*;32import java.nio.channels.*;33import java.util.*;343536public class KeySets {3738static boolean compat;3940static abstract class Catch {41abstract void go() throws Exception;42Catch(Class xc) throws Exception {43try {44go();45} catch (Exception x) {46if (compat)47throw new Exception("Exception thrown", x);48if (xc.isInstance(x))49return;50throw new Exception("Wrong exception", x);51}52if (compat)53return;54throw new Exception("Not thrown as expected: "55+ xc.getName());56}57}5859// 4776783: Closing a selector should make key sets inaccessible60static void testClose() throws Exception {6162final Selector sel = Selector.open();63sel.keys();64sel.selectedKeys();65sel.close();6667new Catch(ClosedSelectorException.class) {68void go() throws Exception {69sel.keys();70}};7172new Catch(ClosedSelectorException.class) {73void go() throws Exception {74sel.selectedKeys();75}};7677}7879static void testNoAddition(final Set s) throws Exception {80new Catch(UnsupportedOperationException.class) {81void go() throws Exception {82s.add(new Object());83}};84new Catch(UnsupportedOperationException.class) {85void go() throws Exception {86ArrayList al = new ArrayList();87al.add(new Object());88s.addAll(al);89}};90}9192static interface Adder {93void add() throws IOException;94}9596static void testNoRemoval(final Set s, final Adder adder)97throws Exception98{99new Catch(UnsupportedOperationException.class) {100void go() throws Exception {101adder.add();102s.clear();103}};104new Catch(UnsupportedOperationException.class) {105void go() throws Exception {106adder.add();107Iterator i = s.iterator();108i.next();109i.remove();110}};111new Catch(UnsupportedOperationException.class) {112void go() throws Exception {113adder.add();114s.remove(s.iterator().next());115}};116new Catch(UnsupportedOperationException.class) {117void go() throws Exception {118adder.add();119HashSet hs = new HashSet();120hs.addAll(s);121s.removeAll(hs);122}};123new Catch(UnsupportedOperationException.class) {124void go() throws Exception {125adder.add();126s.retainAll(Collections.EMPTY_SET);127}};128}129130static SelectionKey reg(Selector sel) throws IOException {131DatagramChannel dc = DatagramChannel.open();132dc.configureBlocking(false);133return dc.register(sel, SelectionKey.OP_WRITE);134}135136static void testMutability() throws Exception {137138final Selector sel = Selector.open();139140// 4778091: Selector.keys() should be immutable141142testNoRemoval(sel.keys(), new Adder() {143public void add() throws IOException {144reg(sel);145}146});147testNoAddition(sel.keys());148149// 4778099: Selector.selectedKeys() should allow removal but not addition150151sel.select();152testNoAddition(sel.selectedKeys());153SelectionKey sk = reg(sel);154sel.select();155int n = sel.selectedKeys().size();156sel.selectedKeys().remove(sk);157if (sel.selectedKeys().size() != n - 1)158throw new Exception("remove failed");159160HashSet hs = new HashSet();161hs.add(reg(sel));162sel.select();163sel.selectedKeys().retainAll(hs);164if (sel.selectedKeys().isEmpty())165throw new Exception("retainAll failed");166sel.selectedKeys().removeAll(hs);167if (!sel.selectedKeys().isEmpty())168throw new Exception("removeAll failed");169170hs.clear();171hs.add(reg(sel));172sel.select();173sel.selectedKeys().clear();174if (!sel.selectedKeys().isEmpty())175throw new Exception("clear failed");176177}178179public static void main(String[] args) throws Exception {180String bl = System.getProperty("sun.nio.ch.bugLevel");181compat = (bl != null) && bl.equals("1.4");182testClose();183testMutability();184}185186}187188189