1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
31
32 public class ChunkedWriterTest {
33
34 @SuppressWarnings("resource")
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 }