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