Skip to content

Commit

Permalink
Test refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jan 12, 2019
1 parent 0c81a26 commit d005e6c
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -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.*;

Expand All @@ -19,64 +20,6 @@
*/
public class TestGeneratorClosing extends BaseTest
{
/*
/**********************************************************
/* 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
Expand All @@ -98,7 +41,7 @@ public void testNoAutoCloseGenerator() throws Exception
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...
Expand All @@ -114,7 +57,7 @@ public void testCloseGenerator() throws Exception
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...
Expand All @@ -130,7 +73,7 @@ public void testNoAutoCloseOutputStream() throws Exception
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());
Expand Down Expand Up @@ -181,53 +124,52 @@ public void testNoAutoCloseArraysAndObjects()
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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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; }
}
Original file line number Diff line number Diff line change
@@ -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; }
}

0 comments on commit d005e6c

Please sign in to comment.