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  
26  import org.junit.jupiter.api.BeforeEach;
27  import org.junit.jupiter.api.Disabled;
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Tests {@link BrokenWriter}.
32   */
33  public class UncheckedFilterWriterTest {
34  
35      private IOException exception;
36  
37      private UncheckedFilterWriter brokenWriter;
38      private UncheckedFilterWriter stringWriter;
39  
40      @SuppressWarnings("resource")
41      @BeforeEach
42      public void setUp() throws IOException {
43          exception = new IOException("test exception");
44          brokenWriter = UncheckedFilterWriter.builder().setWriter(new BrokenWriter(exception)).get();
45          stringWriter = UncheckedFilterWriter.builder().setWriter(new StringWriter()).get();
46      }
47  
48      @SuppressWarnings("resource")
49      @Test
50      public void testAppendChar() {
51          stringWriter.append('1');
52      }
53  
54      @SuppressWarnings("resource")
55      @Test
56      public void testAppendCharSequence() {
57          stringWriter.append("01");
58      }
59  
60      @SuppressWarnings("resource")
61      @Test
62      public void testAppendCharSequenceIndexed() {
63          stringWriter.append("01", 0, 1);
64      }
65  
66      @Test
67      public void testAppendCharSequenceIndexedThrows() {
68          assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.append("01", 0, 1)).getCause());
69      }
70  
71      @Test
72      public void testAppendCharSequenceThrows() {
73          assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.append("01")).getCause());
74      }
75  
76      @Test
77      public void testAppendCharThrows() {
78          assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.append('1')).getCause());
79      }
80  
81      @Test
82      public void testClose() {
83          stringWriter.close();
84      }
85  
86      @Test
87      public void testCloseThrows() {
88          assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.close()).getCause());
89      }
90  
91      @Test
92      public void testEquals() {
93          stringWriter.equals(null);
94      }
95  
96      @Test
97      @Disabled("What should happen here?")
98      public void testEqualsThrows() {
99          assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.equals(null)).getCause());
100     }
101 
102     @Test
103     public void testFlush() {
104         stringWriter.flush();
105     }
106 
107     @Test
108     public void testFlushThrows() {
109         assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.flush()).getCause());
110     }
111 
112     @Test
113     public void testHashCode() {
114         stringWriter.hashCode();
115     }
116 
117     @Test
118     @Disabled("What should happen here?")
119     public void testHashCodeThrows() {
120         assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.hashCode()).getCause());
121     }
122 
123     @Test
124     public void testToString() {
125         stringWriter.toString();
126     }
127 
128     @Test
129     @Disabled("What should happen here?")
130     public void testToStringThrows() {
131         assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.toString()).getCause());
132     }
133 
134     @Test
135     public void testWriteCharArray() {
136         stringWriter.write(new char[1]);
137     }
138 
139     @Test
140     public void testWriteCharArrayIndexed() {
141         stringWriter.write(new char[1], 0, 1);
142     }
143 
144     @Test
145     public void testWriteCharArrayIndexedThrows() {
146         assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.write(new char[1], 0, 1)).getCause());
147     }
148 
149     @Test
150     public void testWriteCharArrayThrows() {
151         assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.write(new char[1])).getCause());
152     }
153 
154     @Test
155     public void testWriteInt() {
156         stringWriter.write(1);
157     }
158 
159     @Test
160     public void testWriteIntThrows() {
161         assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.write(1)).getCause());
162     }
163 
164     @Test
165     public void testWriteString() {
166         stringWriter.write("01");
167     }
168 
169     @Test
170     public void testWriteStringIndexed() {
171         stringWriter.write("01", 0, 1);
172     }
173 
174     @Test
175     public void testWriteStringIndexedThrows() {
176         assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.write("01", 0, 1)).getCause());
177     }
178 
179     @Test
180     public void testWriteStringThrows() {
181         assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.write("01")).getCause());
182     }
183 }