11/*
2- * Copyright 2002-2016 the original author or authors.
2+ * Copyright 2002-2018 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1616
1717package org .springframework .util ;
1818
19+ import java .io .ByteArrayInputStream ;
1920import java .io .ByteArrayOutputStream ;
2021import java .io .IOException ;
2122import java .io .InputStream ;
23+ import java .nio .charset .StandardCharsets ;
2224
23- import org .junit .Before ;
2425import org .junit .Test ;
2526
2627import static org .junit .Assert .*;
2728
2829/**
29- * Test suite for {@link FastByteArrayOutputStream}
30+ * Test suite for {@link FastByteArrayOutputStream}.
31+ *
3032 * @author Craig Andrews
3133 */
3234public class FastByteArrayOutputStreamTests {
3335
3436 private static final int INITIAL_CAPACITY = 256 ;
3537
36- private FastByteArrayOutputStream os ;
37-
38- private byte [] helloBytes ;
38+ private final FastByteArrayOutputStream os = new FastByteArrayOutputStream (INITIAL_CAPACITY );;
3939
40-
41- @ Before
42- public void setUp () throws Exception {
43- this .os = new FastByteArrayOutputStream (INITIAL_CAPACITY );
44- this .helloBytes = "Hello World" .getBytes ("UTF-8" );
45- }
40+ private final byte [] helloBytes = "Hello World" .getBytes (StandardCharsets .UTF_8 );;
4641
4742
4843 @ Test
4944 public void size () throws Exception {
5045 this .os .write (this .helloBytes );
51- assertEquals (this .os . size () , this .helloBytes . length );
46+ assertEquals (this .helloBytes . length , this .os . size () );
5247 }
5348
5449 @ Test
@@ -124,17 +119,26 @@ public void getInputStream() throws Exception {
124119 @ Test
125120 public void getInputStreamAvailable () throws Exception {
126121 this .os .write (this .helloBytes );
127- assertEquals (this .os .getInputStream ().available (), this . helloBytes . length );
122+ assertEquals (this .helloBytes . length , this . os .getInputStream ().available ());
128123 }
129124
130125 @ Test
131126 public void getInputStreamRead () throws Exception {
132127 this .os .write (this .helloBytes );
133128 InputStream inputStream = this .os .getInputStream ();
134- assertEquals (inputStream .read (), this .helloBytes [0 ]);
135- assertEquals (inputStream .read (), this .helloBytes [1 ]);
136- assertEquals (inputStream .read (), this .helloBytes [2 ]);
137- assertEquals (inputStream .read (), this .helloBytes [3 ]);
129+ assertEquals (this .helloBytes [0 ], inputStream .read ());
130+ assertEquals (this .helloBytes [1 ], inputStream .read ());
131+ assertEquals (this .helloBytes [2 ], inputStream .read ());
132+ assertEquals (this .helloBytes [3 ], inputStream .read ());
133+ }
134+
135+ @ Test
136+ public void getInputStreamReadBytePromotion () throws Exception {
137+ byte [] bytes = new byte [] { -1 };
138+ this .os .write (bytes );
139+ InputStream inputStream = this .os .getInputStream ();
140+ ByteArrayInputStream bais = new ByteArrayInputStream (bytes );
141+ assertEquals (bais .read (), inputStream .read ());
138142 }
139143
140144 @ Test
@@ -166,17 +170,17 @@ public void getInputStreamReadBeyondEndOfStream() throws Exception {
166170 public void getInputStreamSkip () throws Exception {
167171 this .os .write (this .helloBytes );
168172 InputStream inputStream = this .os .getInputStream ();
169- assertEquals (inputStream . read (), this .helloBytes [0 ]);
170- assertEquals (inputStream .skip (1 ), 1 );
171- assertEquals (inputStream . read (), this .helloBytes [2 ]);
173+ assertEquals (this .helloBytes [0 ], inputStream . read () );
174+ assertEquals (1 , inputStream .skip (1 ));
175+ assertEquals (this .helloBytes [2 ], inputStream . read () );
172176 assertEquals (this .helloBytes .length - 3 , inputStream .available ());
173177 }
174178
175179 @ Test
176180 public void getInputStreamSkipAll () throws Exception {
177181 this .os .write (this .helloBytes );
178182 InputStream inputStream = this .os .getInputStream ();
179- assertEquals (inputStream .skip (1000 ), this . helloBytes . length );
183+ assertEquals (this . helloBytes . length , inputStream .skip (1000 ));
180184 assertEquals (0 , inputStream .available ());
181185 }
182186
@@ -187,8 +191,7 @@ public void updateMessageDigest() throws Exception {
187191 InputStream inputStream = this .os .getInputStream ();
188192 DigestUtils .appendMd5DigestAsHex (inputStream , builder );
189193 builder .append ("\" " );
190- String actual = builder .toString ();
191- assertEquals ("\" 0b10a8db164e0754105b7a99be72e3fe5\" " , actual );
194+ assertEquals ("\" 0b10a8db164e0754105b7a99be72e3fe5\" " , builder .toString ());
192195 }
193196
194197 @ Test
@@ -201,8 +204,7 @@ public void updateMessageDigestManyBuffers() throws Exception {
201204 InputStream inputStream = this .os .getInputStream ();
202205 DigestUtils .appendMd5DigestAsHex (inputStream , builder );
203206 builder .append ("\" " );
204- String actual = builder .toString ();
205- assertEquals ("\" 06225ca1e4533354c516e74512065331d\" " , actual );
207+ assertEquals ("\" 06225ca1e4533354c516e74512065331d\" " , builder .toString ());
206208 }
207209
208210
0 commit comments