diff --git a/src/test/java/org/apache/commons/csv/CSVFormatTest.java b/src/test/java/org/apache/commons/csv/CSVFormatTest.java index 2d07dea7..f97409dd 100644 --- a/src/test/java/org/apache/commons/csv/CSVFormatTest.java +++ b/src/test/java/org/apache/commons/csv/CSVFormatTest.java @@ -42,36 +42,19 @@ import org.junit.Test; */ public class CSVFormatTest { - @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")); + private static void assertNotEquals(final Object right, final Object left) { + assertFalse(right.equals(left)); + assertFalse(left.equals(right)); } - @SuppressWarnings("boxing") // no need to worry about boxing here - @Test - public void testSerialization() throws Exception { - final ByteArrayOutputStream out = new ByteArrayOutputStream(); + @Test(expected = IllegalStateException.class) + public void testDelimiterSameAsCommentStartThrowsException() { + CSVFormat.DEFAULT.withDelimiter('!').withCommentStart('!').validate(); + } - final ObjectOutputStream oos = new ObjectOutputStream(out); - oos.writeObject(CSVFormat.DEFAULT); - oos.flush(); - oos.close(); - - final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(out.toByteArray())); - final CSVFormat format = (CSVFormat) in.readObject(); - - assertNotNull(format); - assertEquals("delimiter", CSVFormat.DEFAULT.getDelimiter(), format.getDelimiter()); - assertEquals("encapsulator", CSVFormat.DEFAULT.getQuoteChar(), format.getQuoteChar()); - assertEquals("comment start", CSVFormat.DEFAULT.getCommentStart(), format.getCommentStart()); - assertEquals("line separator", CSVFormat.DEFAULT.getRecordSeparator(), format.getRecordSeparator()); - assertEquals("escape", CSVFormat.DEFAULT.getEscape(), format.getEscape()); - assertEquals("trim", CSVFormat.DEFAULT.getIgnoreSurroundingSpaces(), format.getIgnoreSurroundingSpaces()); - assertEquals("empty lines", CSVFormat.DEFAULT.getIgnoreEmptyLines(), format.getIgnoreEmptyLines()); + @Test(expected = IllegalStateException.class) + public void testDelimiterSameAsEscapeThrowsException() { + CSVFormat.DEFAULT.withDelimiter('!').withEscape('!').validate(); } @Test @@ -90,6 +73,18 @@ public class CSVFormatTest { assertEquals(right.hashCode(), left.hashCode()); } + @Test + public void testEqualsCommentStart() { + final CSVFormat right = CSVFormat.newFormat('\'') + .withQuoteChar('"') + .withCommentStart('#') + .withQuotePolicy(Quote.ALL); + final CSVFormat left = CSVFormat.copy(right) + .withCommentStart('!'); + + assertNotEquals(right, left); + } + @Test public void testEqualsDelimiter() { final CSVFormat right = CSVFormat.newFormat('!'); @@ -98,6 +93,65 @@ public class CSVFormatTest { assertNotEquals(right, left); } + @Test + public void testEqualsEscape() { + final CSVFormat right = CSVFormat.newFormat('\'') + .withQuoteChar('"') + .withCommentStart('#') + .withEscape('+') + .withQuotePolicy(Quote.ALL); + final CSVFormat left = CSVFormat.copy(right) + .withEscape('!'); + + assertNotEquals(right, left); + } + + @Test + public void testEqualsHeader() { + final CSVFormat right = CSVFormat.newFormat('\'') + .withRecordSeparator('*') + .withCommentStart('#') + .withEscape('+') + .withHeader("One", "Two", "Three") + .withIgnoreEmptyLines(true) + .withIgnoreSurroundingSpaces(true) + .withQuoteChar('"') + .withQuotePolicy(Quote.ALL); + final CSVFormat left = CSVFormat.copy(right) + .withHeader("Three", "Two", "One"); + + assertNotEquals(right, left); + } + + @Test + public void testEqualsIgnoreEmptyLines() { + final CSVFormat right = CSVFormat.newFormat('\'') + .withCommentStart('#') + .withEscape('+') + .withIgnoreEmptyLines(true) + .withIgnoreSurroundingSpaces(true) + .withQuoteChar('"') + .withQuotePolicy(Quote.ALL); + final CSVFormat left = CSVFormat.copy(right) + .withIgnoreEmptyLines(false); + + assertNotEquals(right, left); + } + + @Test + public void testEqualsIgnoreSurroundingSpaces() { + final CSVFormat right = CSVFormat.newFormat('\'') + .withCommentStart('#') + .withEscape('+') + .withIgnoreSurroundingSpaces(true) + .withQuoteChar('"') + .withQuotePolicy(Quote.ALL); + final CSVFormat left = CSVFormat.copy(right) + .withIgnoreSurroundingSpaces(false); + + assertNotEquals(right, left); + } + @Test public void testEqualsQuoteChar() { final CSVFormat right = CSVFormat.newFormat('\'').withQuoteChar('"'); @@ -117,60 +171,6 @@ public class CSVFormatTest { assertNotEquals(right, left); } - @Test - public void testEqualsCommentStart() { - final CSVFormat right = CSVFormat.newFormat('\'') - .withQuoteChar('"') - .withCommentStart('#') - .withQuotePolicy(Quote.ALL); - final CSVFormat left = CSVFormat.copy(right) - .withCommentStart('!'); - - assertNotEquals(right, left); - } - - @Test - public void testEqualsEscape() { - final CSVFormat right = CSVFormat.newFormat('\'') - .withQuoteChar('"') - .withCommentStart('#') - .withEscape('+') - .withQuotePolicy(Quote.ALL); - final CSVFormat left = CSVFormat.copy(right) - .withEscape('!'); - - assertNotEquals(right, left); - } - - @Test - public void testEqualsIgnoreSurroundingSpaces() { - final CSVFormat right = CSVFormat.newFormat('\'') - .withCommentStart('#') - .withEscape('+') - .withIgnoreSurroundingSpaces(true) - .withQuoteChar('"') - .withQuotePolicy(Quote.ALL); - final CSVFormat left = CSVFormat.copy(right) - .withIgnoreSurroundingSpaces(false); - - assertNotEquals(right, left); - } - - @Test - public void testEqualsIgnoreEmptyLines() { - final CSVFormat right = CSVFormat.newFormat('\'') - .withCommentStart('#') - .withEscape('+') - .withIgnoreEmptyLines(true) - .withIgnoreSurroundingSpaces(true) - .withQuoteChar('"') - .withQuotePolicy(Quote.ALL); - final CSVFormat left = CSVFormat.copy(right) - .withIgnoreEmptyLines(false); - - assertNotEquals(right, left); - } - @Test public void testEqualsRecordSeparator() { final CSVFormat right = CSVFormat.newFormat('\'') @@ -187,52 +187,24 @@ public class CSVFormatTest { assertNotEquals(right, left); } - @Test - public void testEqualsHeader() { - final CSVFormat right = CSVFormat.newFormat('\'') - .withRecordSeparator('*') - .withCommentStart('#') - .withEscape('+') - .withHeader("One", "Two", "Three") - .withIgnoreEmptyLines(true) - .withIgnoreSurroundingSpaces(true) - .withQuoteChar('"') - .withQuotePolicy(Quote.ALL); - final CSVFormat left = CSVFormat.copy(right) - .withHeader("Three", "Two", "One"); + @Test(expected = IllegalStateException.class) + public void testEscapeSameAsCommentStartThrowsException() { + CSVFormat.DEFAULT.withEscape('!').withCommentStart('!').validate(); + } - assertNotEquals(right, left); + @Test(expected = IllegalStateException.class) + public void testEscapeSameAsCommentStartThrowsExceptionForWrapperType() { + // Cannot assume that callers won't use different Character objects + CSVFormat.DEFAULT.withEscape(new Character('!')).withCommentStart(new Character('!')).validate(); } @Test - public void testWithCommentStart() throws Exception { - CSVFormat formatWithCommentStart = CSVFormat.DEFAULT.withCommentStart('#'); - assertEquals( Character.valueOf('#'), formatWithCommentStart.getCommentStart()); - } + public void testFormat() { + final CSVFormat format = CSVFormat.DEFAULT; - @Test - public void testWithDelimiter() throws Exception { - CSVFormat formatWithDelimiter = CSVFormat.DEFAULT.withDelimiter('!'); - assertEquals('!', formatWithDelimiter.getDelimiter()); - } - - @Test - public void testWithEscape() throws Exception { - CSVFormat formatWithEscape = CSVFormat.DEFAULT.withEscape('&'); - assertEquals(Character.valueOf('&'), formatWithEscape.getEscape()); - } - - @Test - public void testWithHeader() throws Exception { - String[] header = new String[]{"one", "two", "three"}; - // withHeader() makes a copy of the header array. - CSVFormat formatWithHeader = CSVFormat.DEFAULT.withHeader(header); - assertArrayEquals(header, formatWithHeader.getHeader()); - assertNotSame(header, formatWithHeader.getHeader()); - header[0] = "A"; - header[1] = "B"; - header[2] = "C"; - assertFalse(Arrays.equals(formatWithHeader.getHeader(), header)); + assertEquals("", format.format()); + assertEquals("a,b,c", format.format("a", "b", "c")); + assertEquals("\"x,y\",z", format.format("x,y", "z")); } @Test @@ -248,63 +220,6 @@ public class CSVFormatTest { assertNotSame(formatWithHeader.getHeader(), headerCopy); } - @Test - public void testWithIgnoreEmptyLines() throws Exception { - assertFalse(CSVFormat.DEFAULT.withIgnoreEmptyLines(false).getIgnoreEmptyLines()); - assertTrue(CSVFormat.DEFAULT.withIgnoreEmptyLines(true).getIgnoreEmptyLines()); - } - - @Test - public void testWithIgnoreSurround() throws Exception { - assertFalse(CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(false).getIgnoreSurroundingSpaces()); - assertTrue(CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true).getIgnoreSurroundingSpaces()); - } - - @Test - public void testWithNullString() throws Exception { - CSVFormat formatWithNullString = CSVFormat.DEFAULT.withNullString("null"); - assertEquals("null", formatWithNullString.getNullString()); - } - - @Test - public void testWithQuoteChar() throws Exception { - CSVFormat formatWithQuoteChar = CSVFormat.DEFAULT.withQuoteChar('"'); - assertEquals(Character.valueOf('"'), formatWithQuoteChar.getQuoteChar()); - } - - @Test - public void testWithQuotePolicy() throws Exception { - CSVFormat formatWithQuotePolicy = CSVFormat.DEFAULT.withQuotePolicy(Quote.ALL); - assertEquals(Quote.ALL, formatWithQuotePolicy.getQuotePolicy()); - } - - @Test - public void testWithRecordSeparator() throws Exception { - CSVFormat formatWithRecordSeparator = CSVFormat.DEFAULT.withRecordSeparator('!'); - assertEquals("!", formatWithRecordSeparator.getRecordSeparator()); - } - - @Test(expected = IllegalStateException.class) - public void testDelimiterSameAsCommentStartThrowsException() { - CSVFormat.DEFAULT.withDelimiter('!').withCommentStart('!').validate(); - } - - @Test(expected = IllegalStateException.class) - public void testDelimiterSameAsEscapeThrowsException() { - CSVFormat.DEFAULT.withDelimiter('!').withEscape('!').validate(); - } - - @Test(expected = IllegalStateException.class) - public void testEscapeSameAsCommentStartThrowsException() { - CSVFormat.DEFAULT.withEscape('!').withCommentStart('!').validate(); - } - - @Test(expected = IllegalStateException.class) - public void testEscapeSameAsCommentStartThrowsExceptionForWrapperType() { - // Cannot assume that callers won't use different Character objects - CSVFormat.DEFAULT.withEscape(new Character('!')).withCommentStart(new Character('!')).validate(); - } - @Test(expected = IllegalStateException.class) public void testQuoteCharSameAsCommentStartThrowsException() { CSVFormat.DEFAULT.withQuoteChar('!').withCommentStart('!').validate(); @@ -337,28 +252,113 @@ public class CSVFormatTest { 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(); + + final ObjectOutputStream oos = new ObjectOutputStream(out); + oos.writeObject(CSVFormat.DEFAULT); + oos.flush(); + oos.close(); + + final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(out.toByteArray())); + final CSVFormat format = (CSVFormat) in.readObject(); + + assertNotNull(format); + assertEquals("delimiter", CSVFormat.DEFAULT.getDelimiter(), format.getDelimiter()); + assertEquals("encapsulator", CSVFormat.DEFAULT.getQuoteChar(), format.getQuoteChar()); + assertEquals("comment start", CSVFormat.DEFAULT.getCommentStart(), format.getCommentStart()); + assertEquals("line separator", CSVFormat.DEFAULT.getRecordSeparator(), format.getRecordSeparator()); + assertEquals("escape", CSVFormat.DEFAULT.getEscape(), format.getEscape()); + assertEquals("trim", CSVFormat.DEFAULT.getIgnoreSurroundingSpaces(), format.getIgnoreSurroundingSpaces()); + assertEquals("empty lines", CSVFormat.DEFAULT.getIgnoreEmptyLines(), format.getIgnoreEmptyLines()); + } + + @Test + public void testWithCommentStart() throws Exception { + CSVFormat formatWithCommentStart = CSVFormat.DEFAULT.withCommentStart('#'); + assertEquals( Character.valueOf('#'), formatWithCommentStart.getCommentStart()); + } + @Test(expected = IllegalArgumentException.class) public void testWithCommentStartCRThrowsException() { CSVFormat.DEFAULT.withCommentStart(CR).validate(); } + @Test + public void testWithDelimiter() throws Exception { + CSVFormat formatWithDelimiter = CSVFormat.DEFAULT.withDelimiter('!'); + assertEquals('!', formatWithDelimiter.getDelimiter()); + } + @Test(expected = IllegalArgumentException.class) public void testWithDelimiterLFThrowsException() { CSVFormat.DEFAULT.withDelimiter(LF).validate(); } + @Test + public void testWithEscape() throws Exception { + CSVFormat formatWithEscape = CSVFormat.DEFAULT.withEscape('&'); + assertEquals(Character.valueOf('&'), formatWithEscape.getEscape()); + } + @Test(expected = IllegalArgumentException.class) public void testWithEscapeCRThrowsExceptions() { CSVFormat.DEFAULT.withEscape(CR).validate(); } + @Test + public void testWithHeader() throws Exception { + String[] header = new String[]{"one", "two", "three"}; + // withHeader() makes a copy of the header array. + CSVFormat formatWithHeader = CSVFormat.DEFAULT.withHeader(header); + assertArrayEquals(header, formatWithHeader.getHeader()); + assertNotSame(header, formatWithHeader.getHeader()); + header[0] = "A"; + header[1] = "B"; + header[2] = "C"; + assertFalse(Arrays.equals(formatWithHeader.getHeader(), header)); + } + + @Test + public void testWithIgnoreEmptyLines() throws Exception { + assertFalse(CSVFormat.DEFAULT.withIgnoreEmptyLines(false).getIgnoreEmptyLines()); + assertTrue(CSVFormat.DEFAULT.withIgnoreEmptyLines(true).getIgnoreEmptyLines()); + } + + @Test + public void testWithIgnoreSurround() throws Exception { + assertFalse(CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(false).getIgnoreSurroundingSpaces()); + assertTrue(CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true).getIgnoreSurroundingSpaces()); + } + + @Test + public void testWithNullString() throws Exception { + CSVFormat formatWithNullString = CSVFormat.DEFAULT.withNullString("null"); + assertEquals("null", formatWithNullString.getNullString()); + } + + @Test + public void testWithQuoteChar() throws Exception { + CSVFormat formatWithQuoteChar = CSVFormat.DEFAULT.withQuoteChar('"'); + assertEquals(Character.valueOf('"'), formatWithQuoteChar.getQuoteChar()); + } + @Test(expected = IllegalArgumentException.class) public void testWithQuoteLFThrowsException() { CSVFormat.DEFAULT.withQuoteChar(LF).validate(); } - private static void assertNotEquals(final Object right, final Object left) { - assertFalse(right.equals(left)); - assertFalse(left.equals(right)); + @Test + public void testWithQuotePolicy() throws Exception { + CSVFormat formatWithQuotePolicy = CSVFormat.DEFAULT.withQuotePolicy(Quote.ALL); + assertEquals(Quote.ALL, formatWithQuotePolicy.getQuotePolicy()); + } + + @Test + public void testWithRecordSeparator() throws Exception { + CSVFormat formatWithRecordSeparator = CSVFormat.DEFAULT.withRecordSeparator('!'); + assertEquals("!", formatWithRecordSeparator.getRecordSeparator()); } }