23 |
23 |
24 package test.java.awt.regtesthelpers.process; |
24 package test.java.awt.regtesthelpers.process; |
25 |
25 |
26 import java.io.*; |
26 import java.io.*; |
27 |
27 |
28 /** This class is created to solve interprocess communication problems. |
28 /** |
|
29 * This class is created to solve interprocess communication problems. |
29 * When you need to write a regression test which should verify inter jvm |
30 * When you need to write a regression test which should verify inter jvm |
30 * behavior such as DnD data transfer, Clipboard data transfer, focus |
31 * behavior such as DnD data transfer, Clipboard data transfer, focus |
31 * transfer etc., you could use the next scenario: |
32 * transfer etc., you could use the next scenario: |
32 * |
33 * |
33 * 1. Write an implementation for the parent JVM, using applet test. |
34 * 1. Write an implementation for the parent JVM, using applet test. |
34 * 2. Write an implimentation for the child JVM or native application, using |
35 * 2. Write an implementation for the child JVM or native application, using |
35 * main() function. |
36 * main() function. |
36 * 3. Execute child process using ProcessCommunicator.executeChildProcess() |
37 * 3. Execute child process using ProcessCommunicator.executeChildProcess() |
37 * method. |
38 * method. |
38 * 4. You can decide whetherthe test is passed on the basis of |
39 * 4. You can decide whether the test is passed on the basis of |
39 * ProcessResults class data. |
40 * ProcessResults class data. |
40 * |
41 * |
41 * Note: The class is not thread safe. You should access its methods only from the same |
42 * Note: The class is not thread safe. You should access its methods only from |
42 * thread. |
43 * the same thread. |
43 */ |
44 */ |
44 |
45 |
45 public class ProcessCommunicator { |
46 public class ProcessCommunicator { |
46 |
47 |
47 private static final String javaHome = System.getProperty("java.home", ""); |
48 private static final String javaHome = System.getProperty("java.home", ""); |
48 private static final String javaPath = javaHome + File.separator + "bin" + |
49 private static final String javaPath = javaHome + File.separator + "bin" + |
49 File.separator + "java "; |
50 File.separator + "java "; |
50 private static String command = ""; |
51 private static String command = ""; |
|
52 private static volatile Process process; |
51 |
53 |
52 private ProcessCommunicator() {} |
54 private ProcessCommunicator() {} |
53 |
55 |
54 /** The same as {#link #executeChildProcess(Class,String)} except |
56 /** |
55 * the {@code classPathArgument} parameter. The class path |
57 * The same as {#link #executeChildProcess(Class,String)} except |
56 * parameter is for the debug purposes |
58 * the {@code classPathArgument} parameter. The class path |
|
59 * parameter is for the debug purposes |
57 * |
60 * |
58 * @param classToExecute is passed to the child JVM |
61 * @param classToExecute is passed to the child JVM |
59 * @param classPathArguments class path for the child JVM |
62 * @param classPathArguments class path for the child JVM |
60 * @param args arguments that will be passed to the executed class |
63 * @param args arguments that will be passed to the executed class |
61 * @return results of the executed {@code Process} |
64 * @return results of the executed {@code Process} |
62 */ |
65 */ |
63 public static ProcessResults executeChildProcess(final Class classToExecute, |
66 public static ProcessResults executeChildProcess(final Class classToExecute, |
64 final String classPathArguments, final String [] args) |
67 final String classPathArguments, final String [] args) |
65 { |
68 { |
66 try { |
69 try { |
67 String command = buildCommand(classToExecute, classPathArguments, args); |
70 String command = buildCommand(classToExecute, classPathArguments, args); |
68 Process process = Runtime.getRuntime().exec(command); |
71 process = Runtime.getRuntime().exec(command); |
69 return doWaitFor(process); |
72 return doWaitFor(process); |
70 } catch (IOException e) { |
73 } catch (IOException e) { |
71 throw new RuntimeException(e); |
74 throw new RuntimeException(e); |
72 } |
75 } |
73 } |
76 } |
74 |
77 |
75 /** Executes child {code Process} |
78 /** |
|
79 * Executes child {code Process} |
76 * |
80 * |
77 * @param classToExecute class to be executed as a child java process |
81 * @param classToExecute class to be executed as a child java process |
78 * @param args args to be passed in to the child process |
82 * @param args args to be passed in to the child process |
79 * @return results of the executed {@code Process} |
83 * @return results of the executed {@code Process} |
80 */ |
84 */ |
84 return executeChildProcess(classToExecute, System.getProperty("java.class.path"), args); |
88 return executeChildProcess(classToExecute, System.getProperty("java.class.path"), args); |
85 } |
89 } |
86 |
90 |
87 /** |
91 /** |
88 * Waits for a process and return its results. |
92 * Waits for a process and return its results. |
89 * This is a workaround for <code>Process.waitFor()</code> never returning. |
93 * This is a workaround for {@code Process.waitFor()} never returning. |
90 * |
94 * |
91 * @return results of the executed {@code Process} |
95 * @return results of the executed {@code Process} |
92 */ |
96 */ |
93 private static ProcessResults doWaitFor(final Process p) { |
97 public static ProcessResults doWaitFor(final Process p) { |
94 ProcessResults pres = new ProcessResults(); |
98 ProcessResults pres = new ProcessResults(); |
95 |
99 |
96 final InputStream in; |
100 final InputStream in; |
97 final InputStream err; |
101 final InputStream err; |
98 |
102 |
131 e.printStackTrace(); |
135 e.printStackTrace(); |
132 } |
136 } |
133 return pres; |
137 return pres; |
134 } |
138 } |
135 |
139 |
136 /** Builds command on the basis of the passed class name, |
140 /** |
137 * class path and arguments. |
141 * Builds command on the basis of the passed class name, |
|
142 * class path and arguments. |
138 * |
143 * |
139 * @param classToExecute with class will be executed in the new JVM |
144 * @param classToExecute with class will be executed in the new JVM |
140 * @param classPathArguments java class path (only for test purposes) |
145 * @param classPathArguments java class path (only for test purposes) |
141 * @param args arguments for the new application. This could be used |
146 * @param args arguments for the new application. This could be used |
142 * to pass some information from the parnent to child JVM. |
147 * to pass some information from the parent to child JVM. |
143 * @return command to execute the {@code Process} |
148 * @return command to execute the {@code Process} |
144 */ |
149 */ |
145 private static String buildCommand(final Class classToExecute, |
150 private static String buildCommand(final Class classToExecute, |
146 final String classPathArguments, final String [] args) |
151 final String classPathArguments, final String [] args) |
147 { |
152 { |