@@ -63,6 +63,15 @@ private static CSVFormat copy(final CSVFormat format) {
6363 return format .withDelimiter (format .getDelimiter ());
6464 }
6565
66+ private void assertNotEquals (String name , String type , Object left , Object right ) {
67+ if (left .equals (right ) || right .equals (left )) {
68+ fail ("Objects must not compare equal for " + name + "(" + type + ")" );
69+ }
70+ if (left .hashCode () == right .hashCode ()) {
71+ fail ("Hash code should not be equal for " + name + "(" + type + ")" );
72+ }
73+ }
74+
6675 @ Test (expected = IllegalArgumentException .class )
6776 public void testDelimiterSameAsCommentStartThrowsException () {
6877 CSVFormat .DEFAULT .withDelimiter ('!' ).withCommentMarker ('!' );
@@ -73,6 +82,14 @@ public void testDelimiterSameAsEscapeThrowsException() {
7382 CSVFormat .DEFAULT .withDelimiter ('!' ).withEscape ('!' );
7483 }
7584
85+ @ Test
86+ public void testDuplicateHeaderElements () {
87+ final String [] header = { "A" , "A" };
88+ final CSVFormat format = CSVFormat .DEFAULT .withHeader (header );
89+ assertEquals (2 , format .getHeader ().length );
90+ assertArrayEquals (header , format .getHeader ());
91+ }
92+
7693 @ Test (expected = IllegalArgumentException .class )
7794 public void testDuplicateHeaderElementsFalse () {
7895 CSVFormat .DEFAULT .withAllowDuplicateHeaderNames (false ).withHeader ("A" , "A" );
@@ -82,14 +99,6 @@ public void testDuplicateHeaderElementsTrue() {
8299 CSVFormat .DEFAULT .withAllowDuplicateHeaderNames (true ).withHeader ("A" , "A" );
83100 }
84101
85- @ Test
86- public void testDuplicateHeaderElements () {
87- final String [] header = { "A" , "A" };
88- final CSVFormat format = CSVFormat .DEFAULT .withHeader (header );
89- assertEquals (2 , format .getHeader ().length );
90- assertArrayEquals (header , format .getHeader ());
91- }
92-
93102 @ Test
94103 public void testEquals () {
95104 final CSVFormat right = CSVFormat .DEFAULT ;
@@ -139,6 +148,54 @@ public void testEqualsEscape() {
139148 assertNotEquals (right , left );
140149 }
141150
151+ @ Test
152+ public void testEqualsHash () throws Exception {
153+ Method [] methods = CSVFormat .class .getDeclaredMethods ();
154+ for (Method method : methods ) {
155+ if (Modifier .isPublic (method .getModifiers ())) {
156+ final String name = method .getName ();
157+ if (name .startsWith ("with" )) {
158+ for (Class <?> cls : method .getParameterTypes ()) {
159+ final String type = cls .getCanonicalName ();
160+ if ("boolean" .equals (type )) {
161+ final Object defTrue = method .invoke (CSVFormat .DEFAULT , new Object [] {Boolean .TRUE });
162+ final Object defFalse = method .invoke (CSVFormat .DEFAULT , new Object [] {Boolean .FALSE });
163+ assertNotEquals (name , type ,defTrue , defFalse );
164+ } else if ("char" .equals (type )){
165+ final Object a = method .invoke (CSVFormat .DEFAULT , new Object [] {'a' });
166+ final Object b = method .invoke (CSVFormat .DEFAULT , new Object [] {'b' });
167+ assertNotEquals (name , type , a , b );
168+ } else if ("java.lang.Character" .equals (type )){
169+ final Object a = method .invoke (CSVFormat .DEFAULT , new Object [] {null });
170+ final Object b = method .invoke (CSVFormat .DEFAULT , new Object [] {new Character ('d' )});
171+ assertNotEquals (name , type , a , b );
172+ } else if ("java.lang.String" .equals (type )){
173+ final Object a = method .invoke (CSVFormat .DEFAULT , new Object [] {null });
174+ final Object b = method .invoke (CSVFormat .DEFAULT , new Object [] {"e" });
175+ assertNotEquals (name , type , a , b );
176+ } else if ("java.lang.String[]" .equals (type )){
177+ final Object a = method .invoke (CSVFormat .DEFAULT , new Object [] {new String [] {null , null }});
178+ final Object b = method .invoke (CSVFormat .DEFAULT , new Object [] {new String [] {"f" , "g" }});
179+ assertNotEquals (name , type , a , b );
180+ } else if ("org.apache.commons.csv.QuoteMode" .equals (type )){
181+ final Object a = method .invoke (CSVFormat .DEFAULT , new Object [] {QuoteMode .MINIMAL });
182+ final Object b = method .invoke (CSVFormat .DEFAULT , new Object [] {QuoteMode .ALL });
183+ assertNotEquals (name , type , a , b );
184+ } else if ("java.lang.Object[]" .equals (type )){
185+ final Object a = method .invoke (CSVFormat .DEFAULT , new Object [] {new Object [] {null , null }});
186+ final Object b = method .invoke (CSVFormat .DEFAULT , new Object [] {new Object [] {new Object (), new Object ()}});
187+ assertNotEquals (name , type , a , b );
188+ } else if ("withHeader" .equals (name )){ // covered above by String[]
189+ // ignored
190+ } else {
191+ fail ("Unhandled method: " +name + "(" + type + ")" );
192+ }
193+ }
194+ }
195+ }
196+ }
197+ }
198+
142199 @ Test
143200 public void testEqualsHeader () {
144201 final CSVFormat right = CSVFormat .newFormat ('\'' )
@@ -878,6 +935,7 @@ public void testWithHeader() throws Exception {
878935 assertNotSame (header , formatWithHeader .getHeader ());
879936 }
880937
938+
881939 @ Test
882940 public void testWithHeaderComments () {
883941
@@ -1039,6 +1097,7 @@ public void testWithHeaderComments() {
10391097
10401098 }
10411099
1100+
10421101 @ Test
10431102 public void testWithHeaderEnum () throws Exception {
10441103 final CSVFormat formatWithHeader = CSVFormat .DEFAULT .withHeader (Header .class );
@@ -1079,14 +1138,12 @@ public void testWithQuoteLFThrowsException() {
10791138 CSVFormat .DEFAULT .withQuote (LF );
10801139 }
10811140
1082-
10831141 @ Test
10841142 public void testWithQuotePolicy () throws Exception {
10851143 final CSVFormat formatWithQuotePolicy = CSVFormat .DEFAULT .withQuoteMode (QuoteMode .ALL );
10861144 assertEquals (QuoteMode .ALL , formatWithQuotePolicy .getQuoteMode ());
10871145 }
10881146
1089-
10901147 @ Test
10911148 public void testWithRecordSeparatorCR () throws Exception {
10921149 final CSVFormat formatWithRecordSeparator = CSVFormat .DEFAULT .withRecordSeparator (CR );
@@ -1110,62 +1167,5 @@ public void testWithSystemRecordSeparator() throws Exception {
11101167 final CSVFormat formatWithRecordSeparator = CSVFormat .DEFAULT .withSystemRecordSeparator ();
11111168 assertEquals (System .getProperty ("line.separator" ), formatWithRecordSeparator .getRecordSeparator ());
11121169 }
1113-
1114- private void assertNotEquals (String name , String type , Object left , Object right ) {
1115- if (left .equals (right ) || right .equals (left )) {
1116- fail ("Objects must not compare equal for " + name + "(" + type + ")" );
1117- }
1118- if (left .hashCode () == right .hashCode ()) {
1119- fail ("Hash code should not be equal for " + name + "(" + type + ")" );
1120- }
1121- }
1122-
1123- @ Test
1124- public void testEqualsHash () throws Exception {
1125- Method [] methods = CSVFormat .class .getDeclaredMethods ();
1126- for (Method method : methods ) {
1127- if (Modifier .isPublic (method .getModifiers ())) {
1128- final String name = method .getName ();
1129- if (name .startsWith ("with" )) {
1130- for (Class <?> cls : method .getParameterTypes ()) {
1131- final String type = cls .getCanonicalName ();
1132- if ("boolean" .equals (type )) {
1133- final Object defTrue = method .invoke (CSVFormat .DEFAULT , new Object [] {Boolean .TRUE });
1134- final Object defFalse = method .invoke (CSVFormat .DEFAULT , new Object [] {Boolean .FALSE });
1135- assertNotEquals (name , type ,defTrue , defFalse );
1136- } else if ("char" .equals (type )){
1137- final Object a = method .invoke (CSVFormat .DEFAULT , new Object [] {'a' });
1138- final Object b = method .invoke (CSVFormat .DEFAULT , new Object [] {'b' });
1139- assertNotEquals (name , type , a , b );
1140- } else if ("java.lang.Character" .equals (type )){
1141- final Object a = method .invoke (CSVFormat .DEFAULT , new Object [] {null });
1142- final Object b = method .invoke (CSVFormat .DEFAULT , new Object [] {new Character ('d' )});
1143- assertNotEquals (name , type , a , b );
1144- } else if ("java.lang.String" .equals (type )){
1145- final Object a = method .invoke (CSVFormat .DEFAULT , new Object [] {null });
1146- final Object b = method .invoke (CSVFormat .DEFAULT , new Object [] {"e" });
1147- assertNotEquals (name , type , a , b );
1148- } else if ("java.lang.String[]" .equals (type )){
1149- final Object a = method .invoke (CSVFormat .DEFAULT , new Object [] {new String [] {null , null }});
1150- final Object b = method .invoke (CSVFormat .DEFAULT , new Object [] {new String [] {"f" , "g" }});
1151- assertNotEquals (name , type , a , b );
1152- } else if ("org.apache.commons.csv.QuoteMode" .equals (type )){
1153- final Object a = method .invoke (CSVFormat .DEFAULT , new Object [] {QuoteMode .MINIMAL });
1154- final Object b = method .invoke (CSVFormat .DEFAULT , new Object [] {QuoteMode .ALL });
1155- assertNotEquals (name , type , a , b );
1156- } else if ("java.lang.Object[]" .equals (type )){
1157- final Object a = method .invoke (CSVFormat .DEFAULT , new Object [] {new Object [] {null , null }});
1158- final Object b = method .invoke (CSVFormat .DEFAULT , new Object [] {new Object [] {new Object (), new Object ()}});
1159- assertNotEquals (name , type , a , b );
1160- } else if ("withHeader" .equals (name )){ // covered above by String[]
1161- // ignored
1162- } else {
1163- fail ("Unhandled method: " +name + "(" + type + ")" );
1164- }
1165- }
1166- }
1167- }
1168- }
1169- }
11701170
11711171}
0 commit comments