Skip to content

Commit 01e1b45

Browse files
author
Gary Gregory
committed
Add BrokenWriter.INSTANCE.
1 parent 6a65e0d commit 01e1b45

4 files changed

Lines changed: 54 additions & 42 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ The <action> type attribute can be add,update,fix,remove.
7878
<action dev="ggregory" type="add" due-to="Gary Gregory">
7979
Add ClosedReader.INSTANCE and deprecate CLOSED_READER.
8080
</action>
81+
<action dev="ggregory" type="add" due-to="Gary Gregory">
82+
Add BrokenWriter.INSTANCE.
83+
</action>
8184
<!-- UPDATE -->
8285
<action dev="ggregory" type="update" due-to="Dependabot">
8386
Bump Maven Javadoc plugin from 3.2.0 to 3.3.0.

src/main/java/org/apache/commons/io/output/BrokenWriter.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.io.IOException;
2020
import java.io.Writer;
2121

22+
import org.apache.commons.io.input.ClosedReader;
23+
2224
/**
2325
* Broken writer. This writer always throws an {@link IOException} from
2426
* all {@link Writer} methods.
@@ -31,6 +33,13 @@
3133
*/
3234
public class BrokenWriter extends Writer {
3335

36+
/**
37+
* The singleton instance.
38+
*
39+
* @since 2.12.0
40+
*/
41+
public static final BrokenWriter INSTANCE = new BrokenWriter();
42+
3443
/**
3544
* The exception that is thrown by all methods of this class.
3645
*/

src/test/java/org/apache/commons/io/output/ProxyCollectionWriterTest.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class ProxyCollectionWriterTest {
3939

4040
@Test
4141
public void testArrayIOExceptionOnAppendChar1() throws IOException {
42-
final Writer badW = new BrokenWriter();
42+
final Writer badW = BrokenWriter.INSTANCE;
4343
final StringWriter goodW = mock(StringWriter.class);
4444
@SuppressWarnings("resource") // not necessary to close this
4545
final ProxyCollectionWriter tw = new ProxyCollectionWriter(badW, goodW, null);
@@ -56,7 +56,7 @@ public void testArrayIOExceptionOnAppendChar1() throws IOException {
5656

5757
@Test
5858
public void testArrayIOExceptionOnAppendChar2() throws IOException {
59-
final Writer badW = new BrokenWriter();
59+
final Writer badW = BrokenWriter.INSTANCE;
6060
final StringWriter goodW = mock(StringWriter.class);
6161
@SuppressWarnings("resource") // not necessary to close this
6262
final ProxyCollectionWriter tw = new ProxyCollectionWriter(goodW, badW, null);
@@ -73,7 +73,7 @@ public void testArrayIOExceptionOnAppendChar2() throws IOException {
7373

7474
@Test
7575
public void testArrayIOExceptionOnAppendCharSequence1() throws IOException {
76-
final Writer badW = new BrokenWriter();
76+
final Writer badW = BrokenWriter.INSTANCE;
7777
final StringWriter goodW = mock(StringWriter.class);
7878
@SuppressWarnings("resource") // not necessary to close this
7979
final ProxyCollectionWriter tw = new ProxyCollectionWriter(badW, goodW, null);
@@ -90,7 +90,7 @@ public void testArrayIOExceptionOnAppendCharSequence1() throws IOException {
9090

9191
@Test
9292
public void testArrayIOExceptionOnAppendCharSequence2() throws IOException {
93-
final Writer badW = new BrokenWriter();
93+
final Writer badW = BrokenWriter.INSTANCE;
9494
final StringWriter goodW = mock(StringWriter.class);
9595
@SuppressWarnings("resource") // not necessary to close this
9696
final ProxyCollectionWriter tw = new ProxyCollectionWriter(goodW, badW, null);
@@ -107,7 +107,7 @@ public void testArrayIOExceptionOnAppendCharSequence2() throws IOException {
107107

108108
@Test
109109
public void testArrayIOExceptionOnAppendCharSequenceIntInt1() throws IOException {
110-
final Writer badW = new BrokenWriter();
110+
final Writer badW = BrokenWriter.INSTANCE;
111111
final StringWriter goodW = mock(StringWriter.class);
112112
@SuppressWarnings("resource") // not necessary to close this
113113
final ProxyCollectionWriter tw = new ProxyCollectionWriter(badW, goodW, null);
@@ -124,7 +124,7 @@ public void testArrayIOExceptionOnAppendCharSequenceIntInt1() throws IOException
124124

125125
@Test
126126
public void testArrayIOExceptionOnAppendCharSequenceIntInt2() throws IOException {
127-
final Writer badW = new BrokenWriter();
127+
final Writer badW = BrokenWriter.INSTANCE;
128128
final StringWriter goodW = mock(StringWriter.class);
129129
@SuppressWarnings("resource") // not necessary to close this
130130
final ProxyCollectionWriter tw = new ProxyCollectionWriter(goodW, badW, null);
@@ -141,7 +141,7 @@ public void testArrayIOExceptionOnAppendCharSequenceIntInt2() throws IOException
141141

142142
@Test
143143
public void testArrayIOExceptionOnClose1() throws IOException {
144-
final Writer badW = new BrokenWriter();
144+
final Writer badW = BrokenWriter.INSTANCE;
145145
final StringWriter goodW = mock(StringWriter.class);
146146
@SuppressWarnings("resource") // not necessary to close this
147147
final ProxyCollectionWriter tw = new ProxyCollectionWriter(badW, goodW, null);
@@ -157,7 +157,7 @@ public void testArrayIOExceptionOnClose1() throws IOException {
157157

158158
@Test
159159
public void testArrayIOExceptionOnClose2() throws IOException {
160-
final Writer badW = new BrokenWriter();
160+
final Writer badW = BrokenWriter.INSTANCE;
161161
final StringWriter goodW = mock(StringWriter.class);
162162
@SuppressWarnings("resource") // not necessary to close this
163163
final ProxyCollectionWriter tw = new ProxyCollectionWriter(goodW, badW, null);
@@ -173,7 +173,7 @@ public void testArrayIOExceptionOnClose2() throws IOException {
173173

174174
@Test
175175
public void testArrayIOExceptionOnFlush1() throws IOException {
176-
final Writer badW = new BrokenWriter();
176+
final Writer badW = BrokenWriter.INSTANCE;
177177
final StringWriter goodW = mock(StringWriter.class);
178178
@SuppressWarnings("resource") // not necessary to close this
179179
final ProxyCollectionWriter tw = new ProxyCollectionWriter(badW, goodW, null);
@@ -189,7 +189,7 @@ public void testArrayIOExceptionOnFlush1() throws IOException {
189189

190190
@Test
191191
public void testArrayIOExceptionOnFlush2() throws IOException {
192-
final Writer badW = new BrokenWriter();
192+
final Writer badW = BrokenWriter.INSTANCE;
193193
final StringWriter goodW = mock(StringWriter.class);
194194
@SuppressWarnings("resource") // not necessary to close this
195195
final ProxyCollectionWriter tw = new ProxyCollectionWriter(goodW, badW, null);
@@ -205,7 +205,7 @@ public void testArrayIOExceptionOnFlush2() throws IOException {
205205

206206
@Test
207207
public void testArrayIOExceptionOnWriteCharArray1() throws IOException {
208-
final Writer badW = new BrokenWriter();
208+
final Writer badW = BrokenWriter.INSTANCE;
209209
final StringWriter goodW = mock(StringWriter.class);
210210
@SuppressWarnings("resource") // not necessary to close this
211211
final ProxyCollectionWriter tw = new ProxyCollectionWriter(badW, goodW, null);
@@ -222,7 +222,7 @@ public void testArrayIOExceptionOnWriteCharArray1() throws IOException {
222222

223223
@Test
224224
public void testArrayIOExceptionOnWriteCharArray2() throws IOException {
225-
final Writer badW = new BrokenWriter();
225+
final Writer badW = BrokenWriter.INSTANCE;
226226
final StringWriter goodW = mock(StringWriter.class);
227227
@SuppressWarnings("resource") // not necessary to close this
228228
final ProxyCollectionWriter tw = new ProxyCollectionWriter(goodW, badW, null);
@@ -239,7 +239,7 @@ public void testArrayIOExceptionOnWriteCharArray2() throws IOException {
239239

240240
@Test
241241
public void testArrayIOExceptionOnWriteCharArrayIntInt1() throws IOException {
242-
final Writer badW = new BrokenWriter();
242+
final Writer badW = BrokenWriter.INSTANCE;
243243
final StringWriter goodW = mock(StringWriter.class);
244244
@SuppressWarnings("resource") // not necessary to close this
245245
final ProxyCollectionWriter tw = new ProxyCollectionWriter(badW, goodW, null);
@@ -256,7 +256,7 @@ public void testArrayIOExceptionOnWriteCharArrayIntInt1() throws IOException {
256256

257257
@Test
258258
public void testArrayIOExceptionOnWriteCharArrayIntInt2() throws IOException {
259-
final Writer badW = new BrokenWriter();
259+
final Writer badW = BrokenWriter.INSTANCE;
260260
final StringWriter goodW = mock(StringWriter.class);
261261
@SuppressWarnings("resource") // not necessary to close this
262262
final ProxyCollectionWriter tw = new ProxyCollectionWriter(goodW, badW, null);
@@ -273,7 +273,7 @@ public void testArrayIOExceptionOnWriteCharArrayIntInt2() throws IOException {
273273

274274
@Test
275275
public void testArrayIOExceptionOnWriteInt1() throws IOException {
276-
final Writer badW = new BrokenWriter();
276+
final Writer badW = BrokenWriter.INSTANCE;
277277
final StringWriter goodW = mock(StringWriter.class);
278278
@SuppressWarnings("resource") // not necessary to close this
279279
final ProxyCollectionWriter tw = new ProxyCollectionWriter(badW, goodW, null);
@@ -290,7 +290,7 @@ public void testArrayIOExceptionOnWriteInt1() throws IOException {
290290

291291
@Test
292292
public void testArrayIOExceptionOnWriteInt2() throws IOException {
293-
final Writer badW = new BrokenWriter();
293+
final Writer badW = BrokenWriter.INSTANCE;
294294
final StringWriter goodW = mock(StringWriter.class);
295295
@SuppressWarnings("resource") // not necessary to close this
296296
final ProxyCollectionWriter tw = new ProxyCollectionWriter(goodW, badW, null);
@@ -307,7 +307,7 @@ public void testArrayIOExceptionOnWriteInt2() throws IOException {
307307

308308
@Test
309309
public void testArrayIOExceptionOnWriteString1() throws IOException {
310-
final Writer badW = new BrokenWriter();
310+
final Writer badW = BrokenWriter.INSTANCE;
311311
final StringWriter goodW = mock(StringWriter.class);
312312
@SuppressWarnings("resource") // not necessary to close this
313313
final ProxyCollectionWriter tw = new ProxyCollectionWriter(badW, goodW, null);
@@ -324,7 +324,7 @@ public void testArrayIOExceptionOnWriteString1() throws IOException {
324324

325325
@Test
326326
public void testArrayIOExceptionOnWriteString2() throws IOException {
327-
final Writer badW = new BrokenWriter();
327+
final Writer badW = BrokenWriter.INSTANCE;
328328
final StringWriter goodW = mock(StringWriter.class);
329329
@SuppressWarnings("resource") // not necessary to close this
330330
final ProxyCollectionWriter tw = new ProxyCollectionWriter(goodW, badW, null);
@@ -342,7 +342,7 @@ public void testArrayIOExceptionOnWriteString2() throws IOException {
342342

343343
@Test
344344
public void testArrayIOExceptionOnWriteStringIntInt1() throws IOException {
345-
final Writer badW = new BrokenWriter();
345+
final Writer badW = BrokenWriter.INSTANCE;
346346
final StringWriter goodW = mock(StringWriter.class);
347347
@SuppressWarnings("resource") // not necessary to close this
348348
final ProxyCollectionWriter tw = new ProxyCollectionWriter(badW, goodW, null);
@@ -359,7 +359,7 @@ public void testArrayIOExceptionOnWriteStringIntInt1() throws IOException {
359359

360360
@Test
361361
public void testArrayIOExceptionOnWriteStringIntInt2() throws IOException {
362-
final Writer badW = new BrokenWriter();
362+
final Writer badW = BrokenWriter.INSTANCE;
363363
final StringWriter goodW = mock(StringWriter.class);
364364
@SuppressWarnings("resource") // not necessary to close this
365365
final ProxyCollectionWriter tw = new ProxyCollectionWriter(goodW, badW, null);
@@ -377,7 +377,7 @@ public void testArrayIOExceptionOnWriteStringIntInt2() throws IOException {
377377

378378
@Test
379379
public void testCollectionCloseBranchIOException() throws IOException {
380-
final Writer badW = new BrokenWriter();
380+
final Writer badW = BrokenWriter.INSTANCE;
381381
final StringWriter goodW = mock(StringWriter.class);
382382
@SuppressWarnings("resource") // not necessary to close this
383383
final ProxyCollectionWriter tw = new ProxyCollectionWriter(Arrays.asList(goodW, badW, null));

0 commit comments

Comments
 (0)