package jp.agentec.sinaburocast.common.proc;

import java.io.IOException;
import java.util.Arrays;

import javax.annotation.Resource;

import jp.agentec.sinaburocast.common.exception.ProcessExecException;
import jp.agentec.sinaburocast.common.io.FileUtil;
import jp.agentec.sinaburocast.common.util.SinaburoUtil;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class ProcessUtilTest extends TestCase {

	@Resource
	private ProcessUtil ProcessUtil;

	public ProcessUtilTest(String string) {
		super(string);
	}

	public static TestSuite suite() {
		TestSuite suite = new TestSuite("ProcessUtilTest");
//		suite.addTest(new ProcessUtilTest("testExecNormal"));
		suite.addTest(new ProcessUtilTest("testExecStdErr"));
//		suite.addTest(new ProcessUtilTest("testExecReaderWorker"));
		return suite;
	}


	/**
	 * @throws java.lang.Exception
	 */
	@Override
	public void setUp() throws Exception {
	}

	/**
	 * @throws java.lang.Exception
	 */
	@Override
	public void tearDown() throws Exception {
	}

	public void testExecNormal() throws IOException, InterruptedException, ProcessExecException {
		StringBuffer stdOut = new StringBuffer();
		String curdir = FileUtil.getCurrentPath();
		// normal pattern, gets std output as StringBuffer object
		int retVal = jp.agentec.sinaburocast.common.proc.ProcessUtil.execute(Arrays.asList("cmd.exe", "/c", "c:\\du.bat", "f:\\mnt\\nas2\\S0001\\C0000\\DLV"), stdOut);

		String str10 = stdOut.toString().replaceAll("F:", "");
		String str11 = str10.toString().replaceAll("\\\\", "/");

		String[] str = str11.toString().split("\n");

		for (String line : str) {
			String[] s = line.split(" ");
			String[] t = s[1].split("/");
			System.out.print("使用率"+ s[0] + "             ");
			System.out.print("contractId" + t[4]);
			System.out.println("サービスoption" + t[6]);
		}
	}

	public void testExecStdErr() throws IOException, InterruptedException, ProcessExecException {
		StringBuffer stdOut = new StringBuffer();
		StringBuffer stdErr = new StringBuffer();
		String curdir = SinaburoUtil.getCurrentDirectory();
		int retVal = 0;
		// gets std output as StringBuffer object and error output as StringBuffer object
		retVal = jp.agentec.sinaburocast.common.proc.ProcessUtil.execute(Arrays.asList("ls","-lR", "notexist"), stdOut, stdErr, null);
//		retVal = ProcessUtil.execute(Arrays.asList("cmd.exe", "/c","ls","-lR", "notexist"), stdOut, stdErr, null);
//		retVal = ProcessUtil.execute(Arrays.asList("cmd.exe", "/c","ls","-lR", curdir + "/src/test/java/jp/agentec/sinaburocast/helper/"), stdOut, stdErr, null);
//		retVal = ProcessUtil.execute(Arrays.asList("cmd.exe", "/c","ls","-lR", "c:/mnt"), stdOut, stdErr, null);
		//		retVal = ProcessUtil.execute(Arrays.asList("cmd.exe", "/c", "c:\\du.bat", "f:\\mnt\\nas1\\S0001"), stdOut, stdErr, null, 10000);
		try {
//			retVal = ProcessUtil.execute(Arrays.asList("cmd.exe", "/c",curdir + "/src/test/java/jp/agentec/sinaburocast/common/proc/blocking.bat"), stdOut, stdErr, null, 1000);
			assertFalse(true);
		} catch (Exception e) {
			System.out.println(e.toString());
		}
		print(retVal, stdOut, stdErr);
	}

	public void testExecReaderWorker() throws IOException, InterruptedException, ProcessExecException {
		// using a reader worker that deals with std output in a separate thread to do with process output in real time.
		@SuppressWarnings("unused")
		ReaderWorker readerWorker = new ReaderWorker() {
			public void run() {
				String line;
				try {
					while ((line = super.bufferedReader.readLine()) != null) {
						System.out.println("worker: " + line);
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}};
		int retVal = jp.agentec.sinaburocast.common.proc.ProcessUtil.execute(Arrays.asList("cmd.exe", "/c", "c:\\du.bat", "f:\\mnt\\nas1\\S0001"), null, null, null, readerWorker, null, 0);
	}

	private void print(int retVal, StringBuffer stdOut, StringBuffer stdErr) {
//		System.out.println("retVal:"+retVal);
		System.out.println("stdOut:"+stdOut);
//		if (stdErr != null) {
//			System.out.println("stdErr:"+stdErr);
//			System.out.println("stdErr blank:"+ StringUtils.isEmpty(stdErr.toString()));
//		}
	}
}