package jnr.process;

import static org.junit.Assert.*;
import org.junit.Test;

/**
 * Created by headius on 1/19/15.
 */
public class TestProcessBuilder {
    @Test
    public void testBasicProcess() throws Exception {
        ProcessBuilder pb = new ProcessBuilder("/bin/sh", "-c", "echo hello");

        Process p = pb.start();

        byte[] hello = new byte[5];
        p.getInputStream().read(hello);

        assertEquals(0, p.waitFor());
        
        assertArrayEquals("hello".getBytes(), hello);
    }
    
    @Test
    public void testEnvironmentVariables() throws Exception {
    	String value = "environment variable";
    	
    	ProcessBuilder pb = new ProcessBuilder("/bin/sh", "-c", "echo \"$envVar\"");
    	pb.environment().put("envVar", value);
    	
    	Process p = pb.start();
    	
    	byte[] message = new byte[value.getBytes().length];
    	p.getInputStream().read(message);
    	
    	assertEquals(0, p.waitFor());
    	assertArrayEquals(value.getBytes(), message);
    }
}

@@ -73,16 +73,135 @@
 .Test;%0A%0A
+import java.nio.channels.SelectableChannel;%0Aimport java.nio.channels.SelectionKey;%0Aimport java.nio.channels.Selector;%0A%0A
 /**%0A * C
@@ -1116,11 +1116,2402 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testSelectableIn() throws Exception %7B%0A        ProcessBuilder pb = new ProcessBuilder(%22/bin/sh%22, %22-c%22, %22echo hello%22);%0A%0A        Process p = pb.start();%0A%0A        SelectableChannel in = p.getIn();%0A%0A        in.configureBlocking(false);%0A        Selector selector = in.provider().openSelector();%0A        SelectionKey key = in.register(selector, SelectionKey.OP_READ);%0A        int selected = selector.select();%0A%0A        assertEquals(1, selected);%0A        assertEquals(key, selector.keys().iterator().next());%0A%0A        selector.close();%0A        in.configureBlocking(true);%0A%0A        byte%5B%5D hello = new byte%5B5%5D;%0A        p.getInputStream().read(hello);%0A%0A        assertEquals(0, p.waitFor());%0A%0A        assertArrayEquals(%22hello%22.getBytes(), hello);%0A    %7D%0A%0A    @Test%0A    public void testSelectableErr() throws Exception %7B%0A        ProcessBuilder pb = new ProcessBuilder(%22/bin/sh%22, %22-c%22, %22echo hello %3E&2%22);%0A%0A        Process p = pb.start();%0A%0A        SelectableChannel err = p.getErr();%0A%0A        err.configureBlocking(false);%0A        Selector selector = err.provider().openSelector();%0A        SelectionKey key = err.register(selector, SelectionKey.OP_READ);%0A        int selected = selector.select();%0A%0A        assertEquals(1, selected);%0A        assertEquals(key, selector.keys().iterator().next());%0A%0A        selector.close();%0A        err.configureBlocking(true);%0A%0A        byte%5B%5D hello = new byte%5B5%5D;%0A        p.getErrorStream().read(hello);%0A%0A        assertEquals(0, p.waitFor());%0A%0A        assertArrayEquals(%22hello%22.getBytes(), hello);%0A    %7D%0A%0A    @Test%0A    public void testSelectableOut() throws Exception %7B%0A        ProcessBuilder pb = new ProcessBuilder(%22/bin/sh%22, %22-c%22, %22cat%22);%0A%0A        Process p = pb.start();%0A%0A        SelectableChannel out = p.getOut();%0A%0A        out.configureBlocking(false);%0A        Selector selector = out.provider().openSelector();%0A        SelectionKey key = out.register(selector, SelectionKey.OP_WRITE);%0A        int selected = selector.select();%0A%0A        assertEquals(1, selected);%0A        assertEquals(key, selector.keys().iterator().next());%0A%0A        selector.close();%0A        out.configureBlocking(true);%0A%0A        p.getOutputStream().write(%22hello%22.getBytes());%0A%0A        byte%5B%5D hello = new byte%5B5%5D;%0A        p.getInputStream().read(hello);%0A%0A        p.kill();%0A%0A        assertEquals(9, p.waitFor());%0A%0A        assertArrayEquals(%22hello%22.getBytes(), hello);%0A    %7D
 %0A%7D%0A

Add tests to clarify selection of in, out, err