|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +package com.facebook.react.modules.network |
| 9 | + |
| 10 | +import java.io.ByteArrayOutputStream |
| 11 | +import java.io.IOException |
| 12 | +import java.io.OutputStream |
| 13 | +import org.assertj.core.api.Assertions.assertThat |
| 14 | +import org.junit.After |
| 15 | +import org.junit.Before |
| 16 | +import org.junit.Test |
| 17 | +import org.mockito.kotlin.any |
| 18 | +import org.mockito.kotlin.mock |
| 19 | +import org.mockito.kotlin.times |
| 20 | +import org.mockito.kotlin.verify |
| 21 | +import org.mockito.kotlin.whenever |
| 22 | + |
| 23 | +class CountingOutputStreamTest { |
| 24 | + |
| 25 | + private lateinit var baseStream: ByteArrayOutputStream |
| 26 | + private lateinit var countingStream: CountingOutputStream |
| 27 | + |
| 28 | + @Before |
| 29 | + fun setUp() { |
| 30 | + baseStream = ByteArrayOutputStream().apply { countingStream = CountingOutputStream(this) } |
| 31 | + } |
| 32 | + |
| 33 | + @After |
| 34 | + fun tearDown() { |
| 35 | + countingStream.close() |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + fun testWriteSingleByteIncrementsCount() { |
| 40 | + countingStream.write(0x01) |
| 41 | + |
| 42 | + assertThat(countingStream.count).isEqualTo(1) |
| 43 | + assertThat(baseStream.toByteArray()).containsExactly(0x01.toByte()) |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + fun testWriteMultipleBytesIndividuallyIncrementsCountCorrectly() { |
| 48 | + countingStream.apply { |
| 49 | + write(0x01) |
| 50 | + write(0x02) |
| 51 | + write(0x03) |
| 52 | + } |
| 53 | + |
| 54 | + assertThat(countingStream.count).isEqualTo(3) |
| 55 | + assertThat(baseStream.toByteArray()) |
| 56 | + .containsExactly(0x01.toByte(), 0x02.toByte(), 0x03.toByte()) |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + fun testWriteByteArrayIncrementsCountCorrectly() { |
| 61 | + byteArrayOf(0x01, 0x02, 0x03).apply { countingStream.write(this) } |
| 62 | + |
| 63 | + assertThat(countingStream.count).isEqualTo(3) |
| 64 | + assertThat(baseStream.toByteArray()).containsExactly(0x01, 0x02, 0x03) |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + fun testWriteByteArrayWithOffsetIncrementsCountCorrectly() { |
| 69 | + byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05).apply { countingStream.write(this, 1, 3) } |
| 70 | + |
| 71 | + assertThat(countingStream.count).isEqualTo(3) |
| 72 | + assertThat(baseStream.toByteArray()).containsExactly(0x02, 0x03, 0x04) |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + fun testWriteLargeByteArrayIncrementsCountCorrectly() { |
| 77 | + val largeByteArray = ByteArray(32768) { it.toByte() }.apply { countingStream.write(this) } |
| 78 | + |
| 79 | + assertThat(countingStream.count).isEqualTo(32768) |
| 80 | + assertThat(baseStream.toByteArray()).isEqualTo(largeByteArray) |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + fun testCloseCallsCloseOnUnderlyingStream() { |
| 85 | + val mockOutputStream = mock<OutputStream>().apply { CountingOutputStream(this).close() } |
| 86 | + |
| 87 | + verify(mockOutputStream, times(1)).close() |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + fun testCloseDoesNotCallFlush() { |
| 92 | + val mockOutputStream = mock<OutputStream>().apply { CountingOutputStream(this).close() } |
| 93 | + |
| 94 | + verify(mockOutputStream, times(1)).close() |
| 95 | + verify(mockOutputStream, times(0)).flush() |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + fun testWriteDoesNotCallFlushOnUnderlyingStream() { |
| 100 | + val mockOutputStream = mock<OutputStream>().apply { CountingOutputStream(this).write(0x01) } |
| 101 | + |
| 102 | + verify(mockOutputStream, times(1)).write(0x01) |
| 103 | + verify(mockOutputStream, times(0)).flush() |
| 104 | + } |
| 105 | + |
| 106 | + @Test(expected = IOException::class) |
| 107 | + fun testWriteThrowsIOExceptionWhenUnderlyingStreamFails() { |
| 108 | + val mockOutputStream = |
| 109 | + mock<OutputStream>().apply { |
| 110 | + whenever(write(any<Int>())).thenThrow(IOException("Write error")) |
| 111 | + } |
| 112 | + |
| 113 | + val stream = CountingOutputStream(mockOutputStream) |
| 114 | + stream.write(0x01) |
| 115 | + } |
| 116 | + |
| 117 | + @Test(expected = IOException::class) |
| 118 | + fun testWriteByteArrayThrowsIOExceptionWhenUnderlyingStreamFails() { |
| 119 | + val mockOutputStream = |
| 120 | + mock<OutputStream>().apply { |
| 121 | + whenever(write(any(), any(), any())).thenThrow(IOException("Write error")) |
| 122 | + } |
| 123 | + |
| 124 | + val stream = CountingOutputStream(mockOutputStream) |
| 125 | + stream.write(byteArrayOf(0x01, 0x02, 0x03)) |
| 126 | + } |
| 127 | +} |
0 commit comments