1
1
/*
2
- * Copyright 2002-2016 the original author or authors.
2
+ * Copyright 2002-2018 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .util ;
18
18
19
+ import java .io .ByteArrayInputStream ;
19
20
import java .io .ByteArrayOutputStream ;
20
21
import java .io .IOException ;
21
22
import java .io .InputStream ;
23
+ import java .nio .charset .StandardCharsets ;
22
24
23
- import org .junit .Before ;
24
25
import org .junit .Test ;
25
26
26
27
import static org .junit .Assert .*;
27
28
28
29
/**
29
- * Test suite for {@link FastByteArrayOutputStream}
30
+ * Test suite for {@link FastByteArrayOutputStream}.
31
+ *
30
32
* @author Craig Andrews
31
33
*/
32
34
public class FastByteArrayOutputStreamTests {
33
35
34
36
private static final int INITIAL_CAPACITY = 256 ;
35
37
36
- private FastByteArrayOutputStream os ;
37
-
38
- private byte [] helloBytes ;
38
+ private final FastByteArrayOutputStream os = new FastByteArrayOutputStream (INITIAL_CAPACITY );;
39
39
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 );;
46
41
47
42
48
43
@ Test
49
44
public void size () throws Exception {
50
45
this .os .write (this .helloBytes );
51
- assertEquals (this .os . size () , this .helloBytes . length );
46
+ assertEquals (this .helloBytes . length , this .os . size () );
52
47
}
53
48
54
49
@ Test
@@ -124,17 +119,26 @@ public void getInputStream() throws Exception {
124
119
@ Test
125
120
public void getInputStreamAvailable () throws Exception {
126
121
this .os .write (this .helloBytes );
127
- assertEquals (this .os .getInputStream ().available (), this . helloBytes . length );
122
+ assertEquals (this .helloBytes . length , this . os .getInputStream ().available ());
128
123
}
129
124
130
125
@ Test
131
126
public void getInputStreamRead () throws Exception {
132
127
this .os .write (this .helloBytes );
133
128
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 ());
138
142
}
139
143
140
144
@ Test
@@ -166,17 +170,17 @@ public void getInputStreamReadBeyondEndOfStream() throws Exception {
166
170
public void getInputStreamSkip () throws Exception {
167
171
this .os .write (this .helloBytes );
168
172
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 () );
172
176
assertEquals (this .helloBytes .length - 3 , inputStream .available ());
173
177
}
174
178
175
179
@ Test
176
180
public void getInputStreamSkipAll () throws Exception {
177
181
this .os .write (this .helloBytes );
178
182
InputStream inputStream = this .os .getInputStream ();
179
- assertEquals (inputStream .skip (1000 ), this . helloBytes . length );
183
+ assertEquals (this . helloBytes . length , inputStream .skip (1000 ));
180
184
assertEquals (0 , inputStream .available ());
181
185
}
182
186
@@ -187,8 +191,7 @@ public void updateMessageDigest() throws Exception {
187
191
InputStream inputStream = this .os .getInputStream ();
188
192
DigestUtils .appendMd5DigestAsHex (inputStream , builder );
189
193
builder .append ("\" " );
190
- String actual = builder .toString ();
191
- assertEquals ("\" 0b10a8db164e0754105b7a99be72e3fe5\" " , actual );
194
+ assertEquals ("\" 0b10a8db164e0754105b7a99be72e3fe5\" " , builder .toString ());
192
195
}
193
196
194
197
@ Test
@@ -201,8 +204,7 @@ public void updateMessageDigestManyBuffers() throws Exception {
201
204
InputStream inputStream = this .os .getInputStream ();
202
205
DigestUtils .appendMd5DigestAsHex (inputStream , builder );
203
206
builder .append ("\" " );
204
- String actual = builder .toString ();
205
- assertEquals ("\" 06225ca1e4533354c516e74512065331d\" " , actual );
207
+ assertEquals ("\" 06225ca1e4533354c516e74512065331d\" " , builder .toString ());
206
208
}
207
209
208
210
0 commit comments