Skip to content

Commit f9633e8

Browse files
authored
Improve test coverage in CSVFormatTest (apache#65)
* Improve Format Coverage to 99percent * code format add space
1 parent cab61c5 commit f9633e8

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

src/test/java/org/apache/commons/csv/CSVFormatTest.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
import java.io.StringReader;
4242
import java.lang.reflect.Method;
4343
import java.lang.reflect.Modifier;
44+
import java.sql.ResultSet;
45+
import java.sql.SQLException;
4446
import java.util.Arrays;
4547

4648
import org.junit.jupiter.api.Test;
@@ -1205,4 +1207,75 @@ public void testFormatToString() throws IOException {
12051207
assertEquals("Delimiter=<,> Escape=<?> QuoteChar=<\"> QuoteMode=<MINIMAL> NullString=<> RecordSeparator=<" +CRLF+
12061208
"> IgnoreHeaderCase:ignored SkipHeaderRecord:false HeaderComments:[This is HeaderComments] Header:[col1, col2, col3]", format.toString());
12071209
}
1210+
1211+
@Test
1212+
public void testDelimiterSameAsRecordSeparatorThrowsException() {
1213+
assertThrows(IllegalArgumentException.class, () -> CSVFormat.newFormat(CR));
1214+
}
1215+
1216+
@Test
1217+
public void testWithHeaderEnumNull() {
1218+
final CSVFormat format = CSVFormat.DEFAULT;
1219+
Class<Enum<?>> simpleName = null;
1220+
format.withHeader(simpleName);
1221+
}
1222+
1223+
@Test
1224+
public void testWithHeaderResultSetNull() throws SQLException {
1225+
final CSVFormat format = CSVFormat.DEFAULT;
1226+
ResultSet resultSet = null;
1227+
format.withHeader(resultSet);
1228+
}
1229+
1230+
@Test
1231+
public void testPrintWithQuoteModeIsNONE() throws IOException {
1232+
final Reader in = new StringReader("a,b,c");
1233+
final Appendable out = new StringBuilder();
1234+
CSVFormat format = CSVFormat.RFC4180.withDelimiter(',').withQuote('"').withEscape('?').withQuoteMode(QuoteMode.NONE);
1235+
format.print(in, out, true);
1236+
assertEquals("a?,b?,c", out.toString());
1237+
}
1238+
1239+
@Test
1240+
public void testPrintWithQuotes() throws IOException {
1241+
final Reader in = new StringReader("\"a,b,c\r\nx,y,z");
1242+
final Appendable out = new StringBuilder();
1243+
CSVFormat format = CSVFormat.RFC4180.withDelimiter(',').withQuote('"').withEscape('?').withQuoteMode(QuoteMode.NON_NUMERIC);
1244+
format.print(in, out, true);
1245+
assertEquals("\"\"\"\"a,b,c\r\nx,y,z\"", out.toString());
1246+
}
1247+
1248+
@Test
1249+
public void testPrintWithoutQuotes() throws IOException {
1250+
final Reader in = new StringReader("");
1251+
final Appendable out = new StringBuilder();
1252+
CSVFormat format = CSVFormat.RFC4180.withDelimiter(',').withQuote('"').withEscape('?').withQuoteMode(QuoteMode.NON_NUMERIC);
1253+
format.print(in, out, true);
1254+
assertEquals("\"\"", out.toString());
1255+
}
1256+
1257+
@Test
1258+
public void testTrim() throws IOException {
1259+
CSVFormat formatWithTrim = CSVFormat.DEFAULT.withDelimiter(',').withTrim().withQuote(null).withRecordSeparator(CRLF);
1260+
1261+
CharSequence in = "a,b,c";
1262+
final StringBuilder out = new StringBuilder();
1263+
formatWithTrim.print(in, out, true);
1264+
assertEquals("a,b,c", out.toString());
1265+
1266+
in = new StringBuilder(" x,y,z");
1267+
out.setLength(0);
1268+
formatWithTrim.print(in, out, true);
1269+
assertEquals("x,y,z", out.toString());
1270+
1271+
in = new StringBuilder("");
1272+
out.setLength(0);
1273+
formatWithTrim.print(in, out, true);
1274+
assertEquals("", out.toString());
1275+
1276+
in = new StringBuilder("header\r\n");
1277+
out.setLength(0);
1278+
formatWithTrim.print(in, out, true);
1279+
assertEquals("header", out.toString());
1280+
}
12081281
}

0 commit comments

Comments
 (0)