/*
 * The MIT License
 *
 * Copyright 2015 mchaberski.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.github.mike10004.nativehelper;

import com.google.common.util.concurrent.ListenableFuture;
import com.novetta.ibg.common.sys.Platforms;
import java.io.IOException;
import java.util.concurrent.Executors;
import org.apache.tools.ant.BuildException;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author mchaberski
 */
public class ProgramWithOutputStringsTest {
    
    @Test
    public void testExecute_stdoutToString() {
        System.out.println("testExecute_stdoutToString");
        testExecute("echo hello", 0, "hello", "");
    }
    
    @Test
    public void testExecute_stderrToString() {
        System.out.println("testExecute_stderrToString");
        testExecute("echo goodbye >&2", 0, "", "goodbye");
    }
    
    private void testExecute(String shellCommand, int expectedExitCode, String expectedStdout, String expectedStderr) throws BuildException {
        Program.Builder builder = ProgramBuilders.shell();
        System.out.println("executing command: " + shellCommand);
        ProgramWithOutputStrings program = builder.arg(shellCommand).outputToStrings();
        ProgramWithOutputStringsResult result = program.execute();
        System.out.println(result);
        assertEquals("exitCode", expectedExitCode, result.getExitCode());
        assertEquals("stdout", expectedStdout, result.getStdoutString());
        assertEquals("stderr", expectedStderr, result.getStderrString());
    }

    @Test
    public void testExecuteAsync() throws Exception {
        System.out.println("testExecuteAsync");
        ProgramWithOutputStrings program = ProgramBuilders.shell().arg("echo hello").outputToStrings();
        ListenableFuture<ProgramWithOutputStringsResult> future = program.executeAsync(Executors.newSingleThreadExecutor());
        ProgramWithOutputStringsResult result = future.get();
        System.out.println("program result: " + result);
        assertEquals("hello", result.getStdoutString());
    }
    
    @Test(timeout = 1000L)
    public void testExecute_inputString() {
        System.out.println("testExecute_inputString");
        String inputString = "hello";
        Program.Builder builder = ProgramBuilders.shell().reading(inputString);
        if (Platforms.getPlatform().isWindows()) {
            builder = builder.arg("type CON");
        } else {
            builder = builder.arg("cat");
        }
        ProgramWithOutputStringsResult result = builder.outputToStrings().execute();
        System.out.println("result: " + result);
        assertEquals(inputString, result.getStdoutString());
    }
}

@@ -1810,32 +1810,75 @@
 derrToString%22);%0A
+        String expectedStderr = %22goodbye%22;%0A
         testExec
@@ -1894,17 +1894,16 @@
  goodbye
- 
 %3E&2%22, 0,
@@ -1907,25 +1907,30 @@
  0, %22%22, 
-%22goodbye%22
+expectedStderr
 );%0A    %7D
@@ -3349,55 +3349,8 @@
 lder
- = ProgramBuilders.shell().reading(inputString)
 ;%0A  
@@ -3424,32 +3424,66 @@
 r = 
-builder.arg(%22type CON%22);
+Program.running(%22findstr.exe%22).arg(%22%5E%22); // greps anything
 %0A   
@@ -3518,26 +3518,30 @@
 ilder = 
-builder.ar
+Program.runnin
 g(%22cat%22)
@@ -3548,24 +3548,72 @@
 ;%0A        %7D%0A
+        builder = builder.reading(inputString);%0A
         Prog

fix unit test expectations on Windows