package com.atlassian.stash.internal.plugin.readme;

import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

import java.nio.charset.Charset;

import static org.junit.Assert.assertEquals;


public class MaxSizeByteArrayOutputStreamTest {

    public static final int INCREMENTAL_BUFFER = 8192;
    public static final int MAX_SIZE = 131072;
    public static final int CONTENT_SIZE = 106496;

    @Test
    public void testWriteLargeStringAboveLimit() throws Exception {
        MaxSizeByteArrayOutputStream stream = new MaxSizeByteArrayOutputStream(512, MAX_SIZE);
        for (int count = 0; count < CONTENT_SIZE; count++) {
            String largeString = StringUtils.repeat("a", INCREMENTAL_BUFFER);
            stream.write(largeString.getBytes(Charset.forName("UTF-8")));
            count += INCREMENTAL_BUFFER;
        }
        assertEquals(CONTENT_SIZE, stream.size());
    }
}
