You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2013/07/30 22:37:18 UTC
svn commit: r1508613 -
/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java
Author: ggregory
Date: Tue Jul 30 20:37:17 2013
New Revision: 1508613
URL: http://svn.apache.org/r1508613
Log:
Sort members.
Modified:
commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java
Modified: commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java?rev=1508613&r1=1508612&r2=1508613&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java (original)
+++ commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java Tue Jul 30 20:37:17 2013
@@ -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();
-
- 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();
+ @Test(expected = IllegalStateException.class)
+ public void testDelimiterSameAsCommentStartThrowsException() {
+ CSVFormat.DEFAULT.withDelimiter('!').withCommentStart('!').validate();
+ }
- 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
@@ -91,40 +74,21 @@ public class CSVFormatTest {
}
@Test
- public void testEqualsDelimiter() {
- final CSVFormat right = CSVFormat.newFormat('!');
- final CSVFormat left = CSVFormat.newFormat('?');
-
- assertNotEquals(right, left);
- }
-
- @Test
- public void testEqualsQuoteChar() {
- final CSVFormat right = CSVFormat.newFormat('\'').withQuoteChar('"');
- final CSVFormat left = CSVFormat.copy(right).withQuoteChar('!');
-
- assertNotEquals(right, left);
- }
-
- @Test
- public void testEqualsQuotePolicy() {
+ public void testEqualsCommentStart() {
final CSVFormat right = CSVFormat.newFormat('\'')
.withQuoteChar('"')
+ .withCommentStart('#')
.withQuotePolicy(Quote.ALL);
final CSVFormat left = CSVFormat.copy(right)
- .withQuotePolicy(Quote.MINIMAL);
+ .withCommentStart('!');
assertNotEquals(right, left);
}
@Test
- public void testEqualsCommentStart() {
- final CSVFormat right = CSVFormat.newFormat('\'')
- .withQuoteChar('"')
- .withCommentStart('#')
- .withQuotePolicy(Quote.ALL);
- final CSVFormat left = CSVFormat.copy(right)
- .withCommentStart('!');
+ public void testEqualsDelimiter() {
+ final CSVFormat right = CSVFormat.newFormat('!');
+ final CSVFormat left = CSVFormat.newFormat('?');
assertNotEquals(right, left);
}
@@ -143,15 +107,18 @@ public class CSVFormatTest {
}
@Test
- public void testEqualsIgnoreSurroundingSpaces() {
+ 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)
- .withIgnoreSurroundingSpaces(false);
+ .withHeader("Three", "Two", "One");
assertNotEquals(right, left);
}
@@ -172,67 +139,72 @@ public class CSVFormatTest {
}
@Test
- public void testEqualsRecordSeparator() {
+ public void testEqualsIgnoreSurroundingSpaces() {
final CSVFormat right = CSVFormat.newFormat('\'')
- .withRecordSeparator('*')
.withCommentStart('#')
.withEscape('+')
- .withIgnoreEmptyLines(true)
.withIgnoreSurroundingSpaces(true)
.withQuoteChar('"')
.withQuotePolicy(Quote.ALL);
final CSVFormat left = CSVFormat.copy(right)
- .withRecordSeparator('!');
+ .withIgnoreSurroundingSpaces(false);
assertNotEquals(right, left);
}
@Test
- public void testEqualsHeader() {
+ public void testEqualsQuoteChar() {
+ final CSVFormat right = CSVFormat.newFormat('\'').withQuoteChar('"');
+ final CSVFormat left = CSVFormat.copy(right).withQuoteChar('!');
+
+ assertNotEquals(right, left);
+ }
+
+ @Test
+ public void testEqualsQuotePolicy() {
+ final CSVFormat right = CSVFormat.newFormat('\'')
+ .withQuoteChar('"')
+ .withQuotePolicy(Quote.ALL);
+ final CSVFormat left = CSVFormat.copy(right)
+ .withQuotePolicy(Quote.MINIMAL);
+
+ assertNotEquals(right, left);
+ }
+
+ @Test
+ public void testEqualsRecordSeparator() {
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");
+ .withRecordSeparator('!');
assertNotEquals(right, left);
}
- @Test
- public void testWithCommentStart() throws Exception {
- CSVFormat formatWithCommentStart = CSVFormat.DEFAULT.withCommentStart('#');
- assertEquals( Character.valueOf('#'), formatWithCommentStart.getCommentStart());
+ @Test(expected = IllegalStateException.class)
+ public void testEscapeSameAsCommentStartThrowsException() {
+ CSVFormat.DEFAULT.withEscape('!').withCommentStart('!').validate();
}
- @Test
- public void testWithDelimiter() throws Exception {
- CSVFormat formatWithDelimiter = CSVFormat.DEFAULT.withDelimiter('!');
- assertEquals('!', formatWithDelimiter.getDelimiter());
+ @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 testWithEscape() throws Exception {
- CSVFormat formatWithEscape = CSVFormat.DEFAULT.withEscape('&');
- assertEquals(Character.valueOf('&'), formatWithEscape.getEscape());
- }
+ public void testFormat() {
+ final CSVFormat format = CSVFormat.DEFAULT;
- @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());
}
}