SPR-7840: comparator dealing with nulls
git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@4156 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
parent
0e2ce565c9
commit
5b07305e7d
|
|
@ -33,9 +33,9 @@ public class StandardTypeComparator implements TypeComparator {
|
||||||
public int compare(Object left, Object right) throws SpelEvaluationException {
|
public int compare(Object left, Object right) throws SpelEvaluationException {
|
||||||
// If one is null, check if the other is
|
// If one is null, check if the other is
|
||||||
if (left == null) {
|
if (left == null) {
|
||||||
return right == null ? 0 : 1;
|
return right == null ? 0 : -1;
|
||||||
} else if (right == null) {
|
} else if (right == null) {
|
||||||
return -1; // left cannot be null
|
return 1; // left cannot be null
|
||||||
}
|
}
|
||||||
|
|
||||||
// Basic number comparisons
|
// Basic number comparisons
|
||||||
|
|
|
||||||
|
|
@ -61,9 +61,9 @@ public class DefaultComparatorUnitTests {
|
||||||
@Test
|
@Test
|
||||||
public void testNulls() throws EvaluationException {
|
public void testNulls() throws EvaluationException {
|
||||||
TypeComparator comparator = new StandardTypeComparator();
|
TypeComparator comparator = new StandardTypeComparator();
|
||||||
Assert.assertTrue(comparator.compare(null,"abc")>0);
|
Assert.assertTrue(comparator.compare(null,"abc")<0);
|
||||||
Assert.assertTrue(comparator.compare(null,null)==0);
|
Assert.assertTrue(comparator.compare(null,null)==0);
|
||||||
Assert.assertTrue(comparator.compare("abc",null)<0);
|
Assert.assertTrue(comparator.compare("abc",null)>0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,8 @@
|
||||||
|
|
||||||
package org.springframework.expression.spel;
|
package org.springframework.expression.spel;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
|
|
@ -767,5 +769,47 @@ public class SpringEL300Tests extends ExpressionTestCase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static class D {
|
||||||
|
public String a;
|
||||||
|
|
||||||
|
private D(String s) {
|
||||||
|
a=s;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return "D("+a+")";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGreaterThanWithNulls_SPR7840() throws Exception {
|
||||||
|
List<D> list = new ArrayList<D>();
|
||||||
|
list.add(new D("aaa"));
|
||||||
|
list.add(new D("bbb"));
|
||||||
|
list.add(new D(null));
|
||||||
|
list.add(new D("ccc"));
|
||||||
|
list.add(new D(null));
|
||||||
|
list.add(new D("zzz"));
|
||||||
|
|
||||||
|
StandardEvaluationContext ctx = new StandardEvaluationContext(list);
|
||||||
|
SpelExpressionParser parser = new SpelExpressionParser();
|
||||||
|
|
||||||
|
String el1 = "#root.?[a < 'hhh']";
|
||||||
|
SpelExpression exp = parser.parseRaw(el1);
|
||||||
|
Object value = exp.getValue(ctx);
|
||||||
|
assertEquals("[D(aaa), D(bbb), D(null), D(ccc), D(null)]",value.toString());
|
||||||
|
|
||||||
|
String el2 = "#root.?[a > 'hhh']";
|
||||||
|
SpelExpression exp2 = parser.parseRaw(el2);
|
||||||
|
Object value2 = exp2.getValue(ctx);
|
||||||
|
assertEquals("[D(zzz)]",value2.toString());
|
||||||
|
|
||||||
|
// trim out the nulls first
|
||||||
|
String el3 = "#root.?[a!=null].?[a < 'hhh']";
|
||||||
|
SpelExpression exp3 = parser.parseRaw(el3);
|
||||||
|
Object value3 = exp3.getValue(ctx);
|
||||||
|
assertEquals("[D(aaa), D(bbb), D(ccc)]",value3.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue