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 java.io.ByteArrayInputStream;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.util.UUID;
23  
24  import org.apache.commons.io.TaggedIOException;
25  import org.junit.Test;
26  
27  import static org.junit.Assert.assertEquals;
28  import static org.junit.Assert.assertFalse;
29  import static org.junit.Assert.assertTrue;
30  import static org.junit.Assert.fail;
31  
32  /**
33   * JUnit Test Case for {@link TaggedInputStream}.
34   */
35  public class TaggedInputStreamTest  {
36  
37      @Test
38      public void testEmptyStream() throws IOException {
39          final InputStream stream = new TaggedInputStream(new ClosedInputStream());
40          assertEquals(0, stream.available());
41          assertEquals(-1, stream.read());
42          assertEquals(-1, stream.read(new byte[1]));
43          assertEquals(-1, stream.read(new byte[1], 0, 1));
44          stream.close();
45      }
46  
47      @Test
48      public void testNormalStream() throws IOException {
49          final InputStream stream = new TaggedInputStream(
50                  new ByteArrayInputStream(new byte[] { 'a', 'b', 'c' }));
51          assertEquals(3, stream.available());
52          assertEquals('a', stream.read());
53          final byte[] buffer = new byte[1];
54          assertEquals(1, stream.read(buffer));
55          assertEquals('b', buffer[0]);
56          assertEquals(1, stream.read(buffer, 0, 1));
57          assertEquals('c', buffer[0]);
58          assertEquals(-1, stream.read());
59          stream.close();
60      }
61  
62      @Test
63      public void testBrokenStream() {
64          final IOException exception = new IOException("test exception");
65          final TaggedInputStream stream =
66              new TaggedInputStream(new BrokenInputStream(exception));
67  
68          // Test the available() method
69          try {
70              stream.available();
71              fail("Expected exception not thrown.");
72          } catch (final IOException e) {
73              assertTrue(stream.isCauseOf(e));
74              try {
75                  stream.throwIfCauseOf(e);
76                  fail("Expected exception not thrown.");
77              } catch (final IOException e2) {
78                  assertEquals(exception, e2);
79              }
80          }
81  
82          // Test the read() method
83          try {
84              stream.read();
85              fail("Expected exception not thrown.");
86          } catch (final IOException e) {
87              assertTrue(stream.isCauseOf(e));
88              try {
89                  stream.throwIfCauseOf(e);
90                  fail("Expected exception not thrown.");
91              } catch (final IOException e2) {
92                  assertEquals(exception, e2);
93              }
94          }
95  
96          // Test the close() method
97          try {
98              stream.close();
99              fail("Expected exception not thrown.");
100         } catch (final IOException e) {
101             assertTrue(stream.isCauseOf(e));
102             try {
103                 stream.throwIfCauseOf(e);
104                 fail("Expected exception not thrown.");
105             } catch (final IOException e2) {
106                 assertEquals(exception, e2);
107             }
108         }
109     }
110 
111     @Test
112     public void testOtherException() throws Exception {
113         final IOException exception = new IOException("test exception");
114         final InputStream closed = new ClosedInputStream();
115         final TaggedInputStream stream = new TaggedInputStream(closed);
116 
117         assertFalse(stream.isCauseOf(exception));
118         assertFalse(stream.isCauseOf(
119                 new TaggedIOException(exception, UUID.randomUUID())));
120 
121         stream.throwIfCauseOf(exception);
122 
123         stream.throwIfCauseOf(
124                     new TaggedIOException(exception, UUID.randomUUID()));
125         stream.close();
126     }
127 
128 }