package aalgTest;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.junit.Assert;
import org.junit.Test;

import aalg.crypto.IInputStreamFilter;
import aalg.crypto.IOutputStreamFilter;

public abstract class PipeTest {
	private static final int MIN_SIZE = 0;
	private static final int MAX_SIZE = 1024;
	
	@Test
	public void test() throws IOException {
		for (int size = MIN_SIZE; size <= MAX_SIZE; size++) {
			final ByteArrayOutputStream processedOutputStream = new ByteArrayOutputStream();
			final IOutputStreamFilter outputFilter = getOutputFilter(processedOutputStream);
			
			for (int i = 0; i < size; i++)
				outputFilter.getSourceStream().write(i);
			
			flushOutputFilter(outputFilter);
			
			final byte[] paddedData = processedOutputStream.toByteArray();
			final ByteArrayInputStream processedInputStream = new ByteArrayInputStream(paddedData);
			final IInputStreamFilter inputFilter = getInputFilter(processedInputStream);
			
			for (int i = 0; i < size; i++) {
				final int b = inputFilter.getTargetStream().read();
				Assert.assertEquals(i & 0xFF, b & 0xFF);
			}
			
			final int b = inputFilter.getTargetStream().read();
			Assert.assertEquals(-1, b);
			
			flushInputFilter(inputFilter);
		}
		
		
	}

	protected abstract IInputStreamFilter getInputFilter(final ByteArrayInputStream inputStream) throws IOException;
	protected abstract void flushInputFilter(final IInputStreamFilter filter) throws IOException;

	protected abstract IOutputStreamFilter getOutputFilter(final ByteArrayOutputStream outputStream) throws IOException;
	protected abstract void flushOutputFilter(final IOutputStreamFilter filter) throws IOException;
}
