Skip to content
Permalink
Browse files
[CSV-158] Fix EOL checking for read array in ExtendedBufferedReader.
Clean up new test methods. Sort methods.
  • Loading branch information
garydgregory committed Jul 7, 2021
1 parent a6ca416 commit 93a9affacf54011487d26b453b7f1ad18990f61c
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 95 deletions.
@@ -58,6 +58,7 @@
<action issue="CSV-148" type="fix" dev="ggregory" due-to="dota17">Add test cases for withIgnoreSurroundingSpaces() and withTrim() #70.</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory">Update CSVParser.parse(File, Charset, CSVFormat) from IO to NIO.</action>
<action issue="CSV-271" type="fix" dev="ggregory" due-to="Amar Prakash Pandey">Missing separator with print(object) followed by printRecord(Object[]) #157.</action>
<action issue="CSV-158" type="fix" dev="ggregory" due-to="Alexander Bondarev, Benedikt Ritter, Gary Gregory, Chen">Fix EOL checking for read array in ExtendedBufferedReader #5.</action>
<!-- ADD -->
<action issue="CSV-275" type="add" dev="ggregory" due-to="Michael Wyraz, Gary Gregory">Make CSVRecord#toList() public.</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add CSVRecord#stream().</action>
@@ -28,10 +28,14 @@
import org.junit.jupiter.api.Test;

