|
| 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 | +} |
0 commit comments