test/jdk/sun/management/jmxremote/LocalRMIServerSocketFactoryTest.java
changeset 47216 71c04702a3d5
parent 44423 306c020eb154
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2008, 2014, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /**
       
    25  * @test LocalRMIServerSocketFactoryTest.java
       
    26  * @bug 6774170
       
    27  * @summary Connect to a server socket returned by the LocalRMIServerSocketFactory.
       
    28  *
       
    29  * @author Daniel Fuchs
       
    30  *
       
    31  * @run compile -XDignore.symbol.file=true -g LocalRMIServerSocketFactoryTest.java
       
    32  * @run main LocalRMIServerSocketFactoryTest
       
    33  */
       
    34 
       
    35 import sun.management.jmxremote.LocalRMIServerSocketFactory;
       
    36 import java.io.IOException;
       
    37 import java.net.InetAddress;
       
    38 import java.net.NetworkInterface;
       
    39 import java.net.ServerSocket;
       
    40 import java.net.Socket;
       
    41 import java.util.concurrent.SynchronousQueue;
       
    42 
       
    43 public class LocalRMIServerSocketFactoryTest {
       
    44 
       
    45     private static final SynchronousQueue<Exception> queue =
       
    46             new SynchronousQueue<Exception>();
       
    47     private static volatile boolean isRunning = true;
       
    48 
       
    49     static final class Result extends Exception {
       
    50 
       
    51         private Result() {
       
    52             super("SUCCESS: No exception was thrown");
       
    53         }
       
    54         static final Result SUCCESS = new Result();
       
    55     }
       
    56 
       
    57     private static void checkError(String message) throws Exception {
       
    58 
       
    59         // Wait for the server to set the error field.
       
    60         final Exception x = queue.take();
       
    61 
       
    62         if (x == Result.SUCCESS) {
       
    63             return;
       
    64         }
       
    65 
       
    66 
       
    67         // case of 6674166: this is very unlikely to happen, even if
       
    68         //     both 6674166 and 6774170 aren't fixed. If it happens
       
    69         //     however, it might indicate that neither defects are fixed.
       
    70 
       
    71         if (x instanceof NullPointerException) {
       
    72             throw new Exception(message + " - " +
       
    73                     "Congratulations! it seems you have triggered 6674166. " +
       
    74                     "Neither 6674166 nor 6774170 seem to be fixed: " + x, x);
       
    75         } else if (x instanceof IOException) {
       
    76             throw new Exception(message + " - " +
       
    77                     "Unexpected IOException. Maybe you triggered 6674166? " +
       
    78                     x, x);
       
    79         } else if (x != null) {
       
    80             throw new Exception(message + " - " +
       
    81                     "Ouch, that's bad. " +
       
    82                     "This is a new kind of unexpected exception " +
       
    83                     x, x);
       
    84         }
       
    85     }
       
    86 
       
    87     public static void main(String[] args) throws Exception {
       
    88         final LocalRMIServerSocketFactory f =
       
    89                 new LocalRMIServerSocketFactory();
       
    90         final ServerSocket s = f.createServerSocket(0);
       
    91         final int port = s.getLocalPort();
       
    92         Thread t = new Thread() {
       
    93 
       
    94             public void run() {
       
    95                 while (isRunning) {
       
    96                     Exception error = Result.SUCCESS;
       
    97                     try {
       
    98                         System.err.println("Accepting: ");
       
    99                         final Socket ss = s.accept();
       
   100                         System.err.println(ss.getInetAddress() + " accepted");
       
   101                     } catch (Exception x) {
       
   102                         if (isRunning) {
       
   103                             x.printStackTrace();
       
   104                         }
       
   105                         error = x;
       
   106                     } finally {
       
   107                         try {
       
   108                             if (isRunning) {
       
   109                                 // wait for the client to get the exception.
       
   110                                 queue.put(error);
       
   111                             }
       
   112                         } catch (Exception x) {
       
   113                             // too bad!
       
   114                             System.err.println("Could't send result to client!");
       
   115                             x.printStackTrace();
       
   116                             return;
       
   117                         }
       
   118                     }
       
   119                 }
       
   120             }
       
   121         };
       
   122 
       
   123         try {
       
   124             t.start();
       
   125 
       
   126             System.err.println("new Socket((String)null, port)");
       
   127             final Socket s1 = new Socket((String) null, port);
       
   128             checkError("new Socket((String)null, port)");
       
   129             s1.close();
       
   130             System.err.println("new Socket((String)null, port): PASSED");
       
   131 
       
   132             System.err.println("new Socket(InetAddress.getByName(null), port)");
       
   133             final Socket s2 = new Socket(InetAddress.getByName(null), port);
       
   134             checkError("new Socket(InetAddress.getByName(null), port)");
       
   135             s2.close();
       
   136             System.err.println("new Socket(InetAddress.getByName(null), port): PASSED");
       
   137 
       
   138             System.err.println("new Socket(localhost, port)");
       
   139             final Socket s3 = new Socket("localhost", port);
       
   140             checkError("new Socket(localhost, port)");
       
   141             s3.close();
       
   142             System.err.println("new Socket(localhost, port): PASSED");
       
   143 
       
   144             System.err.println("new Socket(127.0.0.1, port)");
       
   145             final Socket s4 = new Socket("127.0.0.1", port);
       
   146             checkError("new Socket(127.0.0.1, port)");
       
   147             s4.close();
       
   148             System.err.println("new Socket(127.0.0.1, port): PASSED");
       
   149         }
       
   150         finally {
       
   151             isRunning = false;
       
   152             s.close();
       
   153             t.join();
       
   154         }
       
   155     }
       
   156 }