|
@@ -73,6 +73,36 @@ public class TestIOUtils {
|
|
|
Mockito.verify(outputStream, Mockito.atLeastOnce()).close();
|
|
|
}
|
|
|
|
|
|
+ @Test
|
|
|
+ public void testCopyBytesShouldCloseInputSteamWhenOutputStreamCloseThrowsRunTimeException()
|
|
|
+ throws Exception {
|
|
|
+ InputStream inputStream = Mockito.mock(InputStream.class);
|
|
|
+ OutputStream outputStream = Mockito.mock(OutputStream.class);
|
|
|
+ Mockito.doReturn(-1).when(inputStream).read(new byte[1]);
|
|
|
+ Mockito.doThrow(new RuntimeException()).when(outputStream).close();
|
|
|
+ try {
|
|
|
+ IOUtils.copyBytes(inputStream, outputStream, 1, true);
|
|
|
+ fail("Didn't throw exception");
|
|
|
+ } catch (RuntimeException e) {
|
|
|
+ }
|
|
|
+ Mockito.verify(outputStream, Mockito.atLeastOnce()).close();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testCopyBytesShouldCloseInputSteamWhenInputStreamCloseThrowsRunTimeException()
|
|
|
+ throws Exception {
|
|
|
+ InputStream inputStream = Mockito.mock(InputStream.class);
|
|
|
+ OutputStream outputStream = Mockito.mock(OutputStream.class);
|
|
|
+ Mockito.doReturn(-1).when(inputStream).read(new byte[1]);
|
|
|
+ Mockito.doThrow(new RuntimeException()).when(inputStream).close();
|
|
|
+ try {
|
|
|
+ IOUtils.copyBytes(inputStream, outputStream, 1, true);
|
|
|
+ fail("Didn't throw exception");
|
|
|
+ } catch (RuntimeException e) {
|
|
|
+ }
|
|
|
+ Mockito.verify(inputStream, Mockito.atLeastOnce()).close();
|
|
|
+ }
|
|
|
+
|
|
|
@Test
|
|
|
public void testCopyBytesShouldNotCloseStreamsWhenCloseIsFalse()
|
|
|
throws Exception {
|
|
@@ -83,7 +113,7 @@ public class TestIOUtils {
|
|
|
Mockito.verify(inputStream, Mockito.atMost(0)).close();
|
|
|
Mockito.verify(outputStream, Mockito.atMost(0)).close();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
@Test
|
|
|
public void testCopyBytesWithCountShouldCloseStreamsWhenCloseIsTrue()
|
|
|
throws Exception {
|
|
@@ -124,7 +154,7 @@ public class TestIOUtils {
|
|
|
Mockito.verify(inputStream, Mockito.atLeastOnce()).close();
|
|
|
Mockito.verify(outputStream, Mockito.atLeastOnce()).close();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
@Test
|
|
|
public void testWriteFully() throws IOException {
|
|
|
final int INPUT_BUFFER_LEN = 10000;
|
|
@@ -155,6 +185,7 @@ public class TestIOUtils {
|
|
|
for (int i = HALFWAY; i < input.length; i++) {
|
|
|
assertEquals(input[i - HALFWAY], output[i]);
|
|
|
}
|
|
|
+ raf.close();
|
|
|
} finally {
|
|
|
File f = new File(TEST_FILE_NAME);
|
|
|
if (f.exists()) {
|
|
@@ -184,7 +215,7 @@ public class TestIOUtils {
|
|
|
"Error while reading compressed data", ioe);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
@Test
|
|
|
public void testSkipFully() throws IOException {
|
|
|
byte inArray[] = new byte[] {0, 1, 2, 3, 4};
|