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.Writer;
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 BrokenWriter}.
32   */
33  class BrokenWriterTest {
34  
35      private static BrokenWriter createBrokenWriter(final Throwable exception) {
36          if (exception instanceof IOException) {
37              return new BrokenWriter((IOException) exception);
38          }
39          return new BrokenWriter(exception);
40      }
41  
42      @ParameterizedTest
43      @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
44      void testAppendChar(final Class<Exception> clazz) throws Exception {
45          final Throwable exception = clazz.newInstance();
46          @SuppressWarnings("resource")
47          final BrokenWriter brokenWriter = createBrokenWriter(exception);
48          assertEquals(exception, assertThrows(clazz, () -> brokenWriter.append('1')));
49      }
50  
51      @ParameterizedTest
52      @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
53      void testAppendCharSequence(final Class<Throwable> clazz) throws Exception {
54          final Throwable exception = clazz.newInstance();
55          @SuppressWarnings("resource")
56          final BrokenWriter brokenWriter = createBrokenWriter(exception);
57          assertEquals(exception, assertThrows(clazz, () -> brokenWriter.append("01")));
58          assertEquals(exception, assertThrows(clazz, () -> brokenWriter.append(null)));
59      }
60  
61      @ParameterizedTest
62      @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
63      void testAppendCharSequenceIndexed(final Class<Throwable> clazz) throws Exception {
64          final Throwable exception = clazz.newInstance();
65          @SuppressWarnings("resource")
66          final BrokenWriter brokenWriter = createBrokenWriter(exception);
67          assertEquals(exception, assertThrows(clazz, () -> brokenWriter.append("01", 0, 1)));
68          assertEquals(exception, assertThrows(clazz, () -> brokenWriter.append(null, 0, 4)));
69      }
70  
71      @ParameterizedTest
72      @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
73      void testClose(final Class<Throwable> clazz) throws Exception {
74          final Throwable exception = clazz.newInstance();
75          @SuppressWarnings("resource")
76          final BrokenWriter brokenWriter = createBrokenWriter(exception);
77          assertEquals(exception, assertThrows(clazz, brokenWriter::close));
78      }
79  
80      @ParameterizedTest
81      @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
82      void testFlush(final Class<Throwable> clazz) throws Exception {
83          final Throwable exception = clazz.newInstance();
84          @SuppressWarnings("resource")
85          final BrokenWriter brokenWriter = createBrokenWriter(exception);
86          assertEquals(exception, assertThrows(clazz, brokenWriter::flush));
87      }
88  
89      @Test
90      void testInstance() {
91          assertNotNull(BrokenWriter.INSTANCE);
92      }
93  
94      @Test
95      void testTryWithResources() {
96          final IOException thrown = assertThrows(IOException.class, () -> {
97              try (Writer newWriter = new BrokenWriter()) {
98                  newWriter.write(1);
99              }
100         });
101         assertEquals("Broken writer", thrown.getMessage());
102 
103         final Throwable[] suppressed = thrown.getSuppressed();
104         assertEquals(1, suppressed.length);
105         assertEquals(IOException.class, suppressed[0].getClass());
106         assertEquals("Broken writer", suppressed[0].getMessage());
107     }
108 
109     @ParameterizedTest
110     @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
111     void testWriteCharArray(final Class<Throwable> clazz) throws Exception {
112         final Throwable exception = clazz.newInstance();
113         @SuppressWarnings("resource")
114         final BrokenWriter brokenWriter = createBrokenWriter(exception);
115         assertEquals(exception, assertThrows(clazz, () -> brokenWriter.write(new char[1])));
116     }
117 
118     @ParameterizedTest
119     @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
120     void testWriteCharArrayIndexed(final Class<Throwable> clazz) throws Exception {
121         final Throwable exception = clazz.newInstance();
122         @SuppressWarnings("resource")
123         final BrokenWriter brokenWriter = createBrokenWriter(exception);
124         final char[] cbuf = new char[1];
125         assertEquals(exception, assertThrows(clazz, () -> brokenWriter.write(cbuf, 0, 1)));
126         // Verify that the exception is thrown before checking the parameters.
127         assertEquals(exception, assertThrows(clazz, () -> brokenWriter.write(cbuf, -1, 0)));
128         assertEquals(exception, assertThrows(clazz, () -> brokenWriter.write(cbuf, 0, -1)));
129         assertEquals(exception, assertThrows(clazz, () -> brokenWriter.write(cbuf, 0, 2)));
130         assertEquals(exception, assertThrows(clazz, () -> brokenWriter.write((char[]) null, 0, 0)));
131     }
132 
133     @ParameterizedTest
134     @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
135     void testWriteInt(final Class<Throwable> clazz) throws Exception {
136         final Throwable exception = clazz.newInstance();
137         @SuppressWarnings("resource")
138         final BrokenWriter brokenWriter = createBrokenWriter(exception);
139         assertEquals(exception, assertThrows(clazz, () -> brokenWriter.write(1)));
140     }
141 
142     @ParameterizedTest
143     @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
144     void testWriteString(final Class<Throwable> clazz) throws Exception {
145         final Throwable exception = clazz.newInstance();
146         @SuppressWarnings("resource")
147         final BrokenWriter brokenWriter = createBrokenWriter(exception);
148         assertEquals(exception, assertThrows(clazz, () -> brokenWriter.write("01")));
149     }
150 
151     @ParameterizedTest
152     @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
153     void testWriteStringIndexed(final Class<Throwable> clazz) throws Exception {
154         final Throwable exception = clazz.newInstance();
155         @SuppressWarnings("resource")
156         final BrokenWriter brokenWriter = createBrokenWriter(exception);
157         assertEquals(exception, assertThrows(clazz, () -> brokenWriter.write("01", 0, 1)));
158     }
159 
160 }