/**
*
* Test {@link ExtendedBufferedReader}.
*/
public class ExtendedBufferedReaderTest {

private ExtendedBufferedReader createBufferedReader(final String s) {
return new ExtendedBufferedReader(new StringReader(s));
}

@Test
public void testEmptyInput() throws Exception {
try (final ExtendedBufferedReader br = createBufferedReader("")) {
@@ -43,6 +47,97 @@ public void testEmptyInput() throws Exception {
}
}

/*
* Test to illustrate https://issues.apache.org/jira/browse/CSV-75
*
*/
@Test
public void testReadChar() throws Exception {
final String LF = "\n";
final String CR = "\r";
final String CRLF = CR + LF;
final String LFCR = LF + CR;// easier to read the string below
final String test = "a" + LF + "b" + CR + "c" + LF + LF + "d" + CR + CR + "e" + LFCR + "f " + CRLF;
// EOL eol EOL EOL eol eol EOL+CR EOL
final int EOLeolct = 9;

try (final ExtendedBufferedReader br = createBufferedReader(test)) {
assertEquals(0, br.getCurrentLineNumber());
while (br.readLine() != null) {
// consume all
}
assertEquals(EOLeolct, br.getCurrentLineNumber());
}
try (final ExtendedBufferedReader br = createBufferedReader(test)) {
assertEquals(0, br.getCurrentLineNumber());
while (br.read() != -1) {
// consume all
}
assertEquals(EOLeolct, br.getCurrentLineNumber());
}
try (final ExtendedBufferedReader br = createBufferedReader(test)) {
assertEquals(0, br.getCurrentLineNumber());
final char[] buff = new char[10];
while (br.read(buff, 0, 3) != -1) {
// consume all
}
assertEquals(EOLeolct, br.getCurrentLineNumber());
}
}

@Test
public void testReadingInDifferentBuffer() throws Exception {
char[] tmp1 = new char[2], tmp2 = new char[4];
try (ExtendedBufferedReader reader = createBufferedReader("1\r\n2\r\n")) {
reader.read(tmp1, 0, 2);
reader.read(tmp2, 2, 2);
assertEquals(2, reader.getCurrentLineNumber());
}
}

@Test
public void testReadLine() throws Exception {
try (final ExtendedBufferedReader br = createBufferedReader("")) {
assertNull(br.readLine());
}
try (final ExtendedBufferedReader br = createBufferedReader("\n")) {
assertEquals("", br.readLine());
assertNull(br.readLine());
}
try (final ExtendedBufferedReader br = createBufferedReader("foo\n\nhello")) {
assertEquals(0, br.getCurrentLineNumber());
assertEquals("foo", br.readLine());
assertEquals(1, br.getCurrentLineNumber());
assertEquals("", br.readLine());
assertEquals(2, br.getCurrentLineNumber());
assertEquals("hello", br.readLine());
assertEquals(3, br.getCurrentLineNumber());
assertNull(br.readLine());
assertEquals(3, br.getCurrentLineNumber());
}
try (final ExtendedBufferedReader br = createBufferedReader("foo\n\nhello")) {
assertEquals('f', br.read());
assertEquals('o', br.lookAhead());
assertEquals("oo", br.readLine());
assertEquals(1, br.getCurrentLineNumber());
assertEquals('\n', br.lookAhead());
assertEquals("", br.readLine());
assertEquals(2, br.getCurrentLineNumber());
assertEquals('h', br.lookAhead());
assertEquals("hello", br.readLine());
assertNull(br.readLine());
assertEquals(3, br.getCurrentLineNumber());
}
try (final ExtendedBufferedReader br = createBufferedReader("foo\rbaar\r\nfoo")) {
assertEquals("foo", br.readLine());
assertEquals('b', br.lookAhead());
assertEquals("baar", br.readLine());
assertEquals('f', br.lookAhead());
assertEquals("foo", br.readLine());
assertNull(br.readLine());
}
}

@Test
public void testReadLookahead1() throws Exception {
try (final ExtendedBufferedReader br = createBufferedReader("1\n2\r3\n")) {
@@ -122,98 +217,4 @@ public void testReadLookahead2() throws Exception {
assertEquals('d', br.getLastChar());
}
}

@Test
public void testReadLine() throws Exception {
try (final ExtendedBufferedReader br = createBufferedReader("")) {
assertNull(br.readLine());
}
try (final ExtendedBufferedReader br = createBufferedReader("\n")) {
assertEquals("", br.readLine());
assertNull(br.readLine());
}
try (final ExtendedBufferedReader br = createBufferedReader("foo\n\nhello")) {
assertEquals(0, br.getCurrentLineNumber());
assertEquals("foo", br.readLine());
assertEquals(1, br.getCurrentLineNumber());
assertEquals("", br.readLine());
assertEquals(2, br.getCurrentLineNumber());
assertEquals("hello", br.readLine());
assertEquals(3, br.getCurrentLineNumber());
assertNull(br.readLine());
assertEquals(3, br.getCurrentLineNumber());
}
try (final ExtendedBufferedReader br = createBufferedReader("foo\n\nhello")) {
assertEquals('f', br.read());
assertEquals('o', br.lookAhead());
assertEquals("oo", br.readLine());
assertEquals(1, br.getCurrentLineNumber());
assertEquals('\n', br.lookAhead());
assertEquals("", br.readLine());
assertEquals(2, br.getCurrentLineNumber());
assertEquals('h', br.lookAhead());
assertEquals("hello", br.readLine());
assertNull(br.readLine());
assertEquals(3, br.getCurrentLineNumber());
}
try (final ExtendedBufferedReader br = createBufferedReader("foo\rbaar\r\nfoo")) {
assertEquals("foo", br.readLine());
assertEquals('b', br.lookAhead());
assertEquals("baar", br.readLine());
assertEquals('f', br.lookAhead());
assertEquals("foo", br.readLine());
assertNull(br.readLine());
}
}

/*
* Test to illustrate https://issues.apache.org/jira/browse/CSV-75
*
*/
@Test
public void testReadChar() throws Exception {
final String LF = "\n";
final String CR = "\r";
final String CRLF = CR + LF;
final String LFCR = LF + CR;// easier to read the string below
final String test = "a" + LF + "b" + CR + "c" + LF + LF + "d" + CR + CR + "e" + LFCR + "f " + CRLF;
// EOL eol EOL EOL eol eol EOL+CR EOL
final int EOLeolct = 9;

try (final ExtendedBufferedReader br = createBufferedReader(test)) {
assertEquals(0, br.getCurrentLineNumber());
while (br.readLine() != null) {
// consume all
}
assertEquals(EOLeolct, br.getCurrentLineNumber());
}
try (final ExtendedBufferedReader br = createBufferedReader(test)) {
assertEquals(0, br.getCurrentLineNumber());
while (br.read() != -1) {
// consume all
}
assertEquals(EOLeolct, br.getCurrentLineNumber());
}
try (final ExtendedBufferedReader br = createBufferedReader(test)) {
assertEquals(0, br.getCurrentLineNumber());
final char[] buff = new char[10];
while (br.read(buff, 0, 3) != -1) {
// consume all
}
assertEquals(EOLeolct, br.getCurrentLineNumber());
}
}

@Test
public void testReadingInDifferentBuffer() throws Exception {
char[] tmp1 = new char[2], tmp2 = new char[4];
ExtendedBufferedReader br = createBufferedReader("1\r\n2\r\n");
br.read(tmp1, 0, 2);
br.read(tmp2, 2, 2);
assertEquals(2, br.getCurrentLineNumber());
}

private ExtendedBufferedReader createBufferedReader(final String s) {
return new ExtendedBufferedReader(new StringReader(s));
}
}

0 comments on commit 93a9aff

Please sign in to comment.