Skip to content

Commit

Permalink
Fix #179
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Mar 12, 2020
1 parent 3e481c6 commit 580159d
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.fasterxml.jackson.core.format.InputAccessor;
import com.fasterxml.jackson.core.format.MatchStrength;
import com.fasterxml.jackson.core.io.IOContext;

import com.fasterxml.jackson.dataformat.javaprop.impl.PropertiesBackedGenerator;
import com.fasterxml.jackson.dataformat.javaprop.impl.WriterBackedGenerator;
import com.fasterxml.jackson.dataformat.javaprop.io.Latin1Reader;
Expand Down Expand Up @@ -347,7 +348,7 @@ protected Properties _loadProperties(Reader r0, IOContext ctxt)
{
Properties props = new Properties();
// May or may not want to close the reader, so...
if (ctxt.isResourceManaged()) {
if (ctxt.isResourceManaged() || isEnabled(StreamReadFeature.AUTO_CLOSE_SOURCE)) {
try (Reader r = r0) {
props.load(r);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,12 @@ public int getReadCharsCount() {
@Override
public void close() throws IOException
{
_inputSource = null;
freeBuffers();
InputStream in = _inputSource;
if (in != null) {
_inputSource = null;
freeBuffers();
in.close();
}
}

private char[] _tmpBuffer = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.fasterxml.jackson.dataformat.javaprop;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.javaprop.testutil.CloseStateInputStream;
import com.fasterxml.jackson.dataformat.javaprop.testutil.CloseStateReader;

@SuppressWarnings("resource")
public class StreamClosingTest extends ModuleTestBase
{
// for [dataformats-text#179]
public static class Bean179 {
public int value;

@Override public String toString() { return "[value: "+value+"]"; }
}

private final ObjectMapper PROPS_MAPPER = mapperForProps();

public void testInputStreamClosing() throws Exception
{
// by default, SHOULD close it:
CloseStateInputStream in = CloseStateInputStream.forString("value = 42");
assertFalse(in.closed);
Bean179 result = PROPS_MAPPER.readValue(in, Bean179.class);
assertNotNull(result);
assertTrue(in.closed);

// but not if reconfigured
in = CloseStateInputStream.forString("value = 42");
assertFalse(in.closed);
result = PROPS_MAPPER.readerFor(Bean179.class)
.without(JsonParser.Feature.AUTO_CLOSE_SOURCE)
.readValue(in);
assertNotNull(result);
assertTrue(in.closed);
}

public void testReaderClosing() throws Exception
{
// by default, SHOULD close it:
CloseStateReader r = CloseStateReader.forString("value = 42");
assertFalse(r.closed);
Bean179 result = PROPS_MAPPER.readValue(r, Bean179.class);
assertNotNull(result);
assertTrue(r.closed);

// but not if reconfigured
r = CloseStateReader.forString("value = 42");
assertFalse(r.closed);
result = PROPS_MAPPER.readerFor(Bean179.class)
.without(JsonParser.Feature.AUTO_CLOSE_SOURCE)
.readValue(r);
assertNotNull(result);
assertTrue(r.closed);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.fasterxml.jackson.dataformat.javaprop.testutil;

import java.io.ByteArrayInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

public class CloseStateInputStream extends FilterInputStream
{
public boolean closed = false;

public CloseStateInputStream(InputStream in) { super(in); }

@Override
public void close() throws IOException {
closed = true;
super.close();
}

public static CloseStateInputStream forString(String input) throws IOException {
return new CloseStateInputStream(new ByteArrayInputStream(
input.getBytes("UTF-8")));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.fasterxml.jackson.dataformat.javaprop.testutil;

import java.io.*;

public class CloseStateReader extends FilterReader
{
public boolean closed = false;

public CloseStateReader(Reader r) { super(r); }

@Override
public void close() throws IOException {
closed = true;
super.close();
}

public static CloseStateReader forString(String input) throws IOException {
return new CloseStateReader(new StringReader(input));
}
}
5 changes: 5 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,8 @@ Vincent Boulaye (vboulaye@github)
Piyush Kumar (piyushkumar13@github)
* Reported #163: (yaml) `SequenceWriter` does not create multiple docs in a single yaml file
(2.10.2)

Francisco Colmenares (fcolmenarez@github)
* Reported #179 (properties): `JavaPropsMapper` doesn't close the .properties file
properly after reading
(2.10.4)
3 changes: 3 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ Modules:

2.10.4 (not yet released)

#179 (properties): `JavaPropsMapper` doesn't close the .properties file
properly after reading
(reported by Francisco C)
* (yaml) SnakeYAML 1.24 -> 1.26
2.10.3 (03-Mar-2020)
Expand Down

0 comments on commit 580159d

Please sign in to comment.