1 /* 2 * Copyright (c) 2014, 2016 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package test.shutdowntest; 27 28 import java.io.InputStream; 29 import java.net.ServerSocket; 30 import java.net.Socket; 31 import java.util.ArrayList; 32 import org.junit.Test; 33 34 import static org.junit.Assert.*; 35 import static test.shutdowntest.Constants.*; 36 37 /** 38 * Unit test for calling Platform runLater from a ShutdownHook. 39 */ 40 public class ShutdownHookTest { 41 42 private static final String className = ShutdownHookTest.class.getName(); 43 private static final String pkgName = className.substring(0, className.lastIndexOf(".")); 44 45 private final String testAppName = pkgName + "." + "ShutdownHookApp"; 46 47 @Test (timeout=5000) 48 public void testShutdownHook() throws Exception { 49 // Initilaize the socket 50 final ServerSocket service = new ServerSocket(0); 51 final int port = service.getLocalPort(); 52 53 // Launch the test app 54 final ArrayList<String> cmd 55 = test.util.Util.createApplicationLaunchCommand( 56 testAppName, 57 null, 58 null 59 ); 60 // and add our argument 61 cmd.add(String.valueOf(port)); 62 ProcessBuilder builder; 63 builder = new ProcessBuilder(cmd); 64 builder.redirectError(ProcessBuilder.Redirect.INHERIT); 65 builder.redirectOutput(ProcessBuilder.Redirect.INHERIT); 66 Process process = builder.start(); 67 68 // Accept a connection from the test app 69 final Socket socket = service.accept(); 70 final InputStream in = socket.getInputStream(); 71 72 // Read the "handshake" token 73 int handshake = in.read(); 74 assertEquals("Socket handshake failed,", SOCKET_HANDSHAKE, handshake); 75 76 // Read the status code from the shutdown hook 77 int status = in.read(); 78 switch (status) { 79 case STATUS_OK: 80 break; 81 case STATUS_ILLEGAL_STATE: 82 fail(testAppName 83 + ": IllegalStateException from Platform.runLater"); 84 break; 85 case STATUS_RUNNABLE_EXECUTED: 86 fail(testAppName 87 + ": Unexpected execution of Platform.runLater Runnable from ShutdownHook"); 88 break; 89 case STATUS_UNEXPECTED_EXCEPTION: 90 fail(testAppName + ": Unexpected exception"); 91 break; 92 default: 93 fail(testAppName + ": Unexpected status: " + status); 94 } 95 96 // Make sure that the process exited as expected 97 int retVal = process.waitFor(); 98 switch (retVal) { 99 case ERROR_NONE: 100 break; 101 102 case ERROR_SOCKET: 103 fail(testAppName + ": Error connecting to socket"); 104 break; 105 106 case 0: 107 fail(testAppName + ": Unexpected exit 0"); 108 break; 109 110 case 1: 111 fail(testAppName + ": Unable to launch java application"); 112 break; 113 114 default: 115 fail(testAppName + ": Unexpected error exit: " + retVal); 116 } 117 } 118 119 }