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 ProxyOutputStream}.
34   */
35  class ProxyOutputStreamTest {
36  
37      private ByteArrayOutputStream target;
38  
39      private ProxyOutputStream proxied;
40  
41      private final AtomicBoolean hitArray = new AtomicBoolean();
42      private final AtomicBoolean hitArrayAt = new AtomicBoolean();
43      private final AtomicBoolean hitInt = new AtomicBoolean();
44  
45      @BeforeEach
46      public void setUp() {
47          target = new ByteArrayOutputStream() {
48  
49              @Override
50              public void write(final byte[] ba) {
51                  hitArray.set(true);
52                  super.write(ba);
53              }
54  
55              @Override
56              public void write(final byte[] b, final int off, final int len) {
57                  hitArrayAt.set(true);
58                  super.write(b, off, len);
59              }
60  
61              @Override
62              public synchronized void write(final int ba) {
63                  hitInt.set(true);
64                  super.write(ba);
65              }
66          };
67          proxied = new ProxyOutputStream(target);
68      }
69  
70      @Test
71      void testBuilder() throws Exception {
72          assertSame(target, new ProxyOutputStream.Builder().setOutputStream(target).get().unwrap());
73      }
74  
75      @Test
76      void testSetReference() throws Exception {
77          assertFalse(hitArray.get());
78          proxied.setReference(new ByteArrayOutputStream());
79          proxied.write('y');
80          assertFalse(hitArray.get());
81          assertEquals(0, target.size());
82          assertArrayEquals(ArrayUtils.EMPTY_BYTE_ARRAY, target.toByteArray());
83      }
84  
85      @Test
86      void testUnrwap() throws Exception {
87          assertSame(target, proxied.unwrap());
88      }
89  
90      @Test
91      void testWriteByteArray() throws Exception {
92          assertFalse(hitArray.get());
93          proxied.write(new byte[] { 'y', 'z' });
94          assertTrue(hitArray.get());
95          assertEquals(2, target.size());
96          assertArrayEquals(new byte[] { 'y', 'z' }, target.toByteArray());
97      }
98  
99      @Test
100     void testWriteByteArrayAt() throws Exception {
101         assertFalse(hitArrayAt.get());
102         proxied.write(new byte[] { 'y', 'z' }, 1, 1);
103         assertTrue(hitArrayAt.get());
104         assertEquals(1, target.size());
105         assertArrayEquals(new byte[] { 'z' }, target.toByteArray());
106     }
107 
108     @Test
109     void testWriteByteArrayAtRepeat() throws Exception {
110         // repeat -1
111         proxied.writeRepeat(new byte[] { 'y', 'z' }, 1, 1, 0);
112         assertFalse(hitArrayAt.get());
113         hitArray.set(false);
114         assertEquals(0, target.size());
115         assertArrayEquals(new byte[] {}, target.toByteArray());
116         // repeat 0
117         proxied.writeRepeat(new byte[] { 'y', 'z' }, 1, 1, 0);
118         assertFalse(hitArrayAt.get());
119         hitArray.set(false);
120         assertEquals(0, target.size());
121         assertArrayEquals(new byte[] {}, target.toByteArray());
122         // repeat 1
123         proxied.writeRepeat(new byte[] { 'y', 'z' }, 1, 1, 1);
124         assertTrue(hitArrayAt.get());
125         hitArray.set(false);
126         assertEquals(1, target.size());
127         assertArrayEquals(new byte[] { 'z' }, target.toByteArray());
128         // repeat 2
129         proxied.writeRepeat(new byte[] { 'y', 'x' }, 1, 1, 2);
130         assertTrue(hitArrayAt.get());
131         assertEquals(3, target.size());
132         assertArrayEquals(new byte[] { 'z', 'x', 'x' }, target.toByteArray());
133     }
134 
135     @Test
136     void testWriteByteArrayRepeat() throws Exception {
137         // repeat -1
138         proxied.writeRepeat(new byte[] { 'y', 'z' }, -1);
139         assertFalse(hitArray.get());
140         hitArray.set(false);
141         assertEquals(0, target.size());
142         assertArrayEquals(new byte[] {}, target.toByteArray());
143         // repeat 0
144         proxied.writeRepeat(new byte[] { 'y', 'z' }, 0);
145         assertFalse(hitArray.get());
146         hitArray.set(false);
147         assertEquals(0, target.size());
148         assertArrayEquals(new byte[] {}, target.toByteArray());
149         // repeat 1
150         proxied.writeRepeat(new byte[] { 'y', 'z' }, 1);
151         assertTrue(hitArray.get());
152         hitArray.set(false);
153         assertEquals(2, target.size());
154         assertArrayEquals(new byte[] { 'y', 'z' }, target.toByteArray());
155         // repeat 2
156         proxied.writeRepeat(new byte[] { 'y', 'z' }, 2);
157         assertTrue(hitArray.get());
158         assertEquals(6, target.size());
159         assertArrayEquals(new byte[] { 'y', 'z', 'y', 'z' , 'y', 'z' }, target.toByteArray());
160     }
161 
162     @Test
163     void testWriteInt() throws Exception {
164         assertFalse(hitInt.get());
165         proxied.write('y');
166         assertTrue(hitInt.get());
167         assertEquals(1, target.size());
168         assertEquals('y', target.toByteArray()[0]);
169     }
170 
171     @Test
172     void testWriteIntRepeat() throws Exception {
173         // repeat -1
174         assertFalse(hitInt.get());
175         proxied.writeRepeat('y', -1);
176         assertFalse(hitInt.get());
177         assertEquals(0, target.size());
178         assertArrayEquals(new byte[] {}, target.toByteArray());
179         // repeat 0
180         assertFalse(hitInt.get());
181         proxied.writeRepeat('y', 0);
182         assertFalse(hitInt.get());
183         assertEquals(0, target.size());
184         assertArrayEquals(new byte[] {}, target.toByteArray());
185         // repeat 1
186         assertFalse(hitInt.get());
187         proxied.writeRepeat('y', 1);
188         assertTrue(hitInt.get());
189         hitInt.set(false);
190         assertEquals(1, target.size());
191         assertArrayEquals(new byte[] { 'y' }, target.toByteArray());
192         // repeat 2
193         assertFalse(hitInt.get());
194         proxied.writeRepeat('z', 2);
195         assertTrue(hitInt.get());
196         hitInt.set(false);
197         assertEquals(3, target.size());
198         assertArrayEquals(new byte[] { 'y', 'z', 'z' }, target.toByteArray());
199     }
200 
201     @Test
202     void testWriteNullArrayProxiesToUnderlying() throws Exception {
203         assertFalse(hitArray.get());
204         final byte[] ba = null;
205         assertThrows(NullPointerException.class, () -> target.write(ba));
206         assertTrue(hitArray.get());
207         assertThrows(NullPointerException.class, () -> proxied.write(ba));
208         assertTrue(hitArray.get());
209     }
210 }