1
1
/*
2
- * Copyright 2002-2015 the original author or authors.
2
+ * Copyright 2002-2016 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 static org .junit .Assert .*;
20
-
21
19
import java .io .ByteArrayOutputStream ;
22
20
import java .io .IOException ;
23
21
import java .io .InputStream ;
24
22
25
23
import org .junit .Before ;
26
24
import org .junit .Test ;
27
25
26
+ import static org .junit .Assert .*;
27
+
28
28
/**
29
29
* Test suite for {@link FastByteArrayOutputStream}
30
30
* @author Craig Andrews
@@ -37,21 +37,23 @@ public class FastByteArrayOutputStreamTests {
37
37
38
38
private byte [] helloBytes ;
39
39
40
+
40
41
@ Before
41
42
public void setUp () throws Exception {
42
43
this .os = new FastByteArrayOutputStream (INITIAL_CAPACITY );
43
44
this .helloBytes = "Hello World" .getBytes ("UTF-8" );
44
45
}
45
46
47
+
46
48
@ Test
47
49
public void size () throws Exception {
48
- this .os .write (helloBytes );
49
- assertEquals (this .os .size (), helloBytes .length );
50
+ this .os .write (this . helloBytes );
51
+ assertEquals (this .os .size (), this . helloBytes .length );
50
52
}
51
53
52
54
@ Test
53
55
public void resize () throws Exception {
54
- this .os .write (helloBytes );
56
+ this .os .write (this . helloBytes );
55
57
int sizeBefore = this .os .size ();
56
58
this .os .resize (64 );
57
59
assertByteArrayEqualsString (this .os );
@@ -70,104 +72,118 @@ public void autoGrow() throws IOException {
70
72
71
73
@ Test
72
74
public void write () throws Exception {
73
- this .os .write (helloBytes );
75
+ this .os .write (this . helloBytes );
74
76
assertByteArrayEqualsString (this .os );
75
77
}
76
78
77
79
@ Test
78
80
public void reset () throws Exception {
79
- this .os .write (helloBytes );
81
+ this .os .write (this . helloBytes );
80
82
assertByteArrayEqualsString (this .os );
81
83
this .os .reset ();
82
84
assertEquals (0 , this .os .size ());
83
- this .os .write (helloBytes );
85
+ this .os .write (this . helloBytes );
84
86
assertByteArrayEqualsString (this .os );
85
87
}
86
88
87
89
@ Test (expected = IOException .class )
88
90
public void close () throws Exception {
89
91
this .os .close ();
90
- this .os .write (helloBytes );
92
+ this .os .write (this . helloBytes );
91
93
}
92
94
93
95
@ Test
94
96
public void toByteArrayUnsafe () throws Exception {
95
- this .os .write (helloBytes );
97
+ this .os .write (this . helloBytes );
96
98
assertByteArrayEqualsString (this .os );
97
99
assertSame (this .os .toByteArrayUnsafe (), this .os .toByteArrayUnsafe ());
98
- assertArrayEquals (this .os .toByteArray (), helloBytes );
100
+ assertArrayEquals (this .os .toByteArray (), this . helloBytes );
99
101
}
100
102
101
103
@ Test
102
104
public void writeTo () throws Exception {
103
- this .os .write (helloBytes );
105
+ this .os .write (this . helloBytes );
104
106
assertByteArrayEqualsString (this .os );
105
107
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
106
108
this .os .writeTo (baos );
107
- assertArrayEquals (baos .toByteArray (), helloBytes );
109
+ assertArrayEquals (baos .toByteArray (), this . helloBytes );
108
110
}
109
111
110
112
@ Test (expected = IllegalArgumentException .class )
111
113
public void failResize () throws Exception {
112
- this .os .write (helloBytes );
114
+ this .os .write (this . helloBytes );
113
115
this .os .resize (5 );
114
116
}
115
117
116
118
@ Test
117
119
public void getInputStream () throws Exception {
118
- this .os .write (helloBytes );
120
+ this .os .write (this . helloBytes );
119
121
assertNotNull (this .os .getInputStream ());
120
122
}
121
123
122
124
@ Test
123
125
public void getInputStreamAvailable () throws Exception {
124
- this .os .write (helloBytes );
125
- assertEquals (this .os .getInputStream ().available (), helloBytes .length );
126
+ this .os .write (this . helloBytes );
127
+ assertEquals (this .os .getInputStream ().available (), this . helloBytes .length );
126
128
}
127
129
128
130
@ Test
129
131
public void getInputStreamRead () throws Exception {
130
- this .os .write (helloBytes );
132
+ this .os .write (this . helloBytes );
131
133
InputStream inputStream = this .os .getInputStream ();
132
- assertEquals (inputStream .read (), helloBytes [0 ]);
133
- assertEquals (inputStream .read (), helloBytes [1 ]);
134
- assertEquals (inputStream .read (), helloBytes [2 ]);
135
- assertEquals (inputStream .read (), helloBytes [3 ]);
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 ]);
136
138
}
137
139
138
140
@ Test
139
141
public void getInputStreamReadAll () throws Exception {
140
- this .os .write (helloBytes );
142
+ this .os .write (this . helloBytes );
141
143
InputStream inputStream = this .os .getInputStream ();
142
144
byte [] actual = new byte [inputStream .available ()];
143
145
int bytesRead = inputStream .read (actual );
144
- assertEquals (bytesRead , helloBytes .length );
145
- assertArrayEquals (actual , helloBytes );
146
+ assertEquals (this .helloBytes .length , bytesRead );
147
+ assertArrayEquals (this .helloBytes , actual );
148
+ assertEquals (0 , inputStream .available ());
149
+ }
150
+
151
+ @ Test
152
+ public void getInputStreamReadBeyondEndOfStream () throws Exception {
153
+ this .os .write (this .helloBytes );
154
+ InputStream inputStream = os .getInputStream ();
155
+ byte [] actual = new byte [inputStream .available () + 1 ];
156
+ int bytesRead = inputStream .read (actual );
157
+ assertEquals (this .helloBytes .length , bytesRead );
158
+ for (int i = 0 ; i < bytesRead ; i ++) {
159
+ assertEquals (this .helloBytes [i ], actual [i ]);
160
+ }
161
+ assertEquals (0 , actual [this .helloBytes .length ]);
146
162
assertEquals (0 , inputStream .available ());
147
163
}
148
164
149
165
@ Test
150
166
public void getInputStreamSkip () throws Exception {
151
- this .os .write (helloBytes );
167
+ this .os .write (this . helloBytes );
152
168
InputStream inputStream = this .os .getInputStream ();
153
- assertEquals (inputStream .read (), helloBytes [0 ]);
169
+ assertEquals (inputStream .read (), this . helloBytes [0 ]);
154
170
assertEquals (inputStream .skip (1 ), 1 );
155
- assertEquals (inputStream .read (), helloBytes [2 ]);
156
- assertEquals (helloBytes .length - 3 , inputStream .available ());
171
+ assertEquals (inputStream .read (), this . helloBytes [2 ]);
172
+ assertEquals (this . helloBytes .length - 3 , inputStream .available ());
157
173
}
158
174
159
175
@ Test
160
176
public void getInputStreamSkipAll () throws Exception {
161
- this .os .write (helloBytes );
177
+ this .os .write (this . helloBytes );
162
178
InputStream inputStream = this .os .getInputStream ();
163
- assertEquals (inputStream .skip (1000 ), helloBytes .length );
179
+ assertEquals (inputStream .skip (1000 ), this . helloBytes .length );
164
180
assertEquals (0 , inputStream .available ());
165
181
}
166
182
167
183
@ Test
168
184
public void updateMessageDigest () throws Exception {
169
185
StringBuilder builder = new StringBuilder ("\" 0" );
170
- this .os .write (helloBytes );
186
+ this .os .write (this . helloBytes );
171
187
InputStream inputStream = this .os .getInputStream ();
172
188
DigestUtils .appendMd5DigestAsHex (inputStream , builder );
173
189
builder .append ("\" " );
@@ -180,7 +196,7 @@ public void updateMessageDigestManyBuffers() throws Exception {
180
196
StringBuilder builder = new StringBuilder ("\" 0" );
181
197
// filling at least one 256 buffer
182
198
for ( int i = 0 ; i < 30 ; i ++) {
183
- this .os .write (helloBytes );
199
+ this .os .write (this . helloBytes );
184
200
}
185
201
InputStream inputStream = this .os .getInputStream ();
186
202
DigestUtils .appendMd5DigestAsHex (inputStream , builder );
@@ -189,8 +205,9 @@ public void updateMessageDigestManyBuffers() throws Exception {
189
205
assertEquals ("\" 06225ca1e4533354c516e74512065331d\" " , actual );
190
206
}
191
207
208
+
192
209
private void assertByteArrayEqualsString (FastByteArrayOutputStream actual ) {
193
- assertArrayEquals (helloBytes , actual .toByteArray ());
210
+ assertArrayEquals (this . helloBytes , actual .toByteArray ());
194
211
}
195
212
196
213
}
0 commit comments