1
1
package com .fasterxml .jackson .core .main ;
2
2
3
-
4
3
import com .fasterxml .jackson .core .*;
4
+ import com .fasterxml .jackson .core .testsupport .ByteOutputStreamForTesting ;
5
+ import com .fasterxml .jackson .core .testsupport .StringWriterForTesting ;
5
6
6
7
import java .io .*;
7
8
19
20
*/
20
21
public class TestGeneratorClosing extends BaseTest
21
22
{
22
- /*
23
- /**********************************************************
24
- /* Helper classes
25
- /**********************************************************
26
- */
27
-
28
- final static class MyWriter extends StringWriter
29
- {
30
- boolean _isClosed = false ;
31
-
32
- public MyWriter () { }
33
-
34
- @ Override
35
- public void close () throws IOException {
36
- _isClosed = true ;
37
- super .close ();
38
- }
39
- public boolean isClosed () { return _isClosed ; }
40
- }
41
-
42
- final static class MyStream extends ByteArrayOutputStream
43
- {
44
- boolean _isClosed = false ;
45
-
46
- public MyStream () { }
47
-
48
- @ Override
49
- public void close () throws IOException {
50
- _isClosed = true ;
51
- super .close ();
52
- }
53
- public boolean isClosed () { return _isClosed ; }
54
- }
55
-
56
- static class MyBytes extends ByteArrayOutputStream
57
- {
58
- public int flushed = 0 ;
59
-
60
- @ Override
61
- public void flush () throws IOException
62
- {
63
- ++flushed ;
64
- super .flush ();
65
- }
66
- }
67
-
68
- static class MyChars extends StringWriter
69
- {
70
- public int flushed = 0 ;
71
-
72
- @ Override
73
- public void flush ()
74
- {
75
- ++flushed ;
76
- super .flush ();
77
- }
78
- }
79
-
80
23
/*
81
24
/**********************************************************
82
25
/* Unit tests
@@ -98,7 +41,7 @@ public void testNoAutoCloseGenerator() throws Exception
98
41
f .disable (JsonGenerator .Feature .AUTO_CLOSE_TARGET );
99
42
assertFalse (f .isEnabled (JsonGenerator .Feature .AUTO_CLOSE_TARGET ));
100
43
@ SuppressWarnings ("resource" )
101
- MyWriter output = new MyWriter ();
44
+ ByteOutputStreamForTesting output = new ByteOutputStreamForTesting ();
102
45
JsonGenerator jg = f .createGenerator (output );
103
46
104
47
// shouldn't be closed to begin with...
@@ -114,7 +57,7 @@ public void testCloseGenerator() throws Exception
114
57
JsonFactory f = new JsonFactory ();
115
58
f .enable (JsonGenerator .Feature .AUTO_CLOSE_TARGET );
116
59
@ SuppressWarnings ("resource" )
117
- MyWriter output = new MyWriter ();
60
+ ByteOutputStreamForTesting output = new ByteOutputStreamForTesting ();
118
61
JsonGenerator jg = f .createGenerator (output );
119
62
120
63
// shouldn't be closed to begin with...
@@ -130,7 +73,7 @@ public void testNoAutoCloseOutputStream() throws Exception
130
73
JsonFactory f = new JsonFactory ();
131
74
f .disable (JsonGenerator .Feature .AUTO_CLOSE_TARGET );
132
75
@ SuppressWarnings ("resource" )
133
- MyStream output = new MyStream ();
76
+ ByteOutputStreamForTesting output = new ByteOutputStreamForTesting ();
134
77
JsonGenerator jg = f .createGenerator (output , JsonEncoding .UTF8 );
135
78
136
79
assertFalse (output .isClosed ());
@@ -182,29 +125,28 @@ public void testNoAutoCloseArraysAndObjects()
182
125
assertEquals ("{" , sw .toString ());
183
126
}
184
127
185
- // [JACKSON-401]
186
128
@ SuppressWarnings ("resource" )
187
129
public void testAutoFlushOrNot () throws Exception
188
130
{
189
131
JsonFactory f = new JsonFactory ();
190
132
assertTrue (f .isEnabled (StreamWriteFeature .FLUSH_PASSED_TO_STREAM ));
191
- MyChars sw = new MyChars ();
133
+ StringWriterForTesting sw = new StringWriterForTesting ();
192
134
JsonGenerator jg = f .createGenerator (sw );
193
135
jg .writeStartArray ();
194
136
jg .writeEndArray ();
195
- assertEquals (0 , sw .flushed );
137
+ assertEquals (0 , sw .flushCount );
196
138
jg .flush ();
197
- assertEquals (1 , sw .flushed );
139
+ assertEquals (1 , sw .flushCount );
198
140
jg .close ();
199
141
200
142
// ditto with stream
201
- MyBytes bytes = new MyBytes ();
143
+ ByteOutputStreamForTesting bytes = new ByteOutputStreamForTesting ();
202
144
jg = f .createGenerator (bytes , JsonEncoding .UTF8 );
203
145
jg .writeStartArray ();
204
146
jg .writeEndArray ();
205
- assertEquals (0 , bytes .flushed );
147
+ assertEquals (0 , bytes .flushCount );
206
148
jg .flush ();
207
- assertEquals (1 , bytes .flushed );
149
+ assertEquals (1 , bytes .flushCount );
208
150
assertEquals (2 , bytes .toByteArray ().length );
209
151
jg .close ();
210
152
@@ -213,24 +155,24 @@ public void testAutoFlushOrNot() throws Exception
213
155
.disable (StreamWriteFeature .FLUSH_PASSED_TO_STREAM )
214
156
.build ();
215
157
// first with a Writer
216
- sw = new MyChars ();
158
+ sw = new StringWriterForTesting ();
217
159
jg = f .createGenerator (sw );
218
160
jg .writeStartArray ();
219
161
jg .writeEndArray ();
220
- assertEquals (0 , sw .flushed );
162
+ assertEquals (0 , sw .flushCount );
221
163
jg .flush ();
222
- assertEquals (0 , sw .flushed );
164
+ assertEquals (0 , sw .flushCount );
223
165
jg .close ();
224
166
assertEquals ("[]" , sw .toString ());
225
167
226
168
// and then with OutputStream
227
- bytes = new MyBytes ();
169
+ bytes = new ByteOutputStreamForTesting ();
228
170
jg = f .createGenerator (bytes , JsonEncoding .UTF8 );
229
171
jg .writeStartArray ();
230
172
jg .writeEndArray ();
231
- assertEquals (0 , bytes .flushed );
173
+ assertEquals (0 , bytes .flushCount );
232
174
jg .flush ();
233
- assertEquals (0 , bytes .flushed );
175
+ assertEquals (0 , bytes .flushCount );
234
176
jg .close ();
235
177
assertEquals (2 , bytes .toByteArray ().length );
236
178
}
0 commit comments