Skip to content

Commit 605bc01

Browse files
committed
CSV-242 CSVFormat equals() and hash() don't use all fields
Fix equals() TODO fix hash()
1 parent 3718ec3 commit 605bc01

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

src/main/java/org/apache/commons/csv/CSVFormat.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,24 @@ public boolean equals(final Object obj) {
809809
if (delimiter != other.delimiter) {
810810
return false;
811811
}
812+
if (trailingDelimiter != other.trailingDelimiter) {
813+
return false;
814+
}
815+
if (autoFlush != other.autoFlush) {
816+
return false;
817+
}
818+
if (trim != other.trim) {
819+
return false;
820+
}
821+
if (allowMissingColumnNames != other.allowMissingColumnNames) {
822+
return false;
823+
}
824+
if (allowDuplicateHeaderNames != other.allowDuplicateHeaderNames) {
825+
return false;
826+
}
827+
if (ignoreHeaderCase != other.ignoreHeaderCase) {
828+
return false;
829+
}
812830
if (quoteMode != other.quoteMode) {
813831
return false;
814832
}
@@ -859,6 +877,9 @@ public boolean equals(final Object obj) {
859877
} else if (!recordSeparator.equals(other.recordSeparator)) {
860878
return false;
861879
}
880+
if (!Arrays.equals(headerComments, other.headerComments)) {
881+
return false;
882+
}
862883
return true;
863884
}
864885

src/test/java/org/apache/commons/csv/CSVFormatTest.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import java.io.ByteArrayOutputStream;
3535
import java.io.ObjectInputStream;
3636
import java.io.ObjectOutputStream;
37+
import java.lang.reflect.Method;
38+
import java.lang.reflect.Modifier;
3739
import java.util.Arrays;
3840

3941
import org.junit.Assert;
@@ -1108,4 +1110,58 @@ public void testWithSystemRecordSeparator() throws Exception {
11081110
assertEquals(System.getProperty("line.separator"), formatWithRecordSeparator.getRecordSeparator());
11091111
}
11101112

1113+
private void assertNotEquals(String name, String type, Object left, Object right) {
1114+
if (left.equals(right) || right.equals(left)) {
1115+
System.out.println("Should not be equal for " + name + "(" + type + ")");
1116+
}
1117+
}
1118+
1119+
@Test
1120+
public void testEqualsHash() throws Exception {
1121+
Method[] methods = CSVFormat.class.getDeclaredMethods();
1122+
for (Method method : methods) {
1123+
if (Modifier.isPublic(method.getModifiers())) {
1124+
final String name = method.getName();
1125+
if (name.startsWith("with")) {
1126+
for (Class<?> cls : method.getParameterTypes()) {
1127+
final String type = cls.getCanonicalName();
1128+
if ("boolean".equals(type)) {
1129+
final Object defTrue = method.invoke(CSVFormat.DEFAULT, new Object[] {Boolean.TRUE});
1130+
final Object defFalse = method.invoke(CSVFormat.DEFAULT, new Object[] {Boolean.FALSE});
1131+
assertNotEquals(name, type ,defTrue, defFalse);
1132+
} else if ("char".equals(type)){
1133+
final Object a = method.invoke(CSVFormat.DEFAULT, new Object[] {'a'});
1134+
final Object b = method.invoke(CSVFormat.DEFAULT, new Object[] {'b'});
1135+
assertNotEquals(name, type, a, b);
1136+
} else if ("java.lang.Character".equals(type)){
1137+
final Object a = method.invoke(CSVFormat.DEFAULT, new Object[] {null});
1138+
final Object b = method.invoke(CSVFormat.DEFAULT, new Object[] {new Character('d')});
1139+
assertNotEquals(name, type, a, b);
1140+
} else if ("java.lang.String".equals(type)){
1141+
final Object a = method.invoke(CSVFormat.DEFAULT, new Object[] {null});
1142+
final Object b = method.invoke(CSVFormat.DEFAULT, new Object[] {"e"});
1143+
assertNotEquals(name, type, a, b);
1144+
} else if ("java.lang.String[]".equals(type)){
1145+
final Object a = method.invoke(CSVFormat.DEFAULT, new Object[] {new String[] {null, null}});
1146+
final Object b = method.invoke(CSVFormat.DEFAULT, new Object[] {new String[] {"f", "g"}});
1147+
assertNotEquals(name, type, a, b);
1148+
} else if ("org.apache.commons.csv.QuoteMode".equals(type)){
1149+
final Object a = method.invoke(CSVFormat.DEFAULT, new Object[] {QuoteMode.MINIMAL});
1150+
final Object b = method.invoke(CSVFormat.DEFAULT, new Object[] {QuoteMode.ALL});
1151+
assertNotEquals(name, type, a, b);
1152+
} else if ("java.lang.Object[]".equals(type)){
1153+
final Object a = method.invoke(CSVFormat.DEFAULT, new Object[] {new Object[] {null, null}});
1154+
final Object b = method.invoke(CSVFormat.DEFAULT, new Object[] {new Object[] {new Object(), new Object()}});
1155+
assertNotEquals(name, type, a, b);
1156+
} else if ("withHeader".equals(name)){ // covered above by String[]
1157+
// ignored
1158+
} else {
1159+
fail("Unhandled method: "+name + "(" + type + ")");
1160+
}
1161+
}
1162+
}
1163+
}
1164+
}
1165+
}
1166+
11111167
}

0 commit comments

Comments
 (0)