jdk/test/java/awt/regtesthelpers/process/ProcessCommunicator.java
author denis
Wed, 25 Nov 2009 21:27:06 +0300
changeset 4363 212067ed03e4
parent 2 90ce3da70b43
child 5194 ba7f04720557
permissions -rw-r--r--
4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing Reviewed-by: uta, dav Contributed-by: Damjan Jovanovic <damjan.jov@gmail.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
package test.java.awt.regtesthelpers.process;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/** This class is created to solve interprocess communication problems.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *  When you need to write a regression test which should verify inter jvm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *  behavior such as DnD data transfer, Clipboard data transfer, focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *  transfer etc., you could use the next scenario:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *  1. Write an implementation for the parent JVM, using applet test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *  2. Write an implimentation for the child JVM or native application, using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *     main() function.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *  3. Execute child process using  ProcessCommunicator.executeChildProcess()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *     method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *  4. You can decide whetherthe test is passed on the basis of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *     ProcessResults class data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *  Note: The class is not thread safe. You should access its methods only from the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *        thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public class ProcessCommunicator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final String javaHome = System.getProperty("java.home", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static final String javaPath = javaHome + File.separator + "bin" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            File.separator + "java ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static String command = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private ProcessCommunicator() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /** The same as {#link #executeChildProcess(Class,String)} except
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     *  the {@code classPathArgument} parameter. The class path
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     *  parameter is for the debug purposes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     *  @param classToExecute is passed to the child JVM
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     *  @param classPathArguments class path for the child JVM
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     *  @param args arguments that will be passed to the executed class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     *  @return results of the executed {@code Process}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public static ProcessResults executeChildProcess(final Class classToExecute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                           final String classPathArguments, final String [] args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            String command = buildCommand(classToExecute, classPathArguments, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            Process process = Runtime.getRuntime().exec(command);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            return doWaitFor(process);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /** Executes child {code Process}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @param classToExecute class to be executed as a child java process
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @param args args to be passed in to the child process
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @return results of the executed {@code Process}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public static ProcessResults executeChildProcess(final Class classToExecute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                           final String [] args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        return executeChildProcess(classToExecute, " ", args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Waits for a process and return its results.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * This is a workaround for <code>Process.waitFor()</code> never returning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @return results of the executed {@code Process}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private static ProcessResults doWaitFor(final Process p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        ProcessResults pres = new ProcessResults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        final InputStream in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        final InputStream err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            in = p.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            err = p.getErrorStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            boolean finished = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            while (!finished) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    while (in.available() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                        pres.appendToStdOut((char)in.read());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    while (err.available() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                        pres.appendToStdErr((char)err.read());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    // Ask the process for its exitValue. If the process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    // is not finished, an IllegalThreadStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    // is thrown. If it is finished, we fall through and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    // the variable finished is set to true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    pres.setExitValue(p.exitValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    finished  = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                catch (IllegalThreadStateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    // Process is not finished yet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    // Sleep a little to save on CPU cycles
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    Thread.currentThread().sleep(500);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (in != null) in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            if (err != null) err.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        catch (Throwable e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            System.err.println("doWaitFor(): unexpected exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        return pres;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /** Builds command on the basis of the passed class name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *  class path and arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @param classToExecute with class will be executed in the new JVM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @param classPathArguments java class path (only for test purposes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @param args arguments for the new application. This could be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *             to pass some information from the parnent to child JVM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @return command to execute the {@code Process}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private static String buildCommand(final Class classToExecute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                         final String classPathArguments, final String [] args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        StringBuilder commandBuilder = new StringBuilder();
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   149
        commandBuilder.append(javaPath).append(" ");
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   150
        commandBuilder.append("-cp ").append(System.getProperty("test.classes", ".")).append(File.pathSeparatorChar);
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   151
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   152
        if (classPathArguments.trim().length() > 0) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   153
            commandBuilder.append(classPathArguments).append(" ");
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   154
        }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   155
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   156
        commandBuilder.append(" ");
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   157
        commandBuilder.append(classToExecute.getName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        for (String argument:args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            commandBuilder.append(" ").append(argument);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        command = commandBuilder.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return command;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /** Could be used for the debug purposes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
      * @return command that was build to execute the child process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public static String getExecutionCommand () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return command;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
}