package stream;

import static org.junit.Assert.assertEquals;
import static testutil.TestUtilities.createIOExceptionThrowingOutputStream;

import java.io.ByteArrayOutputStream;

import org.junit.Before;
import org.junit.Test;

public class SafeOutputStreamTest {

    SafeOutputStream safe;
    SafeOutputStream safeWithException;
    ByteArrayOutputStream output;

    @Before
    public void setUp() {
        output = new ByteArrayOutputStream();
        safe = new SafeOutputStream(output);
        safeWithException = new SafeOutputStream(createIOExceptionThrowingOutputStream());
    }

    @Test
    public void writeWorks() {
        safe.write("write".getBytes());
        assertEquals("write", output.toString());
    }

    @Test
    public void flushWorks() {
        safe.flush();
    }

    @Test
    public void closeWorks() {
        safe.close();
    }

    @Test(expected = UncheckedIOException.class)
    public void writeThrowsUncheckedException() {
        safeWithException.write("write".getBytes());
    }

    @Test(expected = UncheckedIOException.class)
    public void flushThrowsUncheckedException() {
        safeWithException.flush();
    }

    @Test(expected = UncheckedIOException.class)
    public void closeThrowsUncheckedException() {
        safeWithException.close();
    }

}
