7676public class CSVPrinterTest {
7777
7878 private static final int TABLE_RECORD_COUNT = 2 ;
79+ private static final int TABLE_AND_HEADER_RECORD_COUNT = TABLE_RECORD_COUNT + 1 ;
7980 private static final char DQUOTE_CHAR = '"' ;
8081 private static final char EURO_CH = '\u20AC' ;
81- private static final int ITERATIONS_FOR_RANDOM_TEST = 50000 ;
82+ private static final int ITERATIONS_FOR_RANDOM_TEST = 50_000 ;
8283 private static final char QUOTE_CH = '\'' ;
8384
8485 private static String printable (final String s ) {
@@ -102,6 +103,12 @@ private void assertInitialState(final CSVPrinter printer) {
102103 assertEquals (0 , printer .getRecordCount ());
103104 }
104105
106+ private void assertRowCount (final CSVFormat format , final String resultString , final int rowCount ) throws IOException {
107+ try (CSVParser parser = format .parse (new StringReader (resultString ))) {
108+ assertEquals (rowCount , parser .getRecords ().size ());
109+ }
110+ }
111+
105112 private File createTempFile () throws IOException {
106113 return createTempPath ().toFile ();
107114 }
@@ -828,35 +835,41 @@ public void testJdbcPrinterWithFirstEmptyValue2() throws IOException, ClassNotFo
828835 @ Test
829836 public void testJdbcPrinterWithResultSet () throws IOException , ClassNotFoundException , SQLException {
830837 final StringWriter sw = new StringWriter ();
838+ final CSVFormat format = CSVFormat .DEFAULT ;
831839 try (Connection connection = getH2Connection ()) {
832840 setUpTable (connection );
833841 try (Statement stmt = connection .createStatement ();
834842 ResultSet resultSet = stmt .executeQuery ("select ID, NAME, TEXT from TEST" );
835- CSVPrinter printer = CSVFormat . DEFAULT .withHeader (resultSet ).print (sw )) {
843+ CSVPrinter printer = format .withHeader (resultSet ).print (sw )) {
836844 printer .printRecords (resultSet );
837845 }
838846 }
847+ final String resultString = sw .toString ();
839848 assertEquals ("ID,NAME,TEXT" + recordSeparator + "1,r1,\" long text 1\" " + recordSeparator + "2,r2,\" " + longText2 + "\" " + recordSeparator ,
840- sw .toString ());
849+ resultString );
850+ assertRowCount (format , resultString , TABLE_AND_HEADER_RECORD_COUNT );
841851 }
842852
843853 @ Test
844854 public void testJdbcPrinterWithResultSetHeader () throws IOException , ClassNotFoundException , SQLException {
845855 final StringWriter sw = new StringWriter ();
846856 try (Connection connection = getH2Connection ()) {
847857 setUpTable (connection );
858+ final CSVFormat format = CSVFormat .DEFAULT ;
848859 try (Statement stmt = connection .createStatement ();
849- CSVPrinter printer = new CSVPrinter (sw , CSVFormat . DEFAULT )) {
860+ CSVPrinter printer = new CSVPrinter (sw , format )) {
850861 try (ResultSet resultSet = stmt .executeQuery ("select ID, NAME from TEST" )) {
851862 printer .printRecords (resultSet , true );
852863 assertEquals (TABLE_RECORD_COUNT , printer .getRecordCount ());
853864 assertEquals ("ID,NAME" + recordSeparator + "1,r1" + recordSeparator + "2,r2" + recordSeparator , sw .toString ());
854865 }
866+ assertRowCount (format , sw .toString (), TABLE_AND_HEADER_RECORD_COUNT );
855867 try (ResultSet resultSet = stmt .executeQuery ("select ID, NAME from TEST" )) {
856868 printer .printRecords (resultSet , false );
857869 assertEquals (TABLE_RECORD_COUNT * 2 , printer .getRecordCount ());
858870 assertNotEquals ("ID,NAME" + recordSeparator + "1,r1" + recordSeparator + "2,r2" + recordSeparator , sw .toString ());
859871 }
872+ assertRowCount (format , sw .toString (), TABLE_AND_HEADER_RECORD_COUNT + TABLE_RECORD_COUNT );
860873 }
861874 }
862875 }
@@ -866,16 +879,18 @@ public void testJdbcPrinterWithResultSetMetaData() throws IOException, ClassNotF
866879 final StringWriter sw = new StringWriter ();
867880 try (Connection connection = getH2Connection ()) {
868881 setUpTable (connection );
882+ final CSVFormat format = CSVFormat .DEFAULT ;
869883 try (Statement stmt = connection .createStatement ();
870884 ResultSet resultSet = stmt .executeQuery ("select ID, NAME, TEXT from TEST" );
871- CSVPrinter printer = CSVFormat . DEFAULT .withHeader (resultSet .getMetaData ()).print (sw )) {
885+ CSVPrinter printer = format .withHeader (resultSet .getMetaData ()).print (sw )) {
872886 // The header is the first record.
873887 assertEquals (1 , printer .getRecordCount ());
874888 printer .printRecords (resultSet );
875889 assertEquals (3 , printer .getRecordCount ());
876890 assertEquals ("ID,NAME,TEXT" + recordSeparator + "1,r1,\" long text 1\" " + recordSeparator + "2,r2,\" " + longText2 + "\" " + recordSeparator ,
877891 sw .toString ());
878892 }
893+ assertRowCount (format , sw .toString (), TABLE_AND_HEADER_RECORD_COUNT );
879894 }
880895 }
881896
0 commit comments