-
-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix #99 do not close stream when autoclose is disabled
- Loading branch information
Showing
4 changed files
with
216 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/deser/ParserAutoCloseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.fasterxml.jackson.dataformat.yaml.deser; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.StringReader; | ||
|
||
import org.junit.Assert; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.dataformat.yaml.ModuleTestBase; | ||
|
||
public class ParserAutoCloseTest extends ModuleTestBase { | ||
|
||
public void testParseReaderWithAutoClose() throws IOException { | ||
ObjectMapper yamlMapper = newObjectMapper(); | ||
|
||
CloseTrackerReader reader = new CloseTrackerReader("foo:bar"); | ||
yamlMapper.readTree(reader); | ||
|
||
Assert.assertEquals(true, reader.isClosed()); | ||
} | ||
|
||
public void testParseStreamWithAutoClose() throws IOException { | ||
ObjectMapper yamlMapper = newObjectMapper(); | ||
|
||
CloseTrackerOutputStream stream = new CloseTrackerOutputStream("foo:bar"); | ||
yamlMapper.readTree(stream); | ||
|
||
Assert.assertEquals(true, stream.isClosed()); | ||
} | ||
|
||
public void testParseReaderWithoutAutoClose() throws IOException { | ||
ObjectMapper yamlMapper = newObjectMapper() | ||
.disable(JsonParser.Feature.AUTO_CLOSE_SOURCE); | ||
|
||
CloseTrackerReader reader = new CloseTrackerReader("foo:bar"); | ||
yamlMapper.readTree(reader); | ||
|
||
Assert.assertEquals(false, reader.isClosed()); | ||
} | ||
|
||
|
||
public void testParseStreamWithoutAutoClose() throws IOException { | ||
ObjectMapper yamlMapper = newObjectMapper() | ||
.disable(JsonParser.Feature.AUTO_CLOSE_SOURCE); | ||
|
||
CloseTrackerOutputStream stream = new CloseTrackerOutputStream("foo:bar"); | ||
yamlMapper.readTree(stream); | ||
|
||
Assert.assertEquals(false, stream.isClosed()); | ||
} | ||
|
||
public static class CloseTrackerReader extends StringReader { | ||
private boolean closed; | ||
|
||
public CloseTrackerReader(String s) { | ||
super(s); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
closed = true; | ||
super.close(); | ||
} | ||
|
||
public boolean isClosed() { | ||
return closed; | ||
} | ||
} | ||
|
||
|
||
public static class CloseTrackerOutputStream extends ByteArrayInputStream { | ||
private boolean closed; | ||
|
||
public CloseTrackerOutputStream(String s) { | ||
super(s.getBytes()); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
closed = true; | ||
super.close(); | ||
} | ||
|
||
public boolean isClosed() { | ||
return closed; | ||
} | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ser/GeneratorAutoCloseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package com.fasterxml.jackson.dataformat.yaml.ser; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.io.StringWriter; | ||
|
||
import org.junit.Assert; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.dataformat.yaml.ModuleTestBase; | ||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; | ||
|
||
public class GeneratorAutoCloseTest extends ModuleTestBase { | ||
|
||
private Pojo pojo = new Pojo("bar"); | ||
|
||
public void testGenerateWriterWithoAutoCloseTarget() throws IOException { | ||
CloseTrackerWriter writer = new CloseTrackerWriter(); | ||
ObjectMapper yamlMapper = newObjectMapper(); | ||
yamlMapper.writeValue(writer, pojo); | ||
Assert.assertEquals(true, writer.isClosed()); | ||
} | ||
|
||
public void testGenerateOutputStreamWithAutoCloseTarget() throws IOException { | ||
CloseTrackerOutputStream stream = new CloseTrackerOutputStream(); | ||
ObjectMapper yamlMapper = newObjectMapper(); | ||
yamlMapper.writeValue(stream, pojo); | ||
Assert.assertEquals(true, stream.isClosed()); | ||
} | ||
|
||
public void testGenerateWriterWithoutAutoCloseTarget() throws IOException { | ||
CloseTrackerWriter writer = new CloseTrackerWriter(); | ||
ObjectMapper yamlMapper = newObjectMapper() | ||
.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET); | ||
yamlMapper.writeValue(writer, pojo); | ||
Assert.assertEquals(false, writer.isClosed()); | ||
} | ||
|
||
public void testGenerateOutputStreamWithoutAutoCloseTarget() throws IOException { | ||
CloseTrackerOutputStream stream = new CloseTrackerOutputStream(); | ||
ObjectMapper yamlMapper = newObjectMapper() | ||
.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET); | ||
yamlMapper.writeValue(stream, pojo); | ||
Assert.assertEquals(false, stream.isClosed()); | ||
} | ||
|
||
public void testGenerateOutputStreamWithoutAutoCloseTargetOnFactory() throws IOException { | ||
CloseTrackerOutputStream stream = new CloseTrackerOutputStream(); | ||
ObjectMapper yamlMapper = new ObjectMapper( | ||
new YAMLFactory() | ||
.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET) | ||
); | ||
yamlMapper.writeValue(stream, pojo); | ||
Assert.assertEquals(false, stream.isClosed()); | ||
} | ||
|
||
|
||
static class CloseTrackerOutputStream extends OutputStream { | ||
private boolean closed; | ||
|
||
@Override | ||
public void write(int b) throws IOException { | ||
|
||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
closed = true; | ||
super.close(); | ||
} | ||
|
||
public boolean isClosed() { | ||
return closed; | ||
} | ||
} | ||
|
||
static class CloseTrackerWriter extends StringWriter { | ||
private boolean closed; | ||
|
||
|
||
@Override | ||
public void close() throws IOException { | ||
closed = true; | ||
super.close(); | ||
} | ||
|
||
public boolean isClosed() { | ||
return closed; | ||
} | ||
} | ||
|
||
static class Pojo { | ||
|
||
public final String foo; | ||
|
||
Pojo(final String foo) { | ||
this.foo = foo; | ||
} | ||
} | ||
} |