< prev index next >

tools/FxTestRunner/src/client/test/runner/TestScript.java

Print this page
rev 319 : 8160349: [TEST BUG] Hanged test blocks port and breaks following tests
Summary: Now each test uses random free port and test runner correctly handles interrupts from javatest.


  32 import client.test.RunModes.RunModeException;
  33 import client.test.runner.CommonTestRunnerWorker.Command;
  34 import client.test.runner.CommonTestRunnerWorker.Command.CommandType;
  35 import client.test.runner.interview.LookAndFeelQuestion;
  36 import client.test.runner.interview.PipelineQuestion;
  37 import client.util.CtrUtils;
  38 import static client.util.CtrUtils.*;
  39 import client.util.CtrUtils.OutputReader;
  40 import client.util.JettyServer;
  41 import com.sun.interview.YesNoQuestion;
  42 import com.sun.javatest.Status;
  43 import com.sun.javatest.TestDescription;
  44 import com.sun.javatest.TestEnvironment;
  45 import com.sun.javatest.TestEnvironment.Fault;
  46 import java.io.*;
  47 import java.lang.reflect.Method;
  48 import java.net.ServerSocket;
  49 import java.net.Socket;
  50 import java.net.URL;
  51 import java.net.UnknownHostException;


  52 import java.util.logging.Level;
  53 import java.util.logging.Logger;
  54 import org.junit.runner.RunWith;
  55 import test.javaclient.shared.CanvasRunner;
  56 import test.javaclient.shared.Utils;
  57 
  58 /**
  59  *
  60  * @author shura, mrkam, Sergey Grinev, Victor Shubov
  61  */
  62 public class TestScript extends htmltestrunner.TestScript {
  63 

  64     private static final boolean verbose = true; //TODO: use real logger
  65     private volatile Process process = null;
  66     private volatile ServerSocket cmdServer = null;
  67     private volatile Socket cmdSocket;
  68     private volatile ObjectOutputStream commandStream;
  69     private volatile TestRunner.Status status = null;
  70     private volatile TestEnvironment savedEnv;
  71     private Thread resultThread;
  72 
  73     @Override
  74     protected synchronized void interrupt(Status status) {
  75         System.out.println("interrupting with status " + status);
  76         super.interrupt(status);
  77     }
  78 
  79     @Override
  80     protected synchronized Status getStatus() {
  81         return super.getStatus();
  82     }
  83 
  84     @Override
  85     protected void before(TestDescription description, String resultDir) throws Throwable {
  86         String testClassName = td.getParameter(RunUITestFinder.UNIT_TEST_CLASS_NAME);
  87         String testName = td.getParameter(RunUITestFinder.TEST_NAME);
  88         final String runMode = savedEnv.lookup(BasicFXInterview.RUN_MODE_PARAM)[0];
  89 
  90         System.out.println("\nTEST: " + testClassName + "/" + testName);
  91         System.out.println("Mode:" + runMode);
  92         System.out.println("Result Dir:" + resultDir);
  93         if (needToRun(testClassName, testName, runMode)) {
  94 
  95             try {
  96                 startServer();
  97 
  98                 if (runMode.equals(BasicFXInterview.RUN_MODE_DESKTOP)
  99                         || runMode.equals(BasicFXInterview.RUN_MODE_DESKTOP_SWING_INTEROPERABILITY)
 100                         || runMode.equals(BasicFXInterview.RUN_MODE_DESKTOP_SWT_INTEROPERABILITY)) {
 101                     runTd(description, resultDir);
 102                 } else {
 103                     runPlugin(td, resultDir, BasicFXInterview.RUN_MODE_JNLP.equals(runMode));
 104                 }
 105             } catch (Throwable e) {
 106                 e.printStackTrace(System.err);
 107                 if (cmdServer != null) {
 108                     try {
 109                         cmdServer.close();
 110                     } catch (IOException ex) {
 111                         ex.printStackTrace(System.err);
 112                     }
 113                 }
 114                 if (cmdSocket != null) {
 115                     try {
 116                         cmdSocket.close();
 117                     } catch (IOException ex) {
 118                         ex.printStackTrace(System.err);
 119                     }
 120                 }
 121                 if (commandStream != null) {
 122                     try {
 123                         commandStream.close();
 124                     } catch (IOException ex) {
 125                         ex.printStackTrace(System.err);
 126                     }
 127                 }
 128                 if (process != null) {
 129                     process.destroy();
 130                 }
 131                 commandStream.close();
 132                 interrupt(Status.error(e.toString()));
 133             }
 134 

 135             resultThread = new Thread(new Runnable() {
 136                 @Override
 137                 public void run() {
 138                     try {
 139                         System.err.println("Waiting for exit");
 140                         ObjectInputStream ois = new ObjectInputStream(cmdSocket.getInputStream());
 141                         status = (TestRunner.Status) ois.readObject();
 142 
 143                         Status jtStatus;
 144                         System.err.println("Process returned status " + status);
 145                         if (status == null) {
 146                             jtStatus = Status.error("Unexpected status: " + status);
 147                         } else if (status.isPassed()) {
 148                             jtStatus = Status.passed(status.getText());
 149                         } else if (status.isFailed()) {
 150                             jtStatus = Status.failed(status.getText());
 151                         } else {
 152                             jtStatus = Status.error(status.getText());
 153                         }
 154                         interrupt(jtStatus);


 174                             }
 175                         }
 176                         try {
 177                             if (process != null) {
 178                                 process.waitFor();
 179                             }
 180                         } catch (InterruptedException ex) {
 181                             ex.printStackTrace(System.err);
 182                         }
 183 //                    if (jemmyProcess != null) {
 184 //                        System.out.println("killed jemmy process");
 185 //                        try {
 186 //                            jemmyProcess.getInputStream().close();
 187 //                        } catch (IOException ex) {
 188 //                            ex.printStackTrace();
 189 //                        }
 190 //
 191 //                        jemmyProcess.destroy();
 192 //                    }
 193                         System.out.println("DONE");

 194                     }
 195                 }
 196             }, "I'm waiting for test's result");
 197             resultThread.start();








 198         }
 199     }
 200 
 201     @Override
 202     protected void showTestDialog(TestDescription td, TestEnvironment env) {
 203         String nodescription = td.getParameter(RunUITestFinder.NO_DESCRIPTION);
 204         boolean dryRun = YesNoQuestion.YES.equals(lookup(BasicFXInterview.DRY_RUN_TAG, YesNoQuestion.NO));
 205         if (!Boolean.parseBoolean(nodescription)) {
 206             if (dryRun) {
 207                 System.err.println("DRY RUN MODE");
 208                 int delay = Integer.parseInt(lookup(BasicFXInterview.DRY_RUN_DURATION_TAG, BasicFXInterview.DRY_RUN_DURATION_TAG.toString()));
 209                 try {
 210                     Thread.sleep(delay * 1000);
 211                 } catch (InterruptedException ex) {
 212                 }
 213                 setStatus(Status.error("Dry Run"));
 214             } else {
 215                 super.showTestDialog(td, env);
 216             }
 217         }


 270         return getStatus();
 271     }
 272 
 273     private String getJvmArgPrismOrder() {
 274         final String pipelineOptions = lookup(PipelineQuestion.PIPELINE_PARAM_NAME, "");
 275         String jvmArgPrismOrder = "";
 276         if (!pipelineOptions.trim().equals("")) {
 277             jvmArgPrismOrder = "-Dprism.order=" + pipelineOptions;
 278         }
 279         return jvmArgPrismOrder;
 280     }
 281 
 282     /**
 283      *
 284      * @param td
 285      * @param resultDir
 286      * @return
 287      * @throws IOException
 288      * @throws Fault
 289      */
 290     protected String[] tdCmdArgs(TestDescription td, String resultDir) throws IOException, Fault {
 291         String testClassName = td.getParameter(RunUITestFinder.UNIT_TEST_CLASS_NAME);
 292 
 293         boolean isJunit = Boolean.parseBoolean(td.getParameter(RunUITestFinder.TYPE_JUNIT));
 294 
 295         //String fxSdkHome = td.getParameter(BasicFXInterview.FX_SDK_HOME_PARAM_NAME);
 296         String fxSdkHome = savedEnv.lookup(BasicFXInterview.FX_SDK_HOME_PARAM_NAME)[0];
 297 
 298         String[] pathArr = savedEnv.lookup(BasicFXInterview.JAVA_PARAM_NAME);
 299         StringBuilder pathStr = new StringBuilder();
 300         for (String p : pathArr) {
 301             if (pathStr.length() > 0) {
 302                 pathStr.append(" ");
 303             }
 304             pathStr.append(p);
 305         }
 306         String javaExec = pathStr.toString();
 307 
 308 
 309         String proxy = lookup(BasicFXInterview.PROXY_PARAM_NAME, "");
 310         String jvmProxyHost = "";


 366                 if ((wunWithAnnotation != null) && (wunWithAnnotation.value().equals(CanvasRunner.class))) {
 367                     swtTestOpt = "-XstartOnFirstThread";
 368                     System.out.println("Use -XstartOnFirstThread option, as we on MacOS, and SWT test is run.");
 369                 }
 370             }
 371         } catch (ClassNotFoundException ex) {
 372             System.err.println("Error : " + ex);
 373         }
 374 
 375         String jvmArgClientTestRoot = "-DtestRoot=" + RunUITestFinder.testRoot;
 376 
 377         String[] command = new String[]{};
 378         command = addToArray(command, javaExec.trim());
 379         command = addToArray(command, jvmVmOptions);
 380         command = addToArray(command, lookAndFeelOptions);
 381         command = addToArray(command, ipV4);
 382         command = addToArray(command, jvmArgPrismOrder, jvmArgLibraryPath, jvmArgImageUtils);
 383         command = addToArray(command, additionalOptions);
 384         command = addToArray(command, jvmArgNoDesc, jvmProxyHost, jvmProxyPort, jvmInterop, swtTestOpt);
 385         command = addToArray(command, jvmArgClientTestRoot);

 386         command = addToArray(command, "-classpath", System.getProperty("java.class.path"));
 387 //        command = addToArray(command, "-Xdebug", "-Xnoagent", "-Djava.compiler=NONE", "-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5858");
 388         command = addToArray(command, isJunit ? JUnit2TestRunner.class.getName() : TestRunner.class.getName(), testClassName);
 389         return command;
 390     }
 391 
 392     /**
 393      *
 394      * @param td
 395      * @param resultDir
 396      * @return
 397      */
 398     protected String[] pluginCmdArgs(TestDescription td, String resultDir) {
 399         return null;
 400     }
 401 
 402     /**
 403      *
 404      * @param command
 405      * @throws IOException
 406      */
 407     protected void doRunTd(String[] command) throws IOException {
 408         process = Runtime.getRuntime().exec(deleteEmptyElements(command));
 409     }
 410 
 411     private void startServer() throws IOException {
 412         System.out.println("Starting server at " + CommonTestRunnerWorker.PORT);
 413         int iRetryCount = 3;
 414         boolean bindDone = false;
 415         while ( (iRetryCount > 0) && (false==bindDone) ) {
 416             iRetryCount = iRetryCount - 1;
 417             
 418             try {
 419                 cmdServer = new ServerSocket(CommonTestRunnerWorker.PORT);

 420                 bindDone = true;
 421             } catch (java.net.BindException be) {
 422                 bindDone = false;
 423                 System.out.println("  === bind exception ===");
 424                 Socket socket = new Socket( "127.0.0.1", CommonTestRunnerWorker.PORT);
 425                 commandStream = new ObjectOutputStream(socket.getOutputStream());
 426                 sendCommand(CommandType.ABORT);
 427                 try {Thread.sleep(100);} catch(Exception e){}
 428             }
 429         
 430         }// retry loop end
 431         cmdServer.setSoTimeout(60000); // we need to be generous for plugin mode, it gets to "download" runnable

 432     }
 433 
 434     private void waitForConnection() throws IOException {
 435         System.out.println("Waiting for connection...");
 436         cmdSocket = cmdServer.accept();
 437         commandStream = new ObjectOutputStream(cmdSocket.getOutputStream());
 438         System.out.println("Connected");
 439     }
 440 
 441     private void sendCommand(CommandType command) throws UnknownHostException, IOException {
 442         sendCommand(command, "-");
 443     }
 444 
 445     private synchronized void sendCommand(CommandType command, String param) throws IOException {
 446         Command trc = new Command(command, param);
 447         if (verbose) {
 448             System.out.println("command " + trc);
 449         }
 450         commandStream.writeObject(trc);
 451     }


 464             ex.printStackTrace(System.err);
 465         }
 466         return result == null || result.length == 0 ? def : result[0];
 467     }
 468 
 469     /**
 470      *
 471      * @param id
 472      * @return
 473      */
 474     protected String[] lookup(String id) {
 475         String[] result = null;
 476         try {
 477             result = savedEnv.lookup(id);
 478         } catch (Fault ex) {
 479             ex.printStackTrace(System.err);
 480         }
 481         return result;
 482     }
 483 
 484     private void runTd(TestDescription td, String resultDir) throws IOException, InterruptedException, Fault {
 485         String[] command = tdCmdArgs(td, resultDir);
 486         dumpProcessExecution(resultDir, command, null);
 487         doRunTd(command);
 488         System.out.println("Logs: " + resultDir + File.separator);
 489 
 490         if (process != null) {
 491             //TODO: who close that streams?
 492             new Thread(new OutputReader(new FileOutputStream(resultDir + File.separator + "process.out"), process.getInputStream())).start();
 493             new Thread(new OutputReader(new FileOutputStream(resultDir + File.separator + "process.err"), process.getErrorStream())).start();
 494         } else {
 495             System.out.println("ERROR: Failed to create process"); // TODO: fail test?
 496         }
 497         waitForConnection();
 498         String testName = td.getParameter(RunUITestFinder.TEST_NAME);
 499         String testClassName = td.getParameter(RunUITestFinder.UNIT_TEST_CLASS_NAME);
 500         sendCommand(CommandType.SET_TEST_CLASS, testClassName);
 501         if (testName != null) {
 502             sendCommand(CommandType.SET_TEST_NAME, testName);
 503         }
 504         sendCommand(CommandType.RUN_TEST);
 505     }
 506 
 507     private static void dumpProcessExecution(String resultDir, String[] cmd, String workdir) throws IOException {
 508         final PrintWriter writer = new PrintWriter(
 509                 new FileWriter(resultDir + File.separator + "process_execution.log"));
 510         writer.println("starting process...");
 511         writer.println("Command: ");
 512         for (String s : cmd) {
 513             writer.print(s);
 514             writer.print("\n");
 515         }
 516         writer.println("");
 517         if (workdir != null) {
 518             writer.println("Workdir: " + workdir);
 519         }
 520         writer.flush();
 521     }
 522     private static final String pluginPath = "./dist-plugin/";
 523     private static final String pluginFile = "JavaClientPluginTest";
 524 
 525     private void runPlugin(TestDescription td, String resultDir, boolean isJnlp) throws IOException, Fault {

 526         String testClassName = td.getParameter(RunUITestFinder.UNIT_TEST_CLASS_NAME);
 527         String testName = td.getParameter(RunUITestFinder.TEST_NAME);
 528 
 529         //TODO: add non-junit tests support
 530         boolean isJunit = Boolean.parseBoolean(td.getParameter(RunUITestFinder.TYPE_JUNIT));
 531         if (!isJunit) {
 532             throw new UnsupportedOperationException("runui is not supported yet");
 533         }
 534         String[] command;
 535         String param;
 536         File workdir = new File(System.getProperty("user.dir"));
 537         final int port = 8485;
 538         if (isJnlp) {
 539             String[] pathArr = savedEnv.lookup(BasicFXInterview.JAVAWS_PARAM_NAME);
 540             StringBuilder pathStr = new StringBuilder();
 541             for (String p : pathArr) {
 542                 if (pathStr.length() > 0) {
 543                     pathStr.append(" ");
 544                 }
 545                 pathStr.append(p);




  32 import client.test.RunModes.RunModeException;
  33 import client.test.runner.CommonTestRunnerWorker.Command;
  34 import client.test.runner.CommonTestRunnerWorker.Command.CommandType;
  35 import client.test.runner.interview.LookAndFeelQuestion;
  36 import client.test.runner.interview.PipelineQuestion;
  37 import client.util.CtrUtils;
  38 import static client.util.CtrUtils.*;
  39 import client.util.CtrUtils.OutputReader;
  40 import client.util.JettyServer;
  41 import com.sun.interview.YesNoQuestion;
  42 import com.sun.javatest.Status;
  43 import com.sun.javatest.TestDescription;
  44 import com.sun.javatest.TestEnvironment;
  45 import com.sun.javatest.TestEnvironment.Fault;
  46 import java.io.*;
  47 import java.lang.reflect.Method;
  48 import java.net.ServerSocket;
  49 import java.net.Socket;
  50 import java.net.URL;
  51 import java.net.UnknownHostException;
  52 import java.util.concurrent.Semaphore;
  53 import java.util.concurrent.TimeUnit;
  54 import java.util.logging.Level;
  55 import java.util.logging.Logger;
  56 import org.junit.runner.RunWith;
  57 import test.javaclient.shared.CanvasRunner;
  58 import test.javaclient.shared.Utils;
  59 
  60 /**
  61  *
  62  * @author shura, mrkam, Sergey Grinev, Victor Shubov
  63  */
  64 public class TestScript extends htmltestrunner.TestScript {
  65     
  66     private static final int FORCED_TERMINATION_TIMEOUT = 5000;
  67     private static final boolean verbose = true; //TODO: use real logger
  68     private volatile Process process = null;
  69     private volatile ServerSocket cmdServer = null;
  70     private volatile Socket cmdSocket;
  71     private volatile ObjectOutputStream commandStream;
  72     private volatile TestRunner.Status status = null;
  73     private volatile TestEnvironment savedEnv;
  74     private Thread resultThread;
  75 
  76     @Override
  77     protected synchronized void interrupt(Status status) {
  78         System.out.println("interrupting with status " + status);
  79         super.interrupt(status);
  80     }
  81 
  82     @Override
  83     protected synchronized Status getStatus() {
  84         return super.getStatus();
  85     }
  86     
  87     @Override
  88     protected void before(TestDescription description, String resultDir) throws Throwable {
  89         String testClassName = td.getParameter(RunUITestFinder.UNIT_TEST_CLASS_NAME);
  90         String testName = td.getParameter(RunUITestFinder.TEST_NAME);
  91         final String runMode = savedEnv.lookup(BasicFXInterview.RUN_MODE_PARAM)[0];
  92 
  93         System.out.println("\nTEST: " + testClassName + "/" + testName);
  94         System.out.println("Mode:" + runMode);
  95         System.out.println("Result Dir:" + resultDir);
  96         if (needToRun(testClassName, testName, runMode)) {
  97 
  98             try {
  99                 int masterPort = startServer();
 100 
 101                 if (runMode.equals(BasicFXInterview.RUN_MODE_DESKTOP)
 102                         || runMode.equals(BasicFXInterview.RUN_MODE_DESKTOP_SWING_INTEROPERABILITY)
 103                         || runMode.equals(BasicFXInterview.RUN_MODE_DESKTOP_SWT_INTEROPERABILITY)) {
 104                     runTd(description, resultDir, masterPort);
 105                 } else {
 106                     runPlugin(td, resultDir, BasicFXInterview.RUN_MODE_JNLP.equals(runMode));
 107                 }
 108             } catch (Throwable e) {
 109                 e.printStackTrace(System.err);
 110                 if (cmdServer != null) {
 111                     try {
 112                         cmdServer.close();
 113                     } catch (IOException ex) {
 114                         ex.printStackTrace(System.err);
 115                     }
 116                 }
 117                 if (cmdSocket != null) {
 118                     try {
 119                         cmdSocket.close();
 120                     } catch (IOException ex) {
 121                         ex.printStackTrace(System.err);
 122                     }
 123                 }
 124                 if (commandStream != null) {
 125                     try {
 126                         commandStream.close();
 127                     } catch (IOException ex) {
 128                         ex.printStackTrace(System.err);
 129                     }
 130                 }
 131                 if (process != null) {
 132                     process.destroy();
 133                 }
 134                 commandStream.close();
 135                 interrupt(Status.error(e.toString()));
 136             }
 137             
 138             Semaphore s = new Semaphore(0);
 139             resultThread = new Thread(new Runnable() {
 140                 @Override
 141                 public void run() {
 142                     try {
 143                         System.err.println("Waiting for exit");
 144                         ObjectInputStream ois = new ObjectInputStream(cmdSocket.getInputStream());
 145                         status = (TestRunner.Status) ois.readObject();
 146                         
 147                         Status jtStatus;
 148                         System.err.println("Process returned status " + status);
 149                         if (status == null) {
 150                             jtStatus = Status.error("Unexpected status: " + status);
 151                         } else if (status.isPassed()) {
 152                             jtStatus = Status.passed(status.getText());
 153                         } else if (status.isFailed()) {
 154                             jtStatus = Status.failed(status.getText());
 155                         } else {
 156                             jtStatus = Status.error(status.getText());
 157                         }
 158                         interrupt(jtStatus);


 178                             }
 179                         }
 180                         try {
 181                             if (process != null) {
 182                                 process.waitFor();
 183                             }
 184                         } catch (InterruptedException ex) {
 185                             ex.printStackTrace(System.err);
 186                         }
 187 //                    if (jemmyProcess != null) {
 188 //                        System.out.println("killed jemmy process");
 189 //                        try {
 190 //                            jemmyProcess.getInputStream().close();
 191 //                        } catch (IOException ex) {
 192 //                            ex.printStackTrace();
 193 //                        }
 194 //
 195 //                        jemmyProcess.destroy();
 196 //                    }
 197                         System.out.println("DONE");
 198                         s.release();
 199                     }
 200                 }
 201             }, "I'm waiting for test's result");
 202             resultThread.start();
 203             try {
 204                 s.acquire();
 205             } catch (InterruptedException ex) {
 206                 System.out.println("Interrupt from test runner: " + ex);
 207                 resultThread.interrupt();
 208                 process.destroyForcibly();
 209                 process.waitFor(FORCED_TERMINATION_TIMEOUT, TimeUnit.MILLISECONDS);
 210             }            
 211         }
 212     }
 213 
 214     @Override
 215     protected void showTestDialog(TestDescription td, TestEnvironment env) {
 216         String nodescription = td.getParameter(RunUITestFinder.NO_DESCRIPTION);
 217         boolean dryRun = YesNoQuestion.YES.equals(lookup(BasicFXInterview.DRY_RUN_TAG, YesNoQuestion.NO));
 218         if (!Boolean.parseBoolean(nodescription)) {
 219             if (dryRun) {
 220                 System.err.println("DRY RUN MODE");
 221                 int delay = Integer.parseInt(lookup(BasicFXInterview.DRY_RUN_DURATION_TAG, BasicFXInterview.DRY_RUN_DURATION_TAG.toString()));
 222                 try {
 223                     Thread.sleep(delay * 1000);
 224                 } catch (InterruptedException ex) {
 225                 }
 226                 setStatus(Status.error("Dry Run"));
 227             } else {
 228                 super.showTestDialog(td, env);
 229             }
 230         }


 283         return getStatus();
 284     }
 285 
 286     private String getJvmArgPrismOrder() {
 287         final String pipelineOptions = lookup(PipelineQuestion.PIPELINE_PARAM_NAME, "");
 288         String jvmArgPrismOrder = "";
 289         if (!pipelineOptions.trim().equals("")) {
 290             jvmArgPrismOrder = "-Dprism.order=" + pipelineOptions;
 291         }
 292         return jvmArgPrismOrder;
 293     }
 294 
 295     /**
 296      *
 297      * @param td
 298      * @param resultDir
 299      * @return
 300      * @throws IOException
 301      * @throws Fault
 302      */
 303     protected String[] tdCmdArgs(TestDescription td, String resultDir, int port) throws IOException, Fault {
 304         String testClassName = td.getParameter(RunUITestFinder.UNIT_TEST_CLASS_NAME);
 305 
 306         boolean isJunit = Boolean.parseBoolean(td.getParameter(RunUITestFinder.TYPE_JUNIT));
 307 
 308         //String fxSdkHome = td.getParameter(BasicFXInterview.FX_SDK_HOME_PARAM_NAME);
 309         String fxSdkHome = savedEnv.lookup(BasicFXInterview.FX_SDK_HOME_PARAM_NAME)[0];
 310 
 311         String[] pathArr = savedEnv.lookup(BasicFXInterview.JAVA_PARAM_NAME);
 312         StringBuilder pathStr = new StringBuilder();
 313         for (String p : pathArr) {
 314             if (pathStr.length() > 0) {
 315                 pathStr.append(" ");
 316             }
 317             pathStr.append(p);
 318         }
 319         String javaExec = pathStr.toString();
 320 
 321         
 322         String proxy = lookup(BasicFXInterview.PROXY_PARAM_NAME, "");
 323         String jvmProxyHost = "";


 379                 if ((wunWithAnnotation != null) && (wunWithAnnotation.value().equals(CanvasRunner.class))) {
 380                     swtTestOpt = "-XstartOnFirstThread";
 381                     System.out.println("Use -XstartOnFirstThread option, as we on MacOS, and SWT test is run.");
 382                 }
 383             }
 384         } catch (ClassNotFoundException ex) {
 385             System.err.println("Error : " + ex);
 386         }
 387 
 388         String jvmArgClientTestRoot = "-DtestRoot=" + RunUITestFinder.testRoot;
 389 
 390         String[] command = new String[]{};
 391         command = addToArray(command, javaExec.trim());
 392         command = addToArray(command, jvmVmOptions);
 393         command = addToArray(command, lookAndFeelOptions);
 394         command = addToArray(command, ipV4);
 395         command = addToArray(command, jvmArgPrismOrder, jvmArgLibraryPath, jvmArgImageUtils);
 396         command = addToArray(command, additionalOptions);
 397         command = addToArray(command, jvmArgNoDesc, jvmProxyHost, jvmProxyPort, jvmInterop, swtTestOpt);
 398         command = addToArray(command, jvmArgClientTestRoot);
 399         command = addToArray(command, "-DmasterPort=" + port);        
 400         command = addToArray(command, "-classpath", System.getProperty("java.class.path"));
 401 //        command = addToArray(command, "-Xdebug", "-Xnoagent", "-Djava.compiler=NONE", "-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5858");
 402         command = addToArray(command, isJunit ? JUnit2TestRunner.class.getName() : TestRunner.class.getName(), testClassName);
 403         return command;
 404     }
 405 
 406     /**
 407      *
 408      * @param td
 409      * @param resultDir
 410      * @return
 411      */
 412     protected String[] pluginCmdArgs(TestDescription td, String resultDir) {
 413         return null;
 414     }
 415 
 416     /**
 417      *
 418      * @param command
 419      * @throws IOException
 420      */
 421     protected void doRunTd(String[] command) throws IOException {
 422         process = Runtime.getRuntime().exec(deleteEmptyElements(command));
 423     }
 424 
 425     private int startServer() throws IOException {
 426         System.out.println("Starting server." );
 427         int iRetryCount = 3;
 428         boolean bindDone = false;
 429         while ( (iRetryCount > 0) && (false==bindDone) ) {
 430             iRetryCount = iRetryCount - 1;
 431 
 432             try {
 433                 cmdServer = new ServerSocket(0);
 434                 System.out.println("Started server at port " + cmdServer.getLocalPort());
 435                 bindDone = true;
 436             } catch (java.net.BindException be) {
 437                 bindDone = false;
 438                 System.out.println("  === bind exception ===");
 439                 Socket socket = new Socket("127.0.0.1", cmdServer.getLocalPort());
 440                 commandStream = new ObjectOutputStream(socket.getOutputStream());
 441                 sendCommand(CommandType.ABORT);
 442                 try {Thread.sleep(100);} catch(Exception e){}
 443                 }
 444 
 445         }// retry loop end
 446         cmdServer.setSoTimeout(60000); // we need to be generous for plugin mode, it gets to "download" runnable
 447         return cmdServer.getLocalPort();
 448     }
 449 
 450     private void waitForConnection() throws IOException {
 451         System.out.println("Waiting for connection...");
 452         cmdSocket = cmdServer.accept();
 453         commandStream = new ObjectOutputStream(cmdSocket.getOutputStream());
 454         System.out.println("Connected");
 455     }
 456 
 457     private void sendCommand(CommandType command) throws UnknownHostException, IOException {
 458         sendCommand(command, "-");
 459     }
 460 
 461     private synchronized void sendCommand(CommandType command, String param) throws IOException {
 462         Command trc = new Command(command, param);
 463         if (verbose) {
 464             System.out.println("command " + trc);
 465         }
 466         commandStream.writeObject(trc);
 467     }


 480             ex.printStackTrace(System.err);
 481         }
 482         return result == null || result.length == 0 ? def : result[0];
 483     }
 484 
 485     /**
 486      *
 487      * @param id
 488      * @return
 489      */
 490     protected String[] lookup(String id) {
 491         String[] result = null;
 492         try {
 493             result = savedEnv.lookup(id);
 494         } catch (Fault ex) {
 495             ex.printStackTrace(System.err);
 496         }
 497         return result;
 498     }
 499 
 500     private void runTd(TestDescription td, String resultDir, int port) throws IOException, InterruptedException, Fault {
 501         String[] command = tdCmdArgs(td, resultDir, port);
 502         dumpProcessExecution(resultDir, command, null);
 503         doRunTd(command);
 504         System.out.println("Logs: " + resultDir + File.separator);
 505 
 506         if (process != null) {
 507             //TODO: who close that streams?
 508             new Thread(new OutputReader(new FileOutputStream(resultDir + File.separator + "process.out"), process.getInputStream())).start();
 509             new Thread(new OutputReader(new FileOutputStream(resultDir + File.separator + "process.err"), process.getErrorStream())).start();
 510         } else {
 511             System.out.println("ERROR: Failed to create process"); // TODO: fail test?
 512         }
 513         waitForConnection();
 514         String testName = td.getParameter(RunUITestFinder.TEST_NAME);
 515         String testClassName = td.getParameter(RunUITestFinder.UNIT_TEST_CLASS_NAME);
 516         sendCommand(CommandType.SET_TEST_CLASS, testClassName);
 517         if (testName != null) {
 518             sendCommand(CommandType.SET_TEST_NAME, testName);
 519         }
 520         sendCommand(CommandType.RUN_TEST);
 521     }
 522 
 523     private static void dumpProcessExecution(String resultDir, String[] cmd, String workdir) throws IOException {
 524         final PrintWriter writer = new PrintWriter(
 525                 new FileWriter(resultDir + File.separator + "process_execution.log"));
 526         writer.println("starting process...");
 527         writer.println("Command: ");
 528         for (String s : cmd) {
 529             writer.print(s);
 530             writer.print("\n");
 531         }
 532         writer.println("");
 533         if (workdir != null) {
 534             writer.println("Workdir: " + workdir);
 535         }
 536         writer.flush();
 537     }
 538     private static final String pluginPath = "./dist-plugin/";
 539     private static final String pluginFile = "JavaClientPluginTest";
 540 
 541     private void runPlugin(TestDescription td, String resultDir, boolean isJnlp)
 542             throws IOException, Fault {
 543         String testClassName = td.getParameter(RunUITestFinder.UNIT_TEST_CLASS_NAME);
 544         String testName = td.getParameter(RunUITestFinder.TEST_NAME);
 545 
 546         //TODO: add non-junit tests support
 547         boolean isJunit = Boolean.parseBoolean(td.getParameter(RunUITestFinder.TYPE_JUNIT));
 548         if (!isJunit) {
 549             throw new UnsupportedOperationException("runui is not supported yet");
 550         }
 551         String[] command;
 552         String param;
 553         File workdir = new File(System.getProperty("user.dir"));
 554         final int port = 8485;
 555         if (isJnlp) {
 556             String[] pathArr = savedEnv.lookup(BasicFXInterview.JAVAWS_PARAM_NAME);
 557             StringBuilder pathStr = new StringBuilder();
 558             for (String p : pathArr) {
 559                 if (pathStr.length() > 0) {
 560                     pathStr.append(" ");
 561                 }
 562                 pathStr.append(p);


< prev index next >