< prev index next >

tests/system/src/test/java/test/shutdowntest/ShutdownHookTest.java

Print this page
rev 9568 : 8147427: refactor test launchers to support jake
Reviewed-by: kcr
   1 /*
   2  * Copyright (c) 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.  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 org.junit.Test;
  32 
  33 import static org.junit.Assert.*;
  34 import static test.shutdowntest.Constants.*;
  35 
  36 /**
  37  * Unit test for calling Platform runLater from a ShutdownHook.
  38  */
  39 public class ShutdownHookTest {
  40 
  41     private static final String className = ShutdownHookTest.class.getName();
  42     private static final String pkgName = className.substring(0, className.lastIndexOf("."));
  43 
  44     private final String testAppName = pkgName + "." + "ShutdownHookApp";
  45 
  46     @Test (timeout=5000)
  47     public void testShutdownHook() throws Exception {
  48         // Initilaize the socket
  49         final ServerSocket service = new ServerSocket(0);
  50         final int port = service.getLocalPort();
  51 
  52         // Launch the test app
  53         final String classpath = System.getProperty("java.class.path");







  54         ProcessBuilder builder;
  55         builder = new ProcessBuilder("java", "-cp", classpath, testAppName,
  56                 String.valueOf(port));
  57         builder.redirectError(ProcessBuilder.Redirect.INHERIT);
  58         builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
  59         Process process = builder.start();
  60 
  61         // Accept a connection from the test app
  62         final Socket socket = service.accept();
  63         final InputStream in = socket.getInputStream();
  64 
  65         // Read the "handshake" token
  66         int handshake = in.read();
  67         assertEquals("Socket handshake failed,", SOCKET_HANDSHAKE, handshake);
  68 
  69         // Read the status code from the shutdown hook
  70         int status = in.read();
  71         switch (status) {
  72             case STATUS_OK:
  73                 break;
  74             case STATUS_ILLEGAL_STATE:
  75                 fail(testAppName
  76                     + ": IllegalStateException from Platform.runLater");


   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");


< prev index next >