kalle
14 years ago
6 changed files with 176 additions and 32 deletions
@ -0,0 +1,88 @@ |
|||||||
|
package com.jayway.jsonpath.eval; |
||||||
|
|
||||||
|
/** |
||||||
|
* User: kalle stenflo |
||||||
|
* Date: 2/4/11 |
||||||
|
* Time: 9:21 PM |
||||||
|
*/ |
||||||
|
public class Expression { |
||||||
|
|
||||||
|
|
||||||
|
public static <T> boolean eval(T actual, String comparator, String expected) { |
||||||
|
|
||||||
|
comparator = comparator.trim(); |
||||||
|
|
||||||
|
if (actual instanceof Long) { |
||||||
|
|
||||||
|
Long a = (Long) actual; |
||||||
|
Long e = Long.parseLong(expected.trim()); |
||||||
|
|
||||||
|
if ("=".equals(comparator)) { |
||||||
|
return a.longValue() == e.longValue(); |
||||||
|
} else if ("!=".equals(comparator) || "<>".equals(comparator)) { |
||||||
|
return a.longValue() != e.longValue(); |
||||||
|
} else if (">".equals(comparator)) { |
||||||
|
return a.longValue() > e.longValue(); |
||||||
|
} else if (">=".equals(comparator)) { |
||||||
|
return a.longValue() >= e.longValue(); |
||||||
|
} else if ("<".equals(comparator)) { |
||||||
|
return a.longValue() < e.longValue(); |
||||||
|
} else if ("<=".equals(comparator)) { |
||||||
|
return a.longValue() <= e.longValue(); |
||||||
|
} |
||||||
|
} else if (actual instanceof Integer) { |
||||||
|
Integer a = (Integer) actual; |
||||||
|
Integer e = Integer.parseInt(expected.trim()); |
||||||
|
|
||||||
|
if ("=".equals(comparator)) { |
||||||
|
return a.longValue() == e.longValue(); |
||||||
|
} else if ("!=".equals(comparator) || "<>".equals(comparator)) { |
||||||
|
return a.longValue() != e.longValue(); |
||||||
|
} else if (">".equals(comparator)) { |
||||||
|
return a.longValue() > e.longValue(); |
||||||
|
} else if (">=".equals(comparator)) { |
||||||
|
return a.longValue() >= e.longValue(); |
||||||
|
} else if ("<".equals(comparator)) { |
||||||
|
return a.longValue() < e.longValue(); |
||||||
|
} else if ("<=".equals(comparator)) { |
||||||
|
return a.longValue() <= e.longValue(); |
||||||
|
} |
||||||
|
} else if (actual instanceof Double) { |
||||||
|
|
||||||
|
Double a = (Double) actual; |
||||||
|
Double e = Double.parseDouble(expected.trim()); |
||||||
|
|
||||||
|
if ("=".equals(comparator)) { |
||||||
|
return a.longValue() == e.longValue(); |
||||||
|
} else if ("!=".equals(comparator) || "<>".equals(comparator)) { |
||||||
|
return a.longValue() != e.longValue(); |
||||||
|
} else if (">".equals(comparator)) { |
||||||
|
return a.longValue() > e.longValue(); |
||||||
|
} else if (">=".equals(comparator)) { |
||||||
|
return a.longValue() >= e.longValue(); |
||||||
|
} else if ("<".equals(comparator)) { |
||||||
|
return a.longValue() < e.longValue(); |
||||||
|
} else if ("<=".equals(comparator)) { |
||||||
|
return a.longValue() <= e.longValue(); |
||||||
|
} |
||||||
|
} else if (actual instanceof String) { |
||||||
|
|
||||||
|
String a = (String)actual; |
||||||
|
expected = expected.trim(); |
||||||
|
if(expected.startsWith("'")) { |
||||||
|
expected = expected.substring(1); |
||||||
|
} |
||||||
|
if(expected.endsWith("'")){ |
||||||
|
expected = expected.substring(0, expected.length()-1); |
||||||
|
} |
||||||
|
|
||||||
|
if ("=".equals(comparator)) { |
||||||
|
return a.equals(expected); |
||||||
|
} else if ("!=".equals(comparator) || "<>".equals(comparator)) { |
||||||
|
return !a.equals(expected); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,65 @@ |
|||||||
|
package com.jayway.jsonpath; |
||||||
|
|
||||||
|
import com.jayway.jsonpath.eval.Expression; |
||||||
|
import org.junit.Test; |
||||||
|
|
||||||
|
import static org.junit.Assert.assertFalse; |
||||||
|
import static org.junit.Assert.assertTrue; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by IntelliJ IDEA. |
||||||
|
* User: kallestenflo |
||||||
|
* Date: 2/4/11 |
||||||
|
* Time: 9:32 PM |
||||||
|
*/ |
||||||
|
public class ExpressionEvalTest { |
||||||
|
|
||||||
|
|
||||||
|
@Test |
||||||
|
public void long_eval() throws Exception { |
||||||
|
|
||||||
|
assertTrue(Expression.eval(1L, "=", "1")); |
||||||
|
assertTrue(Expression.eval(2L, "!=", "1")); |
||||||
|
assertTrue(Expression.eval(2L, ">", "1")); |
||||||
|
assertTrue(Expression.eval(2L, ">=", "1")); |
||||||
|
assertTrue(Expression.eval(2L, ">=", "2")); |
||||||
|
assertTrue(Expression.eval(1L, "<", "2")); |
||||||
|
assertTrue(Expression.eval(2L, "<=", "2")); |
||||||
|
|
||||||
|
assertFalse(Expression.eval(1, ">", "2")); |
||||||
|
assertFalse(Expression.eval(1, ">=", "2")); |
||||||
|
assertFalse(Expression.eval(2, "<", "1")); |
||||||
|
assertFalse(Expression.eval(2, "<=", "1")); |
||||||
|
assertFalse(Expression.eval(1, "=", "2")); |
||||||
|
assertFalse(Expression.eval(1, "!=", "1")); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void double_eval() throws Exception { |
||||||
|
|
||||||
|
assertTrue(Expression.eval(1D, "=", "1")); |
||||||
|
assertTrue(Expression.eval(2D, "!=", "1")); |
||||||
|
assertTrue(Expression.eval(2D, ">", "1")); |
||||||
|
assertTrue(Expression.eval(2D, ">=", "1")); |
||||||
|
assertTrue(Expression.eval(2D, ">=", "2")); |
||||||
|
assertTrue(Expression.eval(1D, "<", "2")); |
||||||
|
assertTrue(Expression.eval(2D, "<=", "2")); |
||||||
|
|
||||||
|
assertFalse(Expression.eval(1D, ">", "2")); |
||||||
|
assertFalse(Expression.eval(1D, ">=", "2")); |
||||||
|
assertFalse(Expression.eval(2D, "<", "1")); |
||||||
|
assertFalse(Expression.eval(2D, "<=", "1")); |
||||||
|
assertFalse(Expression.eval(1D, "=", "2")); |
||||||
|
assertFalse(Expression.eval(1D, "!=", "1")); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void string_eval() throws Exception { |
||||||
|
|
||||||
|
assertTrue(Expression.eval("A", "=", "A")); |
||||||
|
assertTrue(Expression.eval("B", "!=", "A")); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue