|
| 1 | +package org.commonmark.internal.util; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import java.io.*; |
| 6 | +import java.nio.charset.StandardCharsets; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.Arrays; |
| 9 | +import java.util.Objects; |
| 10 | + |
| 11 | +import static java.util.stream.Collectors.joining; |
| 12 | +import static org.commonmark.internal.util.LineReader.CHAR_BUFFER_SIZE; |
| 13 | +import static org.junit.Assert.*; |
| 14 | + |
| 15 | +public class LineReaderTest { |
| 16 | + |
| 17 | + @Test |
| 18 | + public void testReadLine() throws IOException { |
| 19 | + assertLines(); |
| 20 | + |
| 21 | + assertLines("", "\n"); |
| 22 | + assertLines("foo", "\n", "bar", "\n"); |
| 23 | + assertLines("foo", "\n", "bar", null); |
| 24 | + assertLines("", "\n", "", "\n"); |
| 25 | + assertLines(repeat("a", CHAR_BUFFER_SIZE - 1), "\n"); |
| 26 | + assertLines(repeat("a", CHAR_BUFFER_SIZE), "\n"); |
| 27 | + assertLines(repeat("a", CHAR_BUFFER_SIZE) + "b", "\n"); |
| 28 | + |
| 29 | + assertLines("", "\r\n"); |
| 30 | + assertLines("foo", "\r\n", "bar", "\r\n"); |
| 31 | + assertLines("foo", "\r\n", "bar", null); |
| 32 | + assertLines("", "\r\n", "", "\r\n"); |
| 33 | + assertLines(repeat("a", CHAR_BUFFER_SIZE - 2), "\r\n"); |
| 34 | + assertLines(repeat("a", CHAR_BUFFER_SIZE - 1), "\r\n"); |
| 35 | + assertLines(repeat("a", CHAR_BUFFER_SIZE), "\r\n"); |
| 36 | + assertLines(repeat("a", CHAR_BUFFER_SIZE) + "b", "\r\n"); |
| 37 | + |
| 38 | + assertLines("", "\r"); |
| 39 | + assertLines("foo", "\r", "bar", "\r"); |
| 40 | + assertLines("foo", "\r", "bar", null); |
| 41 | + assertLines("", "\r", "", "\r"); |
| 42 | + assertLines(repeat("a", CHAR_BUFFER_SIZE - 1), "\r"); |
| 43 | + assertLines(repeat("a", CHAR_BUFFER_SIZE), "\r"); |
| 44 | + assertLines(repeat("a", CHAR_BUFFER_SIZE) + "b", "\r"); |
| 45 | + |
| 46 | + assertLines("", "\n", "", "\r", "", "\r\n", "", "\n"); |
| 47 | + assertLines("what", "\r", "are", "\r", "", "\r", "you", "\r\n", "", "\r\n", "even", "\n", "doing", null); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testClose() throws IOException { |
| 52 | + var reader = new InputStreamReader(new ByteArrayInputStream("test".getBytes(StandardCharsets.UTF_8))); |
| 53 | + var lineReader = new LineReader(reader); |
| 54 | + lineReader.close(); |
| 55 | + lineReader.close(); |
| 56 | + try { |
| 57 | + reader.read(); |
| 58 | + fail("Expected read to throw after closing reader"); |
| 59 | + } catch (IOException e) { |
| 60 | + // Expected |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private void assertLines(String... s) throws IOException { |
| 65 | + assertTrue("Expected parts needs to be even (pairs of content and terminator)", s.length % 2 == 0); |
| 66 | + var input = Arrays.stream(s).filter(Objects::nonNull).collect(joining("")); |
| 67 | + |
| 68 | + assertLines(new StringReader(input), s); |
| 69 | + assertLines(new SlowStringReader(input), s); |
| 70 | + } |
| 71 | + |
| 72 | + private static void assertLines(Reader reader, String... expectedParts) throws IOException { |
| 73 | + try (var lineReader = new LineReader(reader)) { |
| 74 | + var lines = new ArrayList<>(); |
| 75 | + String line; |
| 76 | + while ((line = lineReader.readLine()) != null) { |
| 77 | + lines.add(line); |
| 78 | + lines.add(lineReader.getLineTerminator()); |
| 79 | + } |
| 80 | + assertNull(lineReader.getLineTerminator()); |
| 81 | + assertEquals(Arrays.asList(expectedParts), lines); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private static String repeat(String s, int count) { |
| 86 | + StringBuilder sb = new StringBuilder(s.length() * count); |
| 87 | + for (int i = 0; i < count; i++) { |
| 88 | + sb.append(s); |
| 89 | + } |
| 90 | + return sb.toString(); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Reader that only reads 0 or 1 chars at a time to test the corner cases. |
| 95 | + */ |
| 96 | + private static class SlowStringReader extends Reader { |
| 97 | + |
| 98 | + private final String s; |
| 99 | + private int position = 0; |
| 100 | + private boolean empty = false; |
| 101 | + |
| 102 | + private SlowStringReader(String s) { |
| 103 | + this.s = s; |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public int read(char[] cbuf, int off, int len) throws IOException { |
| 108 | + Objects.checkFromIndexSize(off, len, cbuf.length); |
| 109 | + if (len == 0) { |
| 110 | + return 0; |
| 111 | + } |
| 112 | + empty = !empty; |
| 113 | + if (empty) { |
| 114 | + // Return 0 every other time to test handling of 0. |
| 115 | + return 0; |
| 116 | + } |
| 117 | + if (position >= s.length()) { |
| 118 | + return -1; |
| 119 | + } |
| 120 | + cbuf[off] = s.charAt(position++); |
| 121 | + return 1; |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public void close() throws IOException { |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments