jdk/test/java/nio/channels/SocketChannel/SocketOptionTests.java
changeset 1152 29d6145d1097
child 2057 3acf8e5e2ca0
equal deleted inserted replaced
1151:4070cecdb99d 1152:29d6145d1097
       
     1 /*
       
     2  * Copyright 2007-2008 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /* @test
       
    25  * @bug 4640544
       
    26  * @summary Unit test to check SocketChannel setOption/getOption/options
       
    27  *          methods.
       
    28  */
       
    29 
       
    30 import java.nio.*;
       
    31 import java.nio.channels.*;
       
    32 import java.net.*;
       
    33 import java.io.IOException;
       
    34 import java.util.*;
       
    35 import static java.net.StandardSocketOption.*;
       
    36 
       
    37 public class SocketOptionTests {
       
    38 
       
    39     static void checkOption(SocketChannel sc, SocketOption name, Object expectedValue)
       
    40         throws IOException
       
    41     {
       
    42         Object value = sc.getOption(name);
       
    43         if (!value.equals(expectedValue))
       
    44             throw new RuntimeException("value not as expected");
       
    45     }
       
    46 
       
    47     public static void main(String[] args) throws IOException {
       
    48         SocketChannel sc = SocketChannel.open();
       
    49 
       
    50         // check supported options
       
    51         Set<SocketOption<?>> options = sc.options();
       
    52         List<? extends SocketOption> expected = Arrays.asList(SO_SNDBUF, SO_RCVBUF,
       
    53             SO_KEEPALIVE, SO_REUSEADDR, SO_LINGER, TCP_NODELAY);
       
    54         for (SocketOption opt: expected) {
       
    55             if (!options.contains(opt))
       
    56                 throw new RuntimeException(opt.name() + " should be supported");
       
    57         }
       
    58 
       
    59         // check specified defaults
       
    60         int linger = sc.<Integer>getOption(SO_LINGER);
       
    61         if (linger >= 0)
       
    62             throw new RuntimeException("initial value of SO_LINGER should be < 0");
       
    63         checkOption(sc, SO_KEEPALIVE, false);
       
    64         checkOption(sc, TCP_NODELAY, false);
       
    65 
       
    66         // allowed to change when not bound
       
    67         sc.setOption(SO_KEEPALIVE, true);
       
    68         checkOption(sc, SO_KEEPALIVE, true);
       
    69         sc.setOption(SO_KEEPALIVE, false);
       
    70         checkOption(sc, SO_KEEPALIVE, false);
       
    71         sc.setOption(SO_SNDBUF, 128*1024);      // can't check
       
    72         sc.setOption(SO_RCVBUF, 256*1024);      // can't check
       
    73         sc.setOption(SO_REUSEADDR, true);
       
    74         checkOption(sc, SO_REUSEADDR, true);
       
    75         sc.setOption(SO_REUSEADDR, false);
       
    76         checkOption(sc, SO_REUSEADDR, false);
       
    77         sc.setOption(SO_LINGER, 10);
       
    78         linger = sc.<Integer>getOption(SO_LINGER);
       
    79         if (linger < 1)
       
    80             throw new RuntimeException("expected linger to be enabled");
       
    81         sc.setOption(SO_LINGER, -1);
       
    82         linger = sc.<Integer>getOption(SO_LINGER);
       
    83         if (linger >= 0)
       
    84             throw new RuntimeException("expected linger to be disabled");
       
    85         sc.setOption(TCP_NODELAY, true);
       
    86         checkOption(sc, TCP_NODELAY, true);
       
    87         sc.setOption(TCP_NODELAY, false);       // can't check
       
    88 
       
    89         // bind socket
       
    90         sc.bind(new InetSocketAddress(0));
       
    91 
       
    92         // allow to change when bound
       
    93         sc.setOption(SO_KEEPALIVE, true);
       
    94         checkOption(sc, SO_KEEPALIVE, true);
       
    95         sc.setOption(SO_KEEPALIVE, false);
       
    96         checkOption(sc, SO_KEEPALIVE, false);
       
    97 
       
    98         sc.setOption(SO_LINGER, 10);
       
    99         linger = sc.<Integer>getOption(SO_LINGER);
       
   100         if (linger < 1)
       
   101             throw new RuntimeException("expected linger to be enabled");
       
   102         sc.setOption(SO_LINGER, -1);
       
   103         linger = sc.<Integer>getOption(SO_LINGER);
       
   104         if (linger >= 0)
       
   105             throw new RuntimeException("expected linger to be disabled");
       
   106         sc.setOption(TCP_NODELAY, true);        // can't check
       
   107         sc.setOption(TCP_NODELAY, false);       // can't check
       
   108 
       
   109         // NullPointerException
       
   110         try {
       
   111             sc.setOption(null, "value");
       
   112             throw new RuntimeException("NullPointerException not thrown");
       
   113         } catch (NullPointerException x) {
       
   114         }
       
   115         try {
       
   116             sc.getOption(null);
       
   117             throw new RuntimeException("NullPointerException not thrown");
       
   118         } catch (NullPointerException x) {
       
   119         }
       
   120 
       
   121         // ClosedChannelException
       
   122         sc.close();
       
   123         try {
       
   124             sc.setOption(TCP_NODELAY, true);
       
   125             throw new RuntimeException("ClosedChannelException not thrown");
       
   126         } catch (ClosedChannelException x) {
       
   127         }
       
   128     }
       
   129 }