@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[] {" ' "}), "' '' '"); }
@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!'"); }