예제 #1
0
  @Test(dataProvider = "blanksProvider")
  public void testWriteBlanks(boolean quoteAllFields, String nullValue, String expectedResult) {
    CsvWriterSettings s = new CsvWriterSettings();
    s.setQuoteAllFields(quoteAllFields);
    s.getFormat().setLineSeparator("\n");
    s.setNullValue(nullValue);
    s.setEmptyValue("//");
    CsvWriter w = new CsvWriter(s);

    CsvParserSettings ps = new CsvParserSettings();
    ps.setNullValue(nullValue);
    ps.setEmptyValue("//");
    CsvParser p = new CsvParser(ps);

    String result = w.writeRowToString("   ", " ", "", "\"\"", null);

    assertEquals(result, expectedResult);

    String[] row = p.parseLine(result);
    if (quoteAllFields) {
      assertEquals(row[0], "//");
      assertEquals(row[1], "//");
    } else {
      assertEquals(row[0], nullValue);
      assertEquals(row[1], nullValue);
    }

    assertEquals(row[2], "//");
    assertEquals(row[3], "\"\"");
    assertEquals(row[4], nullValue);
  }
예제 #2
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;
    }
  }
예제 #3
0
  @Test(enabled = true, dataProvider = "lineSeparatorProvider")
  public void writeSelectedColumnOnly(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);
    settings.selectFields("Model", "Price");

    ByteArrayOutputStream csvResult = new ByteArrayOutputStream();

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

    Object[][] input =
        new Object[][] {
          {"E350", "3000.00"},
          {"Venture \"Extended Edition\"", "4900.00"},
          {"Grand Cherokee", "4799.00"},
          {"Venture \"Extended Edition, Very Large\"", "5000.00"},
          {"Venture \"Extended Edition\"", "4900.00"},
          {null, null},
          {"5", null},
          {"E350", "3000.00"},
        };
    writer.writeHeaders();
    writer.writeRowsAndClose(input);

    Object[][] expectedResult =
        new Object[][] {
          {null, null, "E350", null, "3000.00"},
          {null, null, "Venture \"Extended Edition\"", null, "4900.00"},
          {null, null, "Grand Cherokee", null, "4799.00"},
          {null, null, "Venture \"Extended Edition, Very Large\"", null, "5000.00"},
          {null, null, "Venture \"Extended Edition\"", null, "4900.00"},
          {null, null, null, null, null},
          {null, null, "5", null, null},
          {null, null, "E350", null, "3000.00"},
        };

    String result = csvResult.toString();

    RowListProcessor rowList = new RowListProcessor();
    CsvParserSettings parserSettings = new CsvParserSettings();
    parserSettings.setRowProcessor(rowList);
    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(rowList, expectedHeaders, expectedResult);
    } catch (Error e) {
      System.out.println("FAILED:\n===\n" + result + "\n===");
      throw e;
    }
  }