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 hitByteArray = new AtomicBoolean();
42      private final AtomicBoolean hitByteArrayAt = 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                  hitByteArray.set(true);
52                  super.write(ba);
53              }
54  
55              @Override
56              public void write(final byte[] b, final int off, final int len) {
57                  hitByteArrayAt.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      @SuppressWarnings("resource")
76      @Test
77      void testSetReference() throws Exception {
78          assertFalse(hitByteArray.get());
79          proxied.setReference(new ByteArrayOutputStream());
80          proxied.write('y');
81          assertFalse(hitByteArray.get());
82          assertEquals(0, target.size());
83          assertArrayEquals(ArrayUtils.EMPTY_BYTE_ARRAY, target.toByteArray());
84      }
85  
86      @Test
87      void testWriteByteArray() throws Exception {
88          assertFalse(hitByteArray.get());
89          proxied.write(new byte[] { 'y', 'z' });
90          assertTrue(hitByteArray.get());
91          assertEquals(2, target.size());
92          assertArrayEquals(new byte[] { 'y', 'z' }, target.toByteArray());
93      }
94  
95      @Test
96      void testWriteByteArrayAt() throws Exception {
97          assertFalse(hitByteArrayAt.get());
98          proxied.write(new byte[] { 'y', 'z' }, 1, 1);
99          assertTrue(hitByteArrayAt.get());
100         assertEquals(1, target.size());
101         assertArrayEquals(new byte[] { 'z' }, target.toByteArray());
102     }
103 
104     @Test
105     void testWriteByteArrayAtRepeat() throws Exception {
106         // repeat -1
107         proxied.writeRepeat(new byte[] { 'y', 'z' }, 1, 1, 0);
108         assertFalse(hitByteArrayAt.get());
109         hitByteArray.set(false);
110         assertEquals(0, target.size());
111         assertArrayEquals(new byte[] {}, target.toByteArray());
112         // repeat 0
113         proxied.writeRepeat(new byte[] { 'y', 'z' }, 1, 1, 0);
114         assertFalse(hitByteArrayAt.get());
115         hitByteArray.set(false);
116         assertEquals(0, target.size());
117         assertArrayEquals(new byte[] {}, target.toByteArray());
118         // repeat 1
119         proxied.writeRepeat(new byte[] { 'y', 'z' }, 1, 1, 1);
120         assertTrue(hitByteArrayAt.get());
121         hitByteArray.set(false);
122         assertEquals(1, target.size());
123         assertArrayEquals(new byte[] { 'z' }, target.toByteArray());
124         // repeat 2
125         proxied.writeRepeat(new byte[] { 'y', 'x' }, 1, 1, 2);
126         assertTrue(hitByteArrayAt.get());
127         assertEquals(3, target.size());
128         assertArrayEquals(new byte[] { 'z', 'x', 'x' }, target.toByteArray());
129     }
130 
131     @Test
132     void testWriteByteArrayRepeat() throws Exception {
133         // repeat -1
134         proxied.writeRepeat(new byte[] { 'y', 'z' }, -1);
135         assertFalse(hitByteArray.get());
136         hitByteArray.set(false);
137         assertEquals(0, target.size());
138         assertArrayEquals(new byte[] {}, target.toByteArray());
139         // repeat 0
140         proxied.writeRepeat(new byte[] { 'y', 'z' }, 0);
141         assertFalse(hitByteArray.get());
142         hitByteArray.set(false);
143         assertEquals(0, target.size());
144         assertArrayEquals(new byte[] {}, target.toByteArray());
145         // repeat 1
146         proxied.writeRepeat(new byte[] { 'y', 'z' }, 1);
147         assertTrue(hitByteArray.get());
148         hitByteArray.set(false);
149         assertEquals(2, target.size());
150         assertArrayEquals(new byte[] { 'y', 'z' }, target.toByteArray());
151         // repeat 2
152         proxied.writeRepeat(new byte[] { 'y', 'z' }, 2);
153         assertTrue(hitByteArray.get());
154         assertEquals(6, target.size());
155         assertArrayEquals(new byte[] { 'y', 'z', 'y', 'z' , 'y', 'z' }, target.toByteArray());
156     }
157 
158     @Test
159     void testWriteInt() throws Exception {
160         assertFalse(hitInt.get());
161         proxied.write('y');
162         assertTrue(hitInt.get());
163         assertEquals(1, target.size());
164         assertEquals('y', target.toByteArray()[0]);
165     }
166 
167     @Test
168     void testWriteIntRepeat() throws Exception {
169         // repeat -1
170         assertFalse(hitInt.get());
171         proxied.writeRepeat('y', -1);
172         assertFalse(hitInt.get());
173         assertEquals(0, target.size());
174         assertArrayEquals(new byte[] {}, target.toByteArray());
175         // repeat 0
176         assertFalse(hitInt.get());
177         proxied.writeRepeat('y', 0);
178         assertFalse(hitInt.get());
179         assertEquals(0, target.size());
180         assertArrayEquals(new byte[] {}, target.toByteArray());
181         // repeat 1
182         assertFalse(hitInt.get());
183         proxied.writeRepeat('y', 1);
184         assertTrue(hitInt.get());
185         hitInt.set(false);
186         assertEquals(1, target.size());
187         assertArrayEquals(new byte[] { 'y' }, target.toByteArray());
188         // repeat 2
189         assertFalse(hitInt.get());
190         proxied.writeRepeat('z', 2);
191         assertTrue(hitInt.get());
192         hitInt.set(false);
193         assertEquals(3, target.size());
194         assertArrayEquals(new byte[] { 'y', 'z', 'z' }, target.toByteArray());
195     }
196 
197     @Test
198     void testWriteNullArrayProxiesToUnderlying() throws Exception {
199         assertFalse(hitByteArray.get());
200         final byte[] ba = null;
201         assertThrows(NullPointerException.class, () -> target.write(ba));
202         assertTrue(hitByteArray.get());
203         assertThrows(NullPointerException.class, () -> proxied.write(ba));
204         assertTrue(hitByteArray.get());
205     }
206 }