package com.bc.ceres.core; import org.junit.BeforeClass; import org.junit.Test; import java.io.File; import java.net.URL; import static org.junit.Assert.*; /** * @author Norman Fomferra */ public class ProcessObserverTest { private static final String JAVA_HOME = System.getProperty("java.home", "."); private static final String JAVA_EXEC_PATH = JAVA_HOME + "/bin/java"; private static String classPath; @BeforeClass public static void setUp() throws Exception { URL location = TestExecutable.class.getProtectionDomain().getCodeSource().getLocation(); classPath = new File(location.toURI()).getCanonicalPath(); } @Test public void testJavaProcessOk() throws Exception { final String commandLine = String.format(JAVA_EXEC_PATH + " -cp %s %s 2 10", classPath, TestExecutable.class.getName()); final Process process = Runtime.getRuntime().exec(commandLine); final MyHandler handler = new MyHandler(); new ProcessObserver(process).setHandler(handler).start(); assertTrue(handler.started); assertEquals("Start\n" + "Progress 10%\n" + "Progress 20%\n" + "Progress 30%\n" + "Progress 40%\n" + "Progress 50%\n" + "Progress 60%\n" + "Progress 70%\n" + "Progress 80%\n" + "Progress 90%\n" + "Progress 100%\n" + "Done\n", handler.out); assertEquals("", handler.err); assertTrue(handler.ended); assertEquals(0, handler.exitCode.intValue()); } @Test public void testJavaProcessMissingArg() throws Exception { final String commandLine = String.format(JAVA_EXEC_PATH + " -cp %s %s 2", classPath, TestExecutable.class.getName()); final Process process = Runtime.getRuntime().exec(commandLine); final MyHandler handler = new MyHandler(); new ProcessObserver(process).setHandler(handler).start(); assertTrue(handler.started); assertEquals("", handler.out); assertEquals("Usage: TestExecutable <seconds> <steps>\n", handler.err); assertTrue(handler.ended); assertEquals(1, handler.exitCode.intValue()); } @Test public void testJavaProcessCancel() throws Exception { final String commandLine = String.format(JAVA_EXEC_PATH + " -cp %s %s 10 2", classPath, TestExecutable.class.getName()); final Process process = Runtime.getRuntime().exec(commandLine); final MyHandler handler = new MyHandler(); final ProcessObserver.ObservedProcess observedProcess = new ProcessObserver(process) .setMode(ProcessObserver.Mode.NON_BLOCKING) .setHandler(handler).start(); Thread.sleep(250); observedProcess.cancel(); assertTrue(handler.started); assertEquals("Start\n", handler.out); assertEquals("", handler.err); assertFalse(handler.ended); assertNull(handler.exitCode); } private static class MyHandler implements ProcessObserver.Handler { boolean started; String out = ""; String err = ""; boolean ended; Integer exitCode; @Override public void onObservationStarted(ProcessObserver.ObservedProcess process, ProgressMonitor pm) { started = true; } @Override public void onStdoutLineReceived(ProcessObserver.ObservedProcess process, String line, ProgressMonitor pm) { out += line + "\n"; } @Override public void onStderrLineReceived(ProcessObserver.ObservedProcess process, String line, ProgressMonitor pm) { err += line + "\n"; } @Override public void onObservationEnded(ProcessObserver.ObservedProcess process, Integer exitCode, ProgressMonitor pm) { ended = true; this.exitCode = exitCode; } } }