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