Skip to content

Commit

Permalink
COLLECTIONS-807 - Upgraded org.junit.Test to org.junit.jupiter.api.Te…
Browse files Browse the repository at this point in the history
…st: EmptyPropertiesTest.testSave test fail bug fix
  • Loading branch information
Pradeesh Kumar committed Mar 28, 2022
1 parent 5724f8f commit b705855
Showing 1 changed file with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -261,32 +261,37 @@ public void testSave() throws IOException {
final String comments = "Hello world!";
// actual
try (ByteArrayOutputStream actual = new ByteArrayOutputStream()) {
try (PrintStream out = new PrintStream(actual)) {
PropertiesFactory.EMPTY_PROPERTIES.save(out, comments);
}
PropertiesFactory.EMPTY_PROPERTIES.save(actual, comments);
// expected
try (ByteArrayOutputStream expected = new ByteArrayOutputStream()) {
try (PrintStream out = new PrintStream(expected)) {
PropertiesFactory.INSTANCE.createProperties().save(out, comments);
}
assertArrayEquals(expected.toByteArray(), actual.toByteArray(), () -> {
String s = null;
try {
s = new String(expected.toByteArray(), "UTF-8");
} catch (UnsupportedEncodingException e) {
fail(e.getMessage(), e);
}
return String.format("Expected String '%s' with length '%s'", s, s.length());
});
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(), () -> new String(expected.toByteArray()));
assertArrayEquals(expected.toByteArray(), actual.toByteArray(), expected::toString);
} catch (UnsupportedEncodingException e) {
fail(e.getMessage(), e);
}
}
}

/**
* 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"));
Expand Down

0 comments on commit b705855

Please sign in to comment.