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.output;
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.OutputStream;
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 BrokenOutputStream}.
32   */
33  class BrokenOutputStreamTest {
34      private static BrokenOutputStream createBrokenOutputStream(final Throwable exception) {
35          if (exception instanceof IOException) {
36              return new BrokenOutputStream((IOException) exception);
37          }
38          return new BrokenOutputStream(exception);
39      }
40  
41      @ParameterizedTest
42      @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
43      void testClose(final Class<Throwable> clazz) throws Exception {
44          final Throwable exception = clazz.newInstance();
45          @SuppressWarnings("resource")
46          final BrokenOutputStream stream = createBrokenOutputStream(exception);
47          assertEquals(exception, assertThrows(clazz, () -> stream.close()));
48      }
49  
50      @ParameterizedTest
51      @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
52      void testFlush(final Class<Throwable> clazz) throws Exception {
53          final Throwable exception = clazz.newInstance();
54          @SuppressWarnings("resource")
55          final BrokenOutputStream stream = createBrokenOutputStream(exception);
56          assertEquals(exception, assertThrows(clazz, () -> stream.flush()));
57      }
58  
59      @Test
60      void testInstance() {
61          assertNotNull(BrokenOutputStream.INSTANCE);
62      }
63  
64      @Test
65      void testTryWithResources() {
66          final IOException thrown = assertThrows(IOException.class, () -> {
67              try (OutputStream newStream = new BrokenOutputStream()) {
68                  newStream.write(1);
69              }
70          });
71          assertEquals("Broken output stream: write(int)", thrown.getMessage());
72          final Throwable[] suppressed = thrown.getSuppressed();
73          assertEquals(1, suppressed.length);
74          assertEquals(IOException.class, suppressed[0].getClass());
75          assertEquals("Broken output stream: close()", suppressed[0].getMessage());
76      }
77  
78      @ParameterizedTest
79      @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
80      void testWriteByteArray(final Class<Throwable> clazz) throws Exception {
81          final Throwable exception = clazz.newInstance();
82          @SuppressWarnings("resource")
83          final BrokenOutputStream stream = createBrokenOutputStream(exception);
84          assertEquals(exception, assertThrows(clazz, () -> stream.write(new byte[1])));
85      }
86  
87      @ParameterizedTest
88      @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
89      void testWriteByteArrayIndexed(final Class<Throwable> clazz) throws Exception {
90          final Throwable exception = clazz.newInstance();
91          @SuppressWarnings("resource")
92          final BrokenOutputStream stream = createBrokenOutputStream(exception);
93          assertEquals(exception, assertThrows(clazz, () -> stream.write(new byte[1], 0, 1)));
94      }
95  
96      @ParameterizedTest
97      @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
98      void testWriteInt(final Class<Throwable> clazz) throws Exception {
99          final Throwable exception = clazz.newInstance();
100         @SuppressWarnings("resource")
101         final BrokenOutputStream stream = createBrokenOutputStream(exception);
102         assertEquals(exception, assertThrows(clazz, () -> stream.write(1)));
103     }
104 }