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