blob: d9de99f254ee7df6d680259b92463e718ca18124 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.csv;
import static org.apache.commons.csv.CSVFormat.RFC4180;
import static org.apache.commons.csv.Constants.CR;
import static org.apache.commons.csv.Constants.CRLF;
import static org.apache.commons.csv.Constants.LF;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Reader;
import java.io.IOException;
import java.io.StringReader;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import org.junit.jupiter.api.Test;
/**
* Tests {@link CSVFormat}.
*/
public class CSVFormatTest {
public enum EmptyEnum {
// empty enum.
}
public enum Header {
Name, Email, Phone
}
private static void assertNotEquals(final Object right, final Object left) {
assertFalse(right.equals(left));
assertFalse(left.equals(right));
}
private static CSVFormat copy(final CSVFormat format) {
return format.withDelimiter(format.getDelimiter());
}
private void assertNotEquals(final String name, final String type, final Object left, final Object right) {
if (left.equals(right) || right.equals(left)) {
fail("Objects must not compare equal for " + name + "(" + type + ")");
}
if (left.hashCode() == right.hashCode()) {
fail("Hash code should not be equal for " + name + "(" + type + ")");
}
}
@Test
public void testDelimiterSameAsCommentStartThrowsException() {
assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withDelimiter('!').withCommentMarker('!'));
}
@Test
public void testDelimiterSameAsEscapeThrowsException() {
assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withDelimiter('!').withEscape('!'));
}
@Test
public void testDuplicateHeaderElements() {
final String[] header = { "A", "A" };
final CSVFormat format = CSVFormat.DEFAULT.withHeader(header);
assertEquals(2, format.getHeader().length);
assertArrayEquals(header, format.getHeader());
}
@Test
public void testDuplicateHeaderElementsFalse() {
assertThrows(
IllegalArgumentException.class,
() -> CSVFormat.DEFAULT.withAllowDuplicateHeaderNames(false).withHeader("A", "A"));
}
public void testDuplicateHeaderElementsTrue() {
CSVFormat.DEFAULT.withAllowDuplicateHeaderNames(true).withHeader("A", "A");
}
@Test
public void testEquals() {
final CSVFormat right = CSVFormat.DEFAULT;
final CSVFormat left = copy(right);
assertFalse(right.equals(null));
assertFalse(right.equals("A String Instance"));
assertEquals(right, right);
assertEquals(right, left);
assertEquals(left, right);
assertEquals(right.hashCode(), right.hashCode());
assertEquals(right.hashCode(), left.hashCode());
}
@Test
public void testEqualsCommentStart() {
final CSVFormat right = CSVFormat.newFormat('\'')
.withQuote('"')
.withCommentMarker('#')
.withQuoteMode(QuoteMode.ALL);
final CSVFormat left = right
.withCommentMarker('!');
assertNotEquals(right, left);
}
@Test
public void testEqualsDelimiter() {
final CSVFormat right = CSVFormat.newFormat('!');
final CSVFormat left = CSVFormat.newFormat('?');
assertNotEquals(right, left);
}
@Test
public void testEqualsEscape() {
final CSVFormat right = CSVFormat.newFormat('\'')
.withQuote('"')
.withCommentMarker('#')
.withEscape('+')
.withQuoteMode(QuoteMode.ALL);
final CSVFormat left = right
.withEscape('!');
assertNotEquals(right, left);
}
@Test
public void testEqualsHash() throws Exception {
final Method[] methods = CSVFormat.class.getDeclaredMethods();
for (final Method method : methods) {
if (Modifier.isPublic(method.getModifiers())) {
final String name = method.getName();
if (name.startsWith("with")) {
for (final Class<?> cls : method.getParameterTypes()) {
final String type = cls.getCanonicalName();
if ("boolean".equals(type)) {
final Object defTrue = method.invoke(CSVFormat.DEFAULT, Boolean.TRUE);
final Object defFalse = method.invoke(CSVFormat.DEFAULT, Boolean.FALSE);
assertNotEquals(name, type ,defTrue, defFalse);
} else if ("char".equals(type)){
final Object a = method.invoke(CSVFormat.DEFAULT, 'a');
final Object b = method.invoke(CSVFormat.DEFAULT, 'b');
assertNotEquals(name, type, a, b);
} else if ("java.lang.Character".equals(type)){
final Object a = method.invoke(CSVFormat.DEFAULT, new Object[] {null});
final Object b = method.invoke(CSVFormat.DEFAULT, new Character('d'));
assertNotEquals(name, type, a, b);
} else if ("java.lang.String".equals(type)){
final Object a = method.invoke(CSVFormat.DEFAULT, new Object[] {null});
final Object b = method.invoke(CSVFormat.DEFAULT, "e");
assertNotEquals(name, type, a, b);
} else if ("java.lang.String[]".equals(type)){
final Object a = method.invoke(CSVFormat.DEFAULT, new Object[] {new String[] {null, null}});
final Object b = method.invoke(CSVFormat.DEFAULT, new Object[] {new String[] {"f", "g"}});
assertNotEquals(name, type, a, b);
} else if ("org.apache.commons.csv.QuoteMode".equals(type)){
final Object a = method.invoke(CSVFormat.DEFAULT, QuoteMode.MINIMAL);
final Object b = method.invoke(CSVFormat.DEFAULT, QuoteMode.ALL);
assertNotEquals(name, type, a, b);
} else if ("java.lang.Object[]".equals(type)){
final Object a = method.invoke(CSVFormat.DEFAULT, new Object[] {new Object[] {null, null}});
final Object b = method.invoke(CSVFormat.DEFAULT, new Object[] {new Object[] {new Object(), new Object()}});
assertNotEquals(name, type, a, b);
} else if ("withHeader".equals(name)){ // covered above by String[]
// ignored
} else {
fail("Unhandled method: "+name + "(" + type + ")");
}
}
}
}
}
}
@Test
public void testEqualsHeader() {
final CSVFormat right = CSVFormat.newFormat('\'')
.withRecordSeparator(CR)
.withCommentMarker('#')
.withEscape('+')
.withHeader("One", "Two", "Three")
.withIgnoreEmptyLines()
.withIgnoreSurroundingSpaces()
.withQuote('"')
.withQuoteMode(QuoteMode.ALL);
final CSVFormat left = right
.withHeader("Three", "Two", "One");
assertNotEquals(right, left);
}
@Test
public void testEqualsIgnoreEmptyLines() {
final CSVFormat right = CSVFormat.newFormat('\'')
.withCommentMarker('#')
.withEscape('+')
.withIgnoreEmptyLines()
.withIgnoreSurroundingSpaces()
.withQuote('"')
.withQuoteMode(QuoteMode.ALL);
final CSVFormat left = right
.withIgnoreEmptyLines(false);
assertNotEquals(right, left);
}
@Test
public void testEqualsIgnoreSurroundingSpaces() {
final CSVFormat right = CSVFormat.newFormat('\'')
.withCommentMarker('#')
.withEscape('+')
.withIgnoreSurroundingSpaces()
.withQuote('"')
.withQuoteMode(QuoteMode.ALL);
final CSVFormat left = right
.withIgnoreSurroundingSpaces(false);
assertNotEquals(right, left);
}
@Test
public void testEqualsLeftNoQuoteRightQuote() {
final CSVFormat left = CSVFormat.newFormat(',').withQuote(null);
final CSVFormat right = left.withQuote('#');
assertNotEquals(left, right);
}
@Test
public void testEqualsNoQuotes() {
final CSVFormat left = CSVFormat.newFormat(',').withQuote(null);
final CSVFormat right = left.withQuote(null);
assertEquals(left, right);
}
@Test
public void testEqualsNullString() {
final CSVFormat right = CSVFormat.newFormat('\'')
.withRecordSeparator(CR)
.withCommentMarker('#')
.withEscape('+')
.withIgnoreEmptyLines()
.withIgnoreSurroundingSpaces()
.withQuote('"')
.withQuoteMode(QuoteMode.ALL)
.withNullString("null");
final CSVFormat left = right
.withNullString("---");
assertNotEquals(right, left);
}
@Test
public void testEqualsOne() {
final CSVFormat csvFormatOne = CSVFormat.INFORMIX_UNLOAD;
final CSVFormat csvFormatTwo = CSVFormat.MYSQL;
assertEquals('\\', (char)csvFormatOne.getEscapeCharacter());
assertNull(csvFormatOne.getQuoteMode());
assertTrue(csvFormatOne.getIgnoreEmptyLines());
assertFalse(csvFormatOne.getSkipHeaderRecord());
assertFalse(csvFormatOne.getIgnoreHeaderCase());
assertNull(csvFormatOne.getCommentMarker());
assertFalse(csvFormatOne.isCommentMarkerSet());
assertTrue(csvFormatOne.isQuoteCharacterSet());
assertEquals('|', csvFormatOne.getDelimiter());
assertFalse(csvFormatOne.getAllowMissingColumnNames());
assertTrue(csvFormatOne.isEscapeCharacterSet());
assertEquals("\n", csvFormatOne.getRecordSeparator());
assertEquals('\"', (char)csvFormatOne.getQuoteCharacter());
assertFalse(csvFormatOne.getTrailingDelimiter());
assertFalse(csvFormatOne.getTrim());
assertFalse(csvFormatOne.isNullStringSet());
assertNull(csvFormatOne.getNullString());
assertFalse(csvFormatOne.getIgnoreSurroundingSpaces());
assertTrue(csvFormatTwo.isEscapeCharacterSet());
assertNull(csvFormatTwo.getQuoteCharacter());
assertFalse(csvFormatTwo.getAllowMissingColumnNames());
assertEquals(QuoteMode.ALL_NON_NULL, csvFormatTwo.getQuoteMode());
assertEquals('\t', csvFormatTwo.getDelimiter());
assertEquals("\n", csvFormatTwo.getRecordSeparator());
assertFalse(csvFormatTwo.isQuoteCharacterSet());
assertTrue(csvFormatTwo.isNullStringSet());
assertEquals('\\', (char)csvFormatTwo.getEscapeCharacter());
assertFalse(csvFormatTwo.getIgnoreHeaderCase());
assertFalse(csvFormatTwo.getTrim());
assertFalse(csvFormatTwo.getIgnoreEmptyLines());
assertEquals("\\N", csvFormatTwo.getNullString());
assertFalse(csvFormatTwo.getIgnoreSurroundingSpaces());
assertFalse(csvFormatTwo.getTrailingDelimiter());
assertFalse(csvFormatTwo.getSkipHeaderRecord());
assertNull(csvFormatTwo.getCommentMarker());
assertFalse(csvFormatTwo.isCommentMarkerSet());
assertNotSame(csvFormatTwo, csvFormatOne);
assertFalse(csvFormatTwo.equals(csvFormatOne));
assertEquals('\\', (char)csvFormatOne.getEscapeCharacter());
assertNull(csvFormatOne.getQuoteMode());
assertTrue(csvFormatOne.getIgnoreEmptyLines());
assertFalse(csvFormatOne.getSkipHeaderRecord());
assertFalse(csvFormatOne.getIgnoreHeaderCase());
assertNull(csvFormatOne.getCommentMarker());
assertFalse(csvFormatOne.isCommentMarkerSet());
assertTrue(csvFormatOne.isQuoteCharacterSet());
assertEquals('|', csvFormatOne.getDelimiter());
assertFalse(csvFormatOne.getAllowMissingColumnNames());
assertTrue(csvFormatOne.isEscapeCharacterSet());
assertEquals("\n", csvFormatOne.getRecordSeparator());
assertEquals('\"', (char)csvFormatOne.getQuoteCharacter());
assertFalse(csvFormatOne.getTrailingDelimiter());
assertFalse(csvFormatOne.getTrim());
assertFalse(csvFormatOne.isNullStringSet());
assertNull(csvFormatOne.getNullString());
assertFalse(csvFormatOne.getIgnoreSurroundingSpaces());
assertTrue(csvFormatTwo.isEscapeCharacterSet());
assertNull(csvFormatTwo.getQuoteCharacter());
assertFalse(csvFormatTwo.getAllowMissingColumnNames());
assertEquals(QuoteMode.ALL_NON_NULL, csvFormatTwo.getQuoteMode());
assertEquals('\t', csvFormatTwo.getDelimiter());
assertEquals("\n", csvFormatTwo.getRecordSeparator());
assertFalse(csvFormatTwo.isQuoteCharacterSet());
assertTrue(csvFormatTwo.isNullStringSet());
assertEquals('\\', (char)csvFormatTwo.getEscapeCharacter());
assertFalse(csvFormatTwo.getIgnoreHeaderCase());
assertFalse(csvFormatTwo.getTrim());
assertFalse(csvFormatTwo.getIgnoreEmptyLines());
assertEquals("\\N", csvFormatTwo.getNullString());
assertFalse(csvFormatTwo.getIgnoreSurroundingSpaces());
assertFalse(csvFormatTwo.getTrailingDelimiter());
assertFalse(csvFormatTwo.getSkipHeaderRecord());
assertNull(csvFormatTwo.getCommentMarker());
assertFalse(csvFormatTwo.isCommentMarkerSet());
assertNotSame(csvFormatOne, csvFormatTwo);
assertNotSame(csvFormatTwo, csvFormatOne);
assertFalse(csvFormatOne.equals(csvFormatTwo));
assertFalse(csvFormatTwo.equals(csvFormatOne));
assertFalse(csvFormatTwo.equals(csvFormatOne));
}
@Test
public void testEqualsQuoteChar() {
final CSVFormat right = CSVFormat.newFormat('\'').withQuote('"');
final CSVFormat left = right.withQuote('!');
assertNotEquals(right, left);
}
@Test
public void testEqualsQuotePolicy() {
final CSVFormat right = CSVFormat.newFormat('\'')
.withQuote('"')
.withQuoteMode(QuoteMode.ALL);
final CSVFormat left = right
.withQuoteMode(QuoteMode.MINIMAL);
assertNotEquals(right, left);
}
@Test
public void testEqualsRecordSeparator() {
final CSVFormat right = CSVFormat.newFormat('\'')
.withRecordSeparator(CR)
.withCommentMarker('#')
.withEscape('+')
.withIgnoreEmptyLines()
.withIgnoreSurroundingSpaces()
.withQuote('"')
.withQuoteMode(QuoteMode.ALL);
final CSVFormat left = right
.withRecordSeparator(LF);
assertNotEquals(right, left);
}
@Test
public void testEqualsSkipHeaderRecord() {
final CSVFormat right = CSVFormat.newFormat('\'')
.withRecordSeparator(CR)
.withCommentMarker('#')
.withEscape('+')
.withIgnoreEmptyLines()
.withIgnoreSurroundingSpaces()
.withQuote('"')
.withQuoteMode(QuoteMode.ALL)
.withNullString("null")
.withSkipHeaderRecord();
final CSVFormat left = right
.withSkipHeaderRecord(false);
assertNotEquals(right, left);
}
@Test
public void testEqualsWithNull() {
final CSVFormat csvFormat = CSVFormat.POSTGRESQL_TEXT;
assertEquals('\\', (char)csvFormat.getEscapeCharacter());
assertFalse(csvFormat.getIgnoreSurroundingSpaces());
assertFalse(csvFormat.getTrailingDelimiter());
assertFalse(csvFormat.getTrim());
assertTrue(csvFormat.isQuoteCharacterSet());
assertEquals("\\N", csvFormat.getNullString());
assertFalse(csvFormat.getIgnoreHeaderCase());
assertTrue(csvFormat.isEscapeCharacterSet());
assertFalse(csvFormat.isCommentMarkerSet());
assertNull(csvFormat.getCommentMarker());
assertFalse(csvFormat.getAllowMissingColumnNames());
assertEquals(QuoteMode.ALL_NON_NULL, csvFormat.getQuoteMode());
assertEquals('\t', csvFormat.getDelimiter());
assertFalse(csvFormat.getSkipHeaderRecord());
assertEquals("\n", csvFormat.getRecordSeparator());
assertFalse(csvFormat.getIgnoreEmptyLines());
assertEquals('\"', (char)csvFormat.getQuoteCharacter());
assertTrue(csvFormat.isNullStringSet());
assertEquals('\\', (char)csvFormat.getEscapeCharacter());
assertFalse(csvFormat.getIgnoreSurroundingSpaces());
assertFalse(csvFormat.getTrailingDelimiter());
assertFalse(csvFormat.getTrim());
assertTrue(csvFormat.isQuoteCharacterSet());
assertEquals("\\N", csvFormat.getNullString());
assertFalse(csvFormat.getIgnoreHeaderCase());
assertTrue(csvFormat.isEscapeCharacterSet());
assertFalse(csvFormat.isCommentMarkerSet());
assertNull(csvFormat.getCommentMarker());
assertFalse(csvFormat.getAllowMissingColumnNames());
assertEquals(QuoteMode.ALL_NON_NULL, csvFormat.getQuoteMode());
assertEquals('\t', csvFormat.getDelimiter());
assertFalse(csvFormat.getSkipHeaderRecord());
assertEquals("\n", csvFormat.getRecordSeparator());
assertFalse(csvFormat.getIgnoreEmptyLines());
assertEquals('\"', (char)csvFormat.getQuoteCharacter());
assertTrue(csvFormat.isNullStringSet());
assertFalse(csvFormat.equals( null));
}
@Test
public void testEscapeSameAsCommentStartThrowsException() {
assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withEscape('!').withCommentMarker('!'));
}
@Test
public void testEscapeSameAsCommentStartThrowsExceptionForWrapperType() {
// Cannot assume that callers won't use different Character objects
assertThrows(
IllegalArgumentException.class,
() -> CSVFormat.DEFAULT.withEscape(Character.valueOf('!')).withCommentMarker(new Character('!')));
}
@Test
public void testFormat() {
final CSVFormat format = CSVFormat.DEFAULT;
assertEquals("", format.format());
assertEquals("a,b,c", format.format("a", "b", "c"));
assertEquals("\"x,y\",z", format.format("x,y", "z"));
}
@Test //I assume this to be a defect.
public void testFormatThrowsNullPointerException() {
final CSVFormat csvFormat = CSVFormat.MYSQL;
final NullPointerException e = assertThrows(NullPointerException.class, () -> csvFormat.format((Object[]) null));
assertEquals(CSVFormat.class.getName(), e.getStackTrace()[0].getClassName());
}
@Test
public void testGetHeader() {
final String[] header = new String[]{"one", "two", "three"};
final CSVFormat formatWithHeader = CSVFormat.DEFAULT.withHeader(header);
// getHeader() makes a copy of the header array.
final String[] headerCopy = formatWithHeader.getHeader();
headerCopy[0] = "A";
headerCopy[1] = "B";
headerCopy[2] = "C";
assertFalse(Arrays.equals(formatWithHeader.getHeader(), headerCopy));
assertNotSame(formatWithHeader.getHeader(), headerCopy);
}
@Test
public void testHashCodeAndWithIgnoreHeaderCase() {
final CSVFormat csvFormat = CSVFormat.INFORMIX_UNLOAD_CSV;
final CSVFormat csvFormatTwo = csvFormat.withIgnoreHeaderCase();
csvFormatTwo.hashCode();
assertFalse(csvFormat.getIgnoreHeaderCase());
assertTrue(csvFormatTwo.getIgnoreHeaderCase()); // now different
assertFalse(csvFormatTwo.getTrailingDelimiter());
assertFalse(csvFormatTwo.equals(csvFormat)); // CSV-244 - should not be equal
assertFalse(csvFormatTwo.getAllowMissingColumnNames());
assertFalse(csvFormatTwo.getTrim());
}
@Test
public void testJiraCsv236() {
CSVFormat.DEFAULT.withAllowDuplicateHeaderNames().withHeader("CC","VV","VV");
}
@Test
public void testNewFormat() {
final CSVFormat csvFormat = CSVFormat.newFormat('X');
assertFalse(csvFormat.getSkipHeaderRecord());
assertFalse(csvFormat.isEscapeCharacterSet());
assertNull(csvFormat.getRecordSeparator());
assertNull(csvFormat.getQuoteMode());
assertNull(csvFormat.getCommentMarker());
assertFalse(csvFormat.getIgnoreHeaderCase());
assertFalse(csvFormat.getAllowMissingColumnNames());
assertFalse(csvFormat.getTrim());
assertFalse(csvFormat.isNullStringSet());
assertNull(csvFormat.getEscapeCharacter());
assertFalse(csvFormat.getIgnoreSurroundingSpaces());
assertFalse(csvFormat.getTrailingDelimiter());
assertEquals('X', csvFormat.getDelimiter());
assertNull(csvFormat.getNullString());
assertFalse(csvFormat.isQuoteCharacterSet());
assertFalse(csvFormat.isCommentMarkerSet());
assertNull(csvFormat.getQuoteCharacter());
assertFalse(csvFormat.getIgnoreEmptyLines());
assertFalse(csvFormat.getSkipHeaderRecord());
assertFalse(csvFormat.isEscapeCharacterSet());
assertNull(csvFormat.getRecordSeparator());
assertNull(csvFormat.getQuoteMode());
assertNull(csvFormat.getCommentMarker());
assertFalse(csvFormat.getIgnoreHeaderCase());
assertFalse(csvFormat.getAllowMissingColumnNames());
assertFalse(csvFormat.getTrim());
assertFalse(csvFormat.isNullStringSet());
assertNull(csvFormat.getEscapeCharacter());
assertFalse(csvFormat.getIgnoreSurroundingSpaces());
assertFalse(csvFormat.getTrailingDelimiter());
assertEquals('X', csvFormat.getDelimiter());
assertNull(csvFormat.getNullString());
assertFalse(csvFormat.isQuoteCharacterSet());
assertFalse(csvFormat.isCommentMarkerSet());
assertNull(csvFormat.getQuoteCharacter());
assertFalse(csvFormat.getIgnoreEmptyLines());
}
@Test
public void testNullRecordSeparatorCsv106() {
final CSVFormat format = CSVFormat.newFormat(';').withSkipHeaderRecord().withHeader("H1", "H2");
final String formatStr = format.format("A", "B");
assertNotNull(formatStr);
assertFalse(formatStr.endsWith("null"));
}
@Test
public void testQuoteCharSameAsCommentStartThrowsException() {
assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withQuote('!').withCommentMarker('!'));
}
@Test
public void testQuoteCharSameAsCommentStartThrowsExceptionForWrapperType() {
// Cannot assume that callers won't use different Character objects
assertThrows(
IllegalArgumentException.class,
() -> CSVFormat.DEFAULT.withQuote(Character.valueOf('!')).withCommentMarker('!'));
}
@Test
public void testQuoteCharSameAsDelimiterThrowsException() {
assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withQuote('!').withDelimiter('!'));
}
@Test
public void testQuotePolicyNoneWithoutEscapeThrowsException() {
assertThrows(IllegalArgumentException.class, () -> CSVFormat.newFormat('!').withQuoteMode(QuoteMode.NONE));
}
@Test
public void testRFC4180() {
assertEquals(null, RFC4180.getCommentMarker());
assertEquals(',', RFC4180.getDelimiter());
assertEquals(null, RFC4180.getEscapeCharacter());
assertFalse(RFC4180.getIgnoreEmptyLines());
assertEquals(Character.valueOf('"'), RFC4180.getQuoteCharacter());
assertEquals(null, RFC4180.getQuoteMode());
assertEquals("\r\n", RFC4180.getRecordSeparator());
}
@SuppressWarnings("boxing") // no need to worry about boxing here
@Test
public void testSerialization() throws Exception {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
try (final ObjectOutputStream oos = new ObjectOutputStream(out)) {
oos.writeObject(CSVFormat.DEFAULT);
oos.flush();
}
final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(out.toByteArray()));
final CSVFormat format = (CSVFormat) in.readObject();
assertNotNull(format);
assertEquals(CSVFormat.DEFAULT.getDelimiter(), format.getDelimiter(), "delimiter");
assertEquals(CSVFormat.DEFAULT.getQuoteCharacter(), format.getQuoteCharacter(), "encapsulator");
assertEquals(CSVFormat.DEFAULT.getCommentMarker(), format.getCommentMarker(), "comment start");
assertEquals(CSVFormat.DEFAULT.getRecordSeparator(), format.getRecordSeparator(), "record separator");
assertEquals(CSVFormat.DEFAULT.getEscapeCharacter(), format.getEscapeCharacter(), "escape");
assertEquals(CSVFormat.DEFAULT.getIgnoreSurroundingSpaces(), format.getIgnoreSurroundingSpaces(), "trim");
assertEquals(CSVFormat.DEFAULT.getIgnoreEmptyLines(), format.getIgnoreEmptyLines(), "empty lines");
}
@Test
public void testToString() {
final String string = CSVFormat.INFORMIX_UNLOAD.toString();
assertEquals("Delimiter=<|> Escape=<\\> QuoteChar=<\"> RecordSeparator=<\n> EmptyLines:ignored SkipHeaderRecord:false", string);
}
@Test
public void testToStringAndWithCommentMarkerTakingCharacter() {
final CSVFormat.Predefined csvFormat_Predefined = CSVFormat.Predefined.Default;
final CSVFormat csvFormat = csvFormat_Predefined.getFormat();
assertNull(csvFormat.getEscapeCharacter());
assertTrue(csvFormat.isQuoteCharacterSet());
assertFalse(csvFormat.getTrim());
assertFalse(csvFormat.getIgnoreSurroundingSpaces());
assertFalse(csvFormat.getTrailingDelimiter());
assertEquals(',', csvFormat.getDelimiter());
assertFalse(csvFormat.getIgnoreHeaderCase());
assertEquals("\r\n", csvFormat.getRecordSeparator());
assertFalse(csvFormat.isCommentMarkerSet());
assertNull(csvFormat.getCommentMarker());
assertFalse(csvFormat.isNullStringSet());
assertFalse(csvFormat.getAllowMissingColumnNames());
assertFalse(csvFormat.isEscapeCharacterSet());
assertFalse(csvFormat.getSkipHeaderRecord());
assertNull(csvFormat.getNullString());
assertNull(csvFormat.getQuoteMode());
assertTrue(csvFormat.getIgnoreEmptyLines());
assertEquals('\"', (char)csvFormat.getQuoteCharacter());
final Character character = Character.valueOf('n');
final CSVFormat csvFormatTwo = csvFormat.withCommentMarker(character);
assertNull(csvFormat.getEscapeCharacter());
assertTrue(csvFormat.isQuoteCharacterSet());
assertFalse(csvFormat.getTrim());
assertFalse(csvFormat.getIgnoreSurroundingSpaces());
assertFalse(csvFormat.getTrailingDelimiter());
assertEquals(',', csvFormat.getDelimiter());
assertFalse(csvFormat.getIgnoreHeaderCase());
assertEquals("\r\n", csvFormat.getRecordSeparator());
assertFalse(csvFormat.isCommentMarkerSet());
assertNull(csvFormat.getCommentMarker());
assertFalse(csvFormat.isNullStringSet());
assertFalse(csvFormat.getAllowMissingColumnNames());
assertFalse(csvFormat.isEscapeCharacterSet());
assertFalse(csvFormat.getSkipHeaderRecord());
assertNull(csvFormat.getNullString());
assertNull(csvFormat.getQuoteMode());
assertTrue(csvFormat.getIgnoreEmptyLines());
assertEquals('\"', (char)csvFormat.getQuoteCharacter());
assertFalse(csvFormatTwo.isNullStringSet());
assertFalse(csvFormatTwo.getAllowMissingColumnNames());
assertEquals('\"', (char)csvFormatTwo.getQuoteCharacter());
assertNull(csvFormatTwo.getNullString());
assertEquals(',', csvFormatTwo.getDelimiter());
assertFalse(csvFormatTwo.getTrailingDelimiter());
assertTrue(csvFormatTwo.isCommentMarkerSet());
assertFalse(csvFormatTwo.getIgnoreHeaderCase());
assertFalse(csvFormatTwo.getTrim());
assertNull(csvFormatTwo.getEscapeCharacter());
assertTrue(csvFormatTwo.isQuoteCharacterSet());
assertFalse(csvFormatTwo.getIgnoreSurroundingSpaces());
assertEquals("\r\n", csvFormatTwo.getRecordSeparator());
assertNull(csvFormatTwo.getQuoteMode());
assertEquals('n', (char)csvFormatTwo.getCommentMarker());
assertFalse(csvFormatTwo.getSkipHeaderRecord());
assertFalse(csvFormatTwo.isEscapeCharacterSet());
assertTrue(csvFormatTwo.getIgnoreEmptyLines());
assertNotSame(csvFormat, csvFormatTwo);
assertNotSame(csvFormatTwo, csvFormat);
assertFalse(csvFormatTwo.equals(csvFormat));
assertNull(csvFormat.getEscapeCharacter());
assertTrue(csvFormat.isQuoteCharacterSet());
assertFalse(csvFormat.getTrim());
assertFalse(csvFormat.getIgnoreSurroundingSpaces());
assertFalse(csvFormat.getTrailingDelimiter());
assertEquals(',', csvFormat.getDelimiter());
assertFalse(csvFormat.getIgnoreHeaderCase());
assertEquals("\r\n", csvFormat.getRecordSeparator());
assertFalse(csvFormat.isCommentMarkerSet());
assertNull(csvFormat.getCommentMarker());
assertFalse(csvFormat.isNullStringSet());
assertFalse(csvFormat.getAllowMissingColumnNames());
assertFalse(csvFormat.isEscapeCharacterSet());
assertFalse(csvFormat.getSkipHeaderRecord());
assertNull(csvFormat.getNullString());
assertNull(csvFormat.getQuoteMode());
assertTrue(csvFormat.getIgnoreEmptyLines());
assertEquals('\"', (char)csvFormat.getQuoteCharacter());
assertFalse(csvFormatTwo.isNullStringSet());
assertFalse(csvFormatTwo.getAllowMissingColumnNames());
assertEquals('\"', (char)csvFormatTwo.getQuoteCharacter());
assertNull(csvFormatTwo.getNullString());
assertEquals(',', csvFormatTwo.getDelimiter());
assertFalse(csvFormatTwo.getTrailingDelimiter());
assertTrue(csvFormatTwo.isCommentMarkerSet());
assertFalse(csvFormatTwo.getIgnoreHeaderCase());
assertFalse(csvFormatTwo.getTrim());
assertNull(csvFormatTwo.getEscapeCharacter());
assertTrue(csvFormatTwo.isQuoteCharacterSet());
assertFalse(csvFormatTwo.getIgnoreSurroundingSpaces());
assertEquals("\r\n", csvFormatTwo.getRecordSeparator());
assertNull(csvFormatTwo.getQuoteMode());
assertEquals('n', (char)csvFormatTwo.getCommentMarker());
assertFalse(csvFormatTwo.getSkipHeaderRecord());
assertFalse(csvFormatTwo.isEscapeCharacterSet());
assertTrue(csvFormatTwo.getIgnoreEmptyLines());
assertNotSame(csvFormat, csvFormatTwo);
assertNotSame(csvFormatTwo, csvFormat);
assertFalse(csvFormat.equals(csvFormatTwo));
assertFalse(csvFormatTwo.equals(csvFormat));
assertEquals("Delimiter=<,> QuoteChar=<\"> CommentStart=<n> " +
"RecordSeparator=<\r\n> EmptyLines:ignored SkipHeaderRecord:false"
, csvFormatTwo.toString());
}
@Test
public void testWithCommentStart() {
final CSVFormat formatWithCommentStart = CSVFormat.DEFAULT.withCommentMarker('#');
assertEquals( Character.valueOf('#'), formatWithCommentStart.getCommentMarker());
}
@Test
public void testWithCommentStartCRThrowsException() {
assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withCommentMarker(CR));
}
@Test
public void testWithDelimiter() {
final CSVFormat formatWithDelimiter = CSVFormat.DEFAULT.withDelimiter('!');
assertEquals('!', formatWithDelimiter.getDelimiter());
}
@Test
public void testWithDelimiterLFThrowsException() {
assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withDelimiter(LF));
}
@Test
public void testWithEmptyEnum() {
final CSVFormat formatWithHeader = CSVFormat.DEFAULT.withHeader(EmptyEnum.class);
assertTrue(formatWithHeader.getHeader().length == 0);
}
@Test
public void testWithEscape() {
final CSVFormat formatWithEscape = CSVFormat.DEFAULT.withEscape('&');
assertEquals(Character.valueOf('&'), formatWithEscape.getEscapeCharacter());
}
@Test
public void testWithEscapeCRThrowsExceptions() {
assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withEscape(CR));
}
@Test
public void testWithFirstRecordAsHeader() {
final CSVFormat formatWithFirstRecordAsHeader = CSVFormat.DEFAULT.withFirstRecordAsHeader();
assertTrue(formatWithFirstRecordAsHeader.getSkipHeaderRecord());
assertTrue(formatWithFirstRecordAsHeader.getHeader().length == 0);
}
@Test
public void testWithHeader() {
final String[] header = new String[]{"one", "two", "three"};
// withHeader() makes a copy of the header array.
final CSVFormat formatWithHeader = CSVFormat.DEFAULT.withHeader(header);
assertArrayEquals(header, formatWithHeader.getHeader());
assertNotSame(header, formatWithHeader.getHeader());
}
@Test
public void testWithHeaderComments() {
final CSVFormat csvFormat = CSVFormat.DEFAULT;
assertEquals('\"', (char)csvFormat.getQuoteCharacter());
assertFalse(csvFormat.isCommentMarkerSet());
assertFalse(csvFormat.isEscapeCharacterSet());
assertTrue(csvFormat.isQuoteCharacterSet());
assertFalse(csvFormat.getSkipHeaderRecord());
assertNull(csvFormat.getQuoteMode());
assertEquals(',', csvFormat.getDelimiter());
assertTrue(csvFormat.getIgnoreEmptyLines());
assertFalse(csvFormat.getIgnoreHeaderCase());
assertNull(csvFormat.getCommentMarker());
assertEquals("\r\n", csvFormat.getRecordSeparator());
assertFalse(csvFormat.getTrailingDelimiter());
assertFalse(csvFormat.getAllowMissingColumnNames());
assertFalse(csvFormat.getTrim());
assertFalse(csvFormat.isNullStringSet());
assertNull(csvFormat.getNullString());
assertFalse(csvFormat.getIgnoreSurroundingSpaces());
assertNull(csvFormat.getEscapeCharacter());
final Object[] objectArray = new Object[8];
final CSVFormat csvFormatTwo = csvFormat.withHeaderComments(objectArray);
assertEquals('\"', (char)csvFormat.getQuoteCharacter());
assertFalse(csvFormat.isCommentMarkerSet());
assertFalse(csvFormat.isEscapeCharacterSet());
assertTrue(csvFormat.isQuoteCharacterSet());
assertFalse(csvFormat.getSkipHeaderRecord());
assertNull(csvFormat.getQuoteMode());
assertEquals(',', csvFormat.getDelimiter());
assertTrue(csvFormat.getIgnoreEmptyLines());
assertFalse(csvFormat.getIgnoreHeaderCase());
assertNull(csvFormat.getCommentMarker());
assertEquals("\r\n", csvFormat.getRecordSeparator());
assertFalse(csvFormat.getTrailingDelimiter());
assertFalse(csvFormat.getAllowMissingColumnNames());
assertFalse(csvFormat.getTrim());
assertFalse(csvFormat.isNullStringSet());
assertNull(csvFormat.getNullString());
assertFalse(csvFormat.getIgnoreSurroundingSpaces());
assertNull(csvFormat.getEscapeCharacter());
assertFalse(csvFormatTwo.getIgnoreHeaderCase());
assertNull(csvFormatTwo.getQuoteMode());
assertTrue(csvFormatTwo.getIgnoreEmptyLines());
assertFalse(csvFormatTwo.getIgnoreSurroundingSpaces());
assertNull(csvFormatTwo.getEscapeCharacter());
assertFalse(csvFormatTwo.getTrim());
assertFalse(csvFormatTwo.isEscapeCharacterSet());
assertTrue(csvFormatTwo.isQuoteCharacterSet());
assertFalse(csvFormatTwo.getSkipHeaderRecord());
assertEquals('\"', (char)csvFormatTwo.getQuoteCharacter());
assertFalse(csvFormatTwo.getAllowMissingColumnNames());
assertNull(csvFormatTwo.getNullString());
assertFalse(csvFormatTwo.isNullStringSet());
assertFalse(csvFormatTwo.getTrailingDelimiter());
assertEquals("\r\n", csvFormatTwo.getRecordSeparator());
assertEquals(',', csvFormatTwo.getDelimiter());
assertNull(csvFormatTwo.getCommentMarker());
assertFalse(csvFormatTwo.isCommentMarkerSet());
assertNotSame(csvFormat, csvFormatTwo);
assertNotSame(csvFormatTwo, csvFormat);
assertFalse(csvFormatTwo.equals(csvFormat)); // CSV-244 - should not be equal
final String string = csvFormatTwo.format(objectArray);
assertEquals('\"', (char)csvFormat.getQuoteCharacter());
assertFalse(csvFormat.isCommentMarkerSet());
assertFalse(csvFormat.isEscapeCharacterSet());
assertTrue(csvFormat.isQuoteCharacterSet());
assertFalse(csvFormat.getSkipHeaderRecord());
assertNull(csvFormat.getQuoteMode());
assertEquals(',', csvFormat.getDelimiter());
assertTrue(csvFormat.getIgnoreEmptyLines());
assertFalse(csvFormat.getIgnoreHeaderCase());
assertNull(csvFormat.getCommentMarker());
assertEquals("\r\n", csvFormat.getRecordSeparator());
assertFalse(csvFormat.getTrailingDelimiter());
assertFalse(csvFormat.getAllowMissingColumnNames());
assertFalse(csvFormat.getTrim());
assertFalse(csvFormat.isNullStringSet());
assertNull(csvFormat.getNullString());
assertFalse(csvFormat.getIgnoreSurroundingSpaces());
assertNull(csvFormat.getEscapeCharacter());
assertFalse(csvFormatTwo.getIgnoreHeaderCase());
assertNull(csvFormatTwo.getQuoteMode());
assertTrue(csvFormatTwo.getIgnoreEmptyLines());
assertFalse(csvFormatTwo.getIgnoreSurroundingSpaces());
assertNull(csvFormatTwo.getEscapeCharacter());
assertFalse(csvFormatTwo.getTrim());
assertFalse(csvFormatTwo.isEscapeCharacterSet());
assertTrue(csvFormatTwo.isQuoteCharacterSet());
assertFalse(csvFormatTwo.getSkipHeaderRecord());
assertEquals('\"', (char)csvFormatTwo.getQuoteCharacter());
assertFalse(csvFormatTwo.getAllowMissingColumnNames());
assertNull(csvFormatTwo.getNullString());
assertFalse(csvFormatTwo.isNullStringSet());
assertFalse(csvFormatTwo.getTrailingDelimiter());
assertEquals("\r\n", csvFormatTwo.getRecordSeparator());
assertEquals(',', csvFormatTwo.getDelimiter());
assertNull(csvFormatTwo.getCommentMarker());
assertFalse(csvFormatTwo.isCommentMarkerSet());
assertNotSame(csvFormat, csvFormatTwo);
assertNotSame(csvFormatTwo, csvFormat);
assertNotNull(string);
assertFalse(csvFormat.equals(csvFormatTwo)); // CSV-244 - should not be equal
assertFalse(csvFormatTwo.equals(csvFormat)); // CSV-244 - should not be equal
assertEquals(",,,,,,,", string);
}
@Test
public void testWithHeaderEnum() {
final CSVFormat formatWithHeader = CSVFormat.DEFAULT.withHeader(Header.class);
assertArrayEquals(new String[]{ "Name", "Email", "Phone" }, formatWithHeader.getHeader());
}
@Test
public void testWithIgnoreEmptyLines() {
assertFalse(CSVFormat.DEFAULT.withIgnoreEmptyLines(false).getIgnoreEmptyLines());
assertTrue(CSVFormat.DEFAULT.withIgnoreEmptyLines().getIgnoreEmptyLines());
}
@Test
public void testWithIgnoreSurround() {
assertFalse(CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(false).getIgnoreSurroundingSpaces());
assertTrue(CSVFormat.DEFAULT.withIgnoreSurroundingSpaces().getIgnoreSurroundingSpaces());
}
@Test
public void testWithNullString() {
final CSVFormat formatWithNullString = CSVFormat.DEFAULT.withNullString("null");
assertEquals("null", formatWithNullString.getNullString());
}
@Test
public void testWithQuoteChar() {
final CSVFormat formatWithQuoteChar = CSVFormat.DEFAULT.withQuote('"');
assertEquals(Character.valueOf('"'), formatWithQuoteChar.getQuoteCharacter());
}
@Test
public void testWithQuoteLFThrowsException() {
assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withQuote(LF));
}
@Test
public void testWithQuotePolicy() {
final CSVFormat formatWithQuotePolicy = CSVFormat.DEFAULT.withQuoteMode(QuoteMode.ALL);
assertEquals(QuoteMode.ALL, formatWithQuotePolicy.getQuoteMode());
}
@Test
public void testWithRecordSeparatorCR() {
final CSVFormat formatWithRecordSeparator = CSVFormat.DEFAULT.withRecordSeparator(CR);
assertEquals(String.valueOf(CR), formatWithRecordSeparator.getRecordSeparator());
}
@Test
public void testWithRecordSeparatorCRLF() {
final CSVFormat formatWithRecordSeparator = CSVFormat.DEFAULT.withRecordSeparator(CRLF);
assertEquals(CRLF, formatWithRecordSeparator.getRecordSeparator());
}
@Test
public void testWithRecordSeparatorLF() {
final CSVFormat formatWithRecordSeparator = CSVFormat.DEFAULT.withRecordSeparator(LF);
assertEquals(String.valueOf(LF), formatWithRecordSeparator.getRecordSeparator());
}
@Test
public void testWithSystemRecordSeparator() {
final CSVFormat formatWithRecordSeparator = CSVFormat.DEFAULT.withSystemRecordSeparator();
assertEquals(System.getProperty("line.separator"), formatWithRecordSeparator.getRecordSeparator());
}
@Test
public void testPrintWithEscapesEndWithCRLF() throws IOException {
final Reader in = new StringReader("x,y,x\r\na,?b,c\r\n");
final Appendable out = new StringBuilder();
final CSVFormat format = CSVFormat.RFC4180.withEscape('?').withDelimiter(',').withQuote(null).withRecordSeparator(CRLF);
format.print(in,out,true);
assertEquals("x?,y?,x?r?na?,??b?,c?r?n", out.toString());
}
@Test
public void testPrintWithEscapesEndWithoutCRLF() throws IOException {
final Reader in = new StringReader("x,y,x");
final Appendable out = new StringBuilder();
final CSVFormat format = CSVFormat.RFC4180.withEscape('?').withDelimiter(',').withQuote(null).withRecordSeparator(CRLF);
format.print(in,out,true);
assertEquals("x?,y?,x", out.toString());
}
@Test
public void testFormatToString() throws IOException {
final CSVFormat format = CSVFormat.RFC4180.withEscape('?').withDelimiter(',')
.withQuoteMode(QuoteMode.MINIMAL).withRecordSeparator(CRLF).withQuote('"')
.withNullString("").withIgnoreHeaderCase(true)
.withHeaderComments("This is HeaderComments").withHeader("col1","col2","col3");
assertEquals("Delimiter=<,> Escape=<?> QuoteChar=<\"> QuoteMode=<MINIMAL> NullString=<> RecordSeparator=<" +CRLF+
"> IgnoreHeaderCase:ignored SkipHeaderRecord:false HeaderComments:[This is HeaderComments] Header:[col1, col2, col3]", format.toString());
}
@Test
public void testDelimiterSameAsRecordSeparatorThrowsException() {
assertThrows(IllegalArgumentException.class, () -> CSVFormat.newFormat(CR));
}
@Test
public void testWithHeaderEnumNull() {
final CSVFormat format = CSVFormat.DEFAULT;
final Class<Enum<?>> simpleName = null;
format.withHeader(simpleName);
}
@Test
public void testWithHeaderResultSetNull() throws SQLException {
final CSVFormat format = CSVFormat.DEFAULT;
final ResultSet resultSet = null;
format.withHeader(resultSet);
}
@Test
public void testPrintWithQuoteModeIsNONE() throws IOException {
final Reader in = new StringReader("a,b,c");
final Appendable out = new StringBuilder();
final CSVFormat format = CSVFormat.RFC4180.withDelimiter(',').withQuote('"').withEscape('?').withQuoteMode(QuoteMode.NONE);
format.print(in, out, true);
assertEquals("a?,b?,c", out.toString());
}
@Test
public void testPrintWithQuotes() throws IOException {
final Reader in = new StringReader("\"a,b,c\r\nx,y,z");
final Appendable out = new StringBuilder();
final CSVFormat format = CSVFormat.RFC4180.withDelimiter(',').withQuote('"').withEscape('?').withQuoteMode(QuoteMode.NON_NUMERIC);
format.print(in, out, true);
assertEquals("\"\"\"\"a,b,c\r\nx,y,z\"", out.toString());
}
@Test
public void testPrintWithoutQuotes() throws IOException {
final Reader in = new StringReader("");
final Appendable out = new StringBuilder();
final CSVFormat format = CSVFormat.RFC4180.withDelimiter(',').withQuote('"').withEscape('?').withQuoteMode(QuoteMode.NON_NUMERIC);
format.print(in, out, true);
assertEquals("\"\"", out.toString());
}
@Test
public void testTrim() throws IOException {
final CSVFormat formatWithTrim = CSVFormat.DEFAULT.withDelimiter(',').withTrim().withQuote(null).withRecordSeparator(CRLF);
CharSequence in = "a,b,c";
final StringBuilder out = new StringBuilder();
formatWithTrim.print(in, out, true);
assertEquals("a,b,c", out.toString());
in = new StringBuilder(" x,y,z");
out.setLength(0);
formatWithTrim.print(in, out, true);
assertEquals("x,y,z", out.toString());
in = new StringBuilder("");
out.setLength(0);
formatWithTrim.print(in, out, true);
assertEquals("", out.toString());
in = new StringBuilder("header\r\n");
out.setLength(0);
formatWithTrim.print(in, out, true);
assertEquals("header", out.toString());
}
}