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.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertSame;
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  
24  import java.io.IOException;
25  import java.io.OutputStreamWriter;
26  import java.io.StringWriter;
27  import java.io.UnsupportedEncodingException;
28  import java.io.Writer;
29  import java.util.concurrent.atomic.AtomicBoolean;
30  
31  import org.junit.jupiter.api.BeforeEach;
32  import org.junit.jupiter.api.Test;
33  
34  /**
35   * Test {@link ProxyWriter}.
36   */
37  class ProxyWriterTest {
38  
39      private StringWriter target;
40  
41      private ProxyWriter proxied;
42  
43      private final AtomicBoolean hitArray = new AtomicBoolean();
44      private final AtomicBoolean hitArrayAt = new AtomicBoolean();
45      private final AtomicBoolean hitInt = new AtomicBoolean();
46  
47      @BeforeEach
48      public void setUp() {
49          target = new StringWriter() {
50  
51              @Override
52              public void write(final char[] ba) throws IOException {
53                  hitArray.set(true);
54                  super.write(ba);
55              }
56  
57              @Override
58              public void write(final char[] b, final int off, final int len) {
59                  hitArrayAt.set(true);
60                  super.write(b, off, len);
61              }
62  
63              @Override
64              public synchronized void write(final int ba) {
65                  hitInt.set(true);
66                  super.write(ba);
67              }
68          };
69          proxied = new ProxyWriter(target);
70      }
71  
72      @Test
73      void testAppendChar() throws Exception {
74          try (StringBuilderWriter writer = new StringBuilderWriter();
75                  ProxyWriter proxy = new ProxyWriter(writer)) {
76              proxy.append('c');
77              assertEquals("c", writer.toString());
78          }
79      }
80  
81      @Test
82      void testAppendCharSequence() throws Exception {
83          try (StringBuilderWriter writer = new StringBuilderWriter();
84                  ProxyWriter proxy = new ProxyWriter(writer)) {
85              proxy.append("ABC");
86              assertEquals("ABC", writer.toString());
87          }
88      }
89  
90      @Test
91      void testAppendCharSequence_with_offset() throws Exception {
92          try (StringBuilderWriter writer = new StringBuilderWriter();
93                  ProxyWriter proxy = new ProxyWriter(writer)) {
94              proxy.append("ABC", 1, 3);
95              proxy.flush();
96              assertEquals("BC", writer.toString());
97          }
98      }
99  
100     @Test
101     void testExceptions_in_append_char() throws IOException {
102         try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
103                 OutputStreamWriter osw = new OutputStreamWriter(baos) {
104                     @Override
105                     public void write(final int c) throws IOException {
106                         throw new UnsupportedEncodingException("Bah");
107                     }
108                 }) {
109             try (ProxyWriter proxy = new ProxyWriter(osw)) {
110                 assertThrows(UnsupportedEncodingException.class, () -> proxy.append('c'));
111             }
112         }
113     }
114 
115     @Test
116     void testExceptions_in_append_charSequence() throws IOException {
117         try (OutputStreamWriter osw = new OutputStreamWriter(new ByteArrayOutputStream()) {
118             @Override
119             public Writer append(final CharSequence csq) throws IOException {
120                 throw new UnsupportedEncodingException("Bah");
121             }
122         }) {
123             try (ProxyWriter proxy = new ProxyWriter(osw)) {
124                 assertThrows(UnsupportedEncodingException.class, () -> proxy.append("ABCE"));
125             }
126         }
127     }
128 
129     @Test
130     void testExceptions_in_append_charSequence_offset() throws IOException {
131         try (OutputStreamWriter osw = new OutputStreamWriter(new ByteArrayOutputStream()) {
132             @Override
133             public Writer append(final CharSequence csq, final int start, final int end) throws IOException {
134                 throw new UnsupportedEncodingException("Bah");
135             }
136         }) {
137             try (ProxyWriter proxy = new ProxyWriter(osw)) {
138                 assertThrows(UnsupportedEncodingException.class, () -> proxy.append("ABCE", 1, 2));
139             }
140         }
141     }
142 
143     @Test
144     void testExceptions_in_close() {
145         assertThrows(UnsupportedEncodingException.class, () -> {
146             try (OutputStreamWriter osw = new OutputStreamWriter(new ByteArrayOutputStream()) {
147                 @Override
148                 public void close() throws IOException {
149                     throw new UnsupportedEncodingException("Bah");
150                 }
151             }) {
152                 try (ProxyWriter proxy = new ProxyWriter(osw)) {
153                     // noop
154                 }
155             }
156         });
157     }
158 
159     @Test
160     void testExceptions_in_flush() throws IOException {
161         try (OutputStreamWriter osw = new OutputStreamWriter(new ByteArrayOutputStream()) {
162             @Override
163             public void flush() throws IOException {
164                 throw new UnsupportedEncodingException("Bah");
165             }
166         }) {
167             try (ProxyWriter proxy = new ProxyWriter(osw)) {
168                 assertThrows(UnsupportedEncodingException.class, proxy::flush);
169             }
170         }
171     }
172 
173     @Test
174     void testExceptions_in_write_char_array() throws IOException {
175         try (OutputStreamWriter osw = new OutputStreamWriter(new ByteArrayOutputStream()) {
176             @Override
177             public void write(final char[] cbuf) throws IOException {
178                 throw new UnsupportedEncodingException("Bah");
179             }
180         }) {
181             try (ProxyWriter proxy = new ProxyWriter(osw)) {
182                 assertThrows(UnsupportedEncodingException.class, () -> proxy.write("ABCE".toCharArray()));
183             }
184         }
185     }
186 
187     @Test
188     void testExceptions_in_write_int() throws IOException {
189         try (OutputStreamWriter osw = new OutputStreamWriter(new ByteArrayOutputStream()) {
190             @Override
191             public void write(final int c) throws IOException {
192                 throw new UnsupportedEncodingException("Bah");
193             }
194         }) {
195             try (ProxyWriter proxy = new ProxyWriter(osw)) {
196                 assertThrows(UnsupportedEncodingException.class, () -> proxy.write('a'));
197             }
198         }
199     }
200 
201     @Test
202     void testExceptions_in_write_offset_char_array() throws IOException {
203         try (OutputStreamWriter osw = new OutputStreamWriter(new ByteArrayOutputStream()) {
204             @Override
205             public void write(final char[] cbuf, final int off, final int len) throws IOException {
206                 throw new UnsupportedEncodingException("Bah");
207             }
208         }) {
209             try (ProxyWriter proxy = new ProxyWriter(osw)) {
210                 assertThrows(UnsupportedEncodingException.class, () -> proxy.write("ABCE".toCharArray(), 2, 3));
211             }
212         }
213     }
214 
215     @Test
216     void testExceptions_in_write_string() throws IOException {
217         try (OutputStreamWriter osw = new OutputStreamWriter(new ByteArrayOutputStream()) {
218             @Override
219             public void write(final String str) throws IOException {
220                 throw new UnsupportedEncodingException("Bah");
221             }
222         }) {
223             try (ProxyWriter proxy = new ProxyWriter(osw)) {
224                 assertThrows(UnsupportedEncodingException.class, () -> proxy.write("ABCE"));
225             }
226         }
227     }
228 
229     @Test
230     void testExceptions_in_write_string_offset() throws IOException {
231         try (OutputStreamWriter osw = new OutputStreamWriter(new ByteArrayOutputStream()) {
232             @Override
233             public void write(final String str, final int off, final int len) throws IOException {
234                 throw new UnsupportedEncodingException("Bah");
235             }
236         }) {
237             try (ProxyWriter proxy = new ProxyWriter(osw)) {
238                 assertThrows(UnsupportedEncodingException.class, () -> proxy.write("ABCE", 1, 3));
239             }
240         }
241     }
242 
243     @Test
244     void testNullCharArray() throws Exception {
245         try (ProxyWriter proxy = new ProxyWriter(NullWriter.INSTANCE)) {
246             assertThrows(NullPointerException.class, () -> proxy.write((char[]) null));
247             assertThrows(NullPointerException.class, () -> proxy.write((char[]) null, 0, 0));
248         }
249     }
250 
251     @Test
252     void testNullCharSequence() throws Exception {
253         try (ProxyWriter proxy = new ProxyWriter(NullWriter.INSTANCE)) {
254             proxy.append(null);
255         }
256     }
257 
258     @Test
259     void testNullString() throws Exception {
260         try (ProxyWriter proxy = new ProxyWriter(NullWriter.INSTANCE)) {
261             // Default implementation delegates to write(char[], int, int)
262             assertThrows(NullPointerException.class, () -> proxy.write((String) null));
263             assertThrows(NullPointerException.class, () -> proxy.write((String) null, 0, 0));
264         }
265     }
266 
267     @Test
268     void testSetReference() throws Exception {
269         assertFalse(hitArray.get());
270         proxied.setReference(new StringWriter());
271         proxied.write('y');
272         assertFalse(hitArray.get());
273         assertEquals(0, target.toString().length());
274         assertEquals("", target.toString());
275     }
276 
277     @Test
278     void testUnrwap() throws Exception {
279         assertSame(target, proxied.unwrap());
280     }
281 
282     @Test
283     void testWriteCharArray() throws Exception {
284         try (StringBuilderWriter writer = new StringBuilderWriter();
285                 ProxyWriter proxy = new ProxyWriter(writer)) {
286             proxy.write(new char[] { 'A', 'B', 'C' });
287             assertEquals("ABC", writer.toString());
288         }
289     }
290 
291     @Test
292     void testWriteCharArrayPartial() throws Exception {
293         try (StringBuilderWriter writer = new StringBuilderWriter();
294                 ProxyWriter proxy = new ProxyWriter(writer)) {
295             proxy.write(new char[] { 'A', 'B', 'C' }, 1, 2);
296             assertEquals("BC", writer.toString());
297         }
298     }
299 
300     @Test
301     void testWriteInt() throws Exception {
302         try (StringBuilderWriter writer = new StringBuilderWriter();
303                 ProxyWriter proxy = new ProxyWriter(writer)) {
304             proxy.write(65);
305             assertEquals("A", writer.toString());
306         }
307     }
308 
309     @Test
310     void testWriteString() throws Exception {
311         try (StringBuilderWriter writer = new StringBuilderWriter();
312                 ProxyWriter proxy = new ProxyWriter(writer)) {
313             proxy.write("ABC");
314             assertEquals("ABC", writer.toString());
315         }
316     }
317 
318 
319     @Test
320     void testWriteStringPartial() throws Exception {
321         try (StringBuilderWriter writer = new StringBuilderWriter();
322                 ProxyWriter proxy = new ProxyWriter(writer)) {
323             proxy.write("ABC", 1, 2);
324             assertEquals("BC", writer.toString());
325         }
326     }
327 }