package com.atlassian.bitbucket.mesh.process.handler;

import org.junit.Test;

import java.nio.ByteBuffer;

import static org.junit.Assert.*;

public class NullStdioHandlerTest {

    @Test
    public void testOnStdinReadyWritesNothing() {
        ByteBuffer buffer = ByteBuffer.allocate(1);
        assertFalse((new NullStdioHandler<>()).onStdinReady(buffer));
        assertEquals(0, buffer.limit());
        assertEquals(0, buffer.position());
    }

    @Test
    public void testGetResultReturnsNullByDefault() {
        assertNull((new NullStdioHandler<>()).getResult());
    }

    @Test
    public void testGetResultReturnsSuppliedResult() {
        assertEquals(100500L, (long) (new NullStdioHandler<>(() -> 100500L)).getResult());
    }

    @Test
    public void testSkipsToTheEndOfTheBuffer() {
        byte[] msg = "There are those who said this day would never come. What are they to say now...".getBytes();
        ByteBuffer buffer = ByteBuffer.allocate(msg.length);
        buffer.put(msg);
        buffer.flip();

        NullStdioHandler handler = new NullStdioHandler();
        handler.onStdout(buffer, true);

        assertEquals(msg.length, buffer.position());
    }

    @Test(expected = NullPointerException.class)
    public void testThrowsIfSupplierIsNull() {
        new NullStdioHandler<>(null);
    }
}