@@ -84,10 +84,45 @@ public static Stream<Arguments> inputData() {
8484 // @formatter:on
8585 }
8686
87+ @ TestFactory
88+ public DynamicTest [] bulkReadErrorHandlingTests () {
89+ final QueueInputStream queueInputStream = new QueueInputStream ();
90+ return new DynamicTest [] {
91+ dynamicTest ("Offset too big" , () ->
92+ assertThrows (IndexOutOfBoundsException .class , () ->
93+ queueInputStream .read (EMPTY_BYTE_ARRAY , 1 , 0 ))),
94+
95+ dynamicTest ("Offset negative" , () ->
96+ assertThrows (IndexOutOfBoundsException .class , () ->
97+ queueInputStream .read (EMPTY_BYTE_ARRAY , -1 , 0 ))),
98+
99+ dynamicTest ("Length too big" , () ->
100+ assertThrows (IndexOutOfBoundsException .class , () ->
101+ queueInputStream .read (EMPTY_BYTE_ARRAY , 0 , 1 ))),
102+
103+ dynamicTest ("Length negative" , () ->
104+ assertThrows (IndexOutOfBoundsException .class , () ->
105+ queueInputStream .read (EMPTY_BYTE_ARRAY , 0 , -1 ))),
106+ };
107+ }
108+
87109 private int defaultBufferSize () {
88110 return 8192 ;
89111 }
90112
113+ private void doTestReadLineByLine (final String inputData , final InputStream inputStream , final OutputStream outputStream ) throws IOException {
114+ final String [] lines = inputData .split ("\n " );
115+ try (BufferedReader reader = new BufferedReader (new InputStreamReader (inputStream , UTF_8 ))) {
116+ for (final String line : lines ) {
117+ outputStream .write (line .getBytes (UTF_8 ));
118+ outputStream .write ('\n' );
119+
120+ final String actualLine = reader .readLine ();
121+ assertEquals (line , actualLine );
122+ }
123+ }
124+ }
125+
91126 private String readUnbuffered (final InputStream inputStream ) throws IOException {
92127 return readUnbuffered (inputStream , Integer .MAX_VALUE );
93128 }
@@ -146,74 +181,30 @@ void testBufferedReads(final String inputData) throws IOException {
146181
147182 @ ParameterizedTest (name = "inputData={0}" )
148183 @ MethodSource ("inputData" )
149- void testReadLineByLineQueue (final String inputData ) throws IOException {
150- final String [] lines = inputData .split ("\n " );
184+ void testBufferedReadWrite (final String inputData ) throws IOException {
151185 final BlockingQueue <Integer > queue = new LinkedBlockingQueue <>();
152- try (QueueInputStream inputStream = QueueInputStream .builder ()
153- .setBlockingQueue (queue )
154- .setTimeout (Duration .ofHours (1 ))
155- .get ();
156- QueueOutputStream outputStream = inputStream .newQueueOutputStream ()) {
157-
158- doTestReadLineByLine (inputData , inputStream , outputStream );
186+ try (BufferedInputStream inputStream = new BufferedInputStream (new QueueInputStream (queue ));
187+ BufferedOutputStream outputStream = new BufferedOutputStream (new QueueOutputStream (queue ), defaultBufferSize ())) {
188+ outputStream .write (inputData .getBytes (StandardCharsets .UTF_8 ));
189+ outputStream .flush ();
190+ final String dataCopy = IOUtils .toString (inputStream , StandardCharsets .UTF_8 );
191+ assertEquals (inputData , dataCopy );
159192 }
160193 }
161194
162195 @ ParameterizedTest (name = "inputData={0}" )
163196 @ MethodSource ("inputData" )
164- void testReadLineByLineFile (final String inputData ) throws IOException {
165- final Path tempFile = Files .createTempFile (getClass ().getSimpleName (), ".txt" );
166- try (InputStream inputStream = Files .newInputStream (tempFile );
167- OutputStream outputStream = Files .newOutputStream (tempFile )) {
168-
169- doTestReadLineByLine (inputData , inputStream , outputStream );
170- } finally {
171- Files .delete (tempFile );
172- }
173- }
174-
175- private void doTestReadLineByLine (final String inputData , final InputStream inputStream , final OutputStream outputStream ) throws IOException {
176- final String [] lines = inputData .split ("\n " );
177- try (BufferedReader reader = new BufferedReader (new InputStreamReader (inputStream , UTF_8 ))) {
178- for (final String line : lines ) {
179- outputStream .write (line .getBytes (UTF_8 ));
180- outputStream .write ('\n' );
181-
182- final String actualLine = reader .readLine ();
183- assertEquals (line , actualLine );
184- }
197+ void testBufferedWrites (final String inputData ) throws IOException {
198+ final BlockingQueue <Integer > queue = new LinkedBlockingQueue <>();
199+ try (QueueInputStream inputStream = new QueueInputStream (queue );
200+ BufferedOutputStream outputStream = new BufferedOutputStream (new QueueOutputStream (queue ), defaultBufferSize ())) {
201+ outputStream .write (inputData .getBytes (StandardCharsets .UTF_8 ));
202+ outputStream .flush ();
203+ final String actualData = readUnbuffered (inputStream );
204+ assertEquals (inputData , actualData );
185205 }
186206 }
187207
188- @ TestFactory
189- public DynamicTest [] bulkReadErrorHandlingTests () {
190- final QueueInputStream queueInputStream = new QueueInputStream ();
191- return new DynamicTest [] {
192- dynamicTest ("Offset too big" , () ->
193- assertThrows (IndexOutOfBoundsException .class , () ->
194- queueInputStream .read (EMPTY_BYTE_ARRAY , 1 , 0 ))),
195-
196- dynamicTest ("Offset negative" , () ->
197- assertThrows (IndexOutOfBoundsException .class , () ->
198- queueInputStream .read (EMPTY_BYTE_ARRAY , -1 , 0 ))),
199-
200- dynamicTest ("Length too big" , () ->
201- assertThrows (IndexOutOfBoundsException .class , () ->
202- queueInputStream .read (EMPTY_BYTE_ARRAY , 0 , 1 ))),
203-
204- dynamicTest ("Length negative" , () ->
205- assertThrows (IndexOutOfBoundsException .class , () ->
206- queueInputStream .read (EMPTY_BYTE_ARRAY , 0 , -1 ))),
207- };
208- }
209-
210- @ Test
211- void testBulkReadZeroLength () {
212- final QueueInputStream queueInputStream = new QueueInputStream ();
213- final int read = queueInputStream .read (EMPTY_BYTE_ARRAY , 0 , 0 );
214- assertEquals (0 , read );
215- }
216-
217208 @ ParameterizedTest (name = "inputData={0}" )
218209 @ MethodSource ("inputData" )
219210 void testBulkReadWaiting (final String inputData ) throws IOException {
@@ -256,30 +247,11 @@ public Integer poll(final long timeout, final TimeUnit unit) throws InterruptedE
256247 }
257248 }
258249
259- @ ParameterizedTest (name = "inputData={0}" )
260- @ MethodSource ("inputData" )
261- void testBufferedReadWrite (final String inputData ) throws IOException {
262- final BlockingQueue <Integer > queue = new LinkedBlockingQueue <>();
263- try (BufferedInputStream inputStream = new BufferedInputStream (new QueueInputStream (queue ));
264- BufferedOutputStream outputStream = new BufferedOutputStream (new QueueOutputStream (queue ), defaultBufferSize ())) {
265- outputStream .write (inputData .getBytes (StandardCharsets .UTF_8 ));
266- outputStream .flush ();
267- final String dataCopy = IOUtils .toString (inputStream , StandardCharsets .UTF_8 );
268- assertEquals (inputData , dataCopy );
269- }
270- }
271-
272- @ ParameterizedTest (name = "inputData={0}" )
273- @ MethodSource ("inputData" )
274- void testBufferedWrites (final String inputData ) throws IOException {
275- final BlockingQueue <Integer > queue = new LinkedBlockingQueue <>();
276- try (QueueInputStream inputStream = new QueueInputStream (queue );
277- BufferedOutputStream outputStream = new BufferedOutputStream (new QueueOutputStream (queue ), defaultBufferSize ())) {
278- outputStream .write (inputData .getBytes (StandardCharsets .UTF_8 ));
279- outputStream .flush ();
280- final String actualData = readUnbuffered (inputStream );
281- assertEquals (inputData , actualData );
282- }
250+ @ Test
251+ void testBulkReadZeroLength () {
252+ final QueueInputStream queueInputStream = new QueueInputStream ();
253+ final int read = queueInputStream .read (EMPTY_BYTE_ARRAY , 0 , 0 );
254+ assertEquals (0 , read );
283255 }
284256
285257 @ Test
@@ -299,6 +271,34 @@ void testReadAfterClose(final String inputData) throws IOException {
299271 assertEquals (IOUtils .EOF , shadow .read ());
300272 }
301273
274+ @ ParameterizedTest (name = "inputData={0}" )
275+ @ MethodSource ("inputData" )
276+ void testReadLineByLineFile (final String inputData ) throws IOException {
277+ final Path tempFile = Files .createTempFile (getClass ().getSimpleName (), ".txt" );
278+ try (InputStream inputStream = Files .newInputStream (tempFile );
279+ OutputStream outputStream = Files .newOutputStream (tempFile )) {
280+
281+ doTestReadLineByLine (inputData , inputStream , outputStream );
282+ } finally {
283+ Files .delete (tempFile );
284+ }
285+ }
286+
287+ @ ParameterizedTest (name = "inputData={0}" )
288+ @ MethodSource ("inputData" )
289+ void testReadLineByLineQueue (final String inputData ) throws IOException {
290+ final String [] lines = inputData .split ("\n " );
291+ final BlockingQueue <Integer > queue = new LinkedBlockingQueue <>();
292+ try (QueueInputStream inputStream = QueueInputStream .builder ()
293+ .setBlockingQueue (queue )
294+ .setTimeout (Duration .ofHours (1 ))
295+ .get ();
296+ QueueOutputStream outputStream = inputStream .newQueueOutputStream ()) {
297+
298+ doTestReadLineByLine (inputData , inputStream , outputStream );
299+ }
300+ }
301+
302302 @ Test
303303 void testResetArguments () throws IOException {
304304 try (QueueInputStream queueInputStream = QueueInputStream .builder ().setTimeout (null ).get ()) {
0 commit comments