This is an automated email from the ASF dual-hosted git repository.

kinow pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-collections.git


The following commit(s) were added to refs/heads/master by this push:
     new 1677dacea [COLLECTIONS-812] Open both streams with try-with-resources, 
and assert that only the text is the same, not the time
1677dacea is described below

commit 1677daceab74895fdf5056c9a48aa94f9e709fb9
Author: Bruno P. Kinoshita <ki...@users.noreply.github.com>
AuthorDate: Sun May 1 11:57:40 2022 +1200

    [COLLECTIONS-812] Open both streams with try-with-resources, and assert 
that only the text is the same, not the time
---
 src/changes/changes.xml                            |  3 ++
 .../properties/EmptyPropertiesTest.java            | 58 +++++++++++-----------
 2 files changed, 33 insertions(+), 28 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index ab82fa4ea..4a6697c96 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -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.
diff --git 
a/src/test/java/org/apache/commons/collections4/properties/EmptyPropertiesTest.java
 
b/src/test/java/org/apache/commons/collections4/properties/EmptyPropertiesTest.java
index 3d6ffb6f7..90ac04f99 100644
--- 
a/src/test/java/org/apache/commons/collections4/properties/EmptyPropertiesTest.java
+++ 
b/src/test/java/org/apache/commons/collections4/properties/EmptyPropertiesTest.java
@@ -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;
@@ -31,15 +39,6 @@ import org.apache.commons.io.input.NullReader;
 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
@@ -259,26 +258,29 @@ public class EmptyPropertiesTest {
     @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
         }
     }
 

Reply via email to