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    *      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  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  public class TaggedInputStreamTest  {
36  
37      @Test
38      public 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      @Test
63      public void testEmptyStream() throws IOException {
64          try (InputStream stream = new TaggedInputStream(ClosedInputStream.INSTANCE)) {
65              assertEquals(0, stream.available());
66              assertEquals(-1, stream.read());
67              assertEquals(-1, stream.read(new byte[1]));
68              assertEquals(-1, stream.read(new byte[1], 0, 1));
69          }
70      }
71  
72      @Test
73      public void testNormalStream() throws IOException {
74          try (InputStream stream = new TaggedInputStream(new ByteArrayInputStream(new byte[] {'a', 'b', 'c'}))) {
75              assertEquals(3, stream.available());
76              assertEquals('a', stream.read());
77              final byte[] buffer = new byte[1];
78              assertEquals(1, stream.read(buffer));
79              assertEquals('b', buffer[0]);
80              assertEquals(1, stream.read(buffer, 0, 1));
81              assertEquals('c', buffer[0]);
82              assertEquals(-1, stream.read());
83          }
84      }
85  
86      @Test
87      public void testOtherException() throws Exception {
88          final IOException exception = new IOException("test exception");
89          try (TaggedInputStream stream = new TaggedInputStream(ClosedInputStream.INSTANCE)) {
90  
91              assertFalse(stream.isCauseOf(exception));
92              assertFalse(stream.isCauseOf(new TaggedIOException(exception, UUID.randomUUID())));
93  
94              stream.throwIfCauseOf(exception);
95  
96              stream.throwIfCauseOf(new TaggedIOException(exception, UUID.randomUUID()));
97          }
98      }
99  
100 }