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 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.verify;
23
24 import java.io.ByteArrayOutputStream;
25 import java.io.IOException;
26 import java.io.OutputStream;
27
28 import org.apache.commons.io.test.ThrowOnCloseOutputStream;
29 import org.junit.jupiter.api.Test;
30
31
32
33
34 public class TeeOutputStreamTest {
35
36 private void assertByteArrayEquals(final String msg, final byte[] array1, final byte[] array2) {
37 assertEquals(array1.length, array2.length, msg + ": array size mismatch");
38 for (int i = 0; i < array1.length; i++) {
39 assertEquals(array1[i], array2[i], msg + ": array[ " + i + "] mismatch");
40 }
41 }
42
43
44
45
46
47 @Test
48 public void testIOExceptionOnClose() throws IOException {
49 final OutputStream badOs = new ThrowOnCloseOutputStream();
50 final ByteArrayOutputStream goodOs = mock(ByteArrayOutputStream.class);
51 final TeeOutputStream tos = new TeeOutputStream(badOs, goodOs);
52
53 assertThrows(IOException.class, tos::close);
54 verify(goodOs).close();
55 }
56
57
58
59
60
61 @Test
62 public void testIOExceptionOnCloseBranch() throws IOException {
63 final OutputStream badOs = new ThrowOnCloseOutputStream();
64 final ByteArrayOutputStream goodOs = mock(ByteArrayOutputStream.class);
65 final TeeOutputStream tos = new TeeOutputStream(goodOs, badOs);
66
67 assertThrows(IOException.class, tos::close);
68 verify(goodOs).close();
69 }
70
71 @Test
72 public void testTee() throws IOException {
73 final ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
74 final ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
75 final ByteArrayOutputStream expected = new ByteArrayOutputStream();
76
77 try (TeeOutputStream tos = new TeeOutputStream(baos1, baos2)) {
78 for (int i = 0; i < 20; i++) {
79 tos.write(i);
80 expected.write(i);
81 }
82 assertByteArrayEquals("TeeOutputStream.write(int)", expected.toByteArray(), baos1.toByteArray());
83 assertByteArrayEquals("TeeOutputStream.write(int)", expected.toByteArray(), baos2.toByteArray());
84
85 final byte[] array = new byte[10];
86 for (int i = 20; i < 30; i++) {
87 array[i - 20] = (byte) i;
88 }
89 tos.write(array);
90 expected.write(array);
91 assertByteArrayEquals("TeeOutputStream.write(byte[])", expected.toByteArray(), baos1.toByteArray());
92 assertByteArrayEquals("TeeOutputStream.write(byte[])", expected.toByteArray(), baos2.toByteArray());
93
94 for (int i = 25; i < 35; i++) {
95 array[i - 25] = (byte) i;
96 }
97 tos.write(array, 5, 5);
98 expected.write(array, 5, 5);
99 assertByteArrayEquals("TeeOutputStream.write(byte[], int, int)", expected.toByteArray(),
100 baos1.toByteArray());
101 assertByteArrayEquals("TeeOutputStream.write(byte[], int, int)", expected.toByteArray(),
102 baos2.toByteArray());
103
104 expected.flush();
105 expected.close();
106
107 tos.flush();
108 }
109 }
110
111 }