Skip to content

Commit f88a0fa

Browse files
committed
Merge branch '2.11'
2 parents dd7e11b + ca3fa9a commit f88a0fa

File tree

8 files changed

+117
-4
lines changed

8 files changed

+117
-4
lines changed

csv/src/test/java/com/fasterxml/jackson/dataformat/csv/NullReader122Test.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public void testEmptyStream() throws Exception {
1717
/*Object ob =*/ r.readValue((Reader) null);
1818
fail("Should not pass");
1919
} catch (IllegalArgumentException e) {
20-
verifyException(e, "Argument \"src\" is null");
20+
verifyException(e, "Argument \"r\" is null");
2121
}
2222
}
2323
}

properties/src/main/java/com/fasterxml/jackson/dataformat/javaprop/JavaPropsFactory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ protected Properties _loadProperties(Reader r0, IOContext ctxt) throws IOExcepti
297297
{
298298
Properties props = new Properties();
299299
// May or may not want to close the reader, so...
300-
if (ctxt.isResourceManaged()) {
300+
if (ctxt.isResourceManaged() || isEnabled(StreamReadFeature.AUTO_CLOSE_SOURCE)) {
301301
try (Reader r = r0) {
302302
props.load(r);
303303
}

properties/src/main/java/com/fasterxml/jackson/dataformat/javaprop/io/Latin1Reader.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,12 @@ public int getReadCharsCount() {
9696
@Override
9797
public void close() throws IOException
9898
{
99-
_inputSource = null;
100-
freeBuffers();
99+
InputStream in = _inputSource;
100+
if (in != null) {
101+
_inputSource = null;
102+
freeBuffers();
103+
in.close();
104+
}
101105
}
102106

103107
private char[] _tmpBuffer = null;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.fasterxml.jackson.dataformat.javaprop;
2+
3+
import com.fasterxml.jackson.core.JsonParser;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.fasterxml.jackson.dataformat.javaprop.testutil.CloseStateInputStream;
6+
import com.fasterxml.jackson.dataformat.javaprop.testutil.CloseStateReader;
7+
8+
@SuppressWarnings("resource")
9+
public class StreamClosingTest extends ModuleTestBase
10+
{
11+
// for [dataformats-text#179]
12+
public static class Bean179 {
13+
public int value;
14+
15+
@Override public String toString() { return "[value: "+value+"]"; }
16+
}
17+
18+
private final ObjectMapper PROPS_MAPPER = mapperForProps();
19+
20+
public void testInputStreamClosing() throws Exception
21+
{
22+
// by default, SHOULD close it:
23+
CloseStateInputStream in = CloseStateInputStream.forString("value = 42");
24+
assertFalse(in.closed);
25+
Bean179 result = PROPS_MAPPER.readValue(in, Bean179.class);
26+
assertNotNull(result);
27+
assertTrue(in.closed);
28+
29+
// but not if reconfigured
30+
in = CloseStateInputStream.forString("value = 42");
31+
assertFalse(in.closed);
32+
result = PROPS_MAPPER.readerFor(Bean179.class)
33+
.without(JsonParser.Feature.AUTO_CLOSE_SOURCE)
34+
.readValue(in);
35+
assertNotNull(result);
36+
assertTrue(in.closed);
37+
}
38+
39+
public void testReaderClosing() throws Exception
40+
{
41+
// by default, SHOULD close it:
42+
CloseStateReader r = CloseStateReader.forString("value = 42");
43+
assertFalse(r.closed);
44+
Bean179 result = PROPS_MAPPER.readValue(r, Bean179.class);
45+
assertNotNull(result);
46+
assertTrue(r.closed);
47+
48+
// but not if reconfigured
49+
r = CloseStateReader.forString("value = 42");
50+
assertFalse(r.closed);
51+
result = PROPS_MAPPER.readerFor(Bean179.class)
52+
.without(JsonParser.Feature.AUTO_CLOSE_SOURCE)
53+
.readValue(r);
54+
assertNotNull(result);
55+
assertTrue(r.closed);
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.fasterxml.jackson.dataformat.javaprop.testutil;
2+
3+
import java.io.ByteArrayInputStream;
4+
import java.io.FilterInputStream;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
8+
public class CloseStateInputStream extends FilterInputStream
9+
{
10+
public boolean closed = false;
11+
12+
public CloseStateInputStream(InputStream in) { super(in); }
13+
14+
@Override
15+
public void close() throws IOException {
16+
closed = true;
17+
super.close();
18+
}
19+
20+
public static CloseStateInputStream forString(String input) throws IOException {
21+
return new CloseStateInputStream(new ByteArrayInputStream(
22+
input.getBytes("UTF-8")));
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.fasterxml.jackson.dataformat.javaprop.testutil;
2+
3+
import java.io.*;
4+
5+
public class CloseStateReader extends FilterReader
6+
{
7+
public boolean closed = false;
8+
9+
public CloseStateReader(Reader r) { super(r); }
10+
11+
@Override
12+
public void close() throws IOException {
13+
closed = true;
14+
super.close();
15+
}
16+
17+
public static CloseStateReader forString(String input) throws IOException {
18+
return new CloseStateReader(new StringReader(input));
19+
}
20+
}

release-notes/CREDITS-2.x

+5
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ Piyush Kumar (piyushkumar13@github)
8989
* Reported #163: (yaml) `SequenceWriter` does not create multiple docs in a single yaml file
9090
(2.10.2)
9191

92+
Francisco Colmenares (fcolmenarez@github)
93+
* Reported #179 (properties): `JavaPropsMapper` doesn't close the .properties file
94+
properly after reading
95+
(2.10.4)
96+
9297
Tyler Carpenter-Rivers (tyler2cr@github)
9398
#7: Add `CsvParser.Feature.EMPTY_STRING_AS_NULL` to allow coercing empty Strings
9499
into `null` values

release-notes/VERSION-2.x

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ Modules:
1919
2.10.4 (not yet released)
2020

2121
#178: Upgrade SnakeYAML to 1.26 (from 1.24)
22+
#179 (properties): `JavaPropsMapper` doesn't close the .properties file
23+
properly after reading
24+
(reported by Francisco C)
2225
2326
2.10.3 (03-Mar-2020)
2427

0 commit comments

Comments
 (0)