This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-csv.git
commit c203896177b295c2f5319e8c34b9d8bb9f58564e Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Wed Sep 4 09:54:03 2019 -0400 Sort members. --- .../java/org/apache/commons/csv/CSVFormatTest.java | 134 ++++++++++----------- 1 file changed, 67 insertions(+), 67 deletions(-) diff --git a/src/test/java/org/apache/commons/csv/CSVFormatTest.java b/src/test/java/org/apache/commons/csv/CSVFormatTest.java index b67cc0f..b683a04 100644 --- a/src/test/java/org/apache/commons/csv/CSVFormatTest.java +++ b/src/test/java/org/apache/commons/csv/CSVFormatTest.java @@ -63,6 +63,15 @@ public class CSVFormatTest { return format.withDelimiter(format.getDelimiter()); } + private void assertNotEquals(String name, String type, Object left, 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(expected = IllegalArgumentException.class) public void testDelimiterSameAsCommentStartThrowsException() { CSVFormat.DEFAULT.withDelimiter('!').withCommentMarker('!'); @@ -73,6 +82,14 @@ public class CSVFormatTest { 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(expected = IllegalArgumentException.class) public void testDuplicateHeaderElementsFalse() { CSVFormat.DEFAULT.withAllowDuplicateHeaderNames(false).withHeader("A", "A"); @@ -83,14 +100,6 @@ public class CSVFormatTest { } @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 testEquals() { final CSVFormat right = CSVFormat.DEFAULT; final CSVFormat left = copy(right); @@ -140,6 +149,54 @@ public class CSVFormatTest { } @Test + public void testEqualsHash() throws Exception { + Method[] methods = CSVFormat.class.getDeclaredMethods(); + for (Method method : methods) { + if (Modifier.isPublic(method.getModifiers())) { + final String name = method.getName(); + if (name.startsWith("with")) { + for (Class<?> cls : method.getParameterTypes()) { + final String type = cls.getCanonicalName(); + if ("boolean".equals(type)) { + final Object defTrue = method.invoke(CSVFormat.DEFAULT, new Object[] {Boolean.TRUE}); + final Object defFalse = method.invoke(CSVFormat.DEFAULT, new Object[] {Boolean.FALSE}); + assertNotEquals(name, type ,defTrue, defFalse); + } else if ("char".equals(type)){ + final Object a = method.invoke(CSVFormat.DEFAULT, new Object[] {'a'}); + final Object b = method.invoke(CSVFormat.DEFAULT, new Object[] {'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 Object[] {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, new Object[] {"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, new Object[] {QuoteMode.MINIMAL}); + final Object b = method.invoke(CSVFormat.DEFAULT, new Object[] {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) @@ -878,6 +935,7 @@ public class CSVFormatTest { assertNotSame(header, formatWithHeader.getHeader()); } + @Test public void testWithHeaderComments() { @@ -1039,6 +1097,7 @@ public class CSVFormatTest { } + @Test public void testWithHeaderEnum() throws Exception { final CSVFormat formatWithHeader = CSVFormat.DEFAULT.withHeader(Header.class); @@ -1079,14 +1138,12 @@ public class CSVFormatTest { CSVFormat.DEFAULT.withQuote(LF); } - @Test public void testWithQuotePolicy() throws Exception { final CSVFormat formatWithQuotePolicy = CSVFormat.DEFAULT.withQuoteMode(QuoteMode.ALL); assertEquals(QuoteMode.ALL, formatWithQuotePolicy.getQuoteMode()); } - @Test public void testWithRecordSeparatorCR() throws Exception { final CSVFormat formatWithRecordSeparator = CSVFormat.DEFAULT.withRecordSeparator(CR); @@ -1110,62 +1167,5 @@ public class CSVFormatTest { final CSVFormat formatWithRecordSeparator = CSVFormat.DEFAULT.withSystemRecordSeparator(); assertEquals(System.getProperty("line.separator"), formatWithRecordSeparator.getRecordSeparator()); } - - private void assertNotEquals(String name, String type, Object left, 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 testEqualsHash() throws Exception { - Method[] methods = CSVFormat.class.getDeclaredMethods(); - for (Method method : methods) { - if (Modifier.isPublic(method.getModifiers())) { - final String name = method.getName(); - if (name.startsWith("with")) { - for (Class<?> cls : method.getParameterTypes()) { - final String type = cls.getCanonicalName(); - if ("boolean".equals(type)) { - final Object defTrue = method.invoke(CSVFormat.DEFAULT, new Object[] {Boolean.TRUE}); - final Object defFalse = method.invoke(CSVFormat.DEFAULT, new Object[] {Boolean.FALSE}); - assertNotEquals(name, type ,defTrue, defFalse); - } else if ("char".equals(type)){ - final Object a = method.invoke(CSVFormat.DEFAULT, new Object[] {'a'}); - final Object b = method.invoke(CSVFormat.DEFAULT, new Object[] {'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 Object[] {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, new Object[] {"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, new Object[] {QuoteMode.MINIMAL}); - final Object b = method.invoke(CSVFormat.DEFAULT, new Object[] {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 + ")"); - } - } - } - } - } - } }