1 /*
   2  * Copyright (c) 2009, 2012, 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  */
  24 
  25 package client.test.runner;
  26 
  27 import client.util.CtrUtils;
  28 import com.sun.javatest.Status;
  29 import com.sun.javatest.TestDescription;
  30 import com.sun.javatest.TestEnvironment.Fault;
  31 import com.sun.javatest.TestResult;
  32 import com.sun.javatest.util.StringArray;
  33 import java.io.File;
  34 import java.io.IOException;
  35 import java.io.PrintWriter;
  36 
  37 /**
  38  *
  39  * @author sg
  40  */
  41 public class DistributedTestScript extends TestScript {
  42 
  43     /**
  44      *
  45      */
  46     protected static final String ACTIVE_AGENT_COMMAND = "com.sun.javatest.agent.FXActiveAgentCommand";
  47 
  48     /**
  49      *
  50      */
  51     protected static final String PROCESS_COMMAND = "com.sun.javatest.lib.FXProcessCommand";
  52 
  53     /**
  54      *
  55      */
  56     protected static final String TEST_RUNNER_NAME = "clientTestRunner";
  57 
  58     /**
  59      *
  60      * @param command
  61      * @throws IOException
  62      */
  63     @Override
  64     protected void doRunTd(String[] command) throws IOException {
  65         final String[] cmd = CtrUtils.deleteEmptyElements(command);
  66         new Thread(new Runnable() {
  67             @Override
  68             public void run() {
  69                 invokeAgentCommand(cmd);
  70             }
  71         }).start();
  72     }
  73 
  74     /**
  75      *
  76      * @param td
  77      * @param resultDir
  78      * @return
  79      * @throws IOException
  80      * @throws Fault
  81      */
  82     @Override
  83     protected String[] tdCmdArgs(TestDescription td, String resultDir, int port) throws IOException, Fault {
  84         String id = env.lookup(BasicFXInterview.TESTSUITE_ID)[0];
  85         String host = env.lookup(BasicFXInterview.JAVATEST_HOSTNAME)[0];
  86         String verbose = Boolean.getBoolean("javatest.FXProcessCommand.verbose") ? "-v" : "";
  87 //        String[] args = super.tdCmdArgs(td, id + File.separator + td.getRootRelativePath());
  88         String[] args = super.tdCmdArgs(td, workdirPath + File.separator + td.getParameter("testName"), port);
  89         
  90         String[] ret = {
  91             "-mapArgs",
  92             "-c",
  93             "-classpath",
  94             System.getProperty("java.class.path"),
  95             PROCESS_COMMAND,
  96             id,
  97 //            "-execDir", id,
  98             verbose
  99         };
 100         return TestScript.addToArray(ret, TestScript.addToArray(args, host));
 101     }
 102 
 103     @Override
 104     protected synchronized void interrupt(Status status) {
 105         if(section != null && section.isMutable()){
 106             section.setStatus(status);
 107         }
 108         super.interrupt(status);
 109     }
 110     
 111     private TestResult.Section section;
 112 
 113     /**
 114      *
 115      * @param args
 116      * @return
 117      */
 118     protected Status invokeAgentCommand(String[] args) {
 119         Status s = null;
 120         com.sun.javatest.Command testCommand;
 121 
 122         try {
 123             section = getTestResult().createSection(TEST_RUNNER_NAME);
 124 
 125             section.getMessageWriter().println("command: " + ACTIVE_AGENT_COMMAND + " " + StringArray.join(args));
 126 
 127             PrintWriter out1 = null;
 128             PrintWriter out2 = null;
 129             try {
 130                 out1 = section.createOutput(cmdOut1Name);
 131                 out2 = section.createOutput(cmdOut2Name);
 132 
 133                 Class c = (loader == null ? Class.forName(ACTIVE_AGENT_COMMAND) : loader.loadClass(ACTIVE_AGENT_COMMAND));
 134                 testCommand = (com.sun.javatest.Command) (c.newInstance());
 135                 testCommand.setClassLoader(loader);
 136                 s = testCommand.run(args, out1, out2);
 137 
 138             } finally {
 139                 if (out2 != null) {
 140                     out2.close();
 141                 }
 142                 if (out1 != null) {
 143                     out1.close();
 144                 }
 145             }
 146             if (section.isMutable()) {
 147                 section.setStatus(s);
 148             }
 149             return s;
 150         } catch (Throwable e) {
 151             e.printStackTrace();
 152             return Status.error(e.getMessage());
 153         }
 154     }
 155 }