Update to JUnit 4.10 from 3.8.1.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1300977 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary D. Gregory 2012-03-15 13:44:44 +00:00
parent 38670dbe92
commit 186e9f9804
6 changed files with 271 additions and 216 deletions

View File

@ -16,7 +16,7 @@
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<version>3.8.1</version> <version>4.10</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -22,10 +22,12 @@ import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import junit.framework.TestCase; import org.junit.Assert;
import org.junit.Test;
public class CSVFormatTest extends TestCase { public class CSVFormatTest {
@Test
public void testImmutalibity() { public void testImmutalibity() {
CSVFormat format = new CSVFormat('!', '!', '!', '!', true, true, true, true, "\r\n"); CSVFormat format = new CSVFormat('!', '!', '!', '!', true, true, true, true, "\r\n");
@ -39,91 +41,94 @@ public class CSVFormatTest extends TestCase {
format.withEmptyLinesIgnored(false); format.withEmptyLinesIgnored(false);
format.withUnicodeEscapesInterpreted(false); format.withUnicodeEscapesInterpreted(false);
assertEquals('!', format.getDelimiter()); Assert.assertEquals('!', format.getDelimiter());
assertEquals('!', format.getEncapsulator()); Assert.assertEquals('!', format.getEncapsulator());
assertEquals('!', format.getCommentStart()); Assert.assertEquals('!', format.getCommentStart());
assertEquals('!', format.getEscape()); Assert.assertEquals('!', format.getEscape());
assertEquals("\r\n", format.getLineSeparator()); Assert.assertEquals("\r\n", format.getLineSeparator());
assertEquals(true, format.isLeadingSpacesIgnored()); Assert.assertEquals(true, format.isLeadingSpacesIgnored());
assertEquals(true, format.isTrailingSpacesIgnored()); Assert.assertEquals(true, format.isTrailingSpacesIgnored());
assertEquals(true, format.isEmptyLinesIgnored()); Assert.assertEquals(true, format.isEmptyLinesIgnored());
assertEquals(true, format.isUnicodeEscapesInterpreted()); Assert.assertEquals(true, format.isUnicodeEscapesInterpreted());
} }
@Test
public void testMutators() { public void testMutators() {
CSVFormat format = new CSVFormat('!', '!', '!', '!', true, true, true, true, "\r\n"); CSVFormat format = new CSVFormat('!', '!', '!', '!', true, true, true, true, "\r\n");
assertEquals('?', format.withDelimiter('?').getDelimiter()); Assert.assertEquals('?', format.withDelimiter('?').getDelimiter());
assertEquals('?', format.withEncapsulator('?').getEncapsulator()); Assert.assertEquals('?', format.withEncapsulator('?').getEncapsulator());
assertEquals('?', format.withCommentStart('?').getCommentStart()); Assert.assertEquals('?', format.withCommentStart('?').getCommentStart());
assertEquals("?", format.withLineSeparator("?").getLineSeparator()); Assert.assertEquals("?", format.withLineSeparator("?").getLineSeparator());
assertEquals('?', format.withEscape('?').getEscape()); Assert.assertEquals('?', format.withEscape('?').getEscape());
assertEquals(false, format.withLeadingSpacesIgnored(false).isLeadingSpacesIgnored()); Assert.assertEquals(false, format.withLeadingSpacesIgnored(false).isLeadingSpacesIgnored());
assertEquals(false, format.withTrailingSpacesIgnored(false).isTrailingSpacesIgnored()); Assert.assertEquals(false, format.withTrailingSpacesIgnored(false).isTrailingSpacesIgnored());
assertEquals(false, format.withSurroundingSpacesIgnored(false).isLeadingSpacesIgnored()); Assert.assertEquals(false, format.withSurroundingSpacesIgnored(false).isLeadingSpacesIgnored());
assertEquals(false, format.withSurroundingSpacesIgnored(false).isTrailingSpacesIgnored()); Assert.assertEquals(false, format.withSurroundingSpacesIgnored(false).isTrailingSpacesIgnored());
assertEquals(false, format.withEmptyLinesIgnored(false).isEmptyLinesIgnored()); Assert.assertEquals(false, format.withEmptyLinesIgnored(false).isEmptyLinesIgnored());
assertEquals(false, format.withUnicodeEscapesInterpreted(false).isUnicodeEscapesInterpreted()); Assert.assertEquals(false, format.withUnicodeEscapesInterpreted(false).isUnicodeEscapesInterpreted());
} }
@Test
public void testFormat() { public void testFormat() {
CSVFormat format = CSVFormat.DEFAULT; CSVFormat format = CSVFormat.DEFAULT;
assertEquals("", format.format()); Assert.assertEquals("", format.format());
assertEquals("a,b,c", format.format("a", "b", "c")); Assert.assertEquals("a,b,c", format.format("a", "b", "c"));
assertEquals("\"x,y\",z", format.format("x,y", "z")); Assert.assertEquals("\"x,y\",z", format.format("x,y", "z"));
} }
@Test
public void testValidation() { public void testValidation() {
CSVFormat format = CSVFormat.DEFAULT; CSVFormat format = CSVFormat.DEFAULT;
try { try {
format.withDelimiter('\n'); format.withDelimiter('\n');
fail(); Assert.fail();
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// expected // expected
} }
try { try {
format.withEscape('\r'); format.withEscape('\r');
fail(); Assert.fail();
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// expected // expected
} }
try { try {
format.withEncapsulator('\n'); format.withEncapsulator('\n');
fail(); Assert.fail();
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// expected // expected
} }
try { try {
format.withCommentStart('\r'); format.withCommentStart('\r');
fail(); Assert.fail();
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// expected // expected
} }
try { try {
format.withDelimiter('!').withEscape('!').validate(); format.withDelimiter('!').withEscape('!').validate();
fail(); Assert.fail();
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// expected // expected
} }
try { try {
format.withDelimiter('!').withCommentStart('!').validate(); format.withDelimiter('!').withCommentStart('!').validate();
fail(); Assert.fail();
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// expected // expected
} }
try { try {
format.withEncapsulator('!').withCommentStart('!').validate(); format.withEncapsulator('!').withCommentStart('!').validate();
fail(); Assert.fail();
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// expected // expected
} }
@ -132,7 +137,7 @@ public class CSVFormatTest extends TestCase {
try { try {
format.withEscape('!').withCommentStart('!').validate(); format.withEscape('!').withCommentStart('!').validate();
fail(); Assert.fail();
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// expected // expected
} }
@ -142,12 +147,13 @@ public class CSVFormatTest extends TestCase {
try { try {
format.withEncapsulator('!').withDelimiter('!').validate(); format.withEncapsulator('!').withDelimiter('!').validate();
fail(); Assert.fail();
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// expected // expected
} }
} }
@Test
public void testSerialization() throws Exception { public void testSerialization() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
@ -159,15 +165,15 @@ public class CSVFormatTest extends TestCase {
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(out.toByteArray())); ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(out.toByteArray()));
CSVFormat format = (CSVFormat) in.readObject(); CSVFormat format = (CSVFormat) in.readObject();
assertNotNull(format); Assert.assertNotNull(format);
assertEquals("delimiter", CSVFormat.DEFAULT.getDelimiter(), format.getDelimiter()); Assert.assertEquals("delimiter", CSVFormat.DEFAULT.getDelimiter(), format.getDelimiter());
assertEquals("encapsulator", CSVFormat.DEFAULT.getEncapsulator(), format.getEncapsulator()); Assert.assertEquals("encapsulator", CSVFormat.DEFAULT.getEncapsulator(), format.getEncapsulator());
assertEquals("comment start", CSVFormat.DEFAULT.getCommentStart(), format.getCommentStart()); Assert.assertEquals("comment start", CSVFormat.DEFAULT.getCommentStart(), format.getCommentStart());
assertEquals("line separator", CSVFormat.DEFAULT.getLineSeparator(), format.getLineSeparator()); Assert.assertEquals("line separator", CSVFormat.DEFAULT.getLineSeparator(), format.getLineSeparator());
assertEquals("escape", CSVFormat.DEFAULT.getEscape(), format.getEscape()); Assert.assertEquals("escape", CSVFormat.DEFAULT.getEscape(), format.getEscape());
assertEquals("unicode escape", CSVFormat.DEFAULT.isUnicodeEscapesInterpreted(), format.isUnicodeEscapesInterpreted()); Assert.assertEquals("unicode escape", CSVFormat.DEFAULT.isUnicodeEscapesInterpreted(), format.isUnicodeEscapesInterpreted());
assertEquals("trim left", CSVFormat.DEFAULT.isLeadingSpacesIgnored(), format.isLeadingSpacesIgnored()); Assert.assertEquals("trim left", CSVFormat.DEFAULT.isLeadingSpacesIgnored(), format.isLeadingSpacesIgnored());
assertEquals("trim right", CSVFormat.DEFAULT.isTrailingSpacesIgnored(), format.isTrailingSpacesIgnored()); Assert.assertEquals("trim right", CSVFormat.DEFAULT.isTrailingSpacesIgnored(), format.isTrailingSpacesIgnored());
assertEquals("empty lines", CSVFormat.DEFAULT.isEmptyLinesIgnored(), format.isEmptyLinesIgnored()); Assert.assertEquals("empty lines", CSVFormat.DEFAULT.isEmptyLinesIgnored(), format.isEmptyLinesIgnored());
} }
} }

View File

@ -17,26 +17,30 @@
package org.apache.commons.csv; package org.apache.commons.csv;
import static org.apache.commons.csv.CSVLexer.Token.Type.EOF;
import static org.apache.commons.csv.CSVLexer.Token.Type.EORECORD;
import static org.apache.commons.csv.CSVLexer.Token.Type.TOKEN;
import java.io.IOException; import java.io.IOException;
import java.io.StringReader; import java.io.StringReader;
import junit.framework.TestCase;
import org.apache.commons.csv.CSVLexer.Token; import org.apache.commons.csv.CSVLexer.Token;
import org.junit.Assert;
import org.junit.Test;
import static org.apache.commons.csv.CSVLexer.Token.Type.*; public class CSVLexerTest {
public class CSVLexerTest extends TestCase {
private CSVLexer getLexer(String input, CSVFormat format) { private CSVLexer getLexer(String input, CSVFormat format) {
return new CSVLexer(format, new ExtendedBufferedReader(new StringReader(input))); return new CSVLexer(format, new ExtendedBufferedReader(new StringReader(input)));
} }
private void assertTokenEquals(Token.Type expectedType, String expectedContent, Token token) { private void assertTokenEquals(Token.Type expectedType, String expectedContent, Token token) {
assertEquals("Token type", expectedType, token.type); Assert.assertEquals("Token type", expectedType, token.type);
assertEquals("Token content", expectedContent, token.content.toString()); Assert.assertEquals("Token content", expectedContent, token.content.toString());
} }
// Single line (without comment) // Single line (without comment)
@Test
public void testNextToken1() throws IOException { public void testNextToken1() throws IOException {
String code = "abc,def, hijk, lmnop, qrst,uv ,wxy ,z , ,"; String code = "abc,def, hijk, lmnop, qrst,uv ,wxy ,z , ,";
CSVLexer parser = getLexer(code, CSVFormat.DEFAULT); CSVLexer parser = getLexer(code, CSVFormat.DEFAULT);
@ -53,6 +57,7 @@ public class CSVLexerTest extends TestCase {
} }
// multiline including comments (and empty lines) // multiline including comments (and empty lines)
@Test
public void testNextToken2() throws IOException { public void testNextToken2() throws IOException {
/* file: 1,2,3, /* file: 1,2,3,
* a,b x,c * a,b x,c
@ -84,6 +89,7 @@ public class CSVLexerTest extends TestCase {
} }
// simple token with escaping // simple token with escaping
@Test
public void testNextToken3() throws IOException { public void testNextToken3() throws IOException {
/* file: a,\,,b /* file: a,\,,b
* \,, * \,,
@ -104,6 +110,7 @@ public class CSVLexerTest extends TestCase {
} }
// encapsulator tokenizer (sinle line) // encapsulator tokenizer (sinle line)
@Test
public void testNextToken4() throws IOException { public void testNextToken4() throws IOException {
/* file: a,"foo",b /* file: a,"foo",b
* a, " foo",b * a, " foo",b
@ -128,6 +135,7 @@ public class CSVLexerTest extends TestCase {
} }
// encapsulator tokenizer (multi line, delimiter in string) // encapsulator tokenizer (multi line, delimiter in string)
@Test
public void testNextToken5() throws IOException { public void testNextToken5() throws IOException {
String code = "a,\"foo\n\",b\n\"foo\n baar ,,,\"\n\"\n\t \n\""; String code = "a,\"foo\n\",b\n\"foo\n baar ,,,\"\n\"\n\t \n\"";
CSVLexer parser = getLexer(code, CSVFormat.DEFAULT); CSVLexer parser = getLexer(code, CSVFormat.DEFAULT);
@ -140,6 +148,7 @@ public class CSVLexerTest extends TestCase {
} }
// change delimiters, comment, encapsulater // change delimiters, comment, encapsulater
@Test
public void testNextToken6() throws IOException { public void testNextToken6() throws IOException {
/* file: a;'b and \' more /* file: a;'b and \' more
* ' * '
@ -154,6 +163,7 @@ public class CSVLexerTest extends TestCase {
} }
// From CSV-1 // From CSV-1
@Test
public void testDelimiterIsWhitespace() throws IOException { public void testDelimiterIsWhitespace() throws IOException {
String code = "one\ttwo\t\tfour \t five\t six"; String code = "one\ttwo\t\tfour \t five\t six";
CSVLexer parser = getLexer(code, CSVFormat.TDF); CSVLexer parser = getLexer(code, CSVFormat.TDF);

View File

@ -26,7 +26,9 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import junit.framework.TestCase; import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
/** /**
* CSVParserTest * CSVParserTest
@ -37,7 +39,7 @@ import junit.framework.TestCase;
* fixing a potential bug (its likely that the parser itself fails if the lexer * fixing a potential bug (its likely that the parser itself fails if the lexer
* has problems...). * has problems...).
*/ */
public class CSVParserTest extends TestCase { public class CSVParserTest {
String code = "a,b,c,d\n" String code = "a,b,c,d\n"
+ " a , b , 1 2 \n" + " a , b , 1 2 \n"
@ -51,25 +53,28 @@ public class CSVParserTest extends TestCase {
{"foo\n,,\n\",,\n\"", "d", "e"} {"foo\n,,\n\",,\n\"", "d", "e"}
}; };
@Test
public void testGetLine() throws IOException { public void testGetLine() throws IOException {
CSVParser parser = new CSVParser(new StringReader(code)); CSVParser parser = new CSVParser(new StringReader(code));
for (String[] re : res) { for (String[] re : res) {
assertTrue(Arrays.equals(re, parser.getRecord())); Assert.assertTrue(Arrays.equals(re, parser.getRecord()));
} }
assertTrue(parser.getRecord() == null); Assert.assertTrue(parser.getRecord() == null);
} }
@Test
public void testGetRecords() throws IOException { public void testGetRecords() throws IOException {
CSVParser parser = new CSVParser(new StringReader(code)); CSVParser parser = new CSVParser(new StringReader(code));
String[][] tmp = parser.getRecords(); String[][] tmp = parser.getRecords();
assertEquals(res.length, tmp.length); Assert.assertEquals(res.length, tmp.length);
assertTrue(tmp.length > 0); Assert.assertTrue(tmp.length > 0);
for (int i = 0; i < res.length; i++) { for (int i = 0; i < res.length; i++) {
assertTrue(Arrays.equals(res[i], tmp[i])); Assert.assertTrue(Arrays.equals(res[i], tmp[i]));
} }
} }
@Test
public void testExcelFormat1() throws IOException { public void testExcelFormat1() throws IOException {
String code = String code =
"value1,value2,value3,value4\r\na,b,c,d\r\n x,,," "value1,value2,value3,value4\r\na,b,c,d\r\n x,,,"
@ -83,13 +88,14 @@ public class CSVParserTest extends TestCase {
}; };
CSVParser parser = new CSVParser(code, CSVFormat.EXCEL); CSVParser parser = new CSVParser(code, CSVFormat.EXCEL);
String[][] tmp = parser.getRecords(); String[][] tmp = parser.getRecords();
assertEquals(res.length, tmp.length); Assert.assertEquals(res.length, tmp.length);
assertTrue(tmp.length > 0); Assert.assertTrue(tmp.length > 0);
for (int i = 0; i < res.length; i++) { for (int i = 0; i < res.length; i++) {
assertTrue(Arrays.equals(res[i], tmp[i])); Assert.assertTrue(Arrays.equals(res[i], tmp[i]));
} }
} }
@Test
public void testExcelFormat2() throws Exception { public void testExcelFormat2() throws Exception {
String code = "foo,baar\r\n\r\nhello,\r\n\r\nworld,\r\n"; String code = "foo,baar\r\n\r\nhello,\r\n\r\nworld,\r\n";
String[][] res = { String[][] res = {
@ -101,13 +107,14 @@ public class CSVParserTest extends TestCase {
}; };
CSVParser parser = new CSVParser(code, CSVFormat.EXCEL); CSVParser parser = new CSVParser(code, CSVFormat.EXCEL);
String[][] tmp = parser.getRecords(); String[][] tmp = parser.getRecords();
assertEquals(res.length, tmp.length); Assert.assertEquals(res.length, tmp.length);
assertTrue(tmp.length > 0); Assert.assertTrue(tmp.length > 0);
for (int i = 0; i < res.length; i++) { for (int i = 0; i < res.length; i++) {
assertTrue(Arrays.equals(res[i], tmp[i])); Assert.assertTrue(Arrays.equals(res[i], tmp[i]));
} }
} }
@Test
public void testEndOfFileBehaviourExcel() throws Exception { public void testEndOfFileBehaviourExcel() throws Exception {
String[] codes = { String[] codes = {
"hello,\r\n\r\nworld,\r\n", "hello,\r\n\r\nworld,\r\n",
@ -128,14 +135,15 @@ public class CSVParserTest extends TestCase {
for (String code : codes) { for (String code : codes) {
CSVParser parser = new CSVParser(code, CSVFormat.EXCEL); CSVParser parser = new CSVParser(code, CSVFormat.EXCEL);
String[][] tmp = parser.getRecords(); String[][] tmp = parser.getRecords();
assertEquals(res.length, tmp.length); Assert.assertEquals(res.length, tmp.length);
assertTrue(tmp.length > 0); Assert.assertTrue(tmp.length > 0);
for (int i = 0; i < res.length; i++) { for (int i = 0; i < res.length; i++) {
assertTrue(Arrays.equals(res[i], tmp[i])); Assert.assertTrue(Arrays.equals(res[i], tmp[i]));
} }
} }
} }
@Test
public void testEndOfFileBehaviorCSV() throws Exception { public void testEndOfFileBehaviorCSV() throws Exception {
String[] codes = { String[] codes = {
"hello,\r\n\r\nworld,\r\n", "hello,\r\n\r\nworld,\r\n",
@ -154,14 +162,15 @@ public class CSVParserTest extends TestCase {
for (String code : codes) { for (String code : codes) {
CSVParser parser = new CSVParser(new StringReader(code)); CSVParser parser = new CSVParser(new StringReader(code));
String[][] tmp = parser.getRecords(); String[][] tmp = parser.getRecords();
assertEquals(res.length, tmp.length); Assert.assertEquals(res.length, tmp.length);
assertTrue(tmp.length > 0); Assert.assertTrue(tmp.length > 0);
for (int i = 0; i < res.length; i++) { for (int i = 0; i < res.length; i++) {
assertTrue(Arrays.equals(res[i], tmp[i])); Assert.assertTrue(Arrays.equals(res[i], tmp[i]));
} }
} }
} }
@Test
public void testEmptyLineBehaviourExcel() throws Exception { public void testEmptyLineBehaviourExcel() throws Exception {
String[] codes = { String[] codes = {
"hello,\r\n\r\n\r\n", "hello,\r\n\r\n\r\n",
@ -177,14 +186,15 @@ public class CSVParserTest extends TestCase {
for (String code : codes) { for (String code : codes) {
CSVParser parser = new CSVParser(code, CSVFormat.EXCEL); CSVParser parser = new CSVParser(code, CSVFormat.EXCEL);
String[][] tmp = parser.getRecords(); String[][] tmp = parser.getRecords();
assertEquals(res.length, tmp.length); Assert.assertEquals(res.length, tmp.length);
assertTrue(tmp.length > 0); Assert.assertTrue(tmp.length > 0);
for (int i = 0; i < res.length; i++) { for (int i = 0; i < res.length; i++) {
assertTrue(Arrays.equals(res[i], tmp[i])); Assert.assertTrue(Arrays.equals(res[i], tmp[i]));
} }
} }
} }
@Test
public void testEmptyLineBehaviourCSV() throws Exception { public void testEmptyLineBehaviourCSV() throws Exception {
String[] codes = { String[] codes = {
"hello,\r\n\r\n\r\n", "hello,\r\n\r\n\r\n",
@ -198,15 +208,17 @@ public class CSVParserTest extends TestCase {
for (String code : codes) { for (String code : codes) {
CSVParser parser = new CSVParser(new StringReader(code)); CSVParser parser = new CSVParser(new StringReader(code));
String[][] tmp = parser.getRecords(); String[][] tmp = parser.getRecords();
assertEquals(res.length, tmp.length); Assert.assertEquals(res.length, tmp.length);
assertTrue(tmp.length > 0); Assert.assertTrue(tmp.length > 0);
for (int i = 0; i < res.length; i++) { for (int i = 0; i < res.length; i++) {
assertTrue(Arrays.equals(res[i], tmp[i])); Assert.assertTrue(Arrays.equals(res[i], tmp[i]));
} }
} }
} }
public void OLDtestBackslashEscaping() throws IOException { @Test
@Ignore
public void testBackslashEscapingOld() throws IOException {
String code = String code =
"one,two,three\n" "one,two,three\n"
+ "on\\\"e,two\n" + "on\\\"e,two\n"
@ -230,13 +242,14 @@ public class CSVParserTest extends TestCase {
}; };
CSVParser parser = new CSVParser(new StringReader(code)); CSVParser parser = new CSVParser(new StringReader(code));
String[][] tmp = parser.getRecords(); String[][] tmp = parser.getRecords();
assertEquals(res.length, tmp.length); Assert.assertEquals(res.length, tmp.length);
assertTrue(tmp.length > 0); Assert.assertTrue(tmp.length > 0);
for (int i = 0; i < res.length; i++) { for (int i = 0; i < res.length; i++) {
assertTrue(Arrays.equals(res[i], tmp[i])); Assert.assertTrue(Arrays.equals(res[i], tmp[i]));
} }
} }
@Test
public void testBackslashEscaping() throws IOException { public void testBackslashEscaping() throws IOException {
// To avoid confusion over the need for escaping chars in java code, // To avoid confusion over the need for escaping chars in java code,
@ -273,12 +286,13 @@ public class CSVParserTest extends TestCase {
CSVParser parser = new CSVParser(code, format); CSVParser parser = new CSVParser(code, format);
String[][] tmp = parser.getRecords(); String[][] tmp = parser.getRecords();
assertTrue(tmp.length > 0); Assert.assertTrue(tmp.length > 0);
for (int i = 0; i < res.length; i++) { for (int i = 0; i < res.length; i++) {
assertTrue(Arrays.equals(res[i], tmp[i])); Assert.assertTrue(Arrays.equals(res[i], tmp[i]));
} }
} }
@Test
public void testBackslashEscaping2() throws IOException { public void testBackslashEscaping2() throws IOException {
// To avoid confusion over the need for escaping chars in java code, // To avoid confusion over the need for escaping chars in java code,
@ -301,15 +315,14 @@ public class CSVParserTest extends TestCase {
CSVParser parser = new CSVParser(code, format); CSVParser parser = new CSVParser(code, format);
String[][] tmp = parser.getRecords(); String[][] tmp = parser.getRecords();
assertTrue(tmp.length > 0); Assert.assertTrue(tmp.length > 0);
if (!CSVPrinterTest.equals(res, tmp)) { if (!CSVPrinterTest.equals(res, tmp)) {
assertTrue(false); Assert.assertTrue(false);
} }
} }
@Test
public void testDefaultFormat() throws IOException { public void testDefaultFormat() throws IOException {
String code = "" String code = ""
@ -324,14 +337,14 @@ public class CSVParserTest extends TestCase {
}; };
CSVFormat format = CSVFormat.DEFAULT; CSVFormat format = CSVFormat.DEFAULT;
assertEquals(CSVFormat.DISABLED, format.getCommentStart()); Assert.assertEquals(CSVFormat.DISABLED, format.getCommentStart());
CSVParser parser = new CSVParser(code, format); CSVParser parser = new CSVParser(code, format);
String[][] tmp = parser.getRecords(); String[][] tmp = parser.getRecords();
assertTrue(tmp.length > 0); Assert.assertTrue(tmp.length > 0);
if (!CSVPrinterTest.equals(res, tmp)) { if (!CSVPrinterTest.equals(res, tmp)) {
assertTrue(false); Assert.assertTrue(false);
} }
String[][] res_comments = { String[][] res_comments = {
@ -345,63 +358,69 @@ public class CSVParserTest extends TestCase {
tmp = parser.getRecords(); tmp = parser.getRecords();
if (!CSVPrinterTest.equals(res_comments, tmp)) { if (!CSVPrinterTest.equals(res_comments, tmp)) {
assertTrue(false); Assert.assertTrue(false);
} }
} }
@Test
public void testUnicodeEscape() throws Exception { public void testUnicodeEscape() throws Exception {
String code = "abc,\\u0070\\u0075\\u0062\\u006C\\u0069\\u0063"; String code = "abc,\\u0070\\u0075\\u0062\\u006C\\u0069\\u0063";
CSVParser parser = new CSVParser(code, CSVFormat.DEFAULT.withUnicodeEscapesInterpreted(true)); CSVParser parser = new CSVParser(code, CSVFormat.DEFAULT.withUnicodeEscapesInterpreted(true));
final Iterator<String[]> iterator = parser.iterator(); final Iterator<String[]> iterator = parser.iterator();
String[] data = iterator.next(); String[] data = iterator.next();
assertEquals(2, data.length); Assert.assertEquals(2, data.length);
assertEquals("abc", data[0]); Assert.assertEquals("abc", data[0]);
assertEquals("public", data[1]); Assert.assertEquals("public", data[1]);
assertFalse("Should not have any more records", iterator.hasNext()); Assert.assertFalse("Should not have any more records", iterator.hasNext());
} }
@Test
public void testUnicodeEscapeMySQL() throws Exception { public void testUnicodeEscapeMySQL() throws Exception {
String code = "abc\t\\u0070\\u0075\\u0062\\u006C\\u0069\\u0063"; String code = "abc\t\\u0070\\u0075\\u0062\\u006C\\u0069\\u0063";
CSVParser parser = new CSVParser(code, CSVFormat.MYSQL.withUnicodeEscapesInterpreted(true)); CSVParser parser = new CSVParser(code, CSVFormat.MYSQL.withUnicodeEscapesInterpreted(true));
final Iterator<String[]> iterator = parser.iterator(); final Iterator<String[]> iterator = parser.iterator();
String[] data = iterator.next(); String[] data = iterator.next();
assertEquals(2, data.length); Assert.assertEquals(2, data.length);
assertEquals("abc", data[0]); Assert.assertEquals("abc", data[0]);
assertEquals("public", data[1]); Assert.assertEquals("public", data[1]);
assertFalse("Should not have any more records", iterator.hasNext()); Assert.assertFalse("Should not have any more records", iterator.hasNext());
} }
@Test
public void testCarriageReturnLineFeedEndings() throws IOException { public void testCarriageReturnLineFeedEndings() throws IOException {
String code = "foo\r\nbaar,\r\nhello,world\r\n,kanu"; String code = "foo\r\nbaar,\r\nhello,world\r\n,kanu";
CSVParser parser = new CSVParser(new StringReader(code)); CSVParser parser = new CSVParser(new StringReader(code));
String[][] data = parser.getRecords(); String[][] data = parser.getRecords();
assertEquals(4, data.length); Assert.assertEquals(4, data.length);
} }
@Test
public void testCarriageReturnEndings() throws IOException { public void testCarriageReturnEndings() throws IOException {
String code = "foo\rbaar,\rhello,world\r,kanu"; String code = "foo\rbaar,\rhello,world\r,kanu";
CSVParser parser = new CSVParser(new StringReader(code)); CSVParser parser = new CSVParser(new StringReader(code));
String[][] data = parser.getRecords(); String[][] data = parser.getRecords();
assertEquals(4, data.length); Assert.assertEquals(4, data.length);
} }
@Test
public void testLineFeedEndings() throws IOException { public void testLineFeedEndings() throws IOException {
String code = "foo\nbaar,\nhello,world\n,kanu"; String code = "foo\nbaar,\nhello,world\n,kanu";
CSVParser parser = new CSVParser(new StringReader(code)); CSVParser parser = new CSVParser(new StringReader(code));
String[][] data = parser.getRecords(); String[][] data = parser.getRecords();
assertEquals(4, data.length); Assert.assertEquals(4, data.length);
} }
@Test
public void testIgnoreEmptyLines() throws IOException { public void testIgnoreEmptyLines() throws IOException {
String code = "\nfoo,baar\n\r\n,\n\n,world\r\n\n"; String code = "\nfoo,baar\n\r\n,\n\n,world\r\n\n";
//String code = "world\r\n\n"; //String code = "world\r\n\n";
//String code = "foo;baar\r\n\r\nhello;\r\n\r\nworld;\r\n"; //String code = "foo;baar\r\n\r\nhello;\r\n\r\nworld;\r\n";
CSVParser parser = new CSVParser(new StringReader(code)); CSVParser parser = new CSVParser(new StringReader(code));
String[][] data = parser.getRecords(); String[][] data = parser.getRecords();
assertEquals(3, data.length); Assert.assertEquals(3, data.length);
} }
@Test
public void testForEach() { public void testForEach() {
List<String[]> records = new ArrayList<String[]>(); List<String[]> records = new ArrayList<String[]>();
@ -411,34 +430,35 @@ public class CSVParserTest extends TestCase {
records.add(record); records.add(record);
} }
assertEquals(3, records.size()); Assert.assertEquals(3, records.size());
assertTrue(Arrays.equals(new String[]{"a", "b", "c"}, records.get(0))); Assert.assertTrue(Arrays.equals(new String[]{"a", "b", "c"}, records.get(0)));
assertTrue(Arrays.equals(new String[]{"1", "2", "3"}, records.get(1))); Assert.assertTrue(Arrays.equals(new String[]{"1", "2", "3"}, records.get(1)));
assertTrue(Arrays.equals(new String[]{"x", "y", "z"}, records.get(2))); Assert.assertTrue(Arrays.equals(new String[]{"x", "y", "z"}, records.get(2)));
} }
@Test
public void testIterator() { public void testIterator() {
Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z"); Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
Iterator<String[]> iterator = CSVFormat.DEFAULT.parse(in).iterator(); Iterator<String[]> iterator = CSVFormat.DEFAULT.parse(in).iterator();
assertTrue(iterator.hasNext()); Assert.assertTrue(iterator.hasNext());
try { try {
iterator.remove(); iterator.remove();
fail("expected UnsupportedOperationException"); Assert.fail("expected UnsupportedOperationException");
} catch (UnsupportedOperationException expected) { } catch (UnsupportedOperationException expected) {
} }
assertTrue(Arrays.equals(new String[]{"a", "b", "c"}, iterator.next())); Assert.assertTrue(Arrays.equals(new String[]{"a", "b", "c"}, iterator.next()));
assertTrue(Arrays.equals(new String[]{"1", "2", "3"}, iterator.next())); Assert.assertTrue(Arrays.equals(new String[]{"1", "2", "3"}, iterator.next()));
assertTrue(iterator.hasNext()); Assert.assertTrue(iterator.hasNext());
assertTrue(iterator.hasNext()); Assert.assertTrue(iterator.hasNext());
assertTrue(iterator.hasNext()); Assert.assertTrue(iterator.hasNext());
assertTrue(Arrays.equals(new String[]{"x", "y", "z"}, iterator.next())); Assert.assertTrue(Arrays.equals(new String[]{"x", "y", "z"}, iterator.next()));
assertFalse(iterator.hasNext()); Assert.assertFalse(iterator.hasNext());
try { try {
iterator.next(); iterator.next();
fail("NoSuchElementException expected"); Assert.fail("NoSuchElementException expected");
} catch (NoSuchElementException e) { } catch (NoSuchElementException e) {
// expected // expected
} }

View File

@ -20,109 +20,124 @@ import java.io.IOException;
import java.io.StringWriter; import java.io.StringWriter;
import java.util.Random; import java.util.Random;
import junit.framework.TestCase; import org.junit.Assert;
import org.junit.Test;
/** /**
* CSVPrinterTest * CSVPrinterTest
*/ */
public class CSVPrinterTest extends TestCase { public class CSVPrinterTest {
String lineSeparator = CSVFormat.DEFAULT.getLineSeparator(); String lineSeparator = CSVFormat.DEFAULT.getLineSeparator();
@Test
public void testPrinter1() throws IOException { public void testPrinter1() throws IOException {
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT); CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
printer.println("a", "b"); printer.println("a", "b");
assertEquals("a,b" + lineSeparator, sw.toString()); Assert.assertEquals("a,b" + lineSeparator, sw.toString());
} }
@Test
public void testPrinter2() throws IOException { public void testPrinter2() throws IOException {
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT); CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
printer.println("a,b", "b"); printer.println("a,b", "b");
assertEquals("\"a,b\",b" + lineSeparator, sw.toString()); Assert.assertEquals("\"a,b\",b" + lineSeparator, sw.toString());
} }
@Test
public void testPrinter3() throws IOException { public void testPrinter3() throws IOException {
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT); CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
printer.println("a, b", "b "); printer.println("a, b", "b ");
assertEquals("\"a, b\",\"b \"" + lineSeparator, sw.toString()); Assert.assertEquals("\"a, b\",\"b \"" + lineSeparator, sw.toString());
} }
@Test
public void testPrinter4() throws IOException { public void testPrinter4() throws IOException {
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT); CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
printer.println("a", "b\"c"); printer.println("a", "b\"c");
assertEquals("a,\"b\"\"c\"" + lineSeparator, sw.toString()); Assert.assertEquals("a,\"b\"\"c\"" + lineSeparator, sw.toString());
} }
@Test
public void testPrinter5() throws IOException { public void testPrinter5() throws IOException {
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT); CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
printer.println("a", "b\nc"); printer.println("a", "b\nc");
assertEquals("a,\"b\nc\"" + lineSeparator, sw.toString()); Assert.assertEquals("a,\"b\nc\"" + lineSeparator, sw.toString());
} }
@Test
public void testPrinter6() throws IOException { public void testPrinter6() throws IOException {
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT); CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
printer.println("a", "b\r\nc"); printer.println("a", "b\r\nc");
assertEquals("a,\"b\r\nc\"" + lineSeparator, sw.toString()); Assert.assertEquals("a,\"b\r\nc\"" + lineSeparator, sw.toString());
} }
@Test
public void testPrinter7() throws IOException { public void testPrinter7() throws IOException {
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT); CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
printer.println("a", "b\\c"); printer.println("a", "b\\c");
assertEquals("a,b\\c" + lineSeparator, sw.toString()); Assert.assertEquals("a,b\\c" + lineSeparator, sw.toString());
} }
@Test
public void testExcelPrinter1() throws IOException { public void testExcelPrinter1() throws IOException {
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL); CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
printer.println("a", "b"); printer.println("a", "b");
assertEquals("a,b" + lineSeparator, sw.toString()); Assert.assertEquals("a,b" + lineSeparator, sw.toString());
} }
@Test
public void testExcelPrinter2() throws IOException { public void testExcelPrinter2() throws IOException {
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL); CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
printer.println("a,b", "b"); printer.println("a,b", "b");
assertEquals("\"a,b\",b" + lineSeparator, sw.toString()); Assert.assertEquals("\"a,b\",b" + lineSeparator, sw.toString());
} }
@Test
public void testPrintNullValues() throws IOException { public void testPrintNullValues() throws IOException {
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT); CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
printer.println("a", null, "b"); printer.println("a", null, "b");
assertEquals("a,,b" + lineSeparator, sw.toString()); Assert.assertEquals("a,,b" + lineSeparator, sw.toString());
} }
@Test
public void testDisabledComment() throws IOException { public void testDisabledComment() throws IOException {
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT); CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
printer.printComment("This is a comment"); printer.printComment("This is a comment");
assertEquals("", sw.toString()); Assert.assertEquals("", sw.toString());
} }
@Test
public void testSingleLineComment() throws IOException { public void testSingleLineComment() throws IOException {
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withCommentStart('#')); CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withCommentStart('#'));
printer.printComment("This is a comment"); printer.printComment("This is a comment");
assertEquals("# This is a comment" + lineSeparator, sw.toString()); Assert.assertEquals("# This is a comment" + lineSeparator, sw.toString());
} }
@Test
public void testMultiLineComment() throws IOException { public void testMultiLineComment() throws IOException {
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withCommentStart('#')); CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withCommentStart('#'));
printer.printComment("This is a comment\non multiple lines"); printer.printComment("This is a comment\non multiple lines");
assertEquals("# This is a comment" + lineSeparator + "# on multiple lines" + lineSeparator, sw.toString()); Assert.assertEquals("# This is a comment" + lineSeparator + "# on multiple lines" + lineSeparator, sw.toString());
} }
@Test
public void testRandom() throws Exception { public void testRandom() throws Exception {
int iter = 10000; int iter = 10000;
doRandom(CSVFormat.DEFAULT, iter); doRandom(CSVFormat.DEFAULT, iter);
@ -168,7 +183,7 @@ public class CSVPrinterTest extends TestCase {
if (!equals(lines, parseResult)) { if (!equals(lines, parseResult)) {
System.out.println("Printer output :" + printable(result)); System.out.println("Printer output :" + printable(result));
assertTrue(false); Assert.assertTrue(false);
} }
} }

View File

@ -20,70 +20,73 @@ package org.apache.commons.csv;
import java.io.StringReader; import java.io.StringReader;
import java.util.Arrays; import java.util.Arrays;
import junit.framework.TestCase; import org.junit.Assert;
import org.junit.Test;
public class ExtendedBufferedReaderTest extends TestCase { public class ExtendedBufferedReaderTest {
@Test
public void testEmptyInput() throws Exception { public void testEmptyInput() throws Exception {
ExtendedBufferedReader br = getBufferedReader(""); ExtendedBufferedReader br = getBufferedReader("");
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read()); Assert.assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read());
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead()); Assert.assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead());
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.readAgain()); Assert.assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.readAgain());
assertNull(br.readLine()); Assert.assertNull(br.readLine());
assertEquals(0, br.read(new char[10], 0, 0)); Assert.assertEquals(0, br.read(new char[10], 0, 0));
} }
@Test
public void testReadLookahead1() throws Exception { public void testReadLookahead1() throws Exception {
ExtendedBufferedReader br = getBufferedReader("1\n2\r3\n"); ExtendedBufferedReader br = getBufferedReader("1\n2\r3\n");
assertEquals('1', br.lookAhead()); Assert.assertEquals('1', br.lookAhead());
assertEquals(ExtendedBufferedReader.UNDEFINED, br.readAgain()); Assert.assertEquals(ExtendedBufferedReader.UNDEFINED, br.readAgain());
assertEquals('1', br.read()); Assert.assertEquals('1', br.read());
assertEquals('1', br.readAgain()); Assert.assertEquals('1', br.readAgain());
assertEquals(0, br.getLineNumber()); Assert.assertEquals(0, br.getLineNumber());
assertEquals('\n', br.lookAhead()); Assert.assertEquals('\n', br.lookAhead());
assertEquals(0, br.getLineNumber()); Assert.assertEquals(0, br.getLineNumber());
assertEquals('1', br.readAgain()); Assert.assertEquals('1', br.readAgain());
assertEquals('\n', br.read()); Assert.assertEquals('\n', br.read());
assertEquals(1, br.getLineNumber()); Assert.assertEquals(1, br.getLineNumber());
assertEquals('\n', br.readAgain()); Assert.assertEquals('\n', br.readAgain());
assertEquals(1, br.getLineNumber()); Assert.assertEquals(1, br.getLineNumber());
assertEquals('2', br.lookAhead()); Assert.assertEquals('2', br.lookAhead());
assertEquals(1, br.getLineNumber()); Assert.assertEquals(1, br.getLineNumber());
assertEquals('\n', br.readAgain()); Assert.assertEquals('\n', br.readAgain());
assertEquals(1, br.getLineNumber()); Assert.assertEquals(1, br.getLineNumber());
assertEquals('2', br.read()); Assert.assertEquals('2', br.read());
assertEquals('2', br.readAgain()); Assert.assertEquals('2', br.readAgain());
assertEquals('\r', br.lookAhead()); Assert.assertEquals('\r', br.lookAhead());
assertEquals('2', br.readAgain()); Assert.assertEquals('2', br.readAgain());
assertEquals('\r', br.read()); Assert.assertEquals('\r', br.read());
assertEquals('\r', br.readAgain()); Assert.assertEquals('\r', br.readAgain());
assertEquals('3', br.lookAhead()); Assert.assertEquals('3', br.lookAhead());
assertEquals('\r', br.readAgain()); Assert.assertEquals('\r', br.readAgain());
assertEquals('3', br.read()); Assert.assertEquals('3', br.read());
assertEquals('3', br.readAgain()); Assert.assertEquals('3', br.readAgain());
assertEquals('\n', br.lookAhead()); Assert.assertEquals('\n', br.lookAhead());
assertEquals(1, br.getLineNumber()); Assert.assertEquals(1, br.getLineNumber());
assertEquals('3', br.readAgain()); Assert.assertEquals('3', br.readAgain());
assertEquals('\n', br.read()); Assert.assertEquals('\n', br.read());
assertEquals(2, br.getLineNumber()); Assert.assertEquals(2, br.getLineNumber());
assertEquals('\n', br.readAgain()); Assert.assertEquals('\n', br.readAgain());
assertEquals(2, br.getLineNumber()); Assert.assertEquals(2, br.getLineNumber());
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead()); Assert.assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead());
assertEquals('\n', br.readAgain()); Assert.assertEquals('\n', br.readAgain());
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read()); Assert.assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read());
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.readAgain()); Assert.assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.readAgain());
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read()); Assert.assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read());
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead()); Assert.assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead());
} }
@Test
public void testReadLookahead2() throws Exception { public void testReadLookahead2() throws Exception {
char[] ref = new char[5]; char[] ref = new char[5];
char[] res = new char[5]; char[] res = new char[5];
@ -92,57 +95,58 @@ public class ExtendedBufferedReaderTest extends TestCase {
ref[0] = 'a'; ref[0] = 'a';
ref[1] = 'b'; ref[1] = 'b';
ref[2] = 'c'; ref[2] = 'c';
assertEquals(3, br.read(res, 0, 3)); Assert.assertEquals(3, br.read(res, 0, 3));
assertTrue(Arrays.equals(res, ref)); Assert.assertTrue(Arrays.equals(res, ref));
assertEquals('c', br.readAgain()); Assert.assertEquals('c', br.readAgain());
assertEquals('d', br.lookAhead()); Assert.assertEquals('d', br.lookAhead());
ref[4] = 'd'; ref[4] = 'd';
assertEquals(1, br.read(res, 4, 1)); Assert.assertEquals(1, br.read(res, 4, 1));
assertTrue(Arrays.equals(res, ref)); Assert.assertTrue(Arrays.equals(res, ref));
assertEquals('d', br.readAgain()); Assert.assertEquals('d', br.readAgain());
} }
@Test
public void testReadLine() throws Exception { public void testReadLine() throws Exception {
ExtendedBufferedReader br = getBufferedReader(""); ExtendedBufferedReader br = getBufferedReader("");
assertTrue(br.readLine() == null); Assert.assertTrue(br.readLine() == null);
br = getBufferedReader("\n"); br = getBufferedReader("\n");
assertTrue(br.readLine().equals("")); Assert.assertTrue(br.readLine().equals(""));
assertTrue(br.readLine() == null); Assert.assertTrue(br.readLine() == null);
br = getBufferedReader("foo\n\nhello"); br = getBufferedReader("foo\n\nhello");
assertEquals(0, br.getLineNumber()); Assert.assertEquals(0, br.getLineNumber());
assertTrue(br.readLine().equals("foo")); Assert.assertTrue(br.readLine().equals("foo"));
assertEquals(1, br.getLineNumber()); Assert.assertEquals(1, br.getLineNumber());
assertTrue(br.readLine().equals("")); Assert.assertTrue(br.readLine().equals(""));
assertEquals(2, br.getLineNumber()); Assert.assertEquals(2, br.getLineNumber());
assertTrue(br.readLine().equals("hello")); Assert.assertTrue(br.readLine().equals("hello"));
assertEquals(3, br.getLineNumber()); Assert.assertEquals(3, br.getLineNumber());
assertTrue(br.readLine() == null); Assert.assertTrue(br.readLine() == null);
assertEquals(3, br.getLineNumber()); Assert.assertEquals(3, br.getLineNumber());
br = getBufferedReader("foo\n\nhello"); br = getBufferedReader("foo\n\nhello");
assertEquals('f', br.read()); Assert.assertEquals('f', br.read());
assertEquals('o', br.lookAhead()); Assert.assertEquals('o', br.lookAhead());
assertTrue(br.readLine().equals("oo")); Assert.assertTrue(br.readLine().equals("oo"));
assertEquals(1, br.getLineNumber()); Assert.assertEquals(1, br.getLineNumber());
assertEquals('\n', br.lookAhead()); Assert.assertEquals('\n', br.lookAhead());
assertTrue(br.readLine().equals("")); Assert.assertTrue(br.readLine().equals(""));
assertEquals(2, br.getLineNumber()); Assert.assertEquals(2, br.getLineNumber());
assertEquals('h', br.lookAhead()); Assert.assertEquals('h', br.lookAhead());
assertTrue(br.readLine().equals("hello")); Assert.assertTrue(br.readLine().equals("hello"));
assertTrue(br.readLine() == null); Assert.assertTrue(br.readLine() == null);
assertEquals(3, br.getLineNumber()); Assert.assertEquals(3, br.getLineNumber());
br = getBufferedReader("foo\rbaar\r\nfoo"); br = getBufferedReader("foo\rbaar\r\nfoo");
assertTrue(br.readLine().equals("foo")); Assert.assertTrue(br.readLine().equals("foo"));
assertEquals('b', br.lookAhead()); Assert.assertEquals('b', br.lookAhead());
assertTrue(br.readLine().equals("baar")); Assert.assertTrue(br.readLine().equals("baar"));
assertEquals('f', br.lookAhead()); Assert.assertEquals('f', br.lookAhead());
assertTrue(br.readLine().equals("foo")); Assert.assertTrue(br.readLine().equals("foo"));
assertTrue(br.readLine() == null); Assert.assertTrue(br.readLine() == null);
} }
private ExtendedBufferedReader getBufferedReader(String s) { private ExtendedBufferedReader getBufferedReader(String s) {