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.assertNotNull;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  
23  import java.io.IOException;
24  import java.io.Reader;
25  import java.nio.CharBuffer;
26  
27  import org.junit.jupiter.api.Test;
28  import org.junit.jupiter.params.ParameterizedTest;
29  import org.junit.jupiter.params.provider.MethodSource;
30  
31  /**
32   * Tests {@link BrokenReader}.
33   */
34  class BrokenReaderTest {
35  
36      private static BrokenReader createBrokenReader(final Throwable exception) {
37          if (exception instanceof IOException) {
38              return new BrokenReader((IOException) exception);
39          }
40          return new BrokenReader(exception);
41      }
42  
43      @ParameterizedTest
44      @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
45      void testClose(final Class<Exception> clazz) throws Exception {
46          final Throwable exception = clazz.newInstance();
47          @SuppressWarnings("resource")
48          final BrokenReader brokenReader = createBrokenReader(exception);
49          assertEquals(exception, assertThrows(clazz, brokenReader::close));
50      }
51  
52      @Test
53      void testInstance() {
54          assertNotNull(BrokenReader.INSTANCE);
55      }
56  
57      @ParameterizedTest
58      @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
59      void testMark(final Class<Throwable> clazz) throws Exception {
60          final Throwable exception = clazz.newInstance();
61          @SuppressWarnings("resource")
62          final BrokenReader brokenReader = createBrokenReader(exception);
63          assertEquals(exception, assertThrows(clazz, () -> brokenReader.mark(1)));
64      }
65  
66      @ParameterizedTest
67      @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
68      void testRead(final Class<Throwable> clazz) throws Exception {
69          final Throwable exception = clazz.newInstance();
70          @SuppressWarnings("resource")
71          final BrokenReader brokenReader = createBrokenReader(exception);
72          assertEquals(exception, assertThrows(clazz, brokenReader::read));
73      }
74  
75      @ParameterizedTest
76      @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
77      void testReadCharArray(final Class<Throwable> clazz) throws Exception {
78          final Throwable exception = clazz.newInstance();
79          @SuppressWarnings("resource")
80          final BrokenReader brokenReader = createBrokenReader(exception);
81          assertEquals(exception, assertThrows(clazz, () -> brokenReader.read(new char[1])));
82      }
83  
84      @ParameterizedTest
85      @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
86      void testReadCharArrayIndexed(final Class<Throwable> clazz) throws Exception {
87          final Throwable exception = clazz.newInstance();
88          @SuppressWarnings("resource")
89          final BrokenReader brokenReader = createBrokenReader(exception);
90          final char[] cbuf = new char[1];
91          assertEquals(exception, assertThrows(clazz, () -> brokenReader.read(cbuf, 0, 1)));
92          // Also throws the exception before checking arguments
93          assertEquals(exception, assertThrows(clazz, () -> brokenReader.read(cbuf, -1, 1)));
94          assertEquals(exception, assertThrows(clazz, () -> brokenReader.read(cbuf, 0, -1)));
95          assertEquals(exception, assertThrows(clazz, () -> brokenReader.read(cbuf, 1, 1)));
96          assertEquals(exception, assertThrows(clazz, () -> brokenReader.read(null, 0, 0)));
97  
98      }
99  
100     @ParameterizedTest
101     @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
102     void testReadCharBuffer(final Class<Throwable> clazz) throws Exception {
103         final Throwable exception = clazz.newInstance();
104         @SuppressWarnings("resource")
105         final BrokenReader brokenReader = createBrokenReader(exception);
106         final CharBuffer charBuffer = CharBuffer.allocate(1);
107         assertEquals(exception, assertThrows(clazz, () -> brokenReader.read(charBuffer)));
108     }
109 
110     @ParameterizedTest
111     @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
112     void testReady(final Class<Throwable> clazz) throws Exception {
113         final Throwable exception = clazz.newInstance();
114         @SuppressWarnings("resource")
115         final BrokenReader brokenReader = createBrokenReader(exception);
116         assertEquals(exception, assertThrows(clazz, brokenReader::ready));
117     }
118 
119     @ParameterizedTest
120     @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
121     void testReset(final Class<Throwable> clazz) throws Exception {
122         final Throwable exception = clazz.newInstance();
123         @SuppressWarnings("resource")
124         final BrokenReader brokenReader = createBrokenReader(exception);
125         assertEquals(exception, assertThrows(clazz, brokenReader::reset));
126     }
127 
128     @ParameterizedTest
129     @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
130     void testSkip(final Class<Throwable> clazz) throws Exception {
131         final Throwable exception = clazz.newInstance();
132         @SuppressWarnings("resource")
133         final BrokenReader brokenReader = createBrokenReader(exception);
134         assertEquals(exception, assertThrows(clazz, () -> brokenReader.skip(1)));
135     }
136 
137     @Test
138     void testTryWithResources() {
139         final IOException thrown = assertThrows(IOException.class, () -> {
140             try (Reader newReader = new BrokenReader()) {
141                 newReader.read();
142             }
143         });
144         assertEquals("Broken reader", thrown.getMessage());
145 
146         final Throwable[] suppressed = thrown.getSuppressed();
147         assertEquals(1, suppressed.length);
148         assertEquals(IOException.class, suppressed[0].getClass());
149         assertEquals("Broken reader", suppressed[0].getMessage());
150     }
151 
152 }