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  
18  package org.apache.commons.io.output;
19  
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.io.IOException;
25  import java.io.OutputStream;
26  import java.util.concurrent.atomic.AtomicBoolean;
27  
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Tests {@link ThresholdingOutputStream}.
32   */
33  public class ThresholdingOutputStreamTest {
34  
35      @Test
36      public void testSetByteCount_OutputStream() throws Exception {
37          final AtomicBoolean reached = new AtomicBoolean(false);
38          try (ThresholdingOutputStream tos = new ThresholdingOutputStream(3) {
39              {
40                  setByteCount(2);
41              }
42  
43              @Override
44              protected OutputStream getOutputStream() throws IOException {
45                  return new ByteArrayOutputStream(4);
46              }
47  
48              @Override
49              protected void thresholdReached() throws IOException {
50                  reached.set(true);
51              }
52          }) {
53              tos.write('a');
54              assertFalse(reached.get());
55              tos.write('a');
56              assertTrue(reached.get());
57          }
58      }
59  
60      @Test
61      public void testSetByteCount_Stream() throws Exception {
62          final AtomicBoolean reached = new AtomicBoolean(false);
63          try (ThresholdingOutputStream tos = new ThresholdingOutputStream(3) {
64              {
65                  setByteCount(2);
66              }
67  
68              @Override
69              protected OutputStream getStream() throws IOException {
70                  return new ByteArrayOutputStream(4);
71              }
72  
73              @Override
74              protected void thresholdReached() throws IOException {
75                  reached.set(true);
76              }
77          }) {
78              tos.write('a');
79              assertFalse(reached.get());
80              tos.write('a');
81              assertTrue(reached.get());
82          }
83      }
84  
85      @Test
86      public void testThresholdIOConsumer() throws Exception {
87          final AtomicBoolean reached = new AtomicBoolean();
88          // Null threshold consumer
89          reached.set(false);
90          try (ThresholdingOutputStream tos = new ThresholdingOutputStream(1, null,
91              os -> new ByteArrayOutputStream(4))) {
92              tos.write('a');
93              assertFalse(reached.get());
94              tos.write('a');
95              assertFalse(reached.get());
96          }
97          // Null output stream function
98          reached.set(false);
99          try (ThresholdingOutputStream tos = new ThresholdingOutputStream(1, os -> reached.set(true), null)) {
100             tos.write('a');
101             assertFalse(reached.get());
102             tos.write('a');
103             assertTrue(reached.get());
104         }
105         // non-null inputs.
106         reached.set(false);
107         try (ThresholdingOutputStream tos = new ThresholdingOutputStream(1, os -> reached.set(true),
108             os -> new ByteArrayOutputStream(4))) {
109             tos.write('a');
110             assertFalse(reached.get());
111             tos.write('a');
112             assertTrue(reached.get());
113         }
114     }
115 
116     @Test
117     public void testThresholdIOConsumerIOException() throws Exception {
118         try (ThresholdingOutputStream tos = new ThresholdingOutputStream(1, os -> {
119             throw new IOException("Threshold reached.");
120         }, os -> new ByteArrayOutputStream(4))) {
121             tos.write('a');
122             assertThrows(IOException.class, () -> tos.write('a'));
123         }
124     }
125 
126     @Test
127     public void testThresholdIOConsumerUncheckedException() throws Exception {
128         try (ThresholdingOutputStream tos = new ThresholdingOutputStream(1, os -> {
129             throw new IllegalStateException("Threshold reached.");
130         }, os -> new ByteArrayOutputStream(4))) {
131             tos.write('a');
132             assertThrows(IllegalStateException.class, () -> tos.write('a'));
133         }
134     }
135 }