Use try-with-resources to fix a random failure seen on Java 12.

This commit is contained in:
Gary Gregory 2020-04-26 09:57:57 -04:00
parent bd8e77d607
commit a53127e3a5
1 changed files with 18 additions and 10 deletions

View File

@ -247,19 +247,27 @@ public class EmptyPropertiesTest {
} }
@Test @Test
public void testSave() { public void testSave() throws IOException {
final String comments = "Hello world!"; final String comments = "Hello world!";
// actual // actual
final ByteArrayOutputStream actual = new ByteArrayOutputStream(); try (final ByteArrayOutputStream actual = new ByteArrayOutputStream()) {
PropertiesFactory.EMPTY_PROPERTIES.save(new PrintStream(actual), comments); try (final PrintStream out = new PrintStream(actual)) {
PropertiesFactory.EMPTY_PROPERTIES.save(out, comments);
}
// expected // expected
final ByteArrayOutputStream expected = new ByteArrayOutputStream(); try (final ByteArrayOutputStream expected = new ByteArrayOutputStream()) {
PropertiesFactory.INSTANCE.createProperties().save(new PrintStream(expected), comments); try (final PrintStream out = new PrintStream(expected)) {
PropertiesFactory.INSTANCE.createProperties().save(out, comments);
}
Assert.assertArrayEquals(expected.toByteArray(), actual.toByteArray()); Assert.assertArrayEquals(expected.toByteArray(), actual.toByteArray());
expected.reset(); expected.reset();
new Properties().save(new PrintStream(expected), comments); try (final PrintStream out = new PrintStream(expected)) {
new Properties().save(out, comments);
}
Assert.assertArrayEquals(expected.toByteArray(), actual.toByteArray()); Assert.assertArrayEquals(expected.toByteArray(), actual.toByteArray());
} }
}
}
@Test(expected = UnsupportedOperationException.class) @Test(expected = UnsupportedOperationException.class)
public void testSetProperty() { public void testSetProperty() {