Ejemplo n.º 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);
  }
Ejemplo n.º 2
0
  @Test
  public void testWritingWithIndexExclusion() {
    CsvWriterSettings settings = new CsvWriterSettings();
    settings.setMaxColumns(8);
    settings.excludeIndexes(4, 1);

    CsvWriter writer = new CsvWriter(settings);
    String result1 = writer.writeRowToString(1, 2, 3, 4, 5, 6);
    writer.updateFieldExclusion(1, 3, 5, 7);
    String result2 = writer.writeRowToString(7, 8, 9, 10);

    //		System.out.println(result1);
    //		System.out.println(result2);

    assertEquals(result1, "1,,2,3,,4,5,6");
    assertEquals(result2, "7,,8,,9,,10,");
  }
Ejemplo n.º 3
0
  @Test
  public void testWritingWithIndexSelection() {
    CsvWriterSettings settings = new CsvWriterSettings();
    settings.selectIndexes(4, 1);

    CsvWriter writer = new CsvWriter(settings);
    String result1 = writer.writeRowToString(1, 2);

    writer.updateFieldSelection(0, 3, 5);

    String result2 = writer.writeRowToString('A', 'B', 'C');

    // System.out.println(result1);
    // System.out.println(result2);

    assertEquals(result1, ",2,,,1");
    assertEquals(result2, "A,,,B,,C");
  }
  @Test
  public void testEscapeQuoteInValues() {
    CsvWriterSettings settings = new CsvWriterSettings();
    settings.trimValues(false);
    settings.getFormat().setLineSeparator("\n");
    settings.getFormat().setQuote('\'');
    settings.getFormat().setQuoteEscape('\'');
    settings.getFormat().setCharToEscapeQuoteEscaping('\'');
    settings.setQuoteEscapingEnabled(true);

    CsvWriter writer = new CsvWriter(settings);

    assertEquals(
        writer.writeRowToString(new String[] {"my 'precious' value"}), "'my ''precious'' value'");
    assertEquals(writer.writeRowToString(new String[] {"'"}), "''''");
    assertEquals(writer.writeRowToString(new String[] {" '"}), "' '''");
    assertEquals(writer.writeRowToString(new String[] {" ' "}), "' '' '");
  }
Ejemplo n.º 5
0
  @Test
  public void testWriteToString() throws Exception {
    CsvWriterSettings settings = new CsvWriterSettings();
    settings.getFormat().setLineSeparator("\r\n");
    settings.setIgnoreTrailingWhitespaces(true);

    CsvWriter writer = new CsvWriter(settings);
    String result = writer.writeRowToString(new Object[] {1, "Line1\nLine2 "});

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

    assertEquals(result, expected);
  }
  @Test
  public void testQuotationTriggers() {
    CsvWriterSettings settings = new CsvWriterSettings();
    settings.trimValues(false);
    settings.getFormat().setLineSeparator("\n");
    settings.getFormat().setQuote('\'');
    settings.getFormat().setQuoteEscape('\'');
    settings.getFormat().setCharToEscapeQuoteEscaping('\'');
    settings.setQuotationTriggers(' ', '\t', 'Z');
    settings.setQuoteEscapingEnabled(false);

    CsvWriter writer = new CsvWriter(settings);

    assertEquals(
        writer.writeRowToString(new String[] {"my 'precious' value"}),
        "'my ''precious'' value'"); // quotes because of the spaces
    assertEquals(
        writer.writeRowToString(new String[] {"my'precious'value"}),
        "my'precious'value"); // no triggers here, no quotation applied
    assertEquals(writer.writeRowToString(new String[] {"lulz"}), "lulz");
    assertEquals(
        writer.writeRowToString(new String[] {"lulZ"}), "'lulZ'"); // uppercase Z is a trigger
    assertEquals(writer.writeRowToString(new String[] {"I'm\ta\tTSV!"}), "'I''m\ta\tTSV!'");
  }
Ejemplo n.º 7
0
  @Test(dataProvider = "escapeHandlingParameterProvider")
  public void testHandlingOfEscapeSequences(
      boolean inputEscaped, boolean escapeUnquoted, String expected1, String expected2)
      throws Exception {
    CsvWriterSettings settings = new CsvWriterSettings();
    settings.setInputEscaped(inputEscaped);
    settings.setEscapeUnquotedValues(escapeUnquoted);
    settings.getFormat().setCharToEscapeQuoteEscaping('|');
    settings.getFormat().setQuoteEscape('|');

    String[] line1 = new String[] {"A|\""};
    String[] line2 = new String[] {",B|\""}; // will quote because of the column separator

    CsvWriter writer = new CsvWriter(settings);
    String result1 = writer.writeRowToString(line1);
    String result2 = writer.writeRowToString(line2);

    // System.out.println(result1);
    // System.out.println(result2);
    assertEquals(result1, expected1);
    assertEquals(result2, expected2);
  }