|
| 1 | +package com.bumptech.glide.util; |
| 2 | + |
| 3 | +import static com.google.common.truth.Truth.assertThat; |
| 4 | +import static org.junit.Assert.assertEquals; |
| 5 | +import static org.junit.Assert.assertNull; |
| 6 | +import static org.junit.Assert.assertThrows; |
| 7 | +import static org.junit.Assert.assertTrue; |
| 8 | + |
| 9 | +import java.io.ByteArrayInputStream; |
| 10 | +import java.io.IOException; |
| 11 | +import java.io.InputStream; |
| 12 | +import java.net.SocketTimeoutException; |
| 13 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 14 | +import org.junit.After; |
| 15 | +import org.junit.Before; |
| 16 | +import org.junit.Test; |
| 17 | +import org.junit.function.ThrowingRunnable; |
| 18 | +import org.junit.runner.RunWith; |
| 19 | +import org.junit.runners.JUnit4; |
| 20 | + |
| 21 | +@RunWith(JUnit4.class) |
| 22 | +public class ExceptionPassthroughInputStreamTest { |
| 23 | + |
| 24 | + private final InputStream validInputStream = |
| 25 | + new ByteArrayInputStream( |
| 26 | + new byte[] { |
| 27 | + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, |
| 28 | + }); |
| 29 | + private final InputStream throwingInputStream = new ExceptionThrowingInputStream(); |
| 30 | + private ExceptionPassthroughInputStream validWrapper; |
| 31 | + private ExceptionPassthroughInputStream throwingWrapper; |
| 32 | + |
| 33 | + @Before |
| 34 | + public void setUp() throws Exception { |
| 35 | + validWrapper = new ExceptionPassthroughInputStream(); |
| 36 | + validWrapper.setInputStream(validInputStream); |
| 37 | + throwingWrapper = new ExceptionPassthroughInputStream(); |
| 38 | + throwingWrapper.setInputStream(throwingInputStream); |
| 39 | + } |
| 40 | + |
| 41 | + @After |
| 42 | + public void tearDown() { |
| 43 | + ExceptionPassthroughInputStream.clearQueue(); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testReturnsWrappedAvailable() throws IOException { |
| 48 | + assertEquals(validInputStream.available(), validWrapper.available()); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testCallsCloseOnWrapped() throws IOException { |
| 53 | + ExceptionPassthroughInputStream wrapper = new ExceptionPassthroughInputStream(); |
| 54 | + final AtomicBoolean isClosed = new AtomicBoolean(); |
| 55 | + wrapper.setInputStream( |
| 56 | + new InputStream() { |
| 57 | + @Override |
| 58 | + public int read() { |
| 59 | + return 0; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void close() throws IOException { |
| 64 | + super.close(); |
| 65 | + isClosed.set(true); |
| 66 | + } |
| 67 | + }); |
| 68 | + wrapper.close(); |
| 69 | + assertThat(isClosed.get()).isTrue(); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testCallsMarkOnWrapped() throws IOException { |
| 74 | + int toMark = 5; |
| 75 | + validWrapper.mark(toMark); |
| 76 | + assertThat(validWrapper.read(new byte[5], 0, 5)).isEqualTo(5); |
| 77 | + validInputStream.reset(); |
| 78 | + assertThat(validInputStream.read()).isEqualTo(0); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testReturnsWrappedMarkSupported() { |
| 83 | + assertTrue(validWrapper.markSupported()); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void testCallsReadByteArrayOnWrapped() throws IOException { |
| 88 | + byte[] buffer = new byte[8]; |
| 89 | + assertEquals(buffer.length, validWrapper.read(buffer)); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testCallsReadArrayWithOffsetAndCountOnWrapped() throws IOException { |
| 94 | + int offset = 1; |
| 95 | + int count = 4; |
| 96 | + byte[] buffer = new byte[5]; |
| 97 | + |
| 98 | + assertEquals(count, validWrapper.read(buffer, offset, count)); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void testCallsReadOnWrapped() throws IOException { |
| 103 | + assertEquals(0, validWrapper.read()); |
| 104 | + assertEquals(1, validWrapper.read()); |
| 105 | + assertEquals(2, validWrapper.read()); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void testCallsResetOnWrapped() throws IOException { |
| 110 | + validWrapper.mark(5); |
| 111 | + assertThat(validWrapper.read()).isEqualTo(0); |
| 112 | + assertThat(validWrapper.read()).isEqualTo(1); |
| 113 | + validWrapper.reset(); |
| 114 | + assertThat(validWrapper.read()).isEqualTo(0); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void testCallsSkipOnWrapped() throws IOException { |
| 119 | + int toSkip = 5; |
| 120 | + assertThat(validWrapper.skip(toSkip)).isEqualTo(toSkip); |
| 121 | + assertThat(validWrapper.read()).isEqualTo(5); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void testCatchesExceptionOnRead() { |
| 126 | + SocketTimeoutException expected = |
| 127 | + assertThrows( |
| 128 | + SocketTimeoutException.class, |
| 129 | + new ThrowingRunnable() { |
| 130 | + @Override |
| 131 | + public void run() throws Throwable { |
| 132 | + throwingWrapper.read(); |
| 133 | + } |
| 134 | + }); |
| 135 | + assertEquals(expected, throwingWrapper.getException()); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + public void testCatchesExceptionOnReadBuffer() { |
| 140 | + SocketTimeoutException exception = |
| 141 | + assertThrows( |
| 142 | + SocketTimeoutException.class, |
| 143 | + new ThrowingRunnable() { |
| 144 | + @Override |
| 145 | + public void run() throws Throwable { |
| 146 | + throwingWrapper.read(new byte[1]); |
| 147 | + } |
| 148 | + }); |
| 149 | + assertEquals(exception, throwingWrapper.getException()); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + public void testCatchesExceptionOnReadBufferWithOffsetAndCount() { |
| 154 | + SocketTimeoutException exception = |
| 155 | + assertThrows( |
| 156 | + SocketTimeoutException.class, |
| 157 | + new ThrowingRunnable() { |
| 158 | + @Override |
| 159 | + public void run() throws Throwable { |
| 160 | + throwingWrapper.read(new byte[2], 1, 1); |
| 161 | + } |
| 162 | + }); |
| 163 | + assertEquals(exception, throwingWrapper.getException()); |
| 164 | + } |
| 165 | + |
| 166 | + @Test |
| 167 | + public void testCatchesExceptionOnSkip() { |
| 168 | + SocketTimeoutException exception = |
| 169 | + assertThrows( |
| 170 | + SocketTimeoutException.class, |
| 171 | + new ThrowingRunnable() { |
| 172 | + @Override |
| 173 | + public void run() throws Throwable { |
| 174 | + throwingWrapper.skip(100); |
| 175 | + } |
| 176 | + }); |
| 177 | + assertEquals(exception, throwingWrapper.getException()); |
| 178 | + } |
| 179 | + |
| 180 | + @Test |
| 181 | + public void testExceptionIsNotSetInitially() { |
| 182 | + assertNull(validWrapper.getException()); |
| 183 | + } |
| 184 | + |
| 185 | + @SuppressWarnings("ResultOfMethodCallIgnored") |
| 186 | + @Test |
| 187 | + public void testResetsExceptionToNullOnRelease() { |
| 188 | + assertThrows( |
| 189 | + SocketTimeoutException.class, |
| 190 | + new ThrowingRunnable() { |
| 191 | + @Override |
| 192 | + public void run() throws Throwable { |
| 193 | + throwingWrapper.read(); |
| 194 | + } |
| 195 | + }); |
| 196 | + throwingWrapper.release(); |
| 197 | + assertNull(validWrapper.getException()); |
| 198 | + } |
| 199 | + |
| 200 | + @Test |
| 201 | + public void testCanReleaseAnObtainFromPool() { |
| 202 | + validWrapper.release(); |
| 203 | + InputStream fromPool = ExceptionPassthroughInputStream.obtain(validInputStream); |
| 204 | + assertEquals(validWrapper, fromPool); |
| 205 | + } |
| 206 | + |
| 207 | + @Test |
| 208 | + public void testCanObtainNewStreamFromPool() throws IOException { |
| 209 | + InputStream fromPool = ExceptionPassthroughInputStream.obtain(validInputStream); |
| 210 | + int read = fromPool.read(); |
| 211 | + assertEquals(0, read); |
| 212 | + } |
| 213 | + |
| 214 | + private static final class ExceptionThrowingInputStream extends InputStream { |
| 215 | + @Override |
| 216 | + public int read() throws IOException { |
| 217 | + throw new SocketTimeoutException(); |
| 218 | + } |
| 219 | + } |
| 220 | +} |
0 commit comments