|
34 | 34 | import java.io.ByteArrayOutputStream; |
35 | 35 | import java.io.ObjectInputStream; |
36 | 36 | import java.io.ObjectOutputStream; |
| 37 | +import java.lang.reflect.Method; |
| 38 | +import java.lang.reflect.Modifier; |
37 | 39 | import java.util.Arrays; |
38 | 40 |
|
39 | 41 | import org.junit.Assert; |
@@ -1108,4 +1110,58 @@ public void testWithSystemRecordSeparator() throws Exception { |
1108 | 1110 | assertEquals(System.getProperty("line.separator"), formatWithRecordSeparator.getRecordSeparator()); |
1109 | 1111 | } |
1110 | 1112 |
|
| 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 | + |
1111 | 1167 | } |
0 commit comments