File tree Expand file tree Collapse file tree 3 files changed +108
-5
lines changed
main/java/de/danielbechler/util
java/de/danielbechler/diff/integration/issues
resources/de/danielbechler/util Expand file tree Collapse file tree 3 files changed +108
-5
lines changed Original file line number Diff line number Diff line change @@ -25,14 +25,14 @@ private Comparables()
2525
2626 public static <T extends Comparable <T >> boolean isEqualByComparison (final T a , final T b )
2727 {
28- if (a ! = null )
28+ if (a == null && b = = null )
2929 {
30- return a . compareTo ( b ) == 0 ;
30+ return true ;
3131 }
32- else if (b != null )
32+ else if (a != null && b != null )
3333 {
34- return b .compareTo (a ) == 0 ;
34+ return a .compareTo (b ) == 0 ;
3535 }
36- return true ;
36+ return false ;
3737 }
3838}
Original file line number Diff line number Diff line change 1+ /*
2+ * Copyright 2013 Daniel Bechler
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ package de.danielbechler.diff.integration.issues
18+
19+ import de.danielbechler.diff.ObjectDifferFactory
20+ import spock.lang.Specification
21+
22+ /**
23+ * @author Daniel Bechler
24+ */
25+ class Issue77Test extends Specification
26+ {
27+ def " NullPointerException on null unsafe Comparable implementations" ()
28+ {
29+ expect :
30+ ObjectDifferFactory . getInstance(). compare(new SomeObject (BigDecimal . ONE ), new SomeObject (null ));
31+ }
32+
33+ class SomeObject
34+ {
35+ BigDecimal value;
36+
37+ SomeObject (BigDecimal value )
38+ {
39+ this . value = value;
40+ }
41+
42+ BigDecimal getValue ()
43+ {
44+ return value
45+ }
46+
47+ void setValue (final BigDecimal value )
48+ {
49+ this . value = value
50+ }
51+ }
52+ }
Original file line number Diff line number Diff line change 1+ /*
2+ * Copyright 2013 Daniel Bechler
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ package de.danielbechler.util
18+
19+ import spock.lang.Specification
20+
21+ /**
22+ * @author Daniel Bechler
23+ */
24+ class ComparablesSpec extends Specification
25+ {
26+ def " isEqualByComparison should return true when both parameters are null" ()
27+ {
28+ expect :
29+ Comparables . isEqualByComparison(null , null )
30+ }
31+
32+ def " isEqualByComparison should return true when both arguments are equal by comparison" ()
33+ {
34+ expect :
35+ Comparables . isEqualByComparison(new BigDecimal (10 ), new BigDecimal (10 ))
36+ }
37+
38+ def " isEqualByComparison should be null-safe even if compareTo method of comparable types is not" ()
39+ {
40+ when :
41+ def result = Comparables . isEqualByComparison(a, b)
42+
43+ then :
44+ ! result
45+
46+ where :
47+ a | b
48+ BigDecimal . ONE | null
49+ null | BigDecimal . TEN
50+ }
51+ }
You can’t perform that action at this time.
0 commit comments