### Question:
GreenplumCSV { public String toCsvField(String s, boolean prependQuoteChar, boolean appendQuoteChar) { return toCsvField(s, prependQuoteChar, appendQuoteChar, false); } GreenplumCSV(); String getValueOfNull(); char getQuote(); Character getEscape(); String getNewline(); Character getDelimiter(); GreenplumCSV withValueOfNull(String valueOfNull); GreenplumCSV withQuoteChar(String quoteString); GreenplumCSV withQuoteChar(char quoteChar); GreenplumCSV withEscapeChar(String escapeString); GreenplumCSV withEscapeChar(char escapeChar); GreenplumCSV withNewline(String newline); GreenplumCSV withDelimiter(String delimiterString); GreenplumCSV withDelimiter(char delimiterChar); String toCsvField(String s, boolean prependQuoteChar, boolean appendQuoteChar); String toCsvField(String s,
                             boolean prependQuoteChar,
                             boolean appendQuoteChar,
                             boolean skipIfQuotingIsNotNeeded);  }### Answer:
@Test public void testToCsvFieldNull() { assertNull(gpCSV.toCsvField(null, false, false)); assertNull(gpCSV.toCsvField(null, false, true)); assertNull(gpCSV.toCsvField(null, true, false)); assertNull(gpCSV.toCsvField(null, true, true)); }
@Test public void testToCsvFieldEmptyString() { String input = ""; assertSame(input, gpCSV.toCsvField(input, false, false)); assertEquals("\"" + input, gpCSV.toCsvField(input, true, false)); assertEquals(input + "\"", gpCSV.toCsvField(input, false, true)); assertEquals("\"" + input + "\"", gpCSV.toCsvField(input, true, true)); }
@Test public void testToCsvFieldStringWithCharacterThatNeedToBeQuoted() { String input1 = "a"; assertSame(input1, gpCSV.toCsvField(input1, true, true, true)); String input2 = "\""; assertEquals("\"\"\"\"", gpCSV.toCsvField(input2, true, true, true)); String input3 = ","; assertEquals("\",\"", gpCSV.toCsvField(input3, true, true, true)); String input4 = "\n\",sample"; assertEquals("\"\n\"\",sample\"", gpCSV.toCsvField(input4, true, true, true)); }
@Test public void testToCsvDoesNotAddPrefixAndSuffixWhenSkipIfQuotingIsNotNeeded() { String input = "aábcdefghijklmnñopqrstuvwxyz"; assertSame(input, gpCSV.toCsvField(input, false, false, true)); assertSame(input, gpCSV.toCsvField(input, false, true, true)); assertSame(input, gpCSV.toCsvField(input, true, false, true)); assertSame(input, gpCSV.toCsvField(input, true, true, true)); }
@Test public void testToCsvFieldEscapesQuotes() { String input = "{\"key\": \"value\", \"foo\": \"bar\"}"; String expected = "{\"\"key\"\": \"\"value\"\", \"\"foo\"\": \"\"bar\"\"}"; assertEquals(expected, gpCSV.toCsvField(input, false, false)); assertEquals("\"" + expected, gpCSV.toCsvField(input, true, false)); assertEquals(expected + "\"", gpCSV.toCsvField(input, false, true)); assertEquals("\"" + expected + "\"", gpCSV.toCsvField(input, true, true)); }