package org.jboss.aesh.extensions.grep;

import org.jboss.aesh.console.AeshConsole;
import org.jboss.aesh.console.AeshConsoleBuilder;
import org.jboss.aesh.console.AeshConsoleImpl;
import org.jboss.aesh.console.Prompt;
import org.jboss.aesh.console.command.registry.AeshCommandRegistryBuilder;
import org.jboss.aesh.console.command.registry.CommandRegistry;
import org.jboss.aesh.console.settings.Settings;
import org.jboss.aesh.console.settings.SettingsBuilder;
import org.jboss.aesh.terminal.TestTerminal;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

/**
 * @author <a href="mailto:stale.pedersen@jboss.org">Ståle W. Pedersen</a>
 */
public class GrepTest {

    @Test
    public void testGrep() throws IOException {
        PipedOutputStream outputStream = new PipedOutputStream();
        PipedInputStream pipedInputStream = new PipedInputStream(outputStream);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        Settings settings = new SettingsBuilder()
                .terminal(new TestTerminal())
                .inputStream(pipedInputStream)
                .outputStream(byteArrayOutputStream)
                .logging(true)
                .create();

        Grep grep = new Grep();

        CommandRegistry registry = new AeshCommandRegistryBuilder()
                .command(grep)
                .create();

               AeshConsoleBuilder consoleBuilder = new AeshConsoleBuilder()
                .settings(settings)
                .commandRegistry(registry)
                .prompt(new Prompt(""));

        AeshConsole aeshConsole = consoleBuilder.create();
        aeshConsole.start();

        byteArrayOutputStream.flush();
        outputStream.write(("grep -i 'foo' /tmp\n").getBytes());

        String buffer = ((AeshConsoleImpl) aeshConsole).getBuffer();

        System.out.println("Got out: "+byteArrayOutputStream.toString());
    }
}

@@ -657,16 +657,44 @@
 tStream;
+%0Aimport java.io.PrintStream;
 %0A%0A/**%0A *
@@ -1256,16 +1256,32 @@
 tStream(
+new PrintStream(
 byteArra
@@ -1286,32 +1286,33 @@
 rayOutputStream)
+)
 %0A               

test fix