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.OutputStreamWriter;
24  import java.io.UnsupportedEncodingException;
25  import java.io.Writer;
26  
27  import org.junit.jupiter.api.Test;
28  
29  /**
30   * Test {@link ProxyWriter}.
31   */
32  public class ProxyWriterTest {
33  
34      @Test
35      public void testAppendChar() throws Exception {
36          try (StringBuilderWriter writer = new StringBuilderWriter();
37                  final ProxyWriter proxy = new ProxyWriter(writer)) {
38              proxy.append('c');
39              assertEquals("c", writer.toString());
40          }
41      }
42  
43      @Test
44      public void testAppendCharSequence() throws Exception {
45          try (StringBuilderWriter writer = new StringBuilderWriter();
46                  final ProxyWriter proxy = new ProxyWriter(writer)) {
47              proxy.append("ABC");
48              assertEquals("ABC", writer.toString());
49          }
50      }
51  
52      @Test
53      public void testAppendCharSequence_with_offset() throws Exception {
54          try (StringBuilderWriter writer = new StringBuilderWriter();
55                  final ProxyWriter proxy = new ProxyWriter(writer)) {
56              proxy.append("ABC", 1, 3);
57              proxy.flush();
58              assertEquals("BC", writer.toString());
59          }
60      }
61  
62      @Test
63      public void testExceptions_in_append_char() throws IOException {
64          try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
65                  final OutputStreamWriter osw = new OutputStreamWriter(baos) {
66                      @Override
67                      public void write(final int c) throws IOException {
68                          throw new UnsupportedEncodingException("Bah");
69                      }
70                  }) {
71              try (ProxyWriter proxy = new ProxyWriter(osw)) {
72                  assertThrows(UnsupportedEncodingException.class, () -> proxy.append('c'));
73              }
74          }
75      }
76  
77      @Test
78      public void testExceptions_in_append_charSequence() throws IOException {
79          try (OutputStreamWriter osw = new OutputStreamWriter(new ByteArrayOutputStream()) {
80              @Override
81              public Writer append(final CharSequence csq) throws IOException {
82                  throw new UnsupportedEncodingException("Bah");
83              }
84          }) {
85              try (ProxyWriter proxy = new ProxyWriter(osw)) {
86                  assertThrows(UnsupportedEncodingException.class, () -> proxy.append("ABCE"));
87              }
88          }
89      }
90  
91      @Test
92      public void testExceptions_in_append_charSequence_offset() throws IOException {
93          try (OutputStreamWriter osw = new OutputStreamWriter(new ByteArrayOutputStream()) {
94              @Override
95              public Writer append(final CharSequence csq, final int start, final int end) throws IOException {
96                  throw new UnsupportedEncodingException("Bah");
97              }
98          }) {
99              try (ProxyWriter proxy = new ProxyWriter(osw)) {
100                 assertThrows(UnsupportedEncodingException.class, () -> proxy.append("ABCE", 1, 2));
101             }
102         }
103     }
104 
105     @Test
106     public void testExceptions_in_close() {
107         assertThrows(UnsupportedEncodingException.class, () -> {
108             try (OutputStreamWriter osw = new OutputStreamWriter(new ByteArrayOutputStream()) {
109                 @Override
110                 public void close() throws IOException {
111                     throw new UnsupportedEncodingException("Bah");
112                 }
113             }) {
114                 try (ProxyWriter proxy = new ProxyWriter(osw)) {
115                     // noop
116                 }
117             }
118         });
119     }
120 
121     @Test
122     public void testExceptions_in_flush() throws IOException {
123         try (OutputStreamWriter osw = new OutputStreamWriter(new ByteArrayOutputStream()) {
124             @Override
125             public void flush() throws IOException {
126                 throw new UnsupportedEncodingException("Bah");
127             }
128         }) {
129             try (ProxyWriter proxy = new ProxyWriter(osw)) {
130                 assertThrows(UnsupportedEncodingException.class, proxy::flush);
131             }
132         }
133     }
134 
135     @Test
136     public void testExceptions_in_write_char_array() throws IOException {
137         try (OutputStreamWriter osw = new OutputStreamWriter(new ByteArrayOutputStream()) {
138             @Override
139             public void write(final char[] cbuf) throws IOException {
140                 throw new UnsupportedEncodingException("Bah");
141             }
142         }) {
143             try (ProxyWriter proxy = new ProxyWriter(osw)) {
144                 assertThrows(UnsupportedEncodingException.class, () -> proxy.write("ABCE".toCharArray()));
145             }
146         }
147     }
148 
149     @Test
150     public void testExceptions_in_write_int() throws IOException {
151         try (OutputStreamWriter osw = new OutputStreamWriter(new ByteArrayOutputStream()) {
152             @Override
153             public void write(final int c) throws IOException {
154                 throw new UnsupportedEncodingException("Bah");
155             }
156         }) {
157             try (ProxyWriter proxy = new ProxyWriter(osw)) {
158                 assertThrows(UnsupportedEncodingException.class, () -> proxy.write('a'));
159             }
160         }
161     }
162 
163     @Test
164     public void testExceptions_in_write_offset_char_array() throws IOException {
165         try (OutputStreamWriter osw = new OutputStreamWriter(new ByteArrayOutputStream()) {
166             @Override
167             public void write(final char[] cbuf, final int off, final int len) throws IOException {
168                 throw new UnsupportedEncodingException("Bah");
169             }
170         }) {
171             try (ProxyWriter proxy = new ProxyWriter(osw)) {
172                 assertThrows(UnsupportedEncodingException.class, () -> proxy.write("ABCE".toCharArray(), 2, 3));
173             }
174         }
175     }
176 
177     @Test
178     public void testExceptions_in_write_string() throws IOException {
179         try (OutputStreamWriter osw = new OutputStreamWriter(new ByteArrayOutputStream()) {
180             @Override
181             public void write(final String str) throws IOException {
182                 throw new UnsupportedEncodingException("Bah");
183             }
184         }) {
185             try (ProxyWriter proxy = new ProxyWriter(osw)) {
186                 assertThrows(UnsupportedEncodingException.class, () -> proxy.write("ABCE"));
187             }
188         }
189     }
190 
191     @Test
192     public void testExceptions_in_write_string_offset() throws IOException {
193         try (OutputStreamWriter osw = new OutputStreamWriter(new ByteArrayOutputStream()) {
194             @Override
195             public void write(final String str, final int off, final int len) throws IOException {
196                 throw new UnsupportedEncodingException("Bah");
197             }
198         }) {
199             try (ProxyWriter proxy = new ProxyWriter(osw)) {
200                 assertThrows(UnsupportedEncodingException.class, () -> proxy.write("ABCE", 1, 3));
201             }
202         }
203     }
204 
205     @Test
206     public void testNullCharArray() throws Exception {
207         try (ProxyWriter proxy = new ProxyWriter(NullWriter.INSTANCE)) {
208             proxy.write((char[]) null);
209             proxy.write((char[]) null, 0, 0);
210         }
211     }
212 
213     @Test
214     public void testNullCharSequence() throws Exception {
215         try (ProxyWriter proxy = new ProxyWriter(NullWriter.INSTANCE)) {
216             proxy.append(null);
217         }
218     }
219 
220     @Test
221     public void testNullString() throws Exception {
222         try (ProxyWriter proxy = new ProxyWriter(NullWriter.INSTANCE)) {
223             proxy.write((String) null);
224             proxy.write((String) null, 0, 0);
225         }
226     }
227 
228     @Test
229     public void testWriteCharArray() throws Exception {
230         try (StringBuilderWriter writer = new StringBuilderWriter();
231                 final ProxyWriter proxy = new ProxyWriter(writer)) {
232             proxy.write(new char[] { 'A', 'B', 'C' });
233             assertEquals("ABC", writer.toString());
234         }
235     }
236 
237     @Test
238     public void testWriteCharArrayPartial() throws Exception {
239         try (StringBuilderWriter writer = new StringBuilderWriter();
240                 final ProxyWriter proxy = new ProxyWriter(writer)) {
241             proxy.write(new char[] { 'A', 'B', 'C' }, 1, 2);
242             assertEquals("BC", writer.toString());
243         }
244     }
245 
246     @Test
247     public void testWriteInt() throws Exception {
248         try (StringBuilderWriter writer = new StringBuilderWriter();
249                 final ProxyWriter proxy = new ProxyWriter(writer)) {
250             proxy.write(65);
251             assertEquals("A", writer.toString());
252         }
253     }
254 
255     @Test
256     public void testWriteString() throws Exception {
257         try (StringBuilderWriter writer = new StringBuilderWriter();
258                 final ProxyWriter proxy = new ProxyWriter(writer)) {
259             proxy.write("ABC");
260             assertEquals("ABC", writer.toString());
261         }
262     }
263 
264     @Test
265     public void testWriteStringPartial() throws Exception {
266         try (StringBuilderWriter writer = new StringBuilderWriter();
267                 final ProxyWriter proxy = new ProxyWriter(writer)) {
268             proxy.write("ABC", 1, 2);
269             assertEquals("BC", writer.toString());
270         }
271     }
272 }