2017-08-29 50 views
0

开始杀过程中,我用下面的代码开始通过Apache公地执行libarary一个的testng.xml的执行:通过org.apache.commons.executor

DefaultExecuteResultHandler resultHandler; 
     ExecuteWatchdog watchdog; 
     final Executor executor; 

     resultHandler = new DefaultExecuteResultHandler(); 
     watchdog = new ExecuteWatchdog(-1L); 
     executor = new DefaultExecutor(); 
     executor.setStreamHandler(new PumpStreamHandler(new LogOutputStream() { 

     @Override 
     protected void processLine(final String line, @SuppressWarnings("unused") int level) { 
      Display.getDefault().syncExec(new Runnable() { 

       public void run() { 

        if (line.toLowerCase().indexOf("error") > -1) { 

         textArea.append(line+"\n"); 
        } else if (line.toLowerCase().indexOf("warn") > -1) { 
         textArea.append(line+"\n"); 
        } else { 
         textArea.append(line+"\n"); 


        } 
       } 
      }); 
     } 
    })); 

    executor.setExitValue(1); 
    executor.setWatchdog(watchdog); 
    executor.execute(cl, resultHandler); 

但我想给一个停止按钮来停止这个过程。我试过了:

executor.getWatchdog().destroyProcess(); 

但是这只会破坏看门狗。然而我开始的testng.xml继续在后台运行。我想摧毁我在命令中给予的过程。 这可能吗?

+0

为什么你需要使用Apache执行分拆了'TestNG'套房执行?你最好直接在你的java代码中使用TestNG apis否?还请告诉我们你的'CommandLine'看起来像什么'cl' a.k.a。 –

+0

@KrishnanMahadevan我想直接将控制台输出发送到Jframe textarea,我不确定是否可以通过TestNG API。另外我想运行它作为一个不同的线程,以便主应用程序不挂起。命令c1是:cmd.exe/k cd“D:\ MyProject”&& D:&& java -cp D:\ MyProject \ Utilities \ *; D:\ MyProject \ bin org.testng.TestNG D:\ MyProject \ testng .XML –

回答

0

为什么你的解决方案没有杀死产生的JVM,原因可能是因为你调用了cmd.exe,并且从那里你可能会产生JVM。所以,当你调用destroyProcess()我相信它的cmd.exe正在被杀,但不是java.exe

你应该尝试您的命令行更改为类似如下:

java -cp D:\MyProject\Utilities*;D:\MyProject\bin org.testng.TestNG D:\MyProject\testng.xml 

这里有一个解决方案,不使用Apache的百科全书执行人这样做,但在一个JVM中管理一切,还提供了一种检索TestNG的输出和错误输出的方法。

此解决方案使用TestNG API。

主要测试运行,使用TestNG的运行测试,在不同的线程看起来像下面

import org.testng.TestNG; 

import java.util.Collections; 
import java.util.List; 
import java.util.concurrent.Callable; 
import java.util.concurrent.ExecutionException; 
import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors; 
import java.util.concurrent.Future; 

public class SimpleTestRunner { 
    public static void main(String[] args) throws InterruptedException, ExecutionException { 
     System.err.println("***Main Thread running in Thread [" + Thread.currentThread().getId() + "]***"); 
     ExecutorService service = Executors.newCachedThreadPool(); 
     WorkerThread thread = new WorkerThread(SampleTestClass.class); 
     List<Future<ExecutionResults>> allResults = service.invokeAll(Collections.singletonList(thread)); 
     service.shutdown(); 
     ExecutionResults result = allResults.get(0).get(); 
     System.err.println("******Printing the TestNG output******"); 
     System.err.println(result); 
     System.err.println("**************************************"); 
    } 

    public static class WorkerThread implements Callable<ExecutionResults> { 
     private Class<?>[] classes; 

     WorkerThread(Class<?>... classes) { 
      this.classes = classes; 
     } 

     @Override 
     public ExecutionResults call() throws Exception { 
      System.err.println("***Worker Thread running in Thread [" + Thread.currentThread().getId() + "]***"); 
      TestNG testNG = new TestNG(); 
      ExecutionResults results; 
      testNG.setVerbose(2); 
      ConsoleCapturer capturer = new ConsoleCapturer(); 
      testNG.setTestClasses(classes); 
      try { 
       capturer.start(); 
       testNG.run(); 
      } finally { 
       ConsoleCapturer.CapturedData data = capturer.stop(); 
       results = new ExecutionResults(data, testNG.getStatus()); 
      } 
      return results; 
     } 
    } 

