jdk/test/java/nio/channels/Selector/CloseThenRegister.java
author dfuchs
Wed, 07 Nov 2012 13:24:39 +0100
changeset 14415 7a31b0e0cfaf
parent 7668 d4a77089c587
permissions -rw-r--r--
6720349: (ch) Channels tests depending on hosts inside Sun Summary: This changeset make the nio tests start small TCP or UDP servers from within the tests, instead of relying on external services. Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1449
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5798
diff changeset
     2
 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
1449
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
diff changeset
     4
 *
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
diff changeset
     8
 *
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
diff changeset
    13
 * accompanied this code).
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
diff changeset
    14
 *
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1449
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1449
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1449
diff changeset
    21
 * questions.
1449
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
diff changeset
    22
 */
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
diff changeset
    23
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
diff changeset
    24
/* @test
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
diff changeset
    25
 * @bug 5025260
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
diff changeset
    26
 * @summary ClosedSelectorException is expected when register after close
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
diff changeset
    27
 */
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
diff changeset
    28
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
diff changeset
    29
import java.net.*;
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
diff changeset
    30
import java.nio.channels.*;
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
diff changeset
    31
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
diff changeset
    32
public class CloseThenRegister {
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
diff changeset
    33
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
diff changeset
    34
    public static void main (String [] args) throws Exception {
5798
9cc262cd2a7a 6961630: TEST_BUG: Several SocketChannel and Selector tests can fail with "address already in use"
alanb
parents: 5506
diff changeset
    35
        Selector sel = Selector.open();
9cc262cd2a7a 6961630: TEST_BUG: Several SocketChannel and Selector tests can fail with "address already in use"
alanb
parents: 5506
diff changeset
    36
        sel.close();
9cc262cd2a7a 6961630: TEST_BUG: Several SocketChannel and Selector tests can fail with "address already in use"
alanb
parents: 5506
diff changeset
    37
        ServerSocketChannel ssc = ServerSocketChannel.open();
1449
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
diff changeset
    38
        try {
5798
9cc262cd2a7a 6961630: TEST_BUG: Several SocketChannel and Selector tests can fail with "address already in use"
alanb
parents: 5506
diff changeset
    39
            ssc.bind(new InetSocketAddress(0));
9cc262cd2a7a 6961630: TEST_BUG: Several SocketChannel and Selector tests can fail with "address already in use"
alanb
parents: 5506
diff changeset
    40
            ssc.configureBlocking(false);
9cc262cd2a7a 6961630: TEST_BUG: Several SocketChannel and Selector tests can fail with "address already in use"
alanb
parents: 5506
diff changeset
    41
            ssc.register(sel, SelectionKey.OP_ACCEPT);
9cc262cd2a7a 6961630: TEST_BUG: Several SocketChannel and Selector tests can fail with "address already in use"
alanb
parents: 5506
diff changeset
    42
            throw new RuntimeException("register after close does not cause CSE!");
1449
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
diff changeset
    43
        } catch (ClosedSelectorException cse) {
5798
9cc262cd2a7a 6961630: TEST_BUG: Several SocketChannel and Selector tests can fail with "address already in use"
alanb
parents: 5506
diff changeset
    44
            // expected
9cc262cd2a7a 6961630: TEST_BUG: Several SocketChannel and Selector tests can fail with "address already in use"
alanb
parents: 5506
diff changeset
    45
        } finally {
9cc262cd2a7a 6961630: TEST_BUG: Several SocketChannel and Selector tests can fail with "address already in use"
alanb
parents: 5506
diff changeset
    46
            ssc.close();
1449
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
diff changeset
    47
        }
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
diff changeset
    48
    }
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
diff changeset
    49
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
diff changeset
    50
}