Path: blob/master/test/jdk/sun/management/jmxremote/LocalRMIServerSocketFactoryTest.java
41149 views
/*1* Copyright (c) 2008, 2014, 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/**24* @test LocalRMIServerSocketFactoryTest.java25* @bug 677417026* @summary Connect to a server socket returned by the LocalRMIServerSocketFactory.27*28* @author Daniel Fuchs29*30* @run compile -XDignore.symbol.file=true -g LocalRMIServerSocketFactoryTest.java31* @run main LocalRMIServerSocketFactoryTest32*/3334import sun.management.jmxremote.LocalRMIServerSocketFactory;35import java.io.IOException;36import java.net.InetAddress;37import java.net.NetworkInterface;38import java.net.ServerSocket;39import java.net.Socket;40import java.util.concurrent.SynchronousQueue;4142public class LocalRMIServerSocketFactoryTest {4344private static final SynchronousQueue<Exception> queue =45new SynchronousQueue<Exception>();46private static volatile boolean isRunning = true;4748static final class Result extends Exception {4950private Result() {51super("SUCCESS: No exception was thrown");52}53static final Result SUCCESS = new Result();54}5556private static void checkError(String message) throws Exception {5758// Wait for the server to set the error field.59final Exception x = queue.take();6061if (x == Result.SUCCESS) {62return;63}646566// case of 6674166: this is very unlikely to happen, even if67// both 6674166 and 6774170 aren't fixed. If it happens68// however, it might indicate that neither defects are fixed.6970if (x instanceof NullPointerException) {71throw new Exception(message + " - " +72"Congratulations! it seems you have triggered 6674166. " +73"Neither 6674166 nor 6774170 seem to be fixed: " + x, x);74} else if (x instanceof IOException) {75throw new Exception(message + " - " +76"Unexpected IOException. Maybe you triggered 6674166? " +77x, x);78} else if (x != null) {79throw new Exception(message + " - " +80"Ouch, that's bad. " +81"This is a new kind of unexpected exception " +82x, x);83}84}8586public static void main(String[] args) throws Exception {87final LocalRMIServerSocketFactory f =88new LocalRMIServerSocketFactory();89final ServerSocket s = f.createServerSocket(0);90final int port = s.getLocalPort();91Thread t = new Thread() {9293public void run() {94while (isRunning) {95Exception error = Result.SUCCESS;96try {97System.err.println("Accepting: ");98final Socket ss = s.accept();99System.err.println(ss.getInetAddress() + " accepted");100} catch (Exception x) {101if (isRunning) {102x.printStackTrace();103}104error = x;105} finally {106try {107if (isRunning) {108// wait for the client to get the exception.109queue.put(error);110}111} catch (Exception x) {112// too bad!113System.err.println("Could't send result to client!");114x.printStackTrace();115return;116}117}118}119}120};121122try {123t.start();124125System.err.println("new Socket((String)null, port)");126final Socket s1 = new Socket((String) null, port);127checkError("new Socket((String)null, port)");128s1.close();129System.err.println("new Socket((String)null, port): PASSED");130131System.err.println("new Socket(InetAddress.getByName(null), port)");132final Socket s2 = new Socket(InetAddress.getByName(null), port);133checkError("new Socket(InetAddress.getByName(null), port)");134s2.close();135System.err.println("new Socket(InetAddress.getByName(null), port): PASSED");136137System.err.println("new Socket(localhost, port)");138final Socket s3 = new Socket("localhost", port);139checkError("new Socket(localhost, port)");140s3.close();141System.err.println("new Socket(localhost, port): PASSED");142143System.err.println("new Socket(127.0.0.1, port)");144final Socket s4 = new Socket("127.0.0.1", port);145checkError("new Socket(127.0.0.1, port)");146s4.close();147System.err.println("new Socket(127.0.0.1, port): PASSED");148}149finally {150isRunning = false;151s.close();152t.join();153}154}155}156157158