    public static class ExecutionResults { 
     private ConsoleCapturer.CapturedData data; 
     private int status; 

     public ExecutionResults(ConsoleCapturer.CapturedData data, int status) { 
      this.data = data; 
      this.status = status; 
     } 

     public ConsoleCapturer.CapturedData getData() { 
      return data; 
     } 

     public int getStatus() { 
      return status; 
     } 

     @Override 
     public String toString() { 
      return "ExecutionResults{" + 
        "data=" + getData() + 
        ", status=" + getStatus() + 
        '}'; 
     } 
    } 
} 

的工具类,它重新定向所有输出和错误内容到一个线程,使他们能够被重定向到任何地方,如下所示:

这个类大多是从解决方案Redirect console output to string in java借来的代码和一些即兴创作。

import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.io.OutputStream; 
import java.io.PrintStream; 

/** 
* This class is an improvisation of the solution provided in https://stackoverflow.com/a/30665299/679824 
*/ 
public class ConsoleCapturer { 
    private ByteArrayOutputStream baosOutput, baosError; 
    private PrintStream previousOut, previousError; 
    private boolean capturing; 

    public void start() { 
     if (capturing) { 
      return; 
     } 

     capturing = true; 
     previousOut = System.out; 
     previousError = System.err; 
     baosOutput = new ByteArrayOutputStream(); 
     baosError = new ByteArrayOutputStream(); 

     System.setOut(new PrintStream(new OutputStreamCombiner(previousOut, baosOutput))); 
     System.setErr(new PrintStream(new OutputStreamCombiner(previousError, baosError))); 
    } 

    public CapturedData stop() { 

     if (!capturing) { 
      return new CapturedData(); 
     } 

     System.setOut(previousOut); 
     System.setErr(previousError); 

     String output = baosOutput.toString(); 
     String error = baosError.toString(); 

     try { 
      baosOutput.close(); 
      baosError.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     baosOutput = null; 
     previousOut = null; 
     capturing = false; 

     return new CapturedData(output, error); 
    } 

    private static class OutputStreamCombiner extends OutputStream { 
     private OutputStream[] outputStreams; 

     OutputStreamCombiner(OutputStream... outputStreams) { 
      this.outputStreams = outputStreams; 
     } 

     public void write(int b) throws IOException { 
      for (OutputStream os : outputStreams) { 
       os.write(b); 
      } 
     } 

     public void flush() throws IOException { 
      for (OutputStream os : outputStreams) { 
       os.flush(); 
      } 
     } 

     public void close() throws IOException { 
      for (OutputStream os : outputStreams) { 
       os.close(); 
      } 
     } 
    } 

    public static class CapturedData { 
     private String output; 
     private String error; 

     CapturedData() { 
      this("", ""); 
     } 

     public CapturedData(String output, String error) { 
      this.output = output; 
      this.error = error; 
     } 

     public String getError() { 
      return error; 
     } 

     public String getOutput() { 
      return output; 
     } 

     @Override 
     public String toString() { 
      return "CapturedData{" + 
        "output='" + getOutput() + '\'' + 
        ", error='" + getError() + '\'' + 
        '}'; 
     } 
    } 
} 

测试类使用看起来像下面

import org.testng.annotations.Test; 

public class SampleTestClass { 

    @Test 
    public void testMethod() { 
     System.err.println("This goes into the error console"); 
     System.out.println("This goes into the console"); 
    } 
} 

输出类似于下面

***Main Thread running in Thread [1]*** 
***Worker Thread running in Thread [11]*** 
This goes into the console 
This goes into the error console 
PASSED: testMethod 

=============================================== 
    Command line test 
    Tests run: 1, Failures: 0, Skips: 0 
=============================================== 


=============================================== 
Command line suite 
Total tests run: 1, Failures: 0, Skips: 0 
=============================================== 

******Printing the TestNG output****** 
ExecutionResults{data=CapturedData{output='This goes into the console 
PASSED: testMethod 

=============================================== 
    Command line test 
    Tests run: 1, Failures: 0, Skips: 0 
=============================================== 


=============================================== 
Command line suite 
Total tests run: 1, Failures: 0, Skips: 0 
=============================================== 

', error='This goes into the error console 
'}, status=0} 
************************************** 

Process finished with exit code 0