|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.commons.io.input; |
| 19 | + |
| 20 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 22 | + |
| 23 | +import java.io.BufferedInputStream; |
| 24 | +import java.io.IOException; |
| 25 | +import java.io.UncheckedIOException; |
| 26 | + |
| 27 | +import org.apache.commons.io.IOUtils; |
| 28 | +import org.junit.jupiter.api.BeforeEach; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | + |
| 31 | +/** |
| 32 | + * Tests {@link UncheckedFilterInputStream}. |
| 33 | + */ |
| 34 | +public class UncheckedFilterInputStreamTest { |
| 35 | + |
| 36 | + private UncheckedFilterInputStream stringInputStream; |
| 37 | + private UncheckedFilterInputStream brokenInputStream; |
| 38 | + private IOException exception = new IOException("test exception"); |
| 39 | + |
| 40 | + @SuppressWarnings("resource") |
| 41 | + @BeforeEach |
| 42 | + public void beforeEach() { |
| 43 | + stringInputStream = UncheckedFilterInputStream.on(new BufferedInputStream(new StringInputStream("01"))); |
| 44 | + exception = new IOException("test exception"); |
| 45 | + brokenInputStream = UncheckedFilterInputStream.on(new BrokenInputStream(exception)); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testClose() { |
| 50 | + stringInputStream.close(); |
| 51 | + assertThrows(UncheckedIOException.class, () -> brokenInputStream.read()); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testCloseThrows() { |
| 56 | + assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenInputStream.close()).getCause()); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testMarkReset() { |
| 61 | + stringInputStream.mark(10); |
| 62 | + final int c = stringInputStream.read(); |
| 63 | + stringInputStream.reset(); |
| 64 | + assertEquals(c, stringInputStream.read()); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testRead() { |
| 69 | + try (final UncheckedFilterInputStream uncheckedReader = UncheckedFilterInputStream.on(stringInputStream)) { |
| 70 | + assertEquals('0', uncheckedReader.read()); |
| 71 | + assertEquals('1', uncheckedReader.read()); |
| 72 | + assertEquals(IOUtils.EOF, uncheckedReader.read()); |
| 73 | + assertEquals(IOUtils.EOF, uncheckedReader.read()); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testReadByteArray() { |
| 79 | + try (final UncheckedFilterInputStream uncheckedReader = UncheckedFilterInputStream.on(stringInputStream)) { |
| 80 | + final byte[] array = new byte[1]; |
| 81 | + assertEquals(1, uncheckedReader.read(array)); |
| 82 | + assertEquals('0', array[0]); |
| 83 | + array[0] = 0; |
| 84 | + assertEquals(1, uncheckedReader.read(array)); |
| 85 | + assertEquals('1', array[0]); |
| 86 | + array[0] = 0; |
| 87 | + assertEquals(IOUtils.EOF, uncheckedReader.read(array)); |
| 88 | + assertEquals(0, array[0]); |
| 89 | + assertEquals(IOUtils.EOF, uncheckedReader.read(array)); |
| 90 | + assertEquals(0, array[0]); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void testReadByteArrayIndexed() { |
| 96 | + try (final UncheckedFilterInputStream uncheckedReader = UncheckedFilterInputStream.on(stringInputStream)) { |
| 97 | + final byte[] array = new byte[1]; |
| 98 | + assertEquals(1, uncheckedReader.read(array, 0, 1)); |
| 99 | + assertEquals('0', array[0]); |
| 100 | + array[0] = 0; |
| 101 | + assertEquals(1, uncheckedReader.read(array, 0, 1)); |
| 102 | + assertEquals('1', array[0]); |
| 103 | + array[0] = 0; |
| 104 | + assertEquals(IOUtils.EOF, uncheckedReader.read(array, 0, 1)); |
| 105 | + assertEquals(0, array[0]); |
| 106 | + assertEquals(IOUtils.EOF, uncheckedReader.read(array, 0, 1)); |
| 107 | + assertEquals(0, array[0]); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void testReadByteArrayIndexedThrows() { |
| 113 | + assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenInputStream.read(new byte[1], 0, 1)).getCause()); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void testReadByteArrayThrows() { |
| 118 | + assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenInputStream.read(new byte[1])).getCause()); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void testReadThrows() { |
| 123 | + assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenInputStream.read()).getCause()); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testResetThrows() { |
| 128 | + try (UncheckedFilterInputStream closedReader = UncheckedFilterInputStream.on(ClosedInputStream.CLOSED_INPUT_STREAM)) { |
| 129 | + closedReader.close(); |
| 130 | + assertThrows(UncheckedIOException.class, () -> brokenInputStream.reset()); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void testSkip() { |
| 136 | + assertEquals(1, stringInputStream.skip(1)); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + public void testSkipThrows() { |
| 141 | + assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenInputStream.skip(1)).getCause()); |
| 142 | + } |
| 143 | + |
| 144 | +} |
0 commit comments