/*
 * Copyright (c) 2002-2015 mgm technology partners GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mgmtp.perfload.core.daemon.util;

import static org.testng.Assert.assertTrue;

import java.io.File;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.slf4j.LoggerFactory;
import org.testng.annotations.Test;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Appender;
import ch.qos.logback.core.AppenderBase;

import com.google.common.collect.ImmutableList;
import com.mgmtp.perfload.core.common.config.ProcessConfig;
import com.mgmtp.perfload.core.common.util.LoggingGobbleCallback;
import com.mgmtp.perfload.core.common.util.StreamGobbler;

/**
 * Unit test for {@link ForkedProcessClientRunner}.
 * 
 * @author rnaegele
 */
public class ForkedProcessClientRunnerTest {

	@Test
	public void testProcessExecution() throws InterruptedException, ExecutionException {
		final StringBuilder sb = new StringBuilder();

		Appender<ILoggingEvent> appender = new AppenderBase<ILoggingEvent>() {
			@Override
			protected void append(final ILoggingEvent eventObject) {
				sb.append(eventObject.toString());
			}
		};

		LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
		appender.setContext(lc);
		appender.setName("test");
		appender.start();
		lc.getLogger(LoggingGobbleCallback.class).addAppender(appender);

		ExecutorService executor = Executors.newCachedThreadPool();
		StreamGobbler gobbler = new StreamGobbler(executor);
		ForkedProcessClientRunner fpcr = new ForkedProcessClientRunner(executor, gobbler);
		ProcessConfig proConf = new ProcessConfig(1, 1, ImmutableList.<String>of());

		// Start the process and wait for it
		Future<Integer> future = fpcr.runClient(new File("."), proConf, ImmutableList.<String>of());
		future.get();

		// Check for a class NoClassDefFoundError because the class LtProcess cannot be found. We just want to make sure
		// that we get output from the process. This guarantees that the process was running. The exception may be localized
		// if a JRE is used, so we can just check for error and the class name.
		assertTrue(sb.indexOf("ERROR") >= 0 && sb.indexOf("LtProcess") >= 0);
	}
}

@@ -682,32 +682,70 @@
 org.
-testng.Assert.assertTrue
+mockito.Mockito.verify;%0Aimport static org.mockito.Mockito.when
 ;%0A%0Ai
@@ -764,16 +764,54 @@
 o.File;%0A
+import java.util.concurrent.Callable;%0A
 import j
@@ -899,47 +899,8 @@
 ce;%0A
-import java.util.concurrent.Executors;%0A
 impo
@@ -947,27 +947,30 @@
 org.
-slf4j.LoggerFac
+mockito.ArgumentCap
 tor
-y
 ;%0Aim
@@ -982,204 +982,166 @@
 org.
-testng.annotations.Test;%0A%0Aimport ch.qos.logback.classic.LoggerContext;%0Aimport ch.qos.logback.classic.spi.ILoggingEvent;%0Aimport ch.qos.logback.core.Appender;%0Aimport ch.qos.logback.core.AppenderBase
+mockito.Captor;%0Aimport org.mockito.Mock;%0Aimport org.mockito.MockitoAnnotations;%0Aimport org.testng.annotations.BeforeMethod;%0Aimport org.testng.annotations.Test
 ;%0A%0Ai
@@ -1251,74 +1251,8 @@
 ig;%0A
-import com.mgmtp.perfload.core.common.util.LoggingGobbleCallback;%0A
 impo
@@ -1364,17 +1364,16 @@
 ner%7D.%0A *
- 
 %0A * @aut
@@ -1441,621 +1441,402 @@
 %0A%0A%09@
-Test%0A%09public void testProcessExecution() throws InterruptedException, ExecutionException %7B%0A%09%09final StringBuilder sb = new StringBuilder();%0A%0A%09%09Appender%3CILoggingEvent%3E appender = new AppenderBase%3CILoggingEvent%3E() %7B%0A%09%09%09@Override%0A%09%09%09protected void append(final ILoggingEvent eventObject) %7B%0A%09%09%09%09sb.append(eventObject.toString());%0A%09%09%09%7D%0A%09%09%7D;%0A%0A%09%09LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();%0A%09%09appender.setContext(lc);%0A%09%09appender.setName(%22test%22);%0A%09%09appender.start();%0A%09%09lc.getLogger(LoggingGobbleCallback.class).addAppender(appender);%0A%0A%09%09ExecutorService executor = Executors.newCachedThreadPool(
+Mock%0A%09private Future%3CInteger%3E result;%0A%0A%09@Mock%0A%09private ExecutorService executor;%0A%0A%09@Captor%0A%09private ArgumentCaptor%3CCallable%3CInteger%3E%3E callableCaptor;%0A%0A%09@BeforeMethod%0A%09public void init() %7B%0A%09%09MockitoAnnotations.initMocks(this);%0A%09%7D%0A%0A%09@Test%0A%09public void testProcessExecution() throws InterruptedException, ExecutionException %7B%0A%09%09when(executor.submit(callableCaptor.capture())).thenReturn(result
 );%0A
+%0A
 %09%09St
@@ -2096,33 +2096,8 @@
 t%0A%09%09
-Future%3CInteger%3E future = 
 fpcr
@@ -2162,18 +2162,8 @@
 f())
-;%0A%09%09future
 .get
@@ -2173,383 +2173,57 @@
 %0A%0A%09%09
-// Check for a class NoClassDefFoundError because the class LtProcess cannot be found. We just want to make sure%0A%09%09// that we get output from the process. This guarantees that the process was running. The exception may be localized%0A%09%09// if a JRE is used, so we can just check for error and the class name.%0A%09%09assertTrue(sb.indexOf(%22ERROR%22) %3E= 0 && sb.indexOf(%22LtProcess%22) %3E= 0
+verify(executor).submit(callableCaptor.getValue()
 );%0A%09

Fix test.