[COLLECTIONS-812] Open both streams with try-with-resources, and assert that only the text is the same, not the time
This commit is contained in:
parent
bc326f8ef5
commit
1677daceab
|
@ -94,6 +94,9 @@
|
||||||
<action issue="COLLECTIONS-802" type="fix" dev="kinow" due-to="samabcde, Ben Manes">
|
<action issue="COLLECTIONS-802" type="fix" dev="kinow" due-to="samabcde, Ben Manes">
|
||||||
ReferenceMap iterator remove violates contract #300.
|
ReferenceMap iterator remove violates contract #300.
|
||||||
</action>
|
</action>
|
||||||
|
<action issue="COLLECTIONS-812" type="fix" dev="kinow" due-to="Ng Tsz Sum">
|
||||||
|
Fix flaky EmptyPropertiesTest#testSave.
|
||||||
|
</action>
|
||||||
<!-- ADD -->
|
<!-- ADD -->
|
||||||
<action issue="COLLECTIONS-760" dev="kinow" type="add" due-to="Isira Seneviratne">
|
<action issue="COLLECTIONS-760" dev="kinow" type="add" due-to="Isira Seneviratne">
|
||||||
Add tests for MapUtils.
|
Add tests for MapUtils.
|
||||||
|
|
|
@ -17,12 +17,20 @@
|
||||||
|
|
||||||
package org.apache.commons.collections4.properties;
|
package org.apache.commons.collections4.properties;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
import static org.junit.jupiter.api.Assertions.fail;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.io.UnsupportedEncodingException;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
@ -31,15 +39,6 @@ import org.apache.commons.io.input.NullReader;
|
||||||
import org.apache.commons.lang3.ArrayUtils;
|
import org.apache.commons.lang3.ArrayUtils;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
|
||||||
import static org.junit.jupiter.api.Assertions.fail;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
|
||||||
|
|
||||||
public class EmptyPropertiesTest {
|
public class EmptyPropertiesTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -259,26 +258,29 @@ public class EmptyPropertiesTest {
|
||||||
@Test
|
@Test
|
||||||
public void testSave() throws IOException {
|
public void testSave() throws IOException {
|
||||||
final String comments = "Hello world!";
|
final String comments = "Hello world!";
|
||||||
// actual
|
try (ByteArrayOutputStream actual = new ByteArrayOutputStream(); ByteArrayOutputStream expected = new ByteArrayOutputStream()) {
|
||||||
try (ByteArrayOutputStream actual = new ByteArrayOutputStream()) {
|
// actual
|
||||||
PropertiesFactory.EMPTY_PROPERTIES.save(actual, comments);
|
PropertiesFactory.EMPTY_PROPERTIES.store(actual, comments);
|
||||||
// expected
|
// expected
|
||||||
try (ByteArrayOutputStream expected = new ByteArrayOutputStream()) {
|
PropertiesFactory.INSTANCE.createProperties().store(expected, comments);
|
||||||
PropertiesFactory.INSTANCE.createProperties().save(expected, comments);
|
|
||||||
|
|
||||||
// Properties.save stores the specified comment appended with current time stamp in the next line
|
// Properties.store stores the specified comment appended with current time stamp in the next line
|
||||||
String expectedComment = getFirstLine(expected.toString("UTF-8"));
|
String expectedComment = getFirstLine(expected.toString("UTF-8"));
|
||||||
String actualComment = getFirstLine(actual.toString("UTF-8"));
|
String actualComment = getFirstLine(actual.toString("UTF-8"));
|
||||||
assertEquals(expectedComment, actualComment, () ->
|
assertEquals(expectedComment, actualComment, () ->
|
||||||
String.format("Expected String '%s' with length '%s'", expectedComment, expectedComment.length()));
|
String.format("Expected String '%s' with length '%s'", expectedComment, expectedComment.length()));
|
||||||
expected.reset();
|
expected.reset();
|
||||||
try (PrintStream out = new PrintStream(expected)) {
|
try (PrintStream out = new PrintStream(expected)) {
|
||||||
new Properties().save(out, comments);
|
new Properties().store(out, comments);
|
||||||
}
|
|
||||||
assertArrayEquals(expected.toByteArray(), actual.toByteArray(), expected::toString);
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
fail(e.getMessage(), e);
|
|
||||||
}
|
}
|
||||||
|
String[] expectedLines = expected.toString(StandardCharsets.UTF_8.displayName()).split("\\n");
|
||||||
|
String[] actualLines = actual.toString(StandardCharsets.UTF_8.displayName()).split("\\n");
|
||||||
|
assertEquals(expectedLines.length, actualLines.length);
|
||||||
|
// The assertion below checks that the comment is the same in both files
|
||||||
|
assertEquals(expectedLines[0], actualLines[0]);
|
||||||
|
// N.B.: We must not expect expectedLines[1] and actualLines[1] to have the same value as
|
||||||
|
// it contains the timestamp of when the data was written to the stream, which makes
|
||||||
|
// this test brittle, causing intermitent failures, see COLLECTIONS-812
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue