package com.atlassian.stash.internal.web.util.web;

import org.junit.Test;

import javax.servlet.ServletOutputStream;
import java.io.IOException;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;

public class CountingServletOutputStreamTest {

    @Test
    public void writeByte() throws IOException {
        CountingServletOutputStream underTest = new CountingServletOutputStream(mock(ServletOutputStream.class));
        underTest.write(1);

        assertThat(underTest.getBytesWritten(), is(1L));
    }

    @Test
    public void writeBytes() throws IOException {
        CountingServletOutputStream underTest = new CountingServletOutputStream(mock(ServletOutputStream.class));
        underTest.write("foobar".getBytes());

        assertThat(underTest.getBytesWritten(), is(6L));
    }

    @Test
    public void writeBytesOffsetAndLength() throws IOException {
        CountingServletOutputStream underTest = new CountingServletOutputStream(mock(ServletOutputStream.class));
        underTest.write("foobar".getBytes(), 1, 5);

        assertThat(underTest.getBytesWritten(), is(5L));
    }
}