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  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  /**On
32   * Tests {@link TeeOutputStream}.
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       * Tests that the main {@code OutputStream} is closed when closing the branch {@code OutputStream} throws an
45       * exception on {@link TeeOutputStream#close()}.
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       * Tests that the branch {@code OutputStream} is closed when closing the main {@code OutputStream} throws an
59       * exception on {@link TeeOutputStream#close()}.
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 }