Static imports in the unit tests

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1301084 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Emmanuel Bourg 2012-03-15 16:54:30 +00:00
parent 186e9f9804
commit 3362f9c9d8
5 changed files with 210 additions and 210 deletions

View File

@ -22,9 +22,10 @@ import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.*;
public class CSVFormatTest { public class CSVFormatTest {
@Test @Test
@ -41,43 +42,43 @@ public class CSVFormatTest {
format.withEmptyLinesIgnored(false); format.withEmptyLinesIgnored(false);
format.withUnicodeEscapesInterpreted(false); format.withUnicodeEscapesInterpreted(false);
Assert.assertEquals('!', format.getDelimiter()); assertEquals('!', format.getDelimiter());
Assert.assertEquals('!', format.getEncapsulator()); assertEquals('!', format.getEncapsulator());
Assert.assertEquals('!', format.getCommentStart()); assertEquals('!', format.getCommentStart());
Assert.assertEquals('!', format.getEscape()); assertEquals('!', format.getEscape());
Assert.assertEquals("\r\n", format.getLineSeparator()); assertEquals("\r\n", format.getLineSeparator());
Assert.assertEquals(true, format.isLeadingSpacesIgnored()); assertEquals(true, format.isLeadingSpacesIgnored());
Assert.assertEquals(true, format.isTrailingSpacesIgnored()); assertEquals(true, format.isTrailingSpacesIgnored());
Assert.assertEquals(true, format.isEmptyLinesIgnored()); assertEquals(true, format.isEmptyLinesIgnored());
Assert.assertEquals(true, format.isUnicodeEscapesInterpreted()); assertEquals(true, format.isUnicodeEscapesInterpreted());
} }
@Test @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");
Assert.assertEquals('?', format.withDelimiter('?').getDelimiter()); assertEquals('?', format.withDelimiter('?').getDelimiter());
Assert.assertEquals('?', format.withEncapsulator('?').getEncapsulator()); assertEquals('?', format.withEncapsulator('?').getEncapsulator());
Assert.assertEquals('?', format.withCommentStart('?').getCommentStart()); assertEquals('?', format.withCommentStart('?').getCommentStart());
Assert.assertEquals("?", format.withLineSeparator("?").getLineSeparator()); assertEquals("?", format.withLineSeparator("?").getLineSeparator());
Assert.assertEquals('?', format.withEscape('?').getEscape()); assertEquals('?', format.withEscape('?').getEscape());
Assert.assertEquals(false, format.withLeadingSpacesIgnored(false).isLeadingSpacesIgnored()); assertEquals(false, format.withLeadingSpacesIgnored(false).isLeadingSpacesIgnored());
Assert.assertEquals(false, format.withTrailingSpacesIgnored(false).isTrailingSpacesIgnored()); assertEquals(false, format.withTrailingSpacesIgnored(false).isTrailingSpacesIgnored());
Assert.assertEquals(false, format.withSurroundingSpacesIgnored(false).isLeadingSpacesIgnored()); assertEquals(false, format.withSurroundingSpacesIgnored(false).isLeadingSpacesIgnored());
Assert.assertEquals(false, format.withSurroundingSpacesIgnored(false).isTrailingSpacesIgnored()); assertEquals(false, format.withSurroundingSpacesIgnored(false).isTrailingSpacesIgnored());
Assert.assertEquals(false, format.withEmptyLinesIgnored(false).isEmptyLinesIgnored()); assertEquals(false, format.withEmptyLinesIgnored(false).isEmptyLinesIgnored());
Assert.assertEquals(false, format.withUnicodeEscapesInterpreted(false).isUnicodeEscapesInterpreted()); assertEquals(false, format.withUnicodeEscapesInterpreted(false).isUnicodeEscapesInterpreted());
} }
@Test @Test
public void testFormat() { public void testFormat() {
CSVFormat format = CSVFormat.DEFAULT; CSVFormat format = CSVFormat.DEFAULT;
Assert.assertEquals("", format.format()); assertEquals("", format.format());
Assert.assertEquals("a,b,c", format.format("a", "b", "c")); assertEquals("a,b,c", format.format("a", "b", "c"));
Assert.assertEquals("\"x,y\",z", format.format("x,y", "z")); assertEquals("\"x,y\",z", format.format("x,y", "z"));
} }
@Test @Test
@ -86,49 +87,49 @@ public class CSVFormatTest {
try { try {
format.withDelimiter('\n'); format.withDelimiter('\n');
Assert.fail(); fail();
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// expected // expected
} }
try { try {
format.withEscape('\r'); format.withEscape('\r');
Assert.fail(); fail();
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// expected // expected
} }
try { try {
format.withEncapsulator('\n'); format.withEncapsulator('\n');
Assert.fail(); fail();
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// expected // expected
} }
try { try {
format.withCommentStart('\r'); format.withCommentStart('\r');
Assert.fail(); fail();
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// expected // expected
} }
try { try {
format.withDelimiter('!').withEscape('!').validate(); format.withDelimiter('!').withEscape('!').validate();
Assert.fail(); fail();
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// expected // expected
} }
try { try {
format.withDelimiter('!').withCommentStart('!').validate(); format.withDelimiter('!').withCommentStart('!').validate();
Assert.fail(); fail();
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// expected // expected
} }
try { try {
format.withEncapsulator('!').withCommentStart('!').validate(); format.withEncapsulator('!').withCommentStart('!').validate();
Assert.fail(); fail();
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// expected // expected
} }
@ -137,7 +138,7 @@ public class CSVFormatTest {
try { try {
format.withEscape('!').withCommentStart('!').validate(); format.withEscape('!').withCommentStart('!').validate();
Assert.fail(); fail();
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// expected // expected
} }
@ -147,7 +148,7 @@ public class CSVFormatTest {
try { try {
format.withEncapsulator('!').withDelimiter('!').validate(); format.withEncapsulator('!').withDelimiter('!').validate();
Assert.fail(); fail();
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// expected // expected
} }
@ -165,15 +166,15 @@ public class CSVFormatTest {
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();
Assert.assertNotNull(format); assertNotNull(format);
Assert.assertEquals("delimiter", CSVFormat.DEFAULT.getDelimiter(), format.getDelimiter()); assertEquals("delimiter", CSVFormat.DEFAULT.getDelimiter(), format.getDelimiter());
Assert.assertEquals("encapsulator", CSVFormat.DEFAULT.getEncapsulator(), format.getEncapsulator()); assertEquals("encapsulator", CSVFormat.DEFAULT.getEncapsulator(), format.getEncapsulator());
Assert.assertEquals("comment start", CSVFormat.DEFAULT.getCommentStart(), format.getCommentStart()); assertEquals("comment start", CSVFormat.DEFAULT.getCommentStart(), format.getCommentStart());
Assert.assertEquals("line separator", CSVFormat.DEFAULT.getLineSeparator(), format.getLineSeparator()); assertEquals("line separator", CSVFormat.DEFAULT.getLineSeparator(), format.getLineSeparator());
Assert.assertEquals("escape", CSVFormat.DEFAULT.getEscape(), format.getEscape()); assertEquals("escape", CSVFormat.DEFAULT.getEscape(), format.getEscape());
Assert.assertEquals("unicode escape", CSVFormat.DEFAULT.isUnicodeEscapesInterpreted(), format.isUnicodeEscapesInterpreted()); assertEquals("unicode escape", CSVFormat.DEFAULT.isUnicodeEscapesInterpreted(), format.isUnicodeEscapesInterpreted());
Assert.assertEquals("trim left", CSVFormat.DEFAULT.isLeadingSpacesIgnored(), format.isLeadingSpacesIgnored()); assertEquals("trim left", CSVFormat.DEFAULT.isLeadingSpacesIgnored(), format.isLeadingSpacesIgnored());
Assert.assertEquals("trim right", CSVFormat.DEFAULT.isTrailingSpacesIgnored(), format.isTrailingSpacesIgnored()); assertEquals("trim right", CSVFormat.DEFAULT.isTrailingSpacesIgnored(), format.isTrailingSpacesIgnored());
Assert.assertEquals("empty lines", CSVFormat.DEFAULT.isEmptyLinesIgnored(), format.isEmptyLinesIgnored()); assertEquals("empty lines", CSVFormat.DEFAULT.isEmptyLinesIgnored(), format.isEmptyLinesIgnored());
} }
} }

