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.util.concurrent.atomic.AtomicInteger;
25  
26  import org.apache.commons.io.IOUtils;
27  import org.junit.jupiter.api.Test;
28  
29  /**
30   * Tests {@link ChunkedWriter}.
31   */
32  public class ChunkedWriterTest {
33  
34      @SuppressWarnings("resource") // closed by caller.
35      private OutputStreamWriter getOutputStreamWriter(final AtomicInteger numWrites) {
36          final ByteArrayOutputStream baos = new ByteArrayOutputStream();
37          return new OutputStreamWriter(baos) {
38              @Override
39              public void write(final char[] cbuf, final int off, final int len) throws IOException {
40                  numWrites.incrementAndGet();
41                  super.write(cbuf, off, len);
42              }
43          };
44      }
45  
46      @Test
47      public void testNegative_chunkSize_not_permitted() {
48          assertThrows(IllegalArgumentException.class,
49                 () -> new ChunkedWriter(new OutputStreamWriter(new ByteArrayOutputStream()), 0));
50      }
51  
52      @Test
53      public void testWrite_four_chunks() throws Exception {
54          final AtomicInteger numWrites = new AtomicInteger();
55          try (OutputStreamWriter osw = getOutputStreamWriter(numWrites)) {
56              try (ChunkedWriter chunked = new ChunkedWriter(osw, 10)) {
57                  chunked.write("0123456789012345678901234567891".toCharArray());
58                  chunked.flush();
59                  assertEquals(4, numWrites.get());
60              }
61          }
62      }
63  
64      @Test
65      public void testWrite_two_chunks_default_constructor() throws Exception {
66          final AtomicInteger numWrites = new AtomicInteger();
67          try (OutputStreamWriter osw = getOutputStreamWriter(numWrites)) {
68              try (ChunkedWriter chunked = new ChunkedWriter(osw)) {
69                  chunked.write(new char[IOUtils.DEFAULT_BUFFER_SIZE + 1]);
70                  chunked.flush();
71                  assertEquals(2, numWrites.get());
72              }
73          }
74      }
75  }