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 ());
@@ -181,53 +124,52 @@ public void testNoAutoCloseArraysAndObjects()
181
124
assertEquals ("{" , sw .toString ());
182
125
}
183
126
184
- // [JACKSON-401]
185
127
@ SuppressWarnings ("resource" )
186
128
public void testAutoFlushOrNot () throws Exception
187
129
{
188
130
JsonFactory f = new JsonFactory ();
189
131
assertTrue (f .isEnabled (JsonGenerator .Feature .FLUSH_PASSED_TO_STREAM ));
190
- MyChars sw = new MyChars ();
132
+ StringWriterForTesting sw = new StringWriterForTesting ();
191
133
JsonGenerator jg = f .createGenerator (sw );
192
134
jg .writeStartArray ();
193
135
jg .writeEndArray ();
194
- assertEquals (0 , sw .flushed );
136
+ assertEquals (0 , sw .flushCount );
195
137
jg .flush ();
196
- assertEquals (1 , sw .flushed );
138
+ assertEquals (1 , sw .flushCount );
197
139
jg .close ();
198
140
199
141
// ditto with stream
200
- MyBytes bytes = new MyBytes ();
142
+ ByteOutputStreamForTesting bytes = new ByteOutputStreamForTesting ();
201
143
jg = f .createGenerator (bytes , JsonEncoding .UTF8 );
202
144
jg .writeStartArray ();
203
145
jg .writeEndArray ();
204
- assertEquals (0 , bytes .flushed );
146
+ assertEquals (0 , bytes .flushCount );
205
147
jg .flush ();
206
- assertEquals (1 , bytes .flushed );
148
+ assertEquals (1 , bytes .flushCount );
207
149
assertEquals (2 , bytes .toByteArray ().length );
208
150
jg .close ();
209
151
210
152
// then disable and we should not see flushing again...
211
153
f .disable (JsonGenerator .Feature .FLUSH_PASSED_TO_STREAM );
212
154
// first with a Writer
213
- sw = new MyChars ();
155
+ sw = new StringWriterForTesting ();
214
156
jg = f .createGenerator (sw );
215
157
jg .writeStartArray ();
216
158
jg .writeEndArray ();
217
- assertEquals (0 , sw .flushed );
159
+ assertEquals (0 , sw .flushCount );
218
160
jg .flush ();
219
- assertEquals (0 , sw .flushed );
161
+ assertEquals (0 , sw .flushCount );
220
162
jg .close ();
221
163
assertEquals ("[]" , sw .toString ());
222
164
223
165
// and then with OutputStream
224
- bytes = new MyBytes ();
166
+ bytes = new ByteOutputStreamForTesting ();
225
167
jg = f .createGenerator (bytes , JsonEncoding .UTF8 );
226
168
jg .writeStartArray ();
227
169
jg .writeEndArray ();
228
- assertEquals (0 , bytes .flushed );
170
+ assertEquals (0 , bytes .flushCount );
229
171
jg .flush ();
230
- assertEquals (0 , bytes .flushed );
172
+ assertEquals (0 , bytes .flushCount );
231
173
jg .close ();
232
174
assertEquals (2 , bytes .toByteArray ().length );
233
175
}
0 commit comments