private void doRandom(final CSVFormat format, final int iter) throws Exception { for (int i = 0; i < iter; i++) { doOneRandom(format); } }
@Test public void testRandomExcel() throws Exception { doRandom(CSVFormat.EXCEL, ITERATIONS_FOR_RANDOM_TEST); }
@Test public void testJdbcPrinter() throws IOException, ClassNotFoundException, SQLException { final StringWriter sw = new StringWriter(); try (final Connection connection = geH2Connection()) { setUpTable(connection); try (final Statement stmt = connection.createStatement(); final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT)) { printer.printRecords(stmt.executeQuery("select ID, NAME from TEST")); } } assertEquals("1,r1" + recordSeparator + "2,r2" + recordSeparator, sw.toString()); }
@Test @Ignore public void testJira135All() throws IOException { final CSVFormat format = CSVFormat.DEFAULT.withRecordSeparator('\n').withQuote(DQUOTE_CHAR).withEscape(BACKSLASH_CH); final StringWriter sw = new StringWriter(); final List<String> list = new LinkedList<>(); try (final CSVPrinter printer = new CSVPrinter(sw, format)) { list.add("\""); list.add("\n"); list.add("\\"); printer.printRecord(list); } final String expected = "\"\\\"\",\"\\n\",\"\\\"" + format.getRecordSeparator(); assertEquals(expected, sw.toString()); final String[] record0 = toFirstRecordValues(expected, format); assertArrayEquals(expectNulls(list.toArray(), format), record0); }
private void doOneRandom(final CSVFormat format) throws Exception { final Random r = new Random(); final int nLines = r.nextInt(4) + 1; final int nCol = r.nextInt(3) + 1; // nLines=1;nCol=2; final String[][] lines = generateLines(nLines, nCol); final StringWriter sw = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(sw, format)) { for (int i = 0; i < nLines; i++) { // for (int j=0; j<lines[i].length; j++) System.out.println("### VALUE=:" + printable(lines[i][j])); printer.printRecord((Object[]) lines[i]); } printer.flush(); } final String result = sw.toString(); // System.out.println("### :" + printable(result)); try (final CSVParser parser = CSVParser.parse(result, format)) { final List<CSVRecord> parseResult = parser.getRecords(); final String[][] expected = lines.clone(); for (int i = 0; i < expected.length; i++) { expected[i] = expectNulls(expected[i], format); } Utils.compare("Printer output :" + printable(result), expected, parseResult); } }
@Test public void testHeaderCommentExcel() throws IOException { final StringWriter sw = new StringWriter(); final Date now = new Date(); final CSVFormat format = CSVFormat.EXCEL; try (final CSVPrinter csvPrinter = printWithHeaderComments(sw, now, format)) { assertEquals("# Generated by Apache Commons CSV 1.1\r\n# " + now + "\r\nCol1,Col2\r\nA,B\r\nC,D\r\n", sw.toString()); } }
private String[][] generateLines(final int nLines, final int nCol) { final String[][] lines = new String[nLines][]; for (int i = 0; i < nLines; i++) { final String[] line = new String[nCol]; lines[i] = line; for (int j = 0; j < nCol; j++) { line[j] = randStr(); } } return lines; }
@Test @Ignore public void testJira135_part1() throws IOException { final CSVFormat format = CSVFormat.DEFAULT.withRecordSeparator('\n').withQuote(DQUOTE_CHAR).withEscape(BACKSLASH_CH); final StringWriter sw = new StringWriter(); final List<String> list = new LinkedList<>(); try (final CSVPrinter printer = new CSVPrinter(sw, format)) { list.add("\""); printer.printRecord(list); } final String expected = "\"\\\"\"" + format.getRecordSeparator(); assertEquals(expected, sw.toString()); final String[] record0 = toFirstRecordValues(expected, format); assertArrayEquals(expectNulls(list.toArray(), format), record0); }
@Test public void testHeaderCommentTdf() throws IOException { final StringWriter sw = new StringWriter(); final Date now = new Date(); final CSVFormat format = CSVFormat.TDF; try (final CSVPrinter csvPrinter = printWithHeaderComments(sw, now, format)) { assertEquals("# Generated by Apache Commons CSV 1.1\r\n# " + now + "\r\nCol1\tCol2\r\nA\tB\r\nC\tD\r\n", sw.toString()); } }
@Test @Ignore public void testJira135_part2() throws IOException { final CSVFormat format = CSVFormat.DEFAULT.withRecordSeparator('\n').withQuote(DQUOTE_CHAR).withEscape(BACKSLASH_CH); final StringWriter sw = new StringWriter(); final List<String> list = new LinkedList<>(); try (final CSVPrinter printer = new CSVPrinter(sw, format)) { list.add("\n"); printer.printRecord(list); } final String expected = "\"\\n\"" + format.getRecordSeparator(); assertEquals(expected, sw.toString()); final String[] record0 = toFirstRecordValues(expected, format); assertArrayEquals(expectNulls(list.toArray(), format), record0); }
@Test public void testJdbcPrinterWithResultSet() throws IOException, ClassNotFoundException, SQLException { final StringWriter sw = new StringWriter(); Class.forName("org.h2.Driver"); try (final Connection connection = geH2Connection();) { setUpTable(connection); try (final Statement stmt = connection.createStatement(); final ResultSet resultSet = stmt.executeQuery("select ID, NAME from TEST"); final CSVPrinter printer = CSVFormat.DEFAULT.withHeader(resultSet).print(sw)) { printer.printRecords(resultSet); } } assertEquals("ID,NAME" + recordSeparator + "1,r1" + recordSeparator + "2,r2" + recordSeparator, sw.toString()); }
@Test public void testRandomRfc4180() throws Exception { doRandom(CSVFormat.RFC4180, ITERATIONS_FOR_RANDOM_TEST); }
@Test @Ignore public void testJira135_part3() throws IOException { final CSVFormat format = CSVFormat.DEFAULT.withRecordSeparator('\n').withQuote(DQUOTE_CHAR).withEscape(BACKSLASH_CH); final StringWriter sw = new StringWriter(); final List<String> list = new LinkedList<>(); try (final CSVPrinter printer = new CSVPrinter(sw, format)) { list.add("\\"); printer.printRecord(list); } final String expected = "\"\\\\\"" + format.getRecordSeparator(); assertEquals(expected, sw.toString()); final String[] record0 = toFirstRecordValues(expected, format); assertArrayEquals(expectNulls(list.toArray(), format), record0); }
@Test public void testJdbcPrinterWithResultSetMetaData() throws IOException, ClassNotFoundException, SQLException { final StringWriter sw = new StringWriter(); Class.forName("org.h2.Driver"); try (final Connection connection = geH2Connection()) { setUpTable(connection); try (final Statement stmt = connection.createStatement(); final ResultSet resultSet = stmt.executeQuery("select ID, NAME from TEST"); final CSVPrinter printer = CSVFormat.DEFAULT.withHeader(resultSet.getMetaData()).print(sw)) { printer.printRecords(resultSet); assertEquals("ID,NAME" + recordSeparator + "1,r1" + recordSeparator + "2,r2" + recordSeparator, sw.toString()); } } }
@Test public void testRandomTdf() throws Exception { doRandom(CSVFormat.TDF, ITERATIONS_FOR_RANDOM_TEST); }
String[] record0 = toFirstRecordValues(expected, format); assertArrayEquals(new Object[2], record0); record0 = toFirstRecordValues(expected, format); assertArrayEquals(expectNulls(s, format), record0); record0 = toFirstRecordValues(expected, format); assertArrayEquals(expectNulls(s, format), record0); record0 = toFirstRecordValues(expected, format); assertArrayEquals(expectNulls(s, format), record0); record0 = toFirstRecordValues(expected, format); assertArrayEquals(expectNulls(s, format), record0); record0 = toFirstRecordValues(expected, format); assertArrayEquals(expectNulls(s, format), record0); record0 = toFirstRecordValues(expected, format); assertArrayEquals(expectNulls(s, format), record0); record0 = toFirstRecordValues(expected, format); assertArrayEquals(expectNulls(s, format), record0); record0 = toFirstRecordValues(expected, format); assertArrayEquals(expectNulls(s, format), record0);
@Test public void testRandomDefault() throws Exception { doRandom(CSVFormat.DEFAULT, ITERATIONS_FOR_RANDOM_TEST); }
String[] record0 = toFirstRecordValues(expected, format); assertArrayEquals(new Object[2], record0); record0 = toFirstRecordValues(expected, format); assertArrayEquals(expectNulls(s, format), record0); record0 = toFirstRecordValues(expected, format); assertArrayEquals(expectNulls(s, format), record0); record0 = toFirstRecordValues(expected, format); assertArrayEquals(expectNulls(s, format), record0); record0 = toFirstRecordValues(expected, format); assertArrayEquals(expectNulls(s, format), record0); record0 = toFirstRecordValues(expected, format); assertArrayEquals(expectNulls(s, format), record0); record0 = toFirstRecordValues(expected, format); assertArrayEquals(expectNulls(s, format), record0); record0 = toFirstRecordValues(expected, format); assertArrayEquals(expectNulls(s, format), record0); record0 = toFirstRecordValues(expected, format); assertArrayEquals(expectNulls(s, format), record0);
@Test public void testRandomMySql() throws Exception { doRandom(CSVFormat.MYSQL, ITERATIONS_FOR_RANDOM_TEST); }
String[] record0 = toFirstRecordValues(expected, format); assertArrayEquals(new Object[2], record0); record0 = toFirstRecordValues(expected, format); assertArrayEquals(expectNulls(s, format), record0); record0 = toFirstRecordValues(expected, format); assertArrayEquals(expectNulls(s, format), record0); record0 = toFirstRecordValues(expected, format); assertArrayEquals(expectNulls(s, format), record0); record0 = toFirstRecordValues(expected, format); assertArrayEquals(expectNulls(s, format), record0); record0 = toFirstRecordValues(expected, format); assertArrayEquals(expectNulls(s, format), record0); record0 = toFirstRecordValues(expected, format); assertArrayEquals(expectNulls(s, format), record0); record0 = toFirstRecordValues(expected, format); assertArrayEquals(expectNulls(s, format), record0); record0 = toFirstRecordValues(expected, format); assertArrayEquals(expectNulls(s, format), record0);