summaryrefslogtreecommitdiff
path: root/harmony-tests
diff options
context:
space:
mode:
authorDaulet Zhanguzin <dauletz@google.com>2019-12-09 12:53:17 +0000
committerDaulet Zhanguzin <dauletz@google.com>2019-12-13 14:43:22 +0000
commit2f646b788026376779df085bb750d1cfa542fffa (patch)
tree741a07aa38566409f0d35f135927c6977f64ca9c /harmony-tests
parentdb54a53f8151339fd3f222e65ce6bd5c38901b4c (diff)
Update java.io.FilterOutputStream#close test.
Accidentally missed from https://android-review.googlesource.com/c/platform/libcore/+/1182688 Bug: 122733269 Test: atest CtsLibcoreTestCases:org.apache.harmony.tests.java.io.FilterOutputStreamTest Test: atest CtsLibcoreTestCases:libcore.java.io.OldFilterOutputStreamTest Test: new CtsLibcoreTestCases:org.apache.harmony.tests.java.io.FilterOutputStreamTest#test_close_flushthrows fails in previous implementation Change-Id: I1755e9defa284261e71c970d9f8b6e454c426b65
Diffstat (limited to 'harmony-tests')
-rw-r--r--harmony-tests/src/test/java/org/apache/harmony/tests/java/io/FilterOutputStreamTest.java58
1 files changed, 21 insertions, 37 deletions
diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/io/FilterOutputStreamTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/io/FilterOutputStreamTest.java
index cc5b91b771..2ba710b42c 100644
--- a/harmony-tests/src/test/java/org/apache/harmony/tests/java/io/FilterOutputStreamTest.java
+++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/io/FilterOutputStreamTest.java
@@ -17,14 +17,16 @@
package org.apache.harmony.tests.java.io;
+import junit.framework.TestCase;
+
+import org.mockito.Mockito;
+
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
-import junit.framework.TestCase;
-
public class FilterOutputStreamTest extends TestCase {
private OutputStream os;
@@ -59,44 +61,26 @@ public class FilterOutputStreamTest extends TestCase {
}
/**
- * java.io.FilterOutputStream#close_flushthrows()
+ * java.io.FilterOutputStream#close()
*/
- public void test_close_flushthrows() throws IOException {
- class FakeOutputStream extends OutputStream {
-
- public int closedCount;
-
- @Override
- public void close() throws IOException {
- super.close();
- closedCount++;
- }
-
- @Override
- public void flush() throws IOException {
- super.flush();
- throw new IOException("FakeOutputStream#flush() mocked exception");
- }
-
- @Override
- public void write(int b) throws IOException {
- }
- }
-
- FakeOutputStream fos = new FakeOutputStream();
- os = new FilterOutputStream(fos);
- os.write(42);
-
+ public void test_doubleClose() throws IOException {
+ OutputStream outputStream = Mockito.mock(OutputStream.class);
+ IOException testException = new IOException("test exception");
+ Mockito.doThrow(testException).when(outputStream).flush();
+ FilterOutputStream filterOutputStream = new FilterOutputStream(outputStream);
+ // FilterOutputStream.close() flushes and closes the underlying stream.
try {
- os.close();
+ filterOutputStream.close();
+ fail();
+ } catch (IOException expected) {
+ assertEquals(testException, expected);
}
- catch (IOException e) {
- }
- assertEquals("Underlying stream's close() called even if flush() throws", 1,
- fos.closedCount);
-
- os.close();
- assertEquals("Underlying stream's close() called exactly once", 1, fos.closedCount);
+ Mockito.verify(outputStream, Mockito.times(1)).flush();
+ Mockito.verify(outputStream, Mockito.times(1)).close();
+ // A second close() does not delegate to the already-closed underlying stream again.
+ filterOutputStream.close();
+ Mockito.verify(outputStream, Mockito.times(1)).flush();
+ Mockito.verify(outputStream, Mockito.times(1)).close();
}
/**