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.assertThrows;
21  
22  import java.io.IOException;
23  import java.io.StringWriter;
24  import java.io.UncheckedIOException;
25  import java.nio.charset.Charset;
26  
27  import org.junit.jupiter.api.BeforeEach;
28  import org.junit.jupiter.api.Disabled;
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * Tests {@link BrokenWriter}.
33   */
34  public class UncheckedFilterOutputStreamTest {
35  
36      private IOException exception;
37  
38      private UncheckedFilterOutputStream brokenWriter;
39      private UncheckedFilterOutputStream stringWriter;
40  
41      @SuppressWarnings("resource")
42      @BeforeEach
43      public void setUp() throws IOException {
44          exception = new IOException("test exception");
45          brokenWriter = UncheckedFilterOutputStream.builder().setOutputStream(new BrokenOutputStream(exception)).get();
46          stringWriter = UncheckedFilterOutputStream.builder()
47                  .setOutputStream(WriterOutputStream.builder().setWriter(new StringWriter()).setCharset(Charset.defaultCharset()).get()).get();
48      }
49  
50      @Test
51      public void testClose() {
52          stringWriter.close();
53      }
54  
55      @Test
56      public void testCloseThrows() {
57          assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.close()).getCause());
58      }
59  
60      @Test
61      public void testEquals() {
62          stringWriter.equals(null);
63      }
64  
65      @Test
66      @Disabled("What should happen here?")
67      public void testEqualsThrows() {
68          assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.equals(null)).getCause());
69      }
70  
71      @Test
72      public void testFlush() {
73          stringWriter.flush();
74      }
75  
76      @Test
77      public void testFlushThrows() {
78          assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.flush()).getCause());
79      }
80  
81      @Test
82      public void testHashCode() {
83          stringWriter.hashCode();
84      }
85  
86      @Test
87      @Disabled("What should happen here?")
88      public void testHashCodeThrows() {
89          assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.hashCode()).getCause());
90      }
91  
92      @Test
93      public void testToString() {
94          stringWriter.toString();
95      }
96  
97      @Test
98      @Disabled("What should happen here?")
99      public void testToStringThrows() {
100         assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.toString()).getCause());
101     }
102 
103     @Test
104     public void testWriteInt() {
105         stringWriter.write(1);
106     }
107 
108     @Test
109     public void testWriteIntThrows() {
110         assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.write(1)).getCause());
111     }
112 
113 }