TDD: IOHelper.copyStream()
This commit is contained in:
Родитель
481eaac086
Коммит
a5fdb6f080
|
@ -10,8 +10,12 @@ import java.io.FileInputStream;
|
|||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
|
||||
public class IOHelper {
|
||||
|
||||
static final int BUFFER_SIZE = 4096;
|
||||
|
||||
public static void closeQuietly(final Closeable closeable) {
|
||||
if (closeable != null) {
|
||||
try {
|
||||
|
@ -50,4 +54,13 @@ public class IOHelper {
|
|||
IOHelper.closeQuietly(isr);
|
||||
}
|
||||
}
|
||||
|
||||
public static void copyStream(final InputStream is, final OutputStream os) throws IOException {
|
||||
final byte[] buffer = new byte[BUFFER_SIZE];
|
||||
int bytesRead;
|
||||
while ((bytesRead = is.read(buffer, 0, BUFFER_SIZE)) != -1) {
|
||||
os.write(buffer, 0, bytesRead);
|
||||
}
|
||||
os.flush();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,11 +4,70 @@
|
|||
package com.microsoft.alm.helpers;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* A class to test {@link IOHelper}.
|
||||
*/
|
||||
public class IOHelperTest {
|
||||
|
||||
private static byte[] createRandomByteArray(final int numberOfBytes) {
|
||||
final Random random = new Random(42);
|
||||
final byte[] result = new byte[numberOfBytes];
|
||||
random.nextBytes(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void testCopyStream(final int numberOfBytes) throws IOException {
|
||||
final byte[] input = createRandomByteArray(numberOfBytes);
|
||||
final ByteArrayInputStream bais = new ByteArrayInputStream(input);
|
||||
final ByteArrayOutputStream baos = new ByteArrayOutputStream(numberOfBytes);
|
||||
|
||||
IOHelper.copyStream(bais, baos);
|
||||
|
||||
final byte[] actual = baos.toByteArray();
|
||||
Assert.assertArrayEquals(input, actual);
|
||||
Assert.assertEquals(numberOfBytes, baos.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void copyStream_zeroBuffer() throws Exception {
|
||||
testCopyStream(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void copyStream_oneByte() throws Exception {
|
||||
testCopyStream(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void copyStream_halfSmallerThanBuffer() throws Exception {
|
||||
testCopyStream(IOHelper.BUFFER_SIZE / 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void copyStream_justSmallerThanBuffer() throws Exception {
|
||||
testCopyStream(IOHelper.BUFFER_SIZE - 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void copyStream_sameAsBuffer() throws Exception {
|
||||
testCopyStream(IOHelper.BUFFER_SIZE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void copyStream_oneMoreThanBuffer() throws Exception {
|
||||
testCopyStream(IOHelper.BUFFER_SIZE + 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void copyStream_twiceMoreThanBuffer() throws Exception {
|
||||
testCopyStream(IOHelper.BUFFER_SIZE * 2);
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче