Skip to content

Commit b9d07b9

Browse files
committed
SPR-7840: comparator dealing with nulls
1 parent fadfc76 commit b9d07b9

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

org.springframework.expression/src/main/java/org/springframework/expression/spel/support/StandardTypeComparator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ public class StandardTypeComparator implements TypeComparator {
3333
public int compare(Object left, Object right) throws SpelEvaluationException {
3434
// If one is null, check if the other is
3535
if (left == null) {
36-
return right == null ? 0 : 1;
36+
return right == null ? 0 : -1;
3737
} else if (right == null) {
38-
return -1; // left cannot be null
38+
return 1; // left cannot be null
3939
}
4040

4141
// Basic number comparisons

org.springframework.expression/src/test/java/org/springframework/expression/spel/DefaultComparatorUnitTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ public void testPrimitives() throws EvaluationException {
6161
@Test
6262
public void testNulls() throws EvaluationException {
6363
TypeComparator comparator = new StandardTypeComparator();
64-
Assert.assertTrue(comparator.compare(null,"abc")>0);
64+
Assert.assertTrue(comparator.compare(null,"abc")<0);
6565
Assert.assertTrue(comparator.compare(null,null)==0);
66-
Assert.assertTrue(comparator.compare("abc",null)<0);
66+
Assert.assertTrue(comparator.compare("abc",null)>0);
6767
}
6868

6969
@Test

org.springframework.expression/src/test/java/org/springframework/expression/spel/SpringEL300Tests.java

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.expression.spel;
1818

19+
import static org.junit.Assert.assertEquals;
20+
1921
import java.util.ArrayList;
2022
import java.util.HashMap;
2123
import java.util.LinkedHashMap;
@@ -49,7 +51,7 @@
4951
* @author Andy Clement
5052
*/
5153
public class SpringEL300Tests extends ExpressionTestCase {
52-
54+
5355
@Test
5456
public void testNPE_SPR5661() {
5557
evaluate("joinThreeStrings('a',null,'c')", "anullc", String.class);
@@ -766,6 +768,48 @@ static class C {
766768
ms.put("def","pqr");
767769
}
768770
}
771+
772+
static class D {
773+
public String a;
774+
775+
private D(String s) {
776+
a=s;
777+
}
778+
779+
public String toString() {
780+
return "D("+a+")";
781+
}
782+
}
783+
784+
@Test
785+
public void testGreaterThanWithNulls_SPR7840() throws Exception {
786+
List<D> list = new ArrayList<D>();
787+
list.add(new D("aaa"));
788+
list.add(new D("bbb"));
789+
list.add(new D(null));
790+
list.add(new D("ccc"));
791+
list.add(new D(null));
792+
list.add(new D("zzz"));
793+
794+
StandardEvaluationContext ctx = new StandardEvaluationContext(list);
795+
SpelExpressionParser parser = new SpelExpressionParser();
796+
797+
String el1 = "#root.?[a < 'hhh']";
798+
SpelExpression exp = parser.parseRaw(el1);
799+
Object value = exp.getValue(ctx);
800+
assertEquals("[D(aaa), D(bbb), D(null), D(ccc), D(null)]",value.toString());
801+
802+
String el2 = "#root.?[a > 'hhh']";
803+
SpelExpression exp2 = parser.parseRaw(el2);
804+
Object value2 = exp2.getValue(ctx);
805+
assertEquals("[D(zzz)]",value2.toString());
806+
807+
// trim out the nulls first
808+
String el3 = "#root.?[a!=null].?[a < 'hhh']";
809+
SpelExpression exp3 = parser.parseRaw(el3);
810+
Object value3 = exp3.getValue(ctx);
811+
assertEquals("[D(aaa), D(bbb), D(ccc)]",value3.toString());
812+
}
769813

770814

771815
}

0 commit comments

Comments
 (0)