Skip to content

Commit 75951b7

Browse files
committed
add more tests for IOUtils.contentEqualsIgnoreEOL
1 parent 4b5db34 commit 75951b7

File tree

1 file changed

+45
-22
lines changed

1 file changed

+45
-22
lines changed

src/test/java/org/apache/commons/io/IOUtilsTest.java

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,8 @@ public void testContentEquals_InputStream_InputStream() throws Exception {
525525
assertTrue(IOUtils.contentEquals(new ByteArrayInputStream(dataAbc), new ByteArrayInputStream(dataAbc)));
526526
assertFalse(IOUtils.contentEquals(new ByteArrayInputStream(dataAbcd), new ByteArrayInputStream(dataAbc)));
527527
assertFalse(IOUtils.contentEquals(new ByteArrayInputStream(dataAbc), new ByteArrayInputStream(dataAbcd)));
528+
assertFalse(IOUtils.contentEquals(new ByteArrayInputStream("apache".getBytes(StandardCharsets.UTF_8)),
529+
new ByteArrayInputStream("apacha".getBytes(StandardCharsets.UTF_8))));
528530
// Tests with larger inputs that DEFAULT_BUFFER_SIZE in case internal buffers are used.
529531
final byte[] bytes2XDefaultA = new byte[IOUtils.DEFAULT_BUFFER_SIZE * 2];
530532
final byte[] bytes2XDefaultB = new byte[IOUtils.DEFAULT_BUFFER_SIZE * 2];
@@ -576,6 +578,7 @@ public void testContentEquals_Reader_Reader() throws Exception {
576578
assertTrue(IOUtils.contentEquals(new StringReader("ABC"), new StringReader("ABC")));
577579
assertFalse(IOUtils.contentEquals(new StringReader("ABCD"), new StringReader("ABC")));
578580
assertFalse(IOUtils.contentEquals(new StringReader("ABC"), new StringReader("ABCD")));
581+
assertFalse(IOUtils.contentEquals(new StringReader("apache"), new StringReader("apacha")));
579582
}
580583

581584
@Test
@@ -600,28 +603,48 @@ public void testContentEqualsIgnoreEOL() throws Exception {
600603
assertTrue(IOUtils.contentEqualsIgnoreEOL(input1, input1));
601604
}
602605

603-
Reader r1;
604-
Reader r2;
605-
606-
r1 = new CharArrayReader("".toCharArray());
607-
r2 = new CharArrayReader("".toCharArray());
608-
assertTrue(IOUtils.contentEqualsIgnoreEOL(r1, r2));
609-
610-
r1 = new CharArrayReader("1".toCharArray());
611-
r2 = new CharArrayReader("1".toCharArray());
612-
assertTrue(IOUtils.contentEqualsIgnoreEOL(r1, r2));
613-
614-
r1 = new CharArrayReader("1".toCharArray());
615-
r2 = new CharArrayReader("2".toCharArray());
616-
assertFalse(IOUtils.contentEqualsIgnoreEOL(r1, r2));
617-
618-
r1 = new CharArrayReader("123\rabc".toCharArray());
619-
r2 = new CharArrayReader("123\nabc".toCharArray());
620-
assertTrue(IOUtils.contentEqualsIgnoreEOL(r1, r2));
621-
622-
r1 = new CharArrayReader("321".toCharArray());
623-
r2 = new CharArrayReader("321\r\n".toCharArray());
624-
assertTrue(IOUtils.contentEqualsIgnoreEOL(r1, r2));
606+
testSingleEOL("", "", true);
607+
testSingleEOL("", "\n", false);
608+
testSingleEOL("", "\r", false);
609+
testSingleEOL("", "\r\n", false);
610+
testSingleEOL("", "\r\r", false);
611+
testSingleEOL("", "\n\n", false);
612+
testSingleEOL("1", "1", true);
613+
testSingleEOL("1", "2", false);
614+
testSingleEOL("123\rabc", "123\nabc", true);
615+
testSingleEOL("321", "321\r\n", true);
616+
testSingleEOL("321", "321\r\naabb", false);
617+
testSingleEOL("321", "321\n", true);
618+
testSingleEOL("321", "321\r", true);
619+
testSingleEOL("321", "321\r\n", true);
620+
testSingleEOL("321", "321\r\r", false);
621+
testSingleEOL("321", "321\n\r", false);
622+
testSingleEOL("321\n", "321", true);
623+
testSingleEOL("321\n", "321\n\r", false);
624+
testSingleEOL("321\n", "321\r\n", true);
625+
testSingleEOL("321\r", "321\r\n", true);
626+
testSingleEOL("321\r\n", "321\r\n\r", false);
627+
testSingleEOL("123", "1234", false);
628+
testSingleEOL("1235", "1234", false);
629+
}
630+
631+
public void testSingleEOL(String s1, String s2, boolean ifEquals) throws IOException {
632+
assertEquals(ifEquals, IOUtils.contentEqualsIgnoreEOL(
633+
new CharArrayReader(s1.toCharArray()),
634+
new CharArrayReader(s2.toCharArray())
635+
), "failed at :{" + s1 + "," + s2 + "}");
636+
assertEquals(ifEquals, IOUtils.contentEqualsIgnoreEOL(
637+
new CharArrayReader(s2.toCharArray()),
638+
new CharArrayReader(s1.toCharArray())
639+
), "failed at :{" + s2 + "," + s1 + "}");
640+
assertTrue(IOUtils.contentEqualsIgnoreEOL(
641+
new CharArrayReader(s1.toCharArray()),
642+
new CharArrayReader(s1.toCharArray())
643+
),"failed at :{" + s1 + "," + s1 + "}");
644+
assertTrue(IOUtils.contentEqualsIgnoreEOL(
645+
new CharArrayReader(s2.toCharArray()),
646+
new CharArrayReader(s2.toCharArray())
647+
), "failed at :{" + s2 + "," + s2 + "}");
625648
}
626649

627650
@Test

0 commit comments

Comments
 (0)