View Javadoc
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    *      https://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  package org.apache.commons.io.input;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.io.ByteArrayInputStream;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.util.UUID;
28  
29  import org.apache.commons.io.TaggedIOException;
30  import org.junit.jupiter.api.Test;
31  
32  /**
33   * Tests {@link TaggedInputStream}.
34   */
35  class TaggedInputStreamTest  {
36  
37      @Test
38      void testBrokenStream() {
39          final IOException exception = new IOException("test exception");
40          final TaggedInputStream stream =
41              new TaggedInputStream(new BrokenInputStream(exception));
42  
43          // Test the available() method
44          final IOException exceptionAvailable = assertThrows(IOException.class, stream::available);
45          assertTrue(stream.isCauseOf(exceptionAvailable));
46          final IOException exceptionAvailableCause = assertThrows(IOException.class, () -> stream.throwIfCauseOf(exceptionAvailable));
47          assertEquals(exception, exceptionAvailableCause);
48  
49          // Test the read() method
50          final IOException exceptionRead = assertThrows(IOException.class, stream::read);
51          assertTrue(stream.isCauseOf(exceptionRead));
52          final IOException exceptionReadCause = assertThrows(IOException.class, () -> stream.throwIfCauseOf(exceptionRead));
53          assertEquals(exception, exceptionReadCause);
54  
55          // Test the close() method
56          final IOException exceptionClose = assertThrows(IOException.class, stream::close);
57          assertTrue(stream.isCauseOf(exceptionClose));
58          final IOException exceptionCloseCause = assertThrows(IOException.class, () -> stream.throwIfCauseOf(exceptionClose));
59          assertEquals(exception, exceptionCloseCause);
60      }
61  
62      @SuppressWarnings({ "resource" })
63      @Test
64      void testCloseHandleIOException() throws IOException {
65          ProxyInputStreamTest.testCloseHandleIOException(new TaggedInputStream(new BrokenInputStream((Throwable) new IOException())));
66      }
67  
68      @Test
69      void testEmptyStream() throws IOException {
70          try (InputStream stream = new TaggedInputStream(ClosedInputStream.INSTANCE)) {
71              assertEquals(0, stream.available());
72              assertEquals(-1, stream.read());
73              assertEquals(-1, stream.read(new byte[1]));
74              assertEquals(-1, stream.read(new byte[1], 0, 1));
75          }
76      }
77  
78      @Test
79      void testNormalStream() throws IOException {
80          try (InputStream stream = new TaggedInputStream(new ByteArrayInputStream(new byte[] {'a', 'b', 'c'}))) {
81              assertEquals(3, stream.available());
82              assertEquals('a', stream.read());
83              final byte[] buffer = new byte[1];
84              assertEquals(1, stream.read(buffer));
85              assertEquals('b', buffer[0]);
86              assertEquals(1, stream.read(buffer, 0, 1));
87              assertEquals('c', buffer[0]);
88              assertEquals(-1, stream.read());
89          }
90      }
91  
92      @Test
93      void testOtherException() throws Exception {
94          final IOException exception = new IOException("test exception");
95          try (TaggedInputStream stream = new TaggedInputStream(ClosedInputStream.INSTANCE)) {
96  
97              assertFalse(stream.isCauseOf(exception));
98              assertFalse(stream.isCauseOf(new TaggedIOException(exception, UUID.randomUUID())));
99  
100             stream.throwIfCauseOf(exception);
101 
102             stream.throwIfCauseOf(new TaggedIOException(exception, UUID.randomUUID()));
103         }
104     }
105 
106 }