jdk/test/java/nio/channels/etc/NetworkChannelTests.java
changeset 2057 3acf8e5e2ca0
parent 1152 29d6145d1097
child 5506 202f599c92aa
equal deleted inserted replaced
2056:115e09b7a004 2057:3acf8e5e2ca0
     1 /*
     1 /*
     2  * Copyright 2007-2008 Sun Microsystems, Inc.  All Rights Reserved.
     2  * Copyright 2007-2009 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
   101         if (local.getPort() <= 0)
   101         if (local.getPort() <= 0)
   102             throw new RuntimeException("not bound to local port");
   102             throw new RuntimeException("not bound to local port");
   103 
   103 
   104         // closed
   104         // closed
   105         ch.close();
   105         ch.close();
   106         if (ch.getLocalAddress() != null) {
   106         try {
   107             throw new RuntimeException("Local address return when closed");
   107             ch.getLocalAddress();
   108         }
   108             throw new RuntimeException("ClosedChannelException expected");
       
   109         } catch (ClosedChannelException e) { }
   109     }
   110     }
   110 
   111 
   111     /**
   112     /**
   112      * Exercise getConnectedAddress method (SocketChannel only)
   113      * Exercise getRemoteAddress method (SocketChannel only)
   113      */
   114      */
   114     static void connectedAddressTests() throws IOException {
   115     static void connectedAddressTests() throws IOException {
   115         ServerSocketChannel ssc = ServerSocketChannel.open()
   116         ServerSocketChannel ssc = ServerSocketChannel.open()
   116             .bind(new InetSocketAddress(0));
   117             .bind(new InetSocketAddress(0));
   117         InetSocketAddress local = (InetSocketAddress)(ssc.getLocalAddress());
   118         InetSocketAddress local = (InetSocketAddress)(ssc.getLocalAddress());
   119         InetSocketAddress server = new InetSocketAddress(InetAddress.getLocalHost(), port);
   120         InetSocketAddress server = new InetSocketAddress(InetAddress.getLocalHost(), port);
   120 
   121 
   121         SocketChannel sc = SocketChannel.open();
   122         SocketChannel sc = SocketChannel.open();
   122 
   123 
   123         // not connected
   124         // not connected
   124         if (sc.getConnectedAddress() != null)
   125         if (sc.getRemoteAddress() != null)
   125             throw new RuntimeException("getConnectedAddress returned address when not connected");
   126             throw new RuntimeException("getRemoteAddress returned address when not connected");
   126 
   127 
   127         // connected
   128         // connected
   128         sc.connect(server);
   129         sc.connect(server);
   129         SocketAddress remote = sc.getConnectedAddress();
   130         SocketAddress remote = sc.getRemoteAddress();
   130         if (!remote.equals(server))
   131         if (!remote.equals(server))
   131             throw new RuntimeException("getConnectedAddress returned incorrect address");
   132             throw new RuntimeException("getRemoteAddress returned incorrect address");
   132 
   133 
   133         // closed
   134         // closed
   134         sc.close();
   135         sc.close();
   135         if (sc.getConnectedAddress() != null)
   136         try {
   136             throw new RuntimeException("getConnectedAddress returned address when closed");
   137             sc.getRemoteAddress();
       
   138             throw new RuntimeException("ClosedChannelException expected");
       
   139         } catch (ClosedChannelException e) { }
   137 
   140 
   138         ssc.close();
   141         ssc.close();
   139     }
   142     }
   140 
   143 
   141     public static void main(String[] args) throws IOException {
   144     public static void main(String[] args) throws IOException {