Skip to content

Commit d005e6c

Browse files
committed
Test refactoring
1 parent 0c81a26 commit d005e6c

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());
@@ -181,53 +124,52 @@ public void testNoAutoCloseArraysAndObjects()
181124
assertEquals("{", sw.toString());
182125
}
183126

184-
// [JACKSON-401]
185127
@SuppressWarnings("resource")
186128
public void testAutoFlushOrNot() throws Exception
187129
{
188130
JsonFactory f = new JsonFactory();
189131
assertTrue(f.isEnabled(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM));
190-
MyChars sw = new MyChars();
132+
StringWriterForTesting sw = new StringWriterForTesting();
191133
JsonGenerator jg = f.createGenerator(sw);
192134
jg.writeStartArray();
193135
jg.writeEndArray();
194-
assertEquals(0, sw.flushed);
136+
assertEquals(0, sw.flushCount);
195137
jg.flush();
196-
assertEquals(1, sw.flushed);
138+
assertEquals(1, sw.flushCount);
197139
jg.close();
198140

199141
// ditto with stream
200-
MyBytes bytes = new MyBytes();
142+
ByteOutputStreamForTesting bytes = new ByteOutputStreamForTesting();
201143
jg = f.createGenerator(bytes, JsonEncoding.UTF8);
202144
jg.writeStartArray();
203145
jg.writeEndArray();
204-
assertEquals(0, bytes.flushed);
146+
assertEquals(0, bytes.flushCount);
205147
jg.flush();
206-
assertEquals(1, bytes.flushed);
148+
assertEquals(1, bytes.flushCount);
207149
assertEquals(2, bytes.toByteArray().length);
208150
jg.close();
209151

210152
// then disable and we should not see flushing again...
211153
f.disable(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM);
212154
// first with a Writer
213-
sw = new MyChars();
155+
sw = new StringWriterForTesting();
214156
jg = f.createGenerator(sw);
215157
jg.writeStartArray();
216158
jg.writeEndArray();
217-
assertEquals(0, sw.flushed);
159+
assertEquals(0, sw.flushCount);
218160
jg.flush();
219-
assertEquals(0, sw.flushed);
161+
assertEquals(0, sw.flushCount);
220162
jg.close();
221163
assertEquals("[]", sw.toString());
222164

223165
// and then with OutputStream
224-
bytes = new MyBytes();
166+
bytes = new ByteOutputStreamForTesting();
225167
jg = f.createGenerator(bytes, JsonEncoding.UTF8);
226168
jg.writeStartArray();
227169
jg.writeEndArray();
228-
assertEquals(0, bytes.flushed);
170+
assertEquals(0, bytes.flushCount);
229171
jg.flush();
230-
assertEquals(0, bytes.flushed);
172+
assertEquals(0, bytes.flushCount);
231173
jg.close();
232174
assertEquals(2, bytes.toByteArray().length);
233175
}
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)