Skip to content

Commit 768e458

Browse files
committed
Merge branch '2.9' into 2.10
2 parents 257ec52 + d005e6c commit 768e458

File tree

3 files changed

+77
-75
lines changed

3 files changed

+77
-75
lines changed

src/test/java/com/fasterxml/jackson/core/main/TestGeneratorClosing.java

Lines changed: 17 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.fasterxml.jackson.core.main;
22

3-
43
import com.fasterxml.jackson.core.*;
4+
import com.fasterxml.jackson.core.testsupport.ByteOutputStreamForTesting;
5+
import com.fasterxml.jackson.core.testsupport.StringWriterForTesting;
56

67
import java.io.*;
78

@@ -19,64 +20,6 @@
1920
*/
2021
public class TestGeneratorClosing extends BaseTest
2122
{
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-
8023
/*
8124
/**********************************************************
8225
/* Unit tests
@@ -98,7 +41,7 @@ public void testNoAutoCloseGenerator() throws Exception
9841
f.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
9942
assertFalse(f.isEnabled(JsonGenerator.Feature.AUTO_CLOSE_TARGET));
10043
@SuppressWarnings("resource")
101-
MyWriter output = new MyWriter();
44+
ByteOutputStreamForTesting output = new ByteOutputStreamForTesting();
10245
JsonGenerator jg = f.createGenerator(output);
10346

10447
// shouldn't be closed to begin with...
@@ -114,7 +57,7 @@ public void testCloseGenerator() throws Exception
11457
JsonFactory f = new JsonFactory();
11558
f.enable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
11659
@SuppressWarnings("resource")
117-
MyWriter output = new MyWriter();
60+
ByteOutputStreamForTesting output = new ByteOutputStreamForTesting();
11861
JsonGenerator jg = f.createGenerator(output);
11962

12063
// shouldn't be closed to begin with...
@@ -130,7 +73,7 @@ public void testNoAutoCloseOutputStream() throws Exception
13073
JsonFactory f = new JsonFactory();
13174
f.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
13275
@SuppressWarnings("resource")
133-
MyStream output = new MyStream();
76+
ByteOutputStreamForTesting output = new ByteOutputStreamForTesting();
13477
JsonGenerator jg = f.createGenerator(output, JsonEncoding.UTF8);
13578

13679
assertFalse(output.isClosed());
@@ -182,29 +125,28 @@ public void testNoAutoCloseArraysAndObjects()
182125
assertEquals("{", sw.toString());
183126
}
184127

185-
// [JACKSON-401]
186128
@SuppressWarnings("resource")
187129
public void testAutoFlushOrNot() throws Exception
188130
{
189131
JsonFactory f = new JsonFactory();
190132
assertTrue(f.isEnabled(StreamWriteFeature.FLUSH_PASSED_TO_STREAM));
191-
MyChars sw = new MyChars();
133+
StringWriterForTesting sw = new StringWriterForTesting();
192134
JsonGenerator jg = f.createGenerator(sw);
193135
jg.writeStartArray();
194136
jg.writeEndArray();
195-
assertEquals(0, sw.flushed);
137+
assertEquals(0, sw.flushCount);
196138
jg.flush();
197-
assertEquals(1, sw.flushed);
139+
assertEquals(1, sw.flushCount);
198140
jg.close();
199141

200142
// ditto with stream
201-
MyBytes bytes = new MyBytes();
143+
ByteOutputStreamForTesting bytes = new ByteOutputStreamForTesting();
202144
jg = f.createGenerator(bytes, JsonEncoding.UTF8);
203145
jg.writeStartArray();
204146
jg.writeEndArray();
205-
assertEquals(0, bytes.flushed);
147+
assertEquals(0, bytes.flushCount);
206148
jg.flush();
207-
assertEquals(1, bytes.flushed);
149+
assertEquals(1, bytes.flushCount);
208150
assertEquals(2, bytes.toByteArray().length);
209151
jg.close();
210152

@@ -213,24 +155,24 @@ public void testAutoFlushOrNot() throws Exception
213155
.disable(StreamWriteFeature.FLUSH_PASSED_TO_STREAM)
214156
.build();
215157
// first with a Writer
216-
sw = new MyChars();
158+
sw = new StringWriterForTesting();
217159
jg = f.createGenerator(sw);
218160
jg.writeStartArray();
219161
jg.writeEndArray();
220-
assertEquals(0, sw.flushed);
162+
assertEquals(0, sw.flushCount);
221163
jg.flush();
222-
assertEquals(0, sw.flushed);
164+
assertEquals(0, sw.flushCount);
223165
jg.close();
224166
assertEquals("[]", sw.toString());
225167

226168
// and then with OutputStream
227-
bytes = new MyBytes();
169+
bytes = new ByteOutputStreamForTesting();
228170
jg = f.createGenerator(bytes, JsonEncoding.UTF8);
229171
jg.writeStartArray();
230172
jg.writeEndArray();
231-
assertEquals(0, bytes.flushed);
173+
assertEquals(0, bytes.flushCount);
232174
jg.flush();
233-
assertEquals(0, bytes.flushed);
175+
assertEquals(0, bytes.flushCount);
234176
jg.close();
235177
assertEquals(2, bytes.toByteArray().length);
236178
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fasterxml.jackson.core.testsupport;
2+
3+
import java.io.ByteArrayOutputStream;
4+
import java.io.IOException;
5+
6+
/**
7+
* Helper class for verifying that {@link java.io.OutputStream} is (or is not)
8+
* closed and/or flushed.
9+
*/
10+
public class ByteOutputStreamForTesting extends ByteArrayOutputStream
11+
{
12+
public int closeCount = 0;
13+
public int flushCount = 0;
14+
15+
public ByteOutputStreamForTesting() { }
16+
17+
@Override
18+
public void close() throws IOException {
19+
++closeCount;
20+
super.close();
21+
}
22+
23+
@Override
24+
public void flush() throws IOException
25+
{
26+
++flushCount;
27+
super.flush();
28+
}
29+
30+
public boolean isClosed() { return closeCount > 0; }
31+
public boolean isFlushed() { return flushCount > 0; }
32+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.fasterxml.jackson.core.testsupport;
2+
3+
import java.io.IOException;
4+
import java.io.StringWriter;
5+
6+
public class StringWriterForTesting extends StringWriter
7+
{
8+
public int closeCount = 0;
9+
public int flushCount = 0;
10+
11+
public StringWriterForTesting() { }
12+
13+
@Override
14+
public void close() throws IOException {
15+
++closeCount;
16+
super.close();
17+
}
18+
19+
@Override
20+
public void flush()
21+
{
22+
++flushCount;
23+
super.flush();
24+
}
25+
26+
public boolean isClosed() { return closeCount > 0; }
27+
public boolean isFlushed() { return flushCount > 0; }
28+
}

0 commit comments

Comments
 (0)