xref: /aosp_15_r20/external/apache-commons-io/src/test/java/org/apache/commons/io/input/TaggedReaderTest.java (revision 0c4d7b72e49a04598d65c566f44504b95342d75a)
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.IOException;
25 import java.io.Reader;
26 import java.io.StringReader;
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 TaggedReader}.
34  */
35 public class TaggedReaderTest {
36 
37     @Test
testBrokenReader()38     public void testBrokenReader() {
39         final IOException exception = new IOException("test exception");
40         final TaggedReader reader = new TaggedReader(new BrokenReader(exception));
41 
42         // Test the ready() method
43         final IOException readyException = assertThrows(IOException.class, reader::ready);
44         assertTrue(reader.isCauseOf(readyException));
45         final IOException rethrownReadyException = assertThrows(IOException.class, () -> reader.throwIfCauseOf(readyException));
46         assertEquals(exception, rethrownReadyException);
47 
48         // Test the read() method
49         final IOException readException = assertThrows(IOException.class, reader::read);
50         assertTrue(reader.isCauseOf(readException));
51         final IOException rethrownReadException = assertThrows(IOException.class, () -> reader.throwIfCauseOf(readException));
52         assertEquals(exception, rethrownReadException);
53 
54         // Test the close() method
55         final IOException closeException = assertThrows(IOException.class, reader::close);
56         assertTrue(reader.isCauseOf(closeException));
57         final IOException rethrownCloseException = assertThrows(IOException.class, () -> reader.throwIfCauseOf(closeException));
58         assertEquals(exception, rethrownCloseException);
59     }
60 
61     @Test
testEmptyReader()62     public void testEmptyReader() throws IOException {
63         try (Reader reader = new TaggedReader(ClosedReader.INSTANCE)) {
64             assertFalse(reader.ready());
65             assertEquals(-1, reader.read());
66             assertEquals(-1, reader.read(new char[1]));
67             assertEquals(-1, reader.read(new char[1], 0, 1));
68         }
69     }
70 
71     @Test
testNormalReader()72     public void testNormalReader() throws IOException {
73         try (Reader reader = new TaggedReader(new StringReader("abc"))) {
74             assertTrue(reader.ready());
75             assertEquals('a', reader.read());
76             final char[] buffer = new char[1];
77             assertEquals(1, reader.read(buffer));
78             assertEquals('b', buffer[0]);
79             assertEquals(1, reader.read(buffer, 0, 1));
80             assertEquals('c', buffer[0]);
81             assertEquals(-1, reader.read());
82         }
83     }
84 
85     @Test
testOtherException()86     public void testOtherException() throws Exception {
87         final IOException exception = new IOException("test exception");
88         try (TaggedReader reader = new TaggedReader(ClosedReader.INSTANCE)) {
89 
90             assertFalse(reader.isCauseOf(exception));
91             assertFalse(reader.isCauseOf(new TaggedIOException(exception, UUID.randomUUID())));
92 
93             reader.throwIfCauseOf(exception);
94 
95             reader.throwIfCauseOf(new TaggedIOException(exception, UUID.randomUUID()));
96         }
97     }
98 
99 }
100