[CSV-158] Fix EOL checking for read array in ExtendedBufferedReader.

Clean up new test methods. Sort methods.
This commit is contained in:
Gary Gregory 2021-07-07 08:43:55 -04:00
parent a6ca4165a8
commit 93a9affacf
2 changed files with 97 additions and 95 deletions

View File

@ -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 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 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-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 --> <!-- ADD -->
<action issue="CSV-275" type="add" dev="ggregory" due-to="Michael Wyraz, Gary Gregory">Make CSVRecord#toList() public.</action> <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> <action type="add" dev="ggregory" due-to="Gary Gregory">Add CSVRecord#stream().</action>

View File

@ -28,10 +28,14 @@ import java.io.StringReader;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
/** /**
* * Test {@link ExtendedBufferedReader}.
*/ */
public class ExtendedBufferedReaderTest { public class ExtendedBufferedReaderTest {
private ExtendedBufferedReader createBufferedReader(final String s) {
return new ExtendedBufferedReader(new StringReader(s));
}
@Test @Test
public void testEmptyInput() throws Exception { public void testEmptyInput() throws Exception {
try (final ExtendedBufferedReader br = createBufferedReader("")) { try (final ExtendedBufferedReader br = createBufferedReader("")) {
@ -43,6 +47,97 @@ public class ExtendedBufferedReaderTest {
} }
} }
/*
* 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 @Test
public void testReadLookahead1() throws Exception { public void testReadLookahead1() throws Exception {
try (final ExtendedBufferedReader br = createBufferedReader("1\n2\r3\n")) { try (final ExtendedBufferedReader br = createBufferedReader("1\n2\r3\n")) {
@ -122,98 +217,4 @@ public class ExtendedBufferedReaderTest {
assertEquals('d', br.getLastChar()); 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));
}
} }