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    *      https://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.assertArrayEquals;
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertFalse;
22  import static org.junit.jupiter.api.Assertions.assertSame;
23  import static org.junit.jupiter.api.Assertions.assertThrows;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  
26  import java.util.concurrent.atomic.AtomicBoolean;
27  
28  import org.apache.commons.lang3.ArrayUtils;
29  import org.junit.jupiter.api.BeforeEach;
30  import org.junit.jupiter.api.Test;
31  
32  /**
33   * Tests {@link CloseShieldOutputStream}.
34   */
35  class ProxyOutputStreamTest {
36  
37      private ByteArrayOutputStream original;
38  
39      private ProxyOutputStream proxied;
40  
41      private final AtomicBoolean hit = new AtomicBoolean();
42  
43      @BeforeEach
44      public void setUp() {
45          original = new ByteArrayOutputStream() {
46  
47              @Override
48              public void write(final byte[] ba) {
49                  hit.set(true);
50                  super.write(ba);
51              }
52  
53              @Override
54              public synchronized void write(final int ba) {
55                  hit.set(true);
56                  super.write(ba);
57              }
58          };
59          proxied = new ProxyOutputStream(original);
60      }
61  
62      @Test
63      void testBuilder() throws Exception {
64          assertSame(original, new ProxyOutputStream.Builder().setOutputStream(original).get().unwrap());
65      }
66  
67      @SuppressWarnings("resource")
68      @Test
69      void testSetReference() throws Exception {
70          assertFalse(hit.get());
71          proxied.setReference(new ByteArrayOutputStream());
72          proxied.write('y');
73          assertFalse(hit.get());
74          assertEquals(0, original.size());
75          assertArrayEquals(ArrayUtils.EMPTY_BYTE_ARRAY, original.toByteArray());
76      }
77  
78      @Test
79      void testWrite() throws Exception {
80          assertFalse(hit.get());
81          proxied.write('y');
82          assertTrue(hit.get());
83          assertEquals(1, original.size());
84          assertEquals('y', original.toByteArray()[0]);
85      }
86  
87      @Test
88      void testWriteNullArrayProxiesToUnderlying() throws Exception {
89          assertFalse(hit.get());
90          final byte[] ba = null;
91          assertThrows(NullPointerException.class, () -> original.write(ba));
92          assertTrue(hit.get());
93          assertThrows(NullPointerException.class, () -> proxied.write(ba));
94          assertTrue(hit.get());
95      }
96  }