View File

@ -17,17 +17,15 @@
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 org.apache.commons.csv.CSVLexer.Token; import org.apache.commons.csv.CSVLexer.Token;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import static org.apache.commons.csv.CSVLexer.Token.Type.*;
import static org.junit.Assert.*;
public class CSVLexerTest { public class CSVLexerTest {
private CSVLexer getLexer(String input, CSVFormat format) { private CSVLexer getLexer(String input, CSVFormat format) {
@ -35,8 +33,8 @@ public class CSVLexerTest {
} }
private void assertTokenEquals(Token.Type expectedType, String expectedContent, Token token) { private void assertTokenEquals(Token.Type expectedType, String expectedContent, Token token) {
Assert.assertEquals("Token type", expectedType, token.type); assertEquals("Token type", expectedType, token.type);
Assert.assertEquals("Token content", expectedContent, token.content.toString()); assertEquals("Token content", expectedContent, token.content.toString());
} }
// Single line (without comment) // Single line (without comment)

View File

@ -26,10 +26,11 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import org.junit.Assert;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.*;
/** /**
* CSVParserTest * CSVParserTest
* *
@ -57,20 +58,20 @@ public class CSVParserTest {
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) {
Assert.assertTrue(Arrays.equals(re, parser.getRecord())); assertTrue(Arrays.equals(re, parser.getRecord()));
} }
Assert.assertTrue(parser.getRecord() == null); assertTrue(parser.getRecord() == null);
} }
@Test @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();
Assert.assertEquals(res.length, tmp.length); assertEquals(res.length, tmp.length);
Assert.assertTrue(tmp.length > 0); assertTrue(tmp.length > 0);
for (int i = 0; i < res.length; i++) { for (int i = 0; i < res.length; i++) {
Assert.assertTrue(Arrays.equals(res[i], tmp[i])); assertTrue(Arrays.equals(res[i], tmp[i]));
} }
} }
@ -88,10 +89,10 @@ public class CSVParserTest {
}; };
CSVParser parser = new CSVParser(code, CSVFormat.EXCEL); CSVParser parser = new CSVParser(code, CSVFormat.EXCEL);
String[][] tmp = parser.getRecords(); String[][] tmp = parser.getRecords();
Assert.assertEquals(res.length, tmp.length); assertEquals(res.length, tmp.length);
Assert.assertTrue(tmp.length > 0); assertTrue(tmp.length > 0);
for (int i = 0; i < res.length; i++) { for (int i = 0; i < res.length; i++) {
Assert.assertTrue(Arrays.equals(res[i], tmp[i])); assertTrue(Arrays.equals(res[i], tmp[i]));
} }
} }
@ -107,10 +108,10 @@ public class CSVParserTest {
}; };
CSVParser parser = new CSVParser(code, CSVFormat.EXCEL); CSVParser parser = new CSVParser(code, CSVFormat.EXCEL);
String[][] tmp = parser.getRecords(); String[][] tmp = parser.getRecords();
Assert.assertEquals(res.length, tmp.length); assertEquals(res.length, tmp.length);
Assert.assertTrue(tmp.length > 0); assertTrue(tmp.length > 0);
for (int i = 0; i < res.length; i++) { for (int i = 0; i < res.length; i++) {
Assert.assertTrue(Arrays.equals(res[i], tmp[i])); assertTrue(Arrays.equals(res[i], tmp[i]));
} }
} }
@ -135,10 +136,10 @@ public class CSVParserTest {
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();
Assert.assertEquals(res.length, tmp.length); assertEquals(res.length, tmp.length);
Assert.assertTrue(tmp.length > 0); assertTrue(tmp.length > 0);
for (int i = 0; i < res.length; i++) { for (int i = 0; i < res.length; i++) {
Assert.assertTrue(Arrays.equals(res[i], tmp[i])); assertTrue(Arrays.equals(res[i], tmp[i]));
} }
} }
} }
@ -162,10 +163,10 @@ public class CSVParserTest {
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();
Assert.assertEquals(res.length, tmp.length); assertEquals(res.length, tmp.length);
Assert.assertTrue(tmp.length > 0); assertTrue(tmp.length > 0);
for (int i = 0; i < res.length; i++) { for (int i = 0; i < res.length; i++) {
Assert.assertTrue(Arrays.equals(res[i], tmp[i])); assertTrue(Arrays.equals(res[i], tmp[i]));
} }
} }
} }
@ -186,10 +187,10 @@ public class CSVParserTest {
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();
Assert.assertEquals(res.length, tmp.length); assertEquals(res.length, tmp.length);
Assert.assertTrue(tmp.length > 0); assertTrue(tmp.length > 0);
for (int i = 0; i < res.length; i++) { for (int i = 0; i < res.length; i++) {
Assert.assertTrue(Arrays.equals(res[i], tmp[i])); assertTrue(Arrays.equals(res[i], tmp[i]));
} }
} }
} }
@ -208,10 +209,10 @@ public class CSVParserTest {
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();
Assert.assertEquals(res.length, tmp.length); assertEquals(res.length, tmp.length);
Assert.assertTrue(tmp.length > 0); assertTrue(tmp.length > 0);
for (int i = 0; i < res.length; i++) { for (int i = 0; i < res.length; i++) {
Assert.assertTrue(Arrays.equals(res[i], tmp[i])); assertTrue(Arrays.equals(res[i], tmp[i]));
} }
} }
} }
@ -242,10 +243,10 @@ public class CSVParserTest {
}; };
CSVParser parser = new CSVParser(new StringReader(code)); CSVParser parser = new CSVParser(new StringReader(code));
String[][] tmp = parser.getRecords(); String[][] tmp = parser.getRecords();
Assert.assertEquals(res.length, tmp.length); assertEquals(res.length, tmp.length);
Assert.assertTrue(tmp.length > 0); assertTrue(tmp.length > 0);
for (int i = 0; i < res.length; i++) { for (int i = 0; i < res.length; i++) {
Assert.assertTrue(Arrays.equals(res[i], tmp[i])); assertTrue(Arrays.equals(res[i], tmp[i]));
} }
} }
@ -286,9 +287,9 @@ public class CSVParserTest {
CSVParser parser = new CSVParser(code, format); CSVParser parser = new CSVParser(code, format);
String[][] tmp = parser.getRecords(); String[][] tmp = parser.getRecords();
Assert.assertTrue(tmp.length > 0); assertTrue(tmp.length > 0);
for (int i = 0; i < res.length; i++) { for (int i = 0; i < res.length; i++) {
Assert.assertTrue(Arrays.equals(res[i], tmp[i])); assertTrue(Arrays.equals(res[i], tmp[i]));
} }
} }
@ -315,10 +316,10 @@ public class CSVParserTest {
CSVParser parser = new CSVParser(code, format); CSVParser parser = new CSVParser(code, format);
String[][] tmp = parser.getRecords(); String[][] tmp = parser.getRecords();
Assert.assertTrue(tmp.length > 0); assertTrue(tmp.length > 0);
if (!CSVPrinterTest.equals(res, tmp)) { if (!CSVPrinterTest.equals(res, tmp)) {
Assert.assertTrue(false); assertTrue(false);
} }
} }
@ -337,14 +338,14 @@ public class CSVParserTest {
}; };
CSVFormat format = CSVFormat.DEFAULT; CSVFormat format = CSVFormat.DEFAULT;
Assert.assertEquals(CSVFormat.DISABLED, format.getCommentStart()); 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();
Assert.assertTrue(tmp.length > 0); assertTrue(tmp.length > 0);
if (!CSVPrinterTest.equals(res, tmp)) { if (!CSVPrinterTest.equals(res, tmp)) {
Assert.assertTrue(false); assertTrue(false);
} }
String[][] res_comments = { String[][] res_comments = {
@ -358,7 +359,7 @@ public class CSVParserTest {
tmp = parser.getRecords(); tmp = parser.getRecords();
if (!CSVPrinterTest.equals(res_comments, tmp)) { if (!CSVPrinterTest.equals(res_comments, tmp)) {
Assert.assertTrue(false); assertTrue(false);
} }
} }
@ -368,10 +369,10 @@ public class CSVParserTest {
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();
Assert.assertEquals(2, data.length); assertEquals(2, data.length);
Assert.assertEquals("abc", data[0]); assertEquals("abc", data[0]);
Assert.assertEquals("public", data[1]); assertEquals("public", data[1]);
Assert.assertFalse("Should not have any more records", iterator.hasNext()); assertFalse("Should not have any more records", iterator.hasNext());
} }
@Test @Test
@ -380,10 +381,10 @@ public class CSVParserTest {
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();
Assert.assertEquals(2, data.length); assertEquals(2, data.length);
Assert.assertEquals("abc", data[0]); assertEquals("abc", data[0]);
Assert.assertEquals("public", data[1]); assertEquals("public", data[1]);
Assert.assertFalse("Should not have any more records", iterator.hasNext()); assertFalse("Should not have any more records", iterator.hasNext());
} }
@Test @Test
@ -391,7 +392,7 @@ public class CSVParserTest {
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();
Assert.assertEquals(4, data.length); assertEquals(4, data.length);
} }
@Test @Test
@ -399,7 +400,7 @@ public class CSVParserTest {
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();
Assert.assertEquals(4, data.length); assertEquals(4, data.length);
} }
@Test @Test
@ -407,7 +408,7 @@ public class CSVParserTest {
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();
Assert.assertEquals(4, data.length); assertEquals(4, data.length);
} }
@Test @Test
@ -417,7 +418,7 @@ public class CSVParserTest {
//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();
Assert.assertEquals(3, data.length); assertEquals(3, data.length);
} }
@Test @Test
@ -430,10 +431,10 @@ public class CSVParserTest {
records.add(record); records.add(record);
} }
Assert.assertEquals(3, records.size()); assertEquals(3, records.size());
Assert.assertTrue(Arrays.equals(new String[]{"a", "b", "c"}, records.get(0))); assertTrue(Arrays.equals(new String[]{"a", "b", "c"}, records.get(0)));
Assert.assertTrue(Arrays.equals(new String[]{"1", "2", "3"}, records.get(1))); assertTrue(Arrays.equals(new String[]{"1", "2", "3"}, records.get(1)));
Assert.assertTrue(Arrays.equals(new String[]{"x", "y", "z"}, records.get(2))); assertTrue(Arrays.equals(new String[]{"x", "y", "z"}, records.get(2)));
} }
@Test @Test
@ -442,23 +443,23 @@ public class CSVParserTest {
Iterator<String[]> iterator = CSVFormat.DEFAULT.parse(in).iterator(); Iterator<String[]> iterator = CSVFormat.DEFAULT.parse(in).iterator();
Assert.assertTrue(iterator.hasNext()); assertTrue(iterator.hasNext());
try { try {
iterator.remove(); iterator.remove();
Assert.fail("expected UnsupportedOperationException"); fail("expected UnsupportedOperationException");
} catch (UnsupportedOperationException expected) { } catch (UnsupportedOperationException expected) {
} }
Assert.assertTrue(Arrays.equals(new String[]{"a", "b", "c"}, iterator.next())); assertTrue(Arrays.equals(new String[]{"a", "b", "c"}, iterator.next()));
Assert.assertTrue(Arrays.equals(new String[]{"1", "2", "3"}, iterator.next())); assertTrue(Arrays.equals(new String[]{"1", "2", "3"}, iterator.next()));
Assert.assertTrue(iterator.hasNext()); assertTrue(iterator.hasNext());
Assert.assertTrue(iterator.hasNext()); assertTrue(iterator.hasNext());
Assert.assertTrue(iterator.hasNext()); assertTrue(iterator.hasNext());
Assert.assertTrue(Arrays.equals(new String[]{"x", "y", "z"}, iterator.next())); assertTrue(Arrays.equals(new String[]{"x", "y", "z"}, iterator.next()));
Assert.assertFalse(iterator.hasNext()); assertFalse(iterator.hasNext());
try { try {
iterator.next(); iterator.next();
Assert.fail("NoSuchElementException expected"); fail("NoSuchElementException expected");
} catch (NoSuchElementException e) { } catch (NoSuchElementException e) {
// expected // expected
} }

View File

@ -14,18 +14,17 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.apache.commons.csv; package org.apache.commons.csv;
import java.io.IOException; import java.io.IOException;
import java.io.StringWriter; import java.io.StringWriter;
import java.util.Random; import java.util.Random;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
/** import static org.junit.Assert.*;
* CSVPrinterTest
*/
public class CSVPrinterTest { public class CSVPrinterTest {
String lineSeparator = CSVFormat.DEFAULT.getLineSeparator(); String lineSeparator = CSVFormat.DEFAULT.getLineSeparator();
@ -35,7 +34,7 @@ public class CSVPrinterTest {
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");
Assert.assertEquals("a,b" + lineSeparator, sw.toString()); assertEquals("a,b" + lineSeparator, sw.toString());
} }
@Test @Test
@ -43,7 +42,7 @@ public class CSVPrinterTest {
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");
Assert.assertEquals("\"a,b\",b" + lineSeparator, sw.toString()); assertEquals("\"a,b\",b" + lineSeparator, sw.toString());
} }
@Test @Test
@ -51,7 +50,7 @@ public class CSVPrinterTest {
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 ");
Assert.assertEquals("\"a, b\",\"b \"" + lineSeparator, sw.toString()); assertEquals("\"a, b\",\"b \"" + lineSeparator, sw.toString());
} }
@Test @Test
@ -59,7 +58,7 @@ public class CSVPrinterTest {
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");
Assert.assertEquals("a,\"b\"\"c\"" + lineSeparator, sw.toString()); assertEquals("a,\"b\"\"c\"" + lineSeparator, sw.toString());
} }
@Test @Test
@ -67,7 +66,7 @@ public class CSVPrinterTest {
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");
Assert.assertEquals("a,\"b\nc\"" + lineSeparator, sw.toString()); assertEquals("a,\"b\nc\"" + lineSeparator, sw.toString());
} }
@Test @Test
@ -75,7 +74,7 @@ public class CSVPrinterTest {
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");
Assert.assertEquals("a,\"b\r\nc\"" + lineSeparator, sw.toString()); assertEquals("a,\"b\r\nc\"" + lineSeparator, sw.toString());
} }
@Test @Test
@ -83,7 +82,7 @@ public class CSVPrinterTest {
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");
Assert.assertEquals("a,b\\c" + lineSeparator, sw.toString()); assertEquals("a,b\\c" + lineSeparator, sw.toString());
} }
@Test @Test
@ -91,7 +90,7 @@ public class CSVPrinterTest {
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");
Assert.assertEquals("a,b" + lineSeparator, sw.toString()); assertEquals("a,b" + lineSeparator, sw.toString());
} }
@Test @Test
@ -99,7 +98,7 @@ public class CSVPrinterTest {
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");
Assert.assertEquals("\"a,b\",b" + lineSeparator, sw.toString()); assertEquals("\"a,b\",b" + lineSeparator, sw.toString());
} }
@Test @Test
@ -107,7 +106,7 @@ public class CSVPrinterTest {
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");
Assert.assertEquals("a,,b" + lineSeparator, sw.toString()); assertEquals("a,,b" + lineSeparator, sw.toString());
} }
@Test @Test
@ -116,7 +115,7 @@ public class CSVPrinterTest {
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");
Assert.assertEquals("", sw.toString()); assertEquals("", sw.toString());
} }
@Test @Test
@ -125,7 +124,7 @@ public class CSVPrinterTest {
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");
Assert.assertEquals("# This is a comment" + lineSeparator, sw.toString()); assertEquals("# This is a comment" + lineSeparator, sw.toString());
} }
@Test @Test
@ -134,7 +133,7 @@ public class CSVPrinterTest {
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");
Assert.assertEquals("# This is a comment" + lineSeparator + "# on multiple lines" + lineSeparator, sw.toString()); assertEquals("# This is a comment" + lineSeparator + "# on multiple lines" + lineSeparator, sw.toString());
} }
@Test @Test
@ -183,7 +182,7 @@ public class CSVPrinterTest {
if (!equals(lines, parseResult)) { if (!equals(lines, parseResult)) {
System.out.println("Printer output :" + printable(result)); System.out.println("Printer output :" + printable(result));
Assert.assertTrue(false); assertTrue(false);
} }
} }

