Skip to content

Commit

Permalink
add more tests for IOUtils.contentEqualsIgnoreEOL (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
XenoAmess authored Apr 4, 2022
1 parent 9d70e61 commit 99e66c0
Showing 1 changed file with 45 additions and 22 deletions.
67 changes: 45 additions & 22 deletions src/test/java/org/apache/commons/io/IOUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,8 @@ public void testContentEquals_InputStream_InputStream() throws Exception {
assertTrue(IOUtils.contentEquals(new ByteArrayInputStream(dataAbc), new ByteArrayInputStream(dataAbc)));
assertFalse(IOUtils.contentEquals(new ByteArrayInputStream(dataAbcd), new ByteArrayInputStream(dataAbc)));
assertFalse(IOUtils.contentEquals(new ByteArrayInputStream(dataAbc), new ByteArrayInputStream(dataAbcd)));
assertFalse(IOUtils.contentEquals(new ByteArrayInputStream("apache".getBytes(StandardCharsets.UTF_8)),
new ByteArrayInputStream("apacha".getBytes(StandardCharsets.UTF_8))));
// Tests with larger inputs that DEFAULT_BUFFER_SIZE in case internal buffers are used.
final byte[] bytes2XDefaultA = new byte[IOUtils.DEFAULT_BUFFER_SIZE * 2];
final byte[] bytes2XDefaultB = new byte[IOUtils.DEFAULT_BUFFER_SIZE * 2];
Expand Down Expand Up @@ -582,6 +584,7 @@ public void testContentEquals_Reader_Reader() throws Exception {
assertTrue(IOUtils.contentEquals(new StringReader("ABC"), new StringReader("ABC")));
assertFalse(IOUtils.contentEquals(new StringReader("ABCD"), new StringReader("ABC")));
assertFalse(IOUtils.contentEquals(new StringReader("ABC"), new StringReader("ABCD")));
assertFalse(IOUtils.contentEquals(new StringReader("apache"), new StringReader("apacha")));
}

@Test
Expand All @@ -606,28 +609,48 @@ public void testContentEqualsIgnoreEOL() throws Exception {
assertTrue(IOUtils.contentEqualsIgnoreEOL(input1, input1));
}

Reader r1;
Reader r2;

r1 = new CharArrayReader("".toCharArray());
r2 = new CharArrayReader("".toCharArray());
assertTrue(IOUtils.contentEqualsIgnoreEOL(r1, r2));

r1 = new CharArrayReader("1".toCharArray());
r2 = new CharArrayReader("1".toCharArray());
assertTrue(IOUtils.contentEqualsIgnoreEOL(r1, r2));

r1 = new CharArrayReader("1".toCharArray());
r2 = new CharArrayReader("2".toCharArray());
assertFalse(IOUtils.contentEqualsIgnoreEOL(r1, r2));

r1 = new CharArrayReader("123\rabc".toCharArray());
r2 = new CharArrayReader("123\nabc".toCharArray());
assertTrue(IOUtils.contentEqualsIgnoreEOL(r1, r2));

r1 = new CharArrayReader("321".toCharArray());
r2 = new CharArrayReader("321\r\n".toCharArray());
assertTrue(IOUtils.contentEqualsIgnoreEOL(r1, r2));
testSingleEOL("", "", true);
testSingleEOL("", "\n", false);
testSingleEOL("", "\r", false);
testSingleEOL("", "\r\n", false);
testSingleEOL("", "\r\r", false);
testSingleEOL("", "\n\n", false);
testSingleEOL("1", "1", true);
testSingleEOL("1", "2", false);
testSingleEOL("123\rabc", "123\nabc", true);
testSingleEOL("321", "321\r\n", true);
testSingleEOL("321", "321\r\naabb", false);
testSingleEOL("321", "321\n", true);
testSingleEOL("321", "321\r", true);
testSingleEOL("321", "321\r\n", true);
testSingleEOL("321", "321\r\r", false);
testSingleEOL("321", "321\n\r", false);
testSingleEOL("321\n", "321", true);
testSingleEOL("321\n", "321\n\r", false);
testSingleEOL("321\n", "321\r\n", true);
testSingleEOL("321\r", "321\r\n", true);
testSingleEOL("321\r\n", "321\r\n\r", false);
testSingleEOL("123", "1234", false);
testSingleEOL("1235", "1234", false);
}

public void testSingleEOL(String s1, String s2, boolean ifEquals) throws IOException {
assertEquals(ifEquals, IOUtils.contentEqualsIgnoreEOL(
new CharArrayReader(s1.toCharArray()),
new CharArrayReader(s2.toCharArray())
), "failed at :{" + s1 + "," + s2 + "}");
assertEquals(ifEquals, IOUtils.contentEqualsIgnoreEOL(
new CharArrayReader(s2.toCharArray()),
new CharArrayReader(s1.toCharArray())
), "failed at :{" + s2 + "," + s1 + "}");
assertTrue(IOUtils.contentEqualsIgnoreEOL(
new CharArrayReader(s1.toCharArray()),
new CharArrayReader(s1.toCharArray())
),"failed at :{" + s1 + "," + s1 + "}");
assertTrue(IOUtils.contentEqualsIgnoreEOL(
new CharArrayReader(s2.toCharArray()),
new CharArrayReader(s2.toCharArray())
), "failed at :{" + s2 + "," + s2 + "}");
}

@Test
Expand Down

0 comments on commit 99e66c0

Please sign in to comment.