1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
32
33 public class ThresholdingOutputStreamTest {
34
35 @Test
36 public void testSetByteCount_OutputStream() throws Exception {
37 final AtomicBoolean reached = new AtomicBoolean();
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();
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
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
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
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
136
137
138
139
140 @Test
141 public void testThresholdLessThanZero() throws IOException {
142 final AtomicBoolean reached = new AtomicBoolean();
143 try (final ThresholdingOutputStream out = new ThresholdingOutputStream(-1) {
144 @Override
145 protected void thresholdReached() throws IOException {
146 reached.set(true);
147 }
148 }) {
149 assertFalse(reached.get());
150 out.write(89);
151 assertTrue(reached.get());
152 assertTrue(out.isThresholdExceeded());
153 }
154 }
155
156 @Test
157 public void testThresholdZero() throws IOException {
158 final AtomicBoolean reached = new AtomicBoolean();
159 try (final ThresholdingOutputStream out = new ThresholdingOutputStream(0) {
160 @Override
161 protected void thresholdReached() throws IOException {
162 reached.set(true);
163 }
164 }) {
165 assertFalse(out.isThresholdExceeded());
166 out.write(89);
167 assertTrue(reached.get());
168 assertTrue(out.isThresholdExceeded());
169 }
170 }
171
172
173
174
175
176 @Test
177 public void testThresholdZeroWrite() throws IOException {
178 final AtomicBoolean reached = new AtomicBoolean();
179 try (final ThresholdingOutputStream out = new ThresholdingOutputStream(7) {
180 @Override
181 protected void thresholdReached() throws IOException {
182 reached.set(true);
183 }
184 }) {
185 assertFalse(out.isThresholdExceeded());
186 assertFalse(reached.get());
187 out.write(new byte[0]);
188 assertFalse(out.isThresholdExceeded());
189 assertFalse(reached.get());
190 }
191 }
192 }