You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/08/24 12:01:48 UTC
[commons-collections] branch master updated: [COLLECTIONS-833] Intermittent failure in EmptyPropertiesTest
This is an automated email from the ASF dual-hosted git repository.
ggregory 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 f1e2c878b [COLLECTIONS-833] Intermittent failure in EmptyPropertiesTest
f1e2c878b is described below
commit f1e2c878b8df2d8794a58956babdcc8fa4d74f9c
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Wed Aug 24 08:01:43 2022 -0400
[COLLECTIONS-833] Intermittent failure in EmptyPropertiesTest
Ignore second line of properties files since contain a timestamp, which
is a problem when the time ticks to the next second within some tests
---
.../properties/EmptyPropertiesTest.java | 129 ++++++++++++++-------
1 file changed, 87 insertions(+), 42 deletions(-)
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 90ac04f99..243ab6009 100644
--- a/src/test/java/org/apache/commons/collections4/properties/EmptyPropertiesTest.java
+++ b/src/test/java/org/apache/commons/collections4/properties/EmptyPropertiesTest.java
@@ -31,16 +31,39 @@ 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;
-
import org.apache.commons.io.input.NullReader;
import org.apache.commons.lang3.ArrayUtils;
import org.junit.jupiter.api.Test;
public class EmptyPropertiesTest {
+ /**
+ * Returns the first line from multi-lined string separated by a line separator character
+ *
+ * @param x the multi-lined String
+ * @return the first line from x
+ */
+ private String getFirstLine(final String x) {
+ return x.split("\\R", 2)[0];
+ }
+
+ private PrintStream newPrintStream(final ByteArrayOutputStream baos) throws UnsupportedEncodingException {
+ return new PrintStream(baos, true, StandardCharsets.UTF_8.name());
+ }
+
+ private String removeLine2(final ByteArrayOutputStream baos) {
+ return removeLine2(toString(baos));
+ }
+
+ private String removeLine2(final String x) {
+ final String[] s = x.split("\\R", 2);
+ return s[0] + System.lineSeparator() + (s.length > 2 ? s[2] : "");
+ }
+
@Test
public void testClear() {
PropertiesFactory.EMPTY_PROPERTIES.clear();
@@ -133,8 +156,7 @@ public class EmptyPropertiesTest {
@Test
public void testHashCode() {
- assertEquals(PropertiesFactory.EMPTY_PROPERTIES.hashCode(),
- PropertiesFactory.EMPTY_PROPERTIES.hashCode());
+ assertEquals(PropertiesFactory.EMPTY_PROPERTIES.hashCode(), PropertiesFactory.EMPTY_PROPERTIES.hashCode());
// Should be equals?
// assertEquals(PropertiesFactory.EMPTY_PROPERTIES.hashCode(), new Properties().hashCode());
}
@@ -184,7 +206,8 @@ public class EmptyPropertiesTest {
@Test
public void testLoadFromXML() throws IOException {
- assertThrows(UnsupportedOperationException.class, () -> PropertiesFactory.EMPTY_PROPERTIES.loadFromXML(new ByteArrayInputStream(ArrayUtils.EMPTY_BYTE_ARRAY)));
+ assertThrows(UnsupportedOperationException.class,
+ () -> PropertiesFactory.EMPTY_PROPERTIES.loadFromXML(new ByteArrayInputStream(ArrayUtils.EMPTY_BYTE_ARRAY)));
}
@Test
@@ -265,35 +288,25 @@ public class EmptyPropertiesTest {
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()));
+ final String expectedComment = getFirstLine(expected.toString("UTF-8"));
+ final 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");
+ final String[] expectedLines = expected.toString(StandardCharsets.UTF_8.displayName()).split("\\n");
+ final 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
+ // it contains the timestamp of when the data was written to the stream, which makes
+ // this test brittle, causing intermitent failures, see COLLECTIONS-812
}
}
- /**
- * Returns the first line from multi-lined string separated by a line separator character
- * @param x the multi-lined String
- * @return the first line from x
- */
- private String getFirstLine(final String x) {
- return x.split("\\R", 2)[0];
- }
-
-
@Test
public void testSetProperty() {
assertThrows(UnsupportedOperationException.class, () -> PropertiesFactory.EMPTY_PROPERTIES.setProperty("Key", "Value"));
@@ -306,63 +319,91 @@ public class EmptyPropertiesTest {
@Test
public void testStoreToOutputStream() throws IOException {
+ // Note: The second line is always a comment with a timestamp.
final String comments = "Hello world!";
// actual
final ByteArrayOutputStream actual = new ByteArrayOutputStream();
- PropertiesFactory.EMPTY_PROPERTIES.store(new PrintStream(actual), comments);
+ try (PrintStream ps = newPrintStream(actual)) {
+ PropertiesFactory.EMPTY_PROPERTIES.store(ps, comments);
+ }
// expected
final ByteArrayOutputStream expected = new ByteArrayOutputStream();
- PropertiesFactory.INSTANCE.createProperties().store(new PrintStream(expected), comments);
- assertArrayEquals(expected.toByteArray(), actual.toByteArray());
+ try (PrintStream ps = newPrintStream(expected)) {
+ PropertiesFactory.INSTANCE.createProperties().store(ps, comments);
+ }
+ assertEquals(removeLine2(expected), removeLine2(actual));
expected.reset();
- new Properties().store(new PrintStream(expected), comments);
- assertArrayEquals(expected.toByteArray(), actual.toByteArray());
+ try (PrintStream ps = newPrintStream(expected)) {
+ new Properties().store(ps, comments);
+ }
+ assertEquals(removeLine2(expected), removeLine2(actual), () -> removeLine2(actual));
}
@Test
public void testStoreToPrintWriter() throws IOException {
+ // Note: The second line is always a comment with a timestamp.
final String comments = "Hello world!";
// actual
final ByteArrayOutputStream actual = new ByteArrayOutputStream();
- PropertiesFactory.EMPTY_PROPERTIES.store(new PrintWriter(actual), comments);
+ try (PrintStream ps = newPrintStream(actual)) {
+ PropertiesFactory.EMPTY_PROPERTIES.store(ps, comments);
+ }
// expected
final ByteArrayOutputStream expected = new ByteArrayOutputStream();
- PropertiesFactory.INSTANCE.createProperties().store(new PrintWriter(expected), comments);
- assertArrayEquals(expected.toByteArray(), actual.toByteArray());
+ try (PrintStream ps = newPrintStream(expected)) {
+ PropertiesFactory.INSTANCE.createProperties().store(ps, comments);
+ }
+ assertEquals(removeLine2(expected), removeLine2(actual));
expected.reset();
- new Properties().store(new PrintWriter(expected), comments);
- assertArrayEquals(expected.toByteArray(), actual.toByteArray());
+ try (PrintStream ps = newPrintStream(expected)) {
+ new Properties().store(ps, comments);
+ }
+ assertEquals(removeLine2(expected), removeLine2(actual));
}
@Test
public void testStoreToXMLOutputStream() throws IOException {
+ // Note: The second line is always a comment with a timestamp.
final String comments = "Hello world!";
// actual
final ByteArrayOutputStream actual = new ByteArrayOutputStream();
- PropertiesFactory.EMPTY_PROPERTIES.storeToXML(new PrintStream(actual), comments);
+ try (PrintStream ps = newPrintStream(actual)) {
+ PropertiesFactory.EMPTY_PROPERTIES.storeToXML(ps, comments);
+ }
// expected
final ByteArrayOutputStream expected = new ByteArrayOutputStream();
- PropertiesFactory.INSTANCE.createProperties().storeToXML(new PrintStream(expected), comments);
- assertArrayEquals(expected.toByteArray(), actual.toByteArray());
+ try (PrintStream ps = newPrintStream(expected)) {
+ PropertiesFactory.INSTANCE.createProperties().storeToXML(ps, comments);
+ }
+ assertEquals(toString(expected), toString(actual));
expected.reset();
- new Properties().storeToXML(new PrintStream(expected), comments);
- assertArrayEquals(expected.toByteArray(), actual.toByteArray());
+ try (PrintStream ps = new PrintStream(expected)) {
+ new Properties().storeToXML(ps, comments);
+ }
+ assertEquals(removeLine2(expected), removeLine2(actual));
}
@Test
public void testStoreToXMLOutputStreamWithEncoding() throws IOException {
+ // Note: The second line is always a comment with a timestamp.
final String comments = "Hello world!";
final String encoding = StandardCharsets.UTF_8.name();
// actual
final ByteArrayOutputStream actual = new ByteArrayOutputStream();
- PropertiesFactory.EMPTY_PROPERTIES.storeToXML(new PrintStream(actual), comments, encoding);
+ try (PrintStream ps = newPrintStream(actual)) {
+ PropertiesFactory.EMPTY_PROPERTIES.storeToXML(ps, comments, encoding);
+ }
// expected
final ByteArrayOutputStream expected = new ByteArrayOutputStream();
- PropertiesFactory.INSTANCE.createProperties().storeToXML(new PrintStream(expected), comments, encoding);
- assertArrayEquals(expected.toByteArray(), actual.toByteArray());
+ try (PrintStream ps = newPrintStream(expected)) {
+ PropertiesFactory.INSTANCE.createProperties().storeToXML(ps, comments, encoding);
+ }
+ assertEquals(removeLine2(expected), removeLine2(actual));
expected.reset();
- new Properties().storeToXML(new PrintStream(expected), comments, encoding);
- assertArrayEquals(expected.toByteArray(), actual.toByteArray());
+ try (PrintStream ps = newPrintStream(expected)) {
+ new Properties().storeToXML(ps, comments, encoding);
+ }
+ assertEquals(removeLine2(expected), removeLine2(actual));
}
@Test
@@ -379,4 +420,8 @@ public class EmptyPropertiesTest {
public void testValues() {
assertTrue(PropertiesFactory.EMPTY_PROPERTIES.isEmpty());
}
+
+ private String toString(final ByteArrayOutputStream expected) {
+ return new String(expected.toByteArray(), StandardCharsets.UTF_8);
+ }
}