View File

@ -20,69 +20,70 @@ package org.apache.commons.csv;
import java.io.StringReader; import java.io.StringReader;
import java.util.Arrays; import java.util.Arrays;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.*;
public class ExtendedBufferedReaderTest { public class ExtendedBufferedReaderTest {
@Test @Test
public void testEmptyInput() throws Exception { public void testEmptyInput() throws Exception {
ExtendedBufferedReader br = getBufferedReader(""); ExtendedBufferedReader br = getBufferedReader("");
Assert.assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read()); assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read());
Assert.assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead()); assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead());
Assert.assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.readAgain()); assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.readAgain());
Assert.assertNull(br.readLine()); assertNull(br.readLine());
Assert.assertEquals(0, br.read(new char[10], 0, 0)); assertEquals(0, br.read(new char[10], 0, 0));
} }
@Test @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");
Assert.assertEquals('1', br.lookAhead()); assertEquals('1', br.lookAhead());
Assert.assertEquals(ExtendedBufferedReader.UNDEFINED, br.readAgain()); assertEquals(ExtendedBufferedReader.UNDEFINED, br.readAgain());
Assert.assertEquals('1', br.read()); assertEquals('1', br.read());
Assert.assertEquals('1', br.readAgain()); assertEquals('1', br.readAgain());
Assert.assertEquals(0, br.getLineNumber()); assertEquals(0, br.getLineNumber());
Assert.assertEquals('\n', br.lookAhead()); assertEquals('\n', br.lookAhead());
Assert.assertEquals(0, br.getLineNumber()); assertEquals(0, br.getLineNumber());
Assert.assertEquals('1', br.readAgain()); assertEquals('1', br.readAgain());
Assert.assertEquals('\n', br.read()); assertEquals('\n', br.read());
Assert.assertEquals(1, br.getLineNumber()); assertEquals(1, br.getLineNumber());
Assert.assertEquals('\n', br.readAgain()); assertEquals('\n', br.readAgain());
Assert.assertEquals(1, br.getLineNumber()); assertEquals(1, br.getLineNumber());
Assert.assertEquals('2', br.lookAhead()); assertEquals('2', br.lookAhead());
Assert.assertEquals(1, br.getLineNumber()); assertEquals(1, br.getLineNumber());
Assert.assertEquals('\n', br.readAgain()); assertEquals('\n', br.readAgain());
Assert.assertEquals(1, br.getLineNumber()); assertEquals(1, br.getLineNumber());
Assert.assertEquals('2', br.read()); assertEquals('2', br.read());
Assert.assertEquals('2', br.readAgain()); assertEquals('2', br.readAgain());
Assert.assertEquals('\r', br.lookAhead()); assertEquals('\r', br.lookAhead());
Assert.assertEquals('2', br.readAgain()); assertEquals('2', br.readAgain());
Assert.assertEquals('\r', br.read()); assertEquals('\r', br.read());
Assert.assertEquals('\r', br.readAgain()); assertEquals('\r', br.readAgain());
Assert.assertEquals('3', br.lookAhead()); assertEquals('3', br.lookAhead());
Assert.assertEquals('\r', br.readAgain()); assertEquals('\r', br.readAgain());
Assert.assertEquals('3', br.read()); assertEquals('3', br.read());
Assert.assertEquals('3', br.readAgain()); assertEquals('3', br.readAgain());
Assert.assertEquals('\n', br.lookAhead()); assertEquals('\n', br.lookAhead());
Assert.assertEquals(1, br.getLineNumber()); assertEquals(1, br.getLineNumber());
Assert.assertEquals('3', br.readAgain()); assertEquals('3', br.readAgain());
Assert.assertEquals('\n', br.read()); assertEquals('\n', br.read());
Assert.assertEquals(2, br.getLineNumber()); assertEquals(2, br.getLineNumber());
Assert.assertEquals('\n', br.readAgain()); assertEquals('\n', br.readAgain());
Assert.assertEquals(2, br.getLineNumber()); assertEquals(2, br.getLineNumber());
Assert.assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead()); assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead());
Assert.assertEquals('\n', br.readAgain()); assertEquals('\n', br.readAgain());
Assert.assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read()); assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read());
Assert.assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.readAgain()); assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.readAgain());
Assert.assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read()); assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read());
Assert.assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead()); assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead());
} }
@ -95,58 +96,58 @@ public class ExtendedBufferedReaderTest {
ref[0] = 'a'; ref[0] = 'a';
ref[1] = 'b'; ref[1] = 'b';
ref[2] = 'c'; ref[2] = 'c';
Assert.assertEquals(3, br.read(res, 0, 3)); assertEquals(3, br.read(res, 0, 3));
Assert.assertTrue(Arrays.equals(res, ref)); assertTrue(Arrays.equals(res, ref));
Assert.assertEquals('c', br.readAgain()); assertEquals('c', br.readAgain());
Assert.assertEquals('d', br.lookAhead()); assertEquals('d', br.lookAhead());
ref[4] = 'd'; ref[4] = 'd';
Assert.assertEquals(1, br.read(res, 4, 1)); assertEquals(1, br.read(res, 4, 1));
Assert.assertTrue(Arrays.equals(res, ref)); assertTrue(Arrays.equals(res, ref));
Assert.assertEquals('d', br.readAgain()); assertEquals('d', br.readAgain());
} }
@Test @Test
public void testReadLine() throws Exception { public void testReadLine() throws Exception {
ExtendedBufferedReader br = getBufferedReader(""); ExtendedBufferedReader br = getBufferedReader("");
Assert.assertTrue(br.readLine() == null); assertTrue(br.readLine() == null);
br = getBufferedReader("\n"); br = getBufferedReader("\n");
Assert.assertTrue(br.readLine().equals("")); assertTrue(br.readLine().equals(""));
Assert.assertTrue(br.readLine() == null); assertTrue(br.readLine() == null);
br = getBufferedReader("foo\n\nhello"); br = getBufferedReader("foo\n\nhello");
Assert.assertEquals(0, br.getLineNumber()); assertEquals(0, br.getLineNumber());
Assert.assertTrue(br.readLine().equals("foo")); assertTrue(br.readLine().equals("foo"));
Assert.assertEquals(1, br.getLineNumber()); assertEquals(1, br.getLineNumber());
Assert.assertTrue(br.readLine().equals("")); assertTrue(br.readLine().equals(""));
Assert.assertEquals(2, br.getLineNumber()); assertEquals(2, br.getLineNumber());
Assert.assertTrue(br.readLine().equals("hello")); assertTrue(br.readLine().equals("hello"));
Assert.assertEquals(3, br.getLineNumber()); assertEquals(3, br.getLineNumber());
Assert.assertTrue(br.readLine() == null); assertTrue(br.readLine() == null);
Assert.assertEquals(3, br.getLineNumber()); assertEquals(3, br.getLineNumber());
br = getBufferedReader("foo\n\nhello"); br = getBufferedReader("foo\n\nhello");
Assert.assertEquals('f', br.read()); assertEquals('f', br.read());
Assert.assertEquals('o', br.lookAhead()); assertEquals('o', br.lookAhead());
Assert.assertTrue(br.readLine().equals("oo")); assertTrue(br.readLine().equals("oo"));
Assert.assertEquals(1, br.getLineNumber()); assertEquals(1, br.getLineNumber());
Assert.assertEquals('\n', br.lookAhead()); assertEquals('\n', br.lookAhead());
Assert.assertTrue(br.readLine().equals("")); assertTrue(br.readLine().equals(""));
Assert.assertEquals(2, br.getLineNumber()); assertEquals(2, br.getLineNumber());
Assert.assertEquals('h', br.lookAhead()); assertEquals('h', br.lookAhead());
Assert.assertTrue(br.readLine().equals("hello")); assertTrue(br.readLine().equals("hello"));
Assert.assertTrue(br.readLine() == null); assertTrue(br.readLine() == null);
Assert.assertEquals(3, br.getLineNumber()); assertEquals(3, br.getLineNumber());
br = getBufferedReader("foo\rbaar\r\nfoo"); br = getBufferedReader("foo\rbaar\r\nfoo");
Assert.assertTrue(br.readLine().equals("foo")); assertTrue(br.readLine().equals("foo"));
Assert.assertEquals('b', br.lookAhead()); assertEquals('b', br.lookAhead());
Assert.assertTrue(br.readLine().equals("baar")); assertTrue(br.readLine().equals("baar"));
Assert.assertEquals('f', br.lookAhead()); assertEquals('f', br.lookAhead());
Assert.assertTrue(br.readLine().equals("foo")); assertTrue(br.readLine().equals("foo"));
Assert.assertTrue(br.readLine() == null); assertTrue(br.readLine() == null);
} }
private ExtendedBufferedReader getBufferedReader(String s) { private ExtendedBufferedReader getBufferedReader(String s) {