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.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  public 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      public 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      public 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      }
59  
60      @ParameterizedTest
61      @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
62      public void testAppendCharSequenceIndexed(final Class<Throwable> clazz) throws Exception {
63          final Throwable exception = clazz.newInstance();
64          @SuppressWarnings("resource")
65          final BrokenWriter brokenWriter = createBrokenWriter(exception);
66          assertEquals(exception, assertThrows(clazz, () -> brokenWriter.append("01", 0, 1)));
67      }
68  
69      @ParameterizedTest
70      @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
71      public void testClose(final Class<Throwable> clazz) throws Exception {
72          final Throwable exception = clazz.newInstance();
73          @SuppressWarnings("resource")
74          final BrokenWriter brokenWriter = createBrokenWriter(exception);
75          assertEquals(exception, assertThrows(clazz, () -> brokenWriter.close()));
76      }
77  
78      @ParameterizedTest
79      @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
80      public void testFlush(final Class<Throwable> clazz) throws Exception {
81          final Throwable exception = clazz.newInstance();
82          @SuppressWarnings("resource")
83          final BrokenWriter brokenWriter = createBrokenWriter(exception);
84          assertEquals(exception, assertThrows(clazz, () -> brokenWriter.flush()));
85      }
86  
87      @Test
88      public void testInstance() {
89          assertNotNull(BrokenWriter.INSTANCE);
90      }
91  
92      @Test
93      public void testTryWithResources() {
94          final IOException thrown = assertThrows(IOException.class, () -> {
95              try (Writer newWriter = new BrokenWriter()) {
96                  newWriter.write(1);
97              }
98          });
99          assertEquals("Broken writer", thrown.getMessage());
100 
101         final Throwable[] suppressed = thrown.getSuppressed();
102         assertEquals(1, suppressed.length);
103         assertEquals(IOException.class, suppressed[0].getClass());
104         assertEquals("Broken writer", suppressed[0].getMessage());
105     }
106 
107     @ParameterizedTest
108     @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
109     public void testWriteCharArray(final Class<Throwable> clazz) throws Exception {
110         final Throwable exception = clazz.newInstance();
111         @SuppressWarnings("resource")
112         final BrokenWriter brokenWriter = createBrokenWriter(exception);
113         assertEquals(exception, assertThrows(clazz, () -> brokenWriter.write(new char[1])));
114     }
115 
116     @ParameterizedTest
117     @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
118     public void testWriteCharArrayIndexed(final Class<Throwable> clazz) throws Exception {
119         final Throwable exception = clazz.newInstance();
120         @SuppressWarnings("resource")
121         final BrokenWriter brokenWriter = createBrokenWriter(exception);
122         assertEquals(exception, assertThrows(clazz, () -> brokenWriter.write(new char[1], 0, 1)));
123     }
124 
125     @ParameterizedTest
126     @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
127     public void testWriteInt(final Class<Throwable> clazz) throws Exception {
128         final Throwable exception = clazz.newInstance();
129         @SuppressWarnings("resource")
130         final BrokenWriter brokenWriter = createBrokenWriter(exception);
131         assertEquals(exception, assertThrows(clazz, () -> brokenWriter.write(1)));
132     }
133 
134     @ParameterizedTest
135     @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
136     public void testWriteString(final Class<Throwable> clazz) throws Exception {
137         final Throwable exception = clazz.newInstance();
138         @SuppressWarnings("resource")
139         final BrokenWriter brokenWriter = createBrokenWriter(exception);
140         assertEquals(exception, assertThrows(clazz, () -> brokenWriter.write("01")));
141     }
142 
143     @ParameterizedTest
144     @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
145     public void testWriteStringIndexed(final Class<Throwable> clazz) throws Exception {
146         final Throwable exception = clazz.newInstance();
147         @SuppressWarnings("resource")
148         final BrokenWriter brokenWriter = createBrokenWriter(exception);
149         assertEquals(exception, assertThrows(clazz, () -> brokenWriter.write("01", 0, 1)));
150     }
151 
152 }