@Test
  public void testLineEndingsAreNotModified() {
    CsvWriterSettings settings = new CsvWriterSettings();
    settings.setNormalizeLineEndingsWithinQuotes(false);
    settings.getFormat().setLineSeparator("\r\n");
    settings.trimValues(false);

    StringWriter output = new StringWriter();
    CsvWriter writer = new CsvWriter(output, settings);

    writer.writeRow(new String[] {"1", " Line1 \r\n Line2 \r Line3 \n Line4 \n\r "});
    writer.writeRow(new String[] {"2", " Line10 \r\n Line11 "});
    writer.close();

    String result = output.toString();

    assertEquals(
        result,
        "1,\" Line1 \r\n Line2 \r Line3 \n Line4 \n\r \"\r\n" + "2,\" Line10 \r\n Line11 \"\r\n");
  }
Exemplo n.º 2
0
  @Test
  public void testWritingQuotedValuesIgnoringTrailingWhistespaces() throws Exception {
    Object[] row = new Object[] {1, "Line1\nLine2 "};

    CsvWriterSettings settings = new CsvWriterSettings();
    settings.getFormat().setLineSeparator("\r\n");
    settings.setIgnoreTrailingWhitespaces(true);

    ByteArrayOutputStream csvResult = new ByteArrayOutputStream();
    CsvWriter writer = new CsvWriter(new OutputStreamWriter(csvResult, "UTF-8"), settings);
    writer.writeRow(row);
    writer.close();

    String expected = "1,\"Line1\r\nLine2\"\r\n";

    assertEquals(csvResult.toString(), expected);
  }
Exemplo n.º 3
0
  @Test(enabled = true, dataProvider = "lineSeparatorProvider")
  public void writeTest(boolean quoteAllFields, char[] lineSeparator) throws Exception {
    CsvWriterSettings settings = new CsvWriterSettings();

    String[] expectedHeaders = new String[] {"Year", "Make", "Model", "Description", "Price"};
    settings.setQuoteAllFields(quoteAllFields);
    settings.getFormat().setLineSeparator(lineSeparator);
    settings.setIgnoreLeadingWhitespaces(false);
    settings.setIgnoreTrailingWhitespaces(false);
    settings.setHeaders(expectedHeaders);

    ByteArrayOutputStream csvResult = new ByteArrayOutputStream();

    CsvWriter writer = new CsvWriter(new OutputStreamWriter(csvResult, "UTF-8"), settings);

    Object[][] expectedResult =
        new Object[][] {
          {"1997", "Ford", "E350", "ac, abs, moon", "3000.00"},
          {"1999", "Chevy", "Venture \"Extended Edition\"", null, "4900.00"},
          {"1996", "Jeep", "Grand Cherokee", "MUST SELL!\nair, moon roof, loaded", "4799.00"},
          {"1999", "Chevy", "Venture \"Extended Edition, Very Large\"", null, "5000.00"},
          {null, null, "Venture \"Extended Edition\"", null, "4900.00"},
          {null, null, null, null, null},
          {null, null, null, null, null},
          {null, null, "5", null, null},
          {"1997", "Ford", "E350", "ac, abs, moon", "3000.00"},
          {"1997", "Ford", "E350", " ac, abs, moon ", "3000.00"},
          {"1997", "Ford", "E350", " ac, abs, moon ", "3000.00"},
          {"19 97", "Fo rd", "E350", " ac, abs, moon ", "3000.00"},
          {null, " ", null, "  ", "30 00.00"},
          {"1997", "Ford", "E350", " \" ac, abs, moon \" ", "3000.00"},
          {"1997", "Ford", "E350", "\" ac, abs, moon \" ", "3000.00"},
        };

    writer.writeHeaders();

    for (int i = 0; i < 4; i++) {
      writer.writeRow(expectedResult[i]);
    }
    writer.writeRow("-->skipping this line (10) as well");
    for (int i = 4; i < expectedResult.length; i++) {
      writer.writeRow(expectedResult[i]);
    }
    writer.close();

    String result = csvResult.toString();
    result =
        "This line and the following should be skipped. The third is ignored automatically because it is blank\n\n\n"
                .replaceAll("\n", new String(lineSeparator))
            + result;

    CsvParserSettings parserSettings = new CsvParserSettings();
    parserSettings.setRowProcessor(processor);
    parserSettings.getFormat().setLineSeparator(lineSeparator);
    parserSettings.setHeaderExtractionEnabled(true);
    parserSettings.setIgnoreLeadingWhitespaces(false);
    parserSettings.setIgnoreTrailingWhitespaces(false);

    CsvParser parser = new CsvParser(parserSettings);
    parser.parse(new StringReader(result));

    try {
      assertHeadersAndValuesMatch(expectedHeaders, expectedResult);
    } catch (Error e) {
      System.out.println("FAILED:\n===\n" + result + "\n===");
      throw e;
    }
  }