1616 */
1717package org .apache .commons .io .input ;
1818
19- import static java .nio .charset .StandardCharsets .UTF_8 ;
2019import static org .junit .jupiter .api .Assertions .assertEquals ;
2120import static org .junit .jupiter .api .Assertions .assertThrows ;
2221import static org .junit .jupiter .api .Assertions .assertTimeout ;
2322import static org .junit .jupiter .api .Assertions .assertTrue ;
2423
25- import com .google .common .base .Stopwatch ;
26- import org .apache .commons .io .IOUtils ;
27- import org .apache .commons .io .output .QueueOutputStream ;
28- import org .apache .commons .io .output .QueueOutputStreamTest ;
29- import org .apache .commons .lang3 .StringUtils ;
30- import org .junit .jupiter .api .DisplayName ;
31- import org .junit .jupiter .api .Test ;
32- import org .junit .jupiter .params .ParameterizedTest ;
33- import org .junit .jupiter .params .provider .Arguments ;
34- import org .junit .jupiter .params .provider .MethodSource ;
35-
3624import java .io .BufferedInputStream ;
3725import java .io .BufferedOutputStream ;
3826import java .io .ByteArrayOutputStream ;
4735import java .util .concurrent .atomic .AtomicReference ;
4836import java .util .stream .Stream ;
4937
38+ import org .apache .commons .io .IOUtils ;
39+ import org .apache .commons .io .output .QueueOutputStream ;
40+ import org .apache .commons .io .output .QueueOutputStreamTest ;
41+ import org .apache .commons .lang3 .StringUtils ;
42+ import org .junit .jupiter .api .DisplayName ;
43+ import org .junit .jupiter .api .Test ;
44+ import org .junit .jupiter .params .ParameterizedTest ;
45+ import org .junit .jupiter .params .provider .Arguments ;
46+ import org .junit .jupiter .params .provider .MethodSource ;
47+
48+ import com .google .common .base .Stopwatch ;
49+
5050/**
5151 * Test {@link QueueInputStream}.
5252 *
53- * @see {@link QueueOutputStreamTest}
53+ * @see QueueOutputStreamTest
5454 */
5555public class QueueInputStreamTest {
5656
@@ -71,80 +71,78 @@ public static Stream<Arguments> inputData() {
7171 // @formatter:on
7272 }
7373
74+ private int defaultBufferSize () {
75+ return 8192 ;
76+ }
77+
78+ private String readUnbuffered (final InputStream inputStream ) throws IOException {
79+ return readUnbuffered (inputStream , Integer .MAX_VALUE );
80+ }
81+
82+ private String readUnbuffered (final InputStream inputStream , final int maxBytes ) throws IOException {
83+ if (maxBytes == 0 ) {
84+ return "" ;
85+ }
86+
87+ final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream ();
88+ int n = -1 ;
89+ while ((n = inputStream .read ()) != -1 ) {
90+ byteArrayOutputStream .write (n );
91+ if (byteArrayOutputStream .size () >= maxBytes ) {
92+ break ;
93+ }
94+ }
95+ return byteArrayOutputStream .toString (StandardCharsets .UTF_8 .name ());
96+ }
97+
7498 @ ParameterizedTest (name = "inputData={0}" )
7599 @ MethodSource ("inputData" )
76- public void bufferedReads (final String inputData ) throws IOException {
100+ public void testBufferedReads (final String inputData ) throws IOException {
77101 final BlockingQueue <Integer > queue = new LinkedBlockingQueue <>();
78102 try (BufferedInputStream inputStream = new BufferedInputStream (new QueueInputStream (queue ));
79103 final QueueOutputStream outputStream = new QueueOutputStream (queue )) {
80- outputStream .write (inputData .getBytes (UTF_8 ));
81- final String actualData = IOUtils .toString (inputStream , UTF_8 );
104+ outputStream .write (inputData .getBytes (StandardCharsets . UTF_8 ));
105+ final String actualData = IOUtils .toString (inputStream , StandardCharsets . UTF_8 );
82106 assertEquals (inputData , actualData );
83107 }
84108 }
85109
86110 @ ParameterizedTest (name = "inputData={0}" )
87111 @ MethodSource ("inputData" )
88- public void bufferedReadWrite (final String inputData ) throws IOException {
112+ public void testBufferedReadWrite (final String inputData ) throws IOException {
89113 final BlockingQueue <Integer > queue = new LinkedBlockingQueue <>();
90114 try (BufferedInputStream inputStream = new BufferedInputStream (new QueueInputStream (queue ));
91115 final BufferedOutputStream outputStream = new BufferedOutputStream (new QueueOutputStream (queue ), defaultBufferSize ())) {
92- outputStream .write (inputData .getBytes (UTF_8 ));
116+ outputStream .write (inputData .getBytes (StandardCharsets . UTF_8 ));
93117 outputStream .flush ();
94- final String dataCopy = IOUtils .toString (inputStream , UTF_8 );
118+ final String dataCopy = IOUtils .toString (inputStream , StandardCharsets . UTF_8 );
95119 assertEquals (inputData , dataCopy );
96120 }
97121 }
98122
99123 @ ParameterizedTest (name = "inputData={0}" )
100124 @ MethodSource ("inputData" )
101- public void bufferedWrites (final String inputData ) throws IOException {
125+ public void testBufferedWrites (final String inputData ) throws IOException {
102126 final BlockingQueue <Integer > queue = new LinkedBlockingQueue <>();
103127 try (QueueInputStream inputStream = new QueueInputStream (queue );
104128 final BufferedOutputStream outputStream = new BufferedOutputStream (new QueueOutputStream (queue ), defaultBufferSize ())) {
105- outputStream .write (inputData .getBytes (UTF_8 ));
129+ outputStream .write (inputData .getBytes (StandardCharsets . UTF_8 ));
106130 outputStream .flush ();
107131 final String actualData = readUnbuffered (inputStream );
108132 assertEquals (inputData , actualData );
109133 }
110134 }
111135
112- private int defaultBufferSize () {
113- return 8192 ;
114- }
115-
116136 @ Test
117- public void invalidArguments () {
137+ public void testInvalidArguments () {
118138 assertThrows (NullPointerException .class , () -> new QueueInputStream (null ), "queue is required" );
119- assertThrows (NullPointerException .class , () -> new QueueInputStream (new LinkedBlockingQueue <>(), null ), "waitTime is required" );
120- assertThrows (IllegalArgumentException .class , () -> new QueueInputStream (new LinkedBlockingQueue <>(), Duration .ofMillis (-1 )),
121- "waitTime must not be negative" );
122- }
123-
124- private String readUnbuffered (final InputStream inputStream ) throws IOException {
125- return readUnbuffered (inputStream , Integer .MAX_VALUE );
126- }
127-
128- private String readUnbuffered (final InputStream inputStream , final int maxBytes ) throws IOException {
129- if (maxBytes == 0 ) {
130- return "" ;
131- }
132-
133- final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream ();
134- int n = -1 ;
135- while ((n = inputStream .read ()) != -1 ) {
136- byteArrayOutputStream .write (n );
137- if (byteArrayOutputStream .size () >= maxBytes ) {
138- break ;
139- }
140- }
141- return byteArrayOutputStream .toString (StandardCharsets .UTF_8 .name ());
139+ assertThrows (IllegalArgumentException .class , () -> QueueInputStream .builder ().setTimeout (Duration .ofMillis (-1 )).get (), "waitTime must not be negative" );
142140 }
143141
144142 @ Test
145143 @ DisplayName ("If read is interrupted while waiting, then exception is thrown" )
146- public void timeoutInterrupted () throws Exception {
147- try (QueueInputStream inputStream = new QueueInputStream ( new LinkedBlockingQueue <>(), Duration .ofMinutes (2 ));
144+ public void testTimeoutInterrupted () throws Exception {
145+ try (QueueInputStream inputStream = QueueInputStream . builder (). setTimeout ( Duration .ofMinutes (2 )). get ( );
148146 final QueueOutputStream outputStream = inputStream .newQueueOutputStream ()) {
149147
150148 // read in a background thread
@@ -169,22 +167,21 @@ public void timeoutInterrupted() throws Exception {
169167
170168 @ Test
171169 @ DisplayName ("If data is not available in queue, then read will wait until wait time elapses" )
172- public void timeoutUnavailableData () throws IOException {
173- try (QueueInputStream inputStream = new QueueInputStream ( new LinkedBlockingQueue <>(), Duration .ofMillis (500 ));
170+ public void testTimeoutUnavailableData () throws IOException {
171+ try (QueueInputStream inputStream = QueueInputStream . builder (). setTimeout ( Duration .ofMillis (500 )). get ( );
174172 final QueueOutputStream outputStream = inputStream .newQueueOutputStream ()) {
175-
176173 final Stopwatch stopwatch = Stopwatch .createStarted ();
177174 final String actualData = assertTimeout (Duration .ofSeconds (1 ), () -> readUnbuffered (inputStream , 3 ));
178175 stopwatch .stop ();
179176 assertEquals ("" , actualData );
180177
181- assertTrue (stopwatch .elapsed (TimeUnit .MILLISECONDS ) >= 500 );
178+ assertTrue (stopwatch .elapsed (TimeUnit .MILLISECONDS ) >= 500 , () -> stopwatch . toString () );
182179 }
183180 }
184181
185182 @ ParameterizedTest (name = "inputData={0}" )
186183 @ MethodSource ("inputData" )
187- public void unbufferedReadWrite (final String inputData ) throws IOException {
184+ public void testUnbufferedReadWrite (final String inputData ) throws IOException {
188185 try (QueueInputStream inputStream = new QueueInputStream ();
189186 final QueueOutputStream outputStream = inputStream .newQueueOutputStream ()) {
190187 writeUnbuffered (outputStream , inputData );
@@ -195,8 +192,8 @@ public void unbufferedReadWrite(final String inputData) throws IOException {
195192
196193 @ ParameterizedTest (name = "inputData={0}" )
197194 @ MethodSource ("inputData" )
198- public void unbufferedReadWriteWithTimeout (final String inputData ) throws IOException {
199- try (QueueInputStream inputStream = new QueueInputStream ( new LinkedBlockingQueue <>(), Duration .ofMinutes (2 ));
195+ public void testUnbufferedReadWriteWithTimeout (final String inputData ) throws IOException {
196+ try (QueueInputStream inputStream = QueueInputStream . builder (). setTimeout ( Duration .ofMinutes (2 )). get ( );
200197 final QueueOutputStream outputStream = inputStream .newQueueOutputStream ()) {
201198 writeUnbuffered (outputStream , inputData );
202199 final String actualData = assertTimeout (Duration .ofSeconds (1 ), () -> readUnbuffered (inputStream , inputData .length ()));
@@ -205,7 +202,7 @@ public void unbufferedReadWriteWithTimeout(final String inputData) throws IOExce
205202 }
206203
207204 private void writeUnbuffered (final QueueOutputStream outputStream , final String inputData ) throws IOException {
208- final byte [] bytes = inputData .getBytes (UTF_8 );
205+ final byte [] bytes = inputData .getBytes (StandardCharsets . UTF_8 );
209206 outputStream .write (bytes , 0 , bytes .length );
210207 }
211208}
0 commit comments