Skip to content

Commit

Permalink
[COLLECTIONS-812] Open both streams with try-with-resources, and asse…
Browse files Browse the repository at this point in the history
…rt that only the text is the same, not the time
  • Loading branch information
kinow committed May 1, 2022
1 parent bc326f8 commit 1677dac
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
3 changes: 3 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@
<action issue="COLLECTIONS-802" type="fix" dev="kinow" due-to="samabcde, Ben Manes">
ReferenceMap iterator remove violates contract #300.
</action>
<action issue="COLLECTIONS-812" type="fix" dev="kinow" due-to="Ng Tsz Sum">
Fix flaky EmptyPropertiesTest#testSave.
</action>
<!-- ADD -->
<action issue="COLLECTIONS-760" dev="kinow" type="add" due-to="Isira Seneviratne">
Add tests for MapUtils.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,20 @@

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.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Properties;
Expand All @@ -31,15 +39,6 @@
import org.apache.commons.lang3.ArrayUtils;
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 {

@Test
Expand Down Expand Up @@ -259,26 +258,29 @@ public void testReplaceOldValue() {
@Test
public void testSave() throws IOException {
final String comments = "Hello world!";
// actual
try (ByteArrayOutputStream actual = new ByteArrayOutputStream()) {
PropertiesFactory.EMPTY_PROPERTIES.save(actual, comments);
try (ByteArrayOutputStream actual = new ByteArrayOutputStream(); ByteArrayOutputStream expected = new ByteArrayOutputStream()) {
// actual
PropertiesFactory.EMPTY_PROPERTIES.store(actual, comments);
// expected
try (ByteArrayOutputStream expected = new ByteArrayOutputStream()) {
PropertiesFactory.INSTANCE.createProperties().save(expected, comments);

// Properties.save stores the specified comment appended with current time stamp in the next line
String expectedComment = getFirstLine(expected.toString("UTF-8"));
String actualComment = getFirstLine(actual.toString("UTF-8"));
assertEquals(expectedComment, actualComment, () ->
String.format("Expected String '%s' with length '%s'", expectedComment, expectedComment.length()));
expected.reset();
try (PrintStream out = new PrintStream(expected)) {
new Properties().save(out, comments);
}
assertArrayEquals(expected.toByteArray(), actual.toByteArray(), expected::toString);
} catch (UnsupportedEncodingException e) {
fail(e.getMessage(), e);
PropertiesFactory.INSTANCE.createProperties().store(expected, comments);

// Properties.store stores the specified comment appended with current time stamp in the next line
String expectedComment = getFirstLine(expected.toString("UTF-8"));
String actualComment = getFirstLine(actual.toString("UTF-8"));
assertEquals(expectedComment, actualComment, () ->
String.format("Expected String '%s' with length '%s'", expectedComment, expectedComment.length()));
expected.reset();
try (PrintStream out = new PrintStream(expected)) {
new Properties().store(out, comments);
}
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
}
}

Expand Down

0 comments on commit 1677dac

Please sign in to comment.