Test refactoring
diff --git a/src/test/java/com/fasterxml/jackson/core/main/TestGeneratorClosing.java b/src/test/java/com/fasterxml/jackson/core/main/TestGeneratorClosing.java
index 9b94a9d..92959ad 100644
--- a/src/test/java/com/fasterxml/jackson/core/main/TestGeneratorClosing.java
+++ b/src/test/java/com/fasterxml/jackson/core/main/TestGeneratorClosing.java
@@ -1,7 +1,8 @@
package com.fasterxml.jackson.core.main;
-
import com.fasterxml.jackson.core.*;
+import com.fasterxml.jackson.core.testsupport.ByteOutputStreamForTesting;
+import com.fasterxml.jackson.core.testsupport.StringWriterForTesting;
import java.io.*;
@@ -21,64 +22,6 @@
{
/*
/**********************************************************
- /* Helper classes
- /**********************************************************
- */
-
- final static class MyWriter extends StringWriter
- {
- boolean _isClosed = false;
-
- public MyWriter() { }
-
- @Override
- public void close() throws IOException {
- _isClosed = true;
- super.close();
- }
- public boolean isClosed() { return _isClosed; }
- }
-
- final static class MyStream extends ByteArrayOutputStream
- {
- boolean _isClosed = false;
-
- public MyStream() { }
-
- @Override
- public void close() throws IOException {
- _isClosed = true;
- super.close();
- }
- public boolean isClosed() { return _isClosed; }
- }
-
- static class MyBytes extends ByteArrayOutputStream
- {
- public int flushed = 0;
-
- @Override
- public void flush() throws IOException
- {
- ++flushed;
- super.flush();
- }
- }
-
- static class MyChars extends StringWriter
- {
- public int flushed = 0;
-
- @Override
- public void flush()
- {
- ++flushed;
- super.flush();
- }
- }
-
- /*
- /**********************************************************
/* Unit tests
/**********************************************************
*/
@@ -98,7 +41,7 @@
f.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
assertFalse(f.isEnabled(JsonGenerator.Feature.AUTO_CLOSE_TARGET));
@SuppressWarnings("resource")
- MyWriter output = new MyWriter();
+ ByteOutputStreamForTesting output = new ByteOutputStreamForTesting();
JsonGenerator jg = f.createGenerator(output);
// shouldn't be closed to begin with...
@@ -114,7 +57,7 @@
JsonFactory f = new JsonFactory();
f.enable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
@SuppressWarnings("resource")
- MyWriter output = new MyWriter();
+ ByteOutputStreamForTesting output = new ByteOutputStreamForTesting();
JsonGenerator jg = f.createGenerator(output);
// shouldn't be closed to begin with...
@@ -130,7 +73,7 @@
JsonFactory f = new JsonFactory();
f.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
@SuppressWarnings("resource")
- MyStream output = new MyStream();
+ ByteOutputStreamForTesting output = new ByteOutputStreamForTesting();
JsonGenerator jg = f.createGenerator(output, JsonEncoding.UTF8);
assertFalse(output.isClosed());
@@ -181,53 +124,52 @@
assertEquals("{", sw.toString());
}
- // [JACKSON-401]
@SuppressWarnings("resource")
public void testAutoFlushOrNot() throws Exception
{
JsonFactory f = new JsonFactory();
assertTrue(f.isEnabled(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM));
- MyChars sw = new MyChars();
+ StringWriterForTesting sw = new StringWriterForTesting();
JsonGenerator jg = f.createGenerator(sw);
jg.writeStartArray();
jg.writeEndArray();
- assertEquals(0, sw.flushed);
+ assertEquals(0, sw.flushCount);
jg.flush();
- assertEquals(1, sw.flushed);
+ assertEquals(1, sw.flushCount);
jg.close();
// ditto with stream
- MyBytes bytes = new MyBytes();
+ ByteOutputStreamForTesting bytes = new ByteOutputStreamForTesting();
jg = f.createGenerator(bytes, JsonEncoding.UTF8);
jg.writeStartArray();
jg.writeEndArray();
- assertEquals(0, bytes.flushed);
+ assertEquals(0, bytes.flushCount);
jg.flush();
- assertEquals(1, bytes.flushed);
+ assertEquals(1, bytes.flushCount);
assertEquals(2, bytes.toByteArray().length);
jg.close();
// then disable and we should not see flushing again...
f.disable(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM);
// first with a Writer
- sw = new MyChars();
+ sw = new StringWriterForTesting();
jg = f.createGenerator(sw);
jg.writeStartArray();
jg.writeEndArray();
- assertEquals(0, sw.flushed);
+ assertEquals(0, sw.flushCount);
jg.flush();
- assertEquals(0, sw.flushed);
+ assertEquals(0, sw.flushCount);
jg.close();
assertEquals("[]", sw.toString());
// and then with OutputStream
- bytes = new MyBytes();
+ bytes = new ByteOutputStreamForTesting();
jg = f.createGenerator(bytes, JsonEncoding.UTF8);
jg.writeStartArray();
jg.writeEndArray();
- assertEquals(0, bytes.flushed);
+ assertEquals(0, bytes.flushCount);
jg.flush();
- assertEquals(0, bytes.flushed);
+ assertEquals(0, bytes.flushCount);
jg.close();
assertEquals(2, bytes.toByteArray().length);
}
diff --git a/src/test/java/com/fasterxml/jackson/core/testsupport/ByteOutputStreamForTesting.java b/src/test/java/com/fasterxml/jackson/core/testsupport/ByteOutputStreamForTesting.java
new file mode 100644
index 0000000..1da9b43
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/core/testsupport/ByteOutputStreamForTesting.java
@@ -0,0 +1,32 @@
+package com.fasterxml.jackson.core.testsupport;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+/**
+ * Helper class for verifying that {@link java.io.OutputStream} is (or is not)
+ * closed and/or flushed.
+ */
+public class ByteOutputStreamForTesting extends ByteArrayOutputStream
+{
+ public int closeCount = 0;
+ public int flushCount = 0;
+
+ public ByteOutputStreamForTesting() { }
+
+ @Override
+ public void close() throws IOException {
+ ++closeCount;
+ super.close();
+ }
+
+ @Override
+ public void flush() throws IOException
+ {
+ ++flushCount;
+ super.flush();
+ }
+
+ public boolean isClosed() { return closeCount > 0; }
+ public boolean isFlushed() { return flushCount > 0; }
+}
diff --git a/src/test/java/com/fasterxml/jackson/core/testsupport/StringWriterForTesting.java b/src/test/java/com/fasterxml/jackson/core/testsupport/StringWriterForTesting.java
new file mode 100644
index 0000000..64100cb
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/core/testsupport/StringWriterForTesting.java
@@ -0,0 +1,28 @@
+package com.fasterxml.jackson.core.testsupport;
+
+import java.io.IOException;
+import java.io.StringWriter;
+
+public class StringWriterForTesting extends StringWriter
+{
+ public int closeCount = 0;
+ public int flushCount = 0;
+
+ public StringWriterForTesting() { }
+
+ @Override
+ public void close() throws IOException {
+ ++closeCount;
+ super.close();
+ }
+
+ @Override
+ public void flush()
+ {
+ ++flushCount;
+ super.flush();
+ }
+
+ public boolean isClosed() { return closeCount > 0; }
+ public boolean isFlushed() { return flushCount > 0; }
+}