test/java/awt/regtesthelpers/process/ProcessCommunicator.java

Print this page

        

*** 1,7 **** /* ! * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. --- 1,7 ---- /* ! * Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.
*** 23,59 **** package test.java.awt.regtesthelpers.process; import java.io.*; ! /** This class is created to solve interprocess communication problems. * When you need to write a regression test which should verify inter jvm * behavior such as DnD data transfer, Clipboard data transfer, focus * transfer etc., you could use the next scenario: * * 1. Write an implementation for the parent JVM, using applet test. ! * 2. Write an implimentation for the child JVM or native application, using * main() function. * 3. Execute child process using ProcessCommunicator.executeChildProcess() * method. ! * 4. You can decide whetherthe test is passed on the basis of * ProcessResults class data. * ! * Note: The class is not thread safe. You should access its methods only from the same ! * thread. */ public class ProcessCommunicator { private static final String javaHome = System.getProperty("java.home", ""); private static final String javaPath = javaHome + File.separator + "bin" + File.separator + "java "; private static String command = ""; private ProcessCommunicator() {} ! /** The same as {#link #executeChildProcess(Class,String)} except * the {@code classPathArgument} parameter. The class path * parameter is for the debug purposes * * @param classToExecute is passed to the child JVM * @param classPathArguments class path for the child JVM --- 23,62 ---- package test.java.awt.regtesthelpers.process; import java.io.*; ! /** ! * This class is created to solve interprocess communication problems. * When you need to write a regression test which should verify inter jvm * behavior such as DnD data transfer, Clipboard data transfer, focus * transfer etc., you could use the next scenario: * * 1. Write an implementation for the parent JVM, using applet test. ! * 2. Write an implementation for the child JVM or native application, using * main() function. * 3. Execute child process using ProcessCommunicator.executeChildProcess() * method. ! * 4. You can decide whether the test is passed on the basis of * ProcessResults class data. * ! * Note: The class is not thread safe. You should access its methods only from ! * the same thread. */ public class ProcessCommunicator { private static final String javaHome = System.getProperty("java.home", ""); private static final String javaPath = javaHome + File.separator + "bin" + File.separator + "java "; private static String command = ""; + private static volatile Process process; private ProcessCommunicator() {} ! /** ! * The same as {#link #executeChildProcess(Class,String)} except * the {@code classPathArgument} parameter. The class path * parameter is for the debug purposes * * @param classToExecute is passed to the child JVM * @param classPathArguments class path for the child JVM
*** 63,80 **** public static ProcessResults executeChildProcess(final Class classToExecute, final String classPathArguments, final String [] args) { try { String command = buildCommand(classToExecute, classPathArguments, args); ! Process process = Runtime.getRuntime().exec(command); return doWaitFor(process); } catch (IOException e) { throw new RuntimeException(e); } } ! /** Executes child {code Process} * * @param classToExecute class to be executed as a child java process * @param args args to be passed in to the child process * @return results of the executed {@code Process} */ --- 66,84 ---- public static ProcessResults executeChildProcess(final Class classToExecute, final String classPathArguments, final String [] args) { try { String command = buildCommand(classToExecute, classPathArguments, args); ! process = Runtime.getRuntime().exec(command); return doWaitFor(process); } catch (IOException e) { throw new RuntimeException(e); } } ! /** ! * Executes child {code Process} * * @param classToExecute class to be executed as a child java process * @param args args to be passed in to the child process * @return results of the executed {@code Process} */
*** 84,98 **** return executeChildProcess(classToExecute, System.getProperty("java.class.path"), args); } /** * Waits for a process and return its results. ! * This is a workaround for <code>Process.waitFor()</code> never returning. * * @return results of the executed {@code Process} */ ! private static ProcessResults doWaitFor(final Process p) { ProcessResults pres = new ProcessResults(); final InputStream in; final InputStream err; --- 88,102 ---- return executeChildProcess(classToExecute, System.getProperty("java.class.path"), args); } /** * Waits for a process and return its results. ! * This is a workaround for {@code Process.waitFor()} never returning. * * @return results of the executed {@code Process} */ ! public static ProcessResults doWaitFor(final Process p) { ProcessResults pres = new ProcessResults(); final InputStream in; final InputStream err;
*** 131,147 **** e.printStackTrace(); } return pres; } ! /** Builds command on the basis of the passed class name, * class path and arguments. * * @param classToExecute with class will be executed in the new JVM * @param classPathArguments java class path (only for test purposes) * @param args arguments for the new application. This could be used ! * to pass some information from the parnent to child JVM. * @return command to execute the {@code Process} */ private static String buildCommand(final Class classToExecute, final String classPathArguments, final String [] args) { --- 135,152 ---- e.printStackTrace(); } return pres; } ! /** ! * Builds command on the basis of the passed class name, * class path and arguments. * * @param classToExecute with class will be executed in the new JVM * @param classPathArguments java class path (only for test purposes) * @param args arguments for the new application. This could be used ! * to pass some information from the parent to child JVM. * @return command to execute the {@code Process} */ private static String buildCommand(final Class classToExecute, final String classPathArguments, final String [] args) {
*** 160,172 **** } command = commandBuilder.toString(); return command; } ! /** Could be used for the debug purposes. * * @return command that was build to execute the child process */ public static String getExecutionCommand () { return command; } } --- 165,190 ---- } command = commandBuilder.toString(); return command; } ! /** ! * Could be used for the debug purposes. * * @return command that was build to execute the child process */ public static String getExecutionCommand () { return command; } + + /** + * Terminates the process created by {@code executeChildProcess} methods. + */ + public static void destroyProcess() { + if (process != null) { + if (process.isAlive()) { + process.destroy(); + } + process = null; + } + } }