StringUtil { public static CharSequence escapeCsv(CharSequence value) { return escapeCsv(value, false); } private  StringUtil(); static String substringAfter(String value, char delim); static boolean commonSuffixOfLength(String s, String p, int len); static String byteToHexStringPadded(int value); static T byteToHexStringPadded(T buf, int value); static String toHexStringPadded(byte[] src); static String toHexStringPadded(byte[] src, int offset, int length); static T toHexStringPadded(T dst, byte[] src); static T toHexStringPadded(T dst, byte[] src, int offset, int length); static String byteToHexString(int value); static T byteToHexString(T buf, int value); static String toHexString(byte[] src); static String toHexString(byte[] src, int offset, int length); static T toHexString(T dst, byte[] src); static T toHexString(T dst, byte[] src, int offset, int length); static int decodeHexNibble(final char c); static byte decodeHexByte(CharSequence s, int pos); static byte[] decodeHexDump(CharSequence hexDump, int fromIndex, int length); static byte[] decodeHexDump(CharSequence hexDump); static String simpleClassName(Object o); static String simpleClassName(Class<?> clazz); static CharSequence escapeCsv(CharSequence value); static CharSequence escapeCsv(CharSequence value, boolean trimWhiteSpace); static CharSequence unescapeCsv(CharSequence value); static List<CharSequence> unescapeCsvFields(CharSequence value); static int length(String s); static boolean isNullOrEmpty(String s); static int indexOfNonWhiteSpace(CharSequence seq, int offset); static boolean isSurrogate(char c); static boolean endsWith(CharSequence s, char c); static CharSequence trimOws(CharSequence value); static final String EMPTY_STRING; static final String NEWLINE; static final char DOUBLE_QUOTE; static final char COMMA; static final char LINE_FEED; static final char CARRIAGE_RETURN; static final char TAB; static final char SPACE; }
@Test (expected = NullPointerException.class) public void escapeCsvNull() { StringUtil.escapeCsv(null); }
@Test public void escapeCsvEmpty() { CharSequence value = ""; CharSequence expected = value; escapeCsv(value, expected); }
@Test public void escapeCsvUnquoted() { CharSequence value = "something"; CharSequence expected = value; escapeCsv(value, expected); }
@Test public void escapeCsvAlreadyQuoted() { CharSequence value = "\"something\""; CharSequence expected = "\"something\""; escapeCsv(value, expected); }
@Test public void escapeCsvWithQuote() { CharSequence value = "s\""; CharSequence expected = "\"s\"\"\""; escapeCsv(value, expected); }
@Test public void escapeCsvWithQuoteInMiddle() { CharSequence value = "some text\"and more text"; CharSequence expected = "\"some text\"\"and more text\""; escapeCsv(value, expected); }
@Test public void escapeCsvWithQuoteInMiddleAlreadyQuoted() { CharSequence value = "\"some text\"and more text\""; CharSequence expected = "\"some text\"\"and more text\""; escapeCsv(value, expected); }
@Test public void escapeCsvWithQuotedWords() { CharSequence value = "\"foo\"\"goo\""; CharSequence expected = "\"foo\"\"goo\""; escapeCsv(value, expected); }
@Test public void escapeCsvWithAlreadyEscapedQuote() { CharSequence value = "foo\"\"goo"; CharSequence expected = "foo\"\"goo"; escapeCsv(value, expected); }
@Test public void escapeCsvEndingWithQuote() { CharSequence value = "some\""; CharSequence expected = "\"some\"\"\""; escapeCsv(value, expected); }
@Test public void escapeCsvWithSingleQuote() { CharSequence value = "\""; CharSequence expected = "\"\"\"\""; escapeCsv(value, expected); }
@Test public void escapeCsvWithSingleQuoteAndCharacter() { CharSequence value = "\"f"; CharSequence expected = "\"\"\"f\""; escapeCsv(value, expected); }
@Test public void escapeCsvAlreadyEscapedQuote() { CharSequence value = "\"some\"\""; CharSequence expected = "\"some\"\"\""; escapeCsv(value, expected); }
@Test public void escapeCsvQuoted() { CharSequence value = "\"foo,goo\""; CharSequence expected = value; escapeCsv(value, expected); }
@Test public void escapeCsvWithLineFeed() { CharSequence value = "some text\n more text"; CharSequence expected = "\"some text\n more text\""; escapeCsv(value, expected); }
@Test public void escapeCsvWithSingleLineFeedCharacter() { CharSequence value = "\n"; CharSequence expected = "\"\n\""; escapeCsv(value, expected); }
@Test public void escapeCsvWithMultipleLineFeedCharacter() { CharSequence value = "\n\n"; CharSequence expected = "\"\n\n\""; escapeCsv(value, expected); }
@Test public void escapeCsvWithQuotedAndLineFeedCharacter() { CharSequence value = " \" \n "; CharSequence expected = "\" \"\" \n \""; escapeCsv(value, expected); }
@Test public void escapeCsvWithLineFeedAtEnd() { CharSequence value = "testing\n"; CharSequence expected = "\"testing\n\""; escapeCsv(value, expected); }
@Test public void escapeCsvWithComma() { CharSequence value = "test,ing"; CharSequence expected = "\"test,ing\""; escapeCsv(value, expected); }
@Test public void escapeCsvWithSingleComma() { CharSequence value = ","; CharSequence expected = "\",\""; escapeCsv(value, expected); }
@Test public void escapeCsvWithSingleCarriageReturn() { CharSequence value = "\r"; CharSequence expected = "\"\r\""; escapeCsv(value, expected); }
@Test public void escapeCsvWithMultipleCarriageReturn() { CharSequence value = "\r\r"; CharSequence expected = "\"\r\r\""; escapeCsv(value, expected); }
@Test public void escapeCsvWithCarriageReturn() { CharSequence value = "some text\r more text"; CharSequence expected = "\"some text\r more text\""; escapeCsv(value, expected); }
@Test public void escapeCsvWithQuotedAndCarriageReturnCharacter() { CharSequence value = "\"\r"; CharSequence expected = "\"\"\"\r\""; escapeCsv(value, expected); }
@Test public void escapeCsvWithCarriageReturnAtEnd() { CharSequence value = "testing\r"; CharSequence expected = "\"testing\r\""; escapeCsv(value, expected); }
@Test public void escapeCsvWithCRLFCharacter() { CharSequence value = "\r\n"; CharSequence expected = "\"\r\n\""; escapeCsv(value, expected); }
@Test public void escapeCsvWithTrimming() { assertSame("", StringUtil.escapeCsv("", true)); assertSame("ab", StringUtil.escapeCsv("ab", true)); escapeCsvWithTrimming("", ""); escapeCsvWithTrimming(" \t ", ""); escapeCsvWithTrimming("ab", "ab"); escapeCsvWithTrimming("a b", "a b"); escapeCsvWithTrimming(" \ta \tb", "a \tb"); escapeCsvWithTrimming("a \tb \t", "a \tb"); escapeCsvWithTrimming("\t a \tb \t", "a \tb"); escapeCsvWithTrimming("\"\t a b \"", "\"\t a b \""); escapeCsvWithTrimming(" \"\t a b \"\t", "\"\t a b \""); escapeCsvWithTrimming(" testing\t\n ", "\"testing\t\n\""); escapeCsvWithTrimming("\ttest,ing ", "\"test,ing\""); }
@Test public void escapeCsvGarbageFree() { assertSame("1", StringUtil.escapeCsv("1", true)); assertSame(" 123 ", StringUtil.escapeCsv(" 123 ", false)); assertSame("\" 123 \"", StringUtil.escapeCsv("\" 123 \"", true)); assertSame("\"\"", StringUtil.escapeCsv("\"\"", true)); assertSame("123 \"\"", StringUtil.escapeCsv("123 \"\"", true)); assertSame("123\"\"321", StringUtil.escapeCsv("123\"\"321", true)); assertSame("\"123\"\"321\"", StringUtil.escapeCsv("\"123\"\"321\"", true)); }