Sort members.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1508613 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary D. Gregory 2013-07-30 20:37:17 +00:00
parent 1533facb19
commit 3320c53e27
1 changed files with 182 additions and 182 deletions

View File

@ -42,36 +42,19 @@ import org.junit.Test;
*/ */
public class CSVFormatTest { public class CSVFormatTest {
@Test private static void assertNotEquals(final Object right, final Object left) {
public void testFormat() { assertFalse(right.equals(left));
final CSVFormat format = CSVFormat.DEFAULT; assertFalse(left.equals(right));
assertEquals("", format.format());
assertEquals("a,b,c", format.format("a", "b", "c"));
assertEquals("\"x,y\",z", format.format("x,y", "z"));
} }
@SuppressWarnings("boxing") // no need to worry about boxing here @Test(expected = IllegalStateException.class)
@Test public void testDelimiterSameAsCommentStartThrowsException() {
public void testSerialization() throws Exception { CSVFormat.DEFAULT.withDelimiter('!').withCommentStart('!').validate();
final ByteArrayOutputStream out = new ByteArrayOutputStream(); }
final ObjectOutputStream oos = new ObjectOutputStream(out); @Test(expected = IllegalStateException.class)
oos.writeObject(CSVFormat.DEFAULT); public void testDelimiterSameAsEscapeThrowsException() {
oos.flush(); CSVFormat.DEFAULT.withDelimiter('!').withEscape('!').validate();
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 @Test
@ -90,6 +73,18 @@ public class CSVFormatTest {
assertEquals(right.hashCode(), left.hashCode()); 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 @Test
public void testEqualsDelimiter() { public void testEqualsDelimiter() {
final CSVFormat right = CSVFormat.newFormat('!'); final CSVFormat right = CSVFormat.newFormat('!');
@ -98,6 +93,65 @@ public class CSVFormatTest {
assertNotEquals(right, left); 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 @Test
public void testEqualsQuoteChar() { public void testEqualsQuoteChar() {
final CSVFormat right = CSVFormat.newFormat('\'').withQuoteChar('"'); final CSVFormat right = CSVFormat.newFormat('\'').withQuoteChar('"');
@ -117,60 +171,6 @@ public class CSVFormatTest {
assertNotEquals(right, left); 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 @Test
public void testEqualsRecordSeparator() { public void testEqualsRecordSeparator() {
final CSVFormat right = CSVFormat.newFormat('\'') final CSVFormat right = CSVFormat.newFormat('\'')
@ -187,52 +187,24 @@ public class CSVFormatTest {
assertNotEquals(right, left); assertNotEquals(right, left);
} }
@Test @Test(expected = IllegalStateException.class)
public void testEqualsHeader() { public void testEscapeSameAsCommentStartThrowsException() {
final CSVFormat right = CSVFormat.newFormat('\'') CSVFormat.DEFAULT.withEscape('!').withCommentStart('!').validate();
.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(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 @Test
public void testWithCommentStart() throws Exception { public void testFormat() {
CSVFormat formatWithCommentStart = CSVFormat.DEFAULT.withCommentStart('#'); final CSVFormat format = CSVFormat.DEFAULT;
assertEquals( Character.valueOf('#'), formatWithCommentStart.getCommentStart());
}
@Test assertEquals("", format.format());
public void testWithDelimiter() throws Exception { assertEquals("a,b,c", format.format("a", "b", "c"));
CSVFormat formatWithDelimiter = CSVFormat.DEFAULT.withDelimiter('!'); assertEquals("\"x,y\",z", format.format("x,y", "z"));
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));
} }
@Test @Test
@ -248,63 +220,6 @@ public class CSVFormatTest {
assertNotSame(formatWithHeader.getHeader(), headerCopy); 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) @Test(expected = IllegalStateException.class)
public void testQuoteCharSameAsCommentStartThrowsException() { public void testQuoteCharSameAsCommentStartThrowsException() {
CSVFormat.DEFAULT.withQuoteChar('!').withCommentStart('!').validate(); CSVFormat.DEFAULT.withQuoteChar('!').withCommentStart('!').validate();
@ -337,28 +252,113 @@ public class CSVFormatTest {
assertEquals("\r\n", RFC4180.getRecordSeparator()); 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) @Test(expected = IllegalArgumentException.class)
public void testWithCommentStartCRThrowsException() { public void testWithCommentStartCRThrowsException() {
CSVFormat.DEFAULT.withCommentStart(CR).validate(); CSVFormat.DEFAULT.withCommentStart(CR).validate();
} }
@Test
public void testWithDelimiter() throws Exception {
CSVFormat formatWithDelimiter = CSVFormat.DEFAULT.withDelimiter('!');
assertEquals('!', formatWithDelimiter.getDelimiter());
}
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testWithDelimiterLFThrowsException() { public void testWithDelimiterLFThrowsException() {
CSVFormat.DEFAULT.withDelimiter(LF).validate(); 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) @Test(expected = IllegalArgumentException.class)
public void testWithEscapeCRThrowsExceptions() { public void testWithEscapeCRThrowsExceptions() {
CSVFormat.DEFAULT.withEscape(CR).validate(); 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) @Test(expected = IllegalArgumentException.class)
public void testWithQuoteLFThrowsException() { public void testWithQuoteLFThrowsException() {
CSVFormat.DEFAULT.withQuoteChar(LF).validate(); CSVFormat.DEFAULT.withQuoteChar(LF).validate();
} }
private static void assertNotEquals(final Object right, final Object left) { @Test
assertFalse(right.equals(left)); public void testWithQuotePolicy() throws Exception {
assertFalse(left.equals(right)); 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());
} }
} }