Search in sources :

Example 6 with LineBufferingOutputStream

use of org.gradle.internal.io.LineBufferingOutputStream in project gradle by gradle.

the class LineBufferingOutputStreamTest method logsEachLineAsASeparateLogMessage.

@Test
public void logsEachLineAsASeparateLogMessage() throws IOException {
    LineBufferingOutputStream outputStream = new LineBufferingOutputStream(action, 8);
    context.checking(new Expectations() {

        {
            one(action).text(TextUtil.toPlatformLineSeparators("line 1\n"));
            one(action).text(TextUtil.toPlatformLineSeparators("line 2\n"));
        }
    });
    outputStream.write(TextUtil.toPlatformLineSeparators("line 1\nline 2\n").getBytes());
}
Also used : Expectations(org.jmock.Expectations) LineBufferingOutputStream(org.gradle.internal.io.LineBufferingOutputStream) Test(org.junit.Test)

Example 7 with LineBufferingOutputStream

use of org.gradle.internal.io.LineBufferingOutputStream in project gradle by gradle.

the class LineBufferingOutputStreamTest method cannotWriteAfterClose.

@Test(expected = IOException.class)
public void cannotWriteAfterClose() throws IOException {
    LineBufferingOutputStream outputStream = new LineBufferingOutputStream(action, 8);
    context.checking(new Expectations() {

        {
            one(action).endOfStream(null);
        }
    });
    outputStream.close();
    outputStream.write("ignore me".getBytes());
}
Also used : Expectations(org.jmock.Expectations) LineBufferingOutputStream(org.gradle.internal.io.LineBufferingOutputStream) Test(org.junit.Test)

Example 8 with LineBufferingOutputStream

use of org.gradle.internal.io.LineBufferingOutputStream in project gradle by gradle.

the class LineBufferingOutputStreamTest method buffersTextUntilEndOfLineReached.

@Test
public void buffersTextUntilEndOfLineReached() throws IOException {
    System.setProperty("line.separator", "-");
    LineBufferingOutputStream outputStream = new LineBufferingOutputStream(action, 8);
    context.checking(new Expectations() {

        {
            one(action).text("line 1-");
        }
    });
    outputStream.write("line ".getBytes());
    outputStream.write("1-line 2".getBytes());
    context.checking(new Expectations() {

        {
            one(action).text("line 2-");
        }
    });
    outputStream.write("-".getBytes());
}
Also used : Expectations(org.jmock.Expectations) LineBufferingOutputStream(org.gradle.internal.io.LineBufferingOutputStream) Test(org.junit.Test)

Example 9 with LineBufferingOutputStream

use of org.gradle.internal.io.LineBufferingOutputStream in project gradle by gradle.

the class LineBufferingOutputStreamTest method handlesSingleCharacterLineSeparator.

@Test
public void handlesSingleCharacterLineSeparator() throws IOException {
    System.setProperty("line.separator", "-");
    LineBufferingOutputStream outputStream = new LineBufferingOutputStream(action, 8);
    context.checking(new Expectations() {

        {
            one(action).text("line 1-");
            one(action).text("line 2-");
        }
    });
    outputStream.write(String.format("line 1-line 2-").getBytes());
}
Also used : Expectations(org.jmock.Expectations) LineBufferingOutputStream(org.gradle.internal.io.LineBufferingOutputStream) Test(org.junit.Test)

Example 10 with LineBufferingOutputStream

use of org.gradle.internal.io.LineBufferingOutputStream in project gradle by gradle.

the class InputForwarder method start.

public InputForwarder start() {
    lifecycleLock.lock();
    try {
        if (started) {
            throw new IllegalStateException("input forwarder has already been started");
        }
        disconnectableInput = new DisconnectableInputStream(input, bufferSize);
        outputBuffer = new LineBufferingOutputStream(handler, bufferSize);
        forwardingExecuter = executorFactory.create("Forward input");
        forwardingExecuter.execute(new Runnable() {

            public void run() {
                byte[] buffer = new byte[bufferSize];
                int readCount;
                Throwable readFailure = null;
                try {
                    while (true) {
                        try {
                            readCount = disconnectableInput.read(buffer, 0, bufferSize);
                            if (readCount < 0) {
                                break;
                            }
                        } catch (AsynchronousCloseException e) {
                            break;
                        } catch (IOException e) {
                            readFailure = e;
                            break;
                        }
                        outputBuffer.write(buffer, 0, readCount);
                    }
                    // will flush any unterminated lines out synchronously
                    outputBuffer.flush();
                } catch (IOException e) {
                    // should not happen
                    throw UncheckedException.throwAsUncheckedException(e);
                } finally {
                    handler.endOfStream(readFailure);
                }
            }
        });
        started = true;
    } finally {
        lifecycleLock.unlock();
    }
    return this;
}
Also used : DisconnectableInputStream(org.gradle.util.DisconnectableInputStream) AsynchronousCloseException(java.nio.channels.AsynchronousCloseException) LineBufferingOutputStream(org.gradle.internal.io.LineBufferingOutputStream) IOException(java.io.IOException)

Aggregations

LineBufferingOutputStream (org.gradle.internal.io.LineBufferingOutputStream)14 Expectations (org.jmock.Expectations)12 Test (org.junit.Test)12 IOException (java.io.IOException)1 AsynchronousCloseException (java.nio.channels.AsynchronousCloseException)1 TeeOutputStream (org.apache.commons.io.output.TeeOutputStream)1 ConsoleOutput (org.gradle.api.logging.configuration.ConsoleOutput)1 TextStream (org.gradle.internal.io.TextStream)1 LoggingManagerInternal (org.gradle.internal.logging.LoggingManagerInternal)1 DisconnectableInputStream (org.gradle.util.DisconnectableInputStream)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1