|
|
|
@ -14,6 +14,8 @@
|
|
|
|
|
*/ |
|
|
|
|
package com.jayway.jsonpath.internal.filter.eval; |
|
|
|
|
|
|
|
|
|
import java.math.BigDecimal; |
|
|
|
|
import java.math.BigInteger; |
|
|
|
|
import java.util.Collections; |
|
|
|
|
import java.util.HashMap; |
|
|
|
|
import java.util.Map; |
|
|
|
@ -25,7 +27,7 @@ public class ExpressionEvaluator {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public enum Operator { |
|
|
|
|
equal("=="), not_equal("!="), less_or_greater_than("<>"),greater_than(">"), greater_than_or_equal(">="), less_than("<"), less_than_or_equal("<="); |
|
|
|
|
equal("=="), not_equal("!="), less_or_greater_than("<>"), greater_than(">"), greater_than_or_equal(">="), less_than("<"), less_than_or_equal("<="); |
|
|
|
|
|
|
|
|
|
private final String representation; |
|
|
|
|
|
|
|
|
@ -42,7 +44,7 @@ public class ExpressionEvaluator {
|
|
|
|
|
|
|
|
|
|
static { |
|
|
|
|
Map<String, Operator> map = new HashMap<String, Operator>(); |
|
|
|
|
for (Operator op : Operator.values()){ |
|
|
|
|
for (Operator op : Operator.values()) { |
|
|
|
|
map.put(op.getRepresentation(), op); |
|
|
|
|
} |
|
|
|
|
ExpressionEvaluator.operatorsByRepresentation = Collections.unmodifiableMap(map); |
|
|
|
@ -52,7 +54,7 @@ public class ExpressionEvaluator {
|
|
|
|
|
public static <T> boolean eval(T actual, String comparator, String expected) { |
|
|
|
|
|
|
|
|
|
Operator operator = operatorsByRepresentation.get(comparator); |
|
|
|
|
if (operator == null){ |
|
|
|
|
if (operator == null) { |
|
|
|
|
throw new IllegalArgumentException("Unsupported operator " + comparator); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -60,7 +62,7 @@ public class ExpressionEvaluator {
|
|
|
|
|
|
|
|
|
|
Long a = (Long) actual; |
|
|
|
|
Long e = Long.parseLong(expected.trim()); |
|
|
|
|
switch (operator){ |
|
|
|
|
switch (operator) { |
|
|
|
|
case equal: |
|
|
|
|
return a.longValue() == e.longValue(); |
|
|
|
|
case not_equal: |
|
|
|
@ -83,7 +85,7 @@ public class ExpressionEvaluator {
|
|
|
|
|
Integer a = (Integer) actual; |
|
|
|
|
Integer e = Integer.parseInt(expected.trim()); |
|
|
|
|
|
|
|
|
|
switch (operator){ |
|
|
|
|
switch (operator) { |
|
|
|
|
case equal: |
|
|
|
|
return a.intValue() == e.intValue(); |
|
|
|
|
case not_equal: |
|
|
|
@ -105,7 +107,7 @@ public class ExpressionEvaluator {
|
|
|
|
|
Double a = (Double) actual; |
|
|
|
|
Double e = Double.parseDouble(expected.trim()); |
|
|
|
|
|
|
|
|
|
switch (operator){ |
|
|
|
|
switch (operator) { |
|
|
|
|
case equal: |
|
|
|
|
return a.doubleValue() == e.doubleValue(); |
|
|
|
|
case not_equal: |
|
|
|
@ -124,7 +126,7 @@ public class ExpressionEvaluator {
|
|
|
|
|
} |
|
|
|
|
} else if (actual instanceof String) { |
|
|
|
|
|
|
|
|
|
switch (operator){ |
|
|
|
|
switch (operator) { |
|
|
|
|
case greater_than: |
|
|
|
|
case greater_than_or_equal: |
|
|
|
|
case less_than: |
|
|
|
@ -134,13 +136,13 @@ public class ExpressionEvaluator {
|
|
|
|
|
case equal: |
|
|
|
|
case not_equal: |
|
|
|
|
case less_or_greater_than: |
|
|
|
|
String a = (String)actual; |
|
|
|
|
String a = (String) actual; |
|
|
|
|
String expectedTrimmed = expected.trim(); |
|
|
|
|
if(expectedTrimmed.startsWith("'")) { |
|
|
|
|
if (expectedTrimmed.startsWith("'")) { |
|
|
|
|
expectedTrimmed = expectedTrimmed.substring(1); |
|
|
|
|
} |
|
|
|
|
if(expectedTrimmed.endsWith("'")){ |
|
|
|
|
expectedTrimmed = expectedTrimmed.substring(0, expected.length()-1); |
|
|
|
|
if (expectedTrimmed.endsWith("'")) { |
|
|
|
|
expectedTrimmed = expectedTrimmed.substring(0, expected.length() - 1); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (operator == Operator.equal) { |
|
|
|
@ -152,7 +154,7 @@ public class ExpressionEvaluator {
|
|
|
|
|
throw new UnsupportedOperationException("Cannot handle operator " + operator); |
|
|
|
|
} |
|
|
|
|
} else if (actual instanceof Boolean) { |
|
|
|
|
switch (operator){ |
|
|
|
|
switch (operator) { |
|
|
|
|
case equal: |
|
|
|
|
case not_equal: |
|
|
|
|
case less_or_greater_than: |
|
|
|
@ -166,6 +168,53 @@ public class ExpressionEvaluator {
|
|
|
|
|
default: |
|
|
|
|
throw new UnsupportedOperationException("Cannot handle operator " + operator); |
|
|
|
|
} |
|
|
|
|
} else if (actual instanceof BigInteger) { |
|
|
|
|
|
|
|
|
|
BigInteger a = (BigInteger) actual; |
|
|
|
|
BigInteger e = new BigInteger(expected.trim()); |
|
|
|
|
|
|
|
|
|
switch (operator){ |
|
|
|
|
case equal: |
|
|
|
|
return a.compareTo(e) == 0; |
|
|
|
|
case not_equal: |
|
|
|
|
return a.compareTo(e) != 0; |
|
|
|
|
case less_or_greater_than: |
|
|
|
|
return a.compareTo(e) != 0; |
|
|
|
|
case greater_than: |
|
|
|
|
return a.compareTo(e) > 0; |
|
|
|
|
case greater_than_or_equal: |
|
|
|
|
return a.compareTo(e) >= 0; |
|
|
|
|
case less_than: |
|
|
|
|
return a.compareTo(e) < 0; |
|
|
|
|
case less_than_or_equal: |
|
|
|
|
return a.compareTo(e) <= 0; |
|
|
|
|
default: |
|
|
|
|
throw new UnsupportedOperationException("Cannot handle operator " + operator); |
|
|
|
|
} |
|
|
|
|
} else if (actual instanceof BigDecimal) { |
|
|
|
|
|
|
|
|
|
BigDecimal a = (BigDecimal) actual; |
|
|
|
|
BigDecimal e = new BigDecimal(expected); |
|
|
|
|
|
|
|
|
|
switch (operator){ |
|
|
|
|
case equal: |
|
|
|
|
return a.compareTo(e) == 0; |
|
|
|
|
case not_equal: |
|
|
|
|
return a.compareTo(e) != 0; |
|
|
|
|
case less_or_greater_than: |
|
|
|
|
return a.compareTo(e) != 0; |
|
|
|
|
case greater_than: |
|
|
|
|
return a.compareTo(e) > 0; |
|
|
|
|
case greater_than_or_equal: |
|
|
|
|
return a.compareTo(e) >= 0; |
|
|
|
|
case less_than: |
|
|
|
|
return a.compareTo(e) < 0; |
|
|
|
|
case less_than_or_equal: |
|
|
|
|
return a.compareTo(e) <= 0; |
|
|
|
|
default: |
|
|
|
|
throw new UnsupportedOperationException("Cannot handle operator " + operator); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|