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>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>

View File

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

View File

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

View File

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

View File

@ -20,109 +20,124 @@ import java.io.IOException;
import java.io.StringWriter;
import java.util.Random;
import junit.framework.TestCase;
import org.junit.Assert;
import org.junit.Test;
/**
* CSVPrinterTest
*/
public class CSVPrinterTest extends TestCase {
public class CSVPrinterTest {
String lineSeparator = CSVFormat.DEFAULT.getLineSeparator();
@Test
public void testPrinter1() throws IOException {
StringWriter sw = new StringWriter();
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
printer.println("a", "b");
assertEquals("a,b" + lineSeparator, sw.toString());
Assert.assertEquals("a,b" + lineSeparator, sw.toString());
}
@Test
public void testPrinter2() throws IOException {
StringWriter sw = new StringWriter();
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
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 {
StringWriter sw = new StringWriter();
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
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 {
StringWriter sw = new StringWriter();
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
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 {
StringWriter sw = new StringWriter();
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
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 {
StringWriter sw = new StringWriter();
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
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 {
StringWriter sw = new StringWriter();
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
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 {
StringWriter sw = new StringWriter();
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
printer.println("a", "b");
assertEquals("a,b" + lineSeparator, sw.toString());
Assert.assertEquals("a,b" + lineSeparator, sw.toString());
}
@Test
public void testExcelPrinter2() throws IOException {
StringWriter sw = new StringWriter();
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
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 {
StringWriter sw = new StringWriter();
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
printer.println("a", null, "b");
assertEquals("a,,b" + lineSeparator, sw.toString());
Assert.assertEquals("a,,b" + lineSeparator, sw.toString());
}
@Test
public void testDisabledComment() throws IOException {
StringWriter sw = new StringWriter();
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
printer.printComment("This is a comment");
assertEquals("", sw.toString());
Assert.assertEquals("", sw.toString());
}
@Test
public void testSingleLineComment() throws IOException {
StringWriter sw = new StringWriter();
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withCommentStart('#'));
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 {
StringWriter sw = new StringWriter();
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withCommentStart('#'));
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 {
int iter = 10000;
doRandom(CSVFormat.DEFAULT, iter);
@ -168,7 +183,7 @@ public class CSVPrinterTest extends TestCase {
if (!equals(lines, parseResult)) {
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.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 {
ExtendedBufferedReader br = getBufferedReader("");
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read());
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead());
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.readAgain());
assertNull(br.readLine());
assertEquals(0, br.read(new char[10], 0, 0));
Assert.assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read());
Assert.assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead());
Assert.assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.readAgain());
Assert.assertNull(br.readLine());
Assert.assertEquals(0, br.read(new char[10], 0, 0));
}
@Test
public void testReadLookahead1() throws Exception {
ExtendedBufferedReader br = getBufferedReader("1\n2\r3\n");
assertEquals('1', br.lookAhead());
assertEquals(ExtendedBufferedReader.UNDEFINED, br.readAgain());
assertEquals('1', br.read());
assertEquals('1', br.readAgain());
Assert.assertEquals('1', br.lookAhead());
Assert.assertEquals(ExtendedBufferedReader.UNDEFINED, br.readAgain());
Assert.assertEquals('1', br.read());
Assert.assertEquals('1', br.readAgain());
assertEquals(0, br.getLineNumber());
assertEquals('\n', br.lookAhead());
assertEquals(0, br.getLineNumber());
assertEquals('1', br.readAgain());
assertEquals('\n', br.read());
assertEquals(1, br.getLineNumber());
assertEquals('\n', br.readAgain());
assertEquals(1, br.getLineNumber());
Assert.assertEquals(0, br.getLineNumber());
Assert.assertEquals('\n', br.lookAhead());
Assert.assertEquals(0, br.getLineNumber());
Assert.assertEquals('1', br.readAgain());
Assert.assertEquals('\n', br.read());
Assert.assertEquals(1, br.getLineNumber());
Assert.assertEquals('\n', br.readAgain());
Assert.assertEquals(1, br.getLineNumber());
assertEquals('2', br.lookAhead());
assertEquals(1, br.getLineNumber());
assertEquals('\n', br.readAgain());
assertEquals(1, br.getLineNumber());
assertEquals('2', br.read());
assertEquals('2', br.readAgain());
Assert.assertEquals('2', br.lookAhead());
Assert.assertEquals(1, br.getLineNumber());
Assert.assertEquals('\n', br.readAgain());
Assert.assertEquals(1, br.getLineNumber());
Assert.assertEquals('2', br.read());
Assert.assertEquals('2', br.readAgain());
assertEquals('\r', br.lookAhead());
assertEquals('2', br.readAgain());
assertEquals('\r', br.read());
assertEquals('\r', br.readAgain());
Assert.assertEquals('\r', br.lookAhead());
Assert.assertEquals('2', br.readAgain());
Assert.assertEquals('\r', br.read());
Assert.assertEquals('\r', br.readAgain());
assertEquals('3', br.lookAhead());
assertEquals('\r', br.readAgain());
assertEquals('3', br.read());
assertEquals('3', br.readAgain());
Assert.assertEquals('3', br.lookAhead());
Assert.assertEquals('\r', br.readAgain());
Assert.assertEquals('3', br.read());
Assert.assertEquals('3', br.readAgain());
assertEquals('\n', br.lookAhead());
assertEquals(1, br.getLineNumber());
assertEquals('3', br.readAgain());
assertEquals('\n', br.read());
assertEquals(2, br.getLineNumber());
assertEquals('\n', br.readAgain());
assertEquals(2, br.getLineNumber());
Assert.assertEquals('\n', br.lookAhead());
Assert.assertEquals(1, br.getLineNumber());
Assert.assertEquals('3', br.readAgain());
Assert.assertEquals('\n', br.read());
Assert.assertEquals(2, br.getLineNumber());
Assert.assertEquals('\n', br.readAgain());
Assert.assertEquals(2, br.getLineNumber());
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead());
assertEquals('\n', br.readAgain());
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read());
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.readAgain());
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read());
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead());
Assert.assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead());
Assert.assertEquals('\n', br.readAgain());
Assert.assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read());
Assert.assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.readAgain());
Assert.assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read());
Assert.assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead());
}
@Test
public void testReadLookahead2() throws Exception {
char[] ref = new char[5];
char[] res = new char[5];
@ -92,57 +95,58 @@ public class ExtendedBufferedReaderTest extends TestCase {
ref[0] = 'a';
ref[1] = 'b';
ref[2] = 'c';
assertEquals(3, br.read(res, 0, 3));
assertTrue(Arrays.equals(res, ref));
assertEquals('c', br.readAgain());
Assert.assertEquals(3, br.read(res, 0, 3));
Assert.assertTrue(Arrays.equals(res, ref));
Assert.assertEquals('c', br.readAgain());
assertEquals('d', br.lookAhead());
Assert.assertEquals('d', br.lookAhead());
ref[4] = 'd';
assertEquals(1, br.read(res, 4, 1));
assertTrue(Arrays.equals(res, ref));
assertEquals('d', br.readAgain());
Assert.assertEquals(1, br.read(res, 4, 1));
Assert.assertTrue(Arrays.equals(res, ref));
Assert.assertEquals('d', br.readAgain());
}
@Test
public void testReadLine() throws Exception {
ExtendedBufferedReader br = getBufferedReader("");
assertTrue(br.readLine() == null);
Assert.assertTrue(br.readLine() == null);
br = getBufferedReader("\n");
assertTrue(br.readLine().equals(""));
assertTrue(br.readLine() == null);
Assert.assertTrue(br.readLine().equals(""));
Assert.assertTrue(br.readLine() == null);
br = getBufferedReader("foo\n\nhello");
assertEquals(0, br.getLineNumber());
assertTrue(br.readLine().equals("foo"));
assertEquals(1, br.getLineNumber());
assertTrue(br.readLine().equals(""));
assertEquals(2, br.getLineNumber());
assertTrue(br.readLine().equals("hello"));
assertEquals(3, br.getLineNumber());
assertTrue(br.readLine() == null);
assertEquals(3, br.getLineNumber());
Assert.assertEquals(0, br.getLineNumber());
Assert.assertTrue(br.readLine().equals("foo"));
Assert.assertEquals(1, br.getLineNumber());
Assert.assertTrue(br.readLine().equals(""));
Assert.assertEquals(2, br.getLineNumber());
Assert.assertTrue(br.readLine().equals("hello"));
Assert.assertEquals(3, br.getLineNumber());
Assert.assertTrue(br.readLine() == null);
Assert.assertEquals(3, br.getLineNumber());
br = getBufferedReader("foo\n\nhello");
assertEquals('f', br.read());
assertEquals('o', br.lookAhead());
assertTrue(br.readLine().equals("oo"));
assertEquals(1, br.getLineNumber());
assertEquals('\n', br.lookAhead());
assertTrue(br.readLine().equals(""));
assertEquals(2, br.getLineNumber());
assertEquals('h', br.lookAhead());
assertTrue(br.readLine().equals("hello"));
assertTrue(br.readLine() == null);
assertEquals(3, br.getLineNumber());
Assert.assertEquals('f', br.read());
Assert.assertEquals('o', br.lookAhead());
Assert.assertTrue(br.readLine().equals("oo"));
Assert.assertEquals(1, br.getLineNumber());
Assert.assertEquals('\n', br.lookAhead());
Assert.assertTrue(br.readLine().equals(""));
Assert.assertEquals(2, br.getLineNumber());
Assert.assertEquals('h', br.lookAhead());
Assert.assertTrue(br.readLine().equals("hello"));
Assert.assertTrue(br.readLine() == null);
Assert.assertEquals(3, br.getLineNumber());
br = getBufferedReader("foo\rbaar\r\nfoo");
assertTrue(br.readLine().equals("foo"));
assertEquals('b', br.lookAhead());
assertTrue(br.readLine().equals("baar"));
assertEquals('f', br.lookAhead());
assertTrue(br.readLine().equals("foo"));
assertTrue(br.readLine() == null);
Assert.assertTrue(br.readLine().equals("foo"));
Assert.assertEquals('b', br.lookAhead());
Assert.assertTrue(br.readLine().equals("baar"));
Assert.assertEquals('f', br.lookAhead());
Assert.assertTrue(br.readLine().equals("foo"));
Assert.assertTrue(br.readLine() == null);
}
private ExtendedBufferedReader getBufferedReader(String s) {