|
|
|
@ -6,7 +6,10 @@ import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
import java.math.BigDecimal; |
|
|
|
|
import java.math.BigInteger; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.assertEquals; |
|
|
|
|
import static org.junit.Assert.assertFalse; |
|
|
|
|
import static org.junit.Assert.assertTrue; |
|
|
|
|
|
|
|
|
@ -18,6 +21,51 @@ import static org.junit.Assert.assertTrue;
|
|
|
|
|
*/ |
|
|
|
|
public class ExpressionEvalTest { |
|
|
|
|
|
|
|
|
|
public static final String DOCUMENT= "{\n" + |
|
|
|
|
" \"characters\": [\n" + |
|
|
|
|
" {\n" + |
|
|
|
|
" \"aliases\": [], \n" + |
|
|
|
|
" \"name\": \"Kleeg Lars\", \n" + |
|
|
|
|
" \"occupation\": \"Moisture farmer\", \n" + |
|
|
|
|
" \"offspring\": []\n" + |
|
|
|
|
" }, \n" + |
|
|
|
|
" {\n" + |
|
|
|
|
" \"aliases\": [], \n" + |
|
|
|
|
" \"name\": \"Shmi Skywalker\", \n" + |
|
|
|
|
" \"occupation\": \"Virgin mother\", \n" + |
|
|
|
|
" \"offspring\": [\n" + |
|
|
|
|
" \"AnakinSkywalker\"\n" + |
|
|
|
|
" ]\n" + |
|
|
|
|
" }, \n" + |
|
|
|
|
" {\n" + |
|
|
|
|
" \"aliases\": [\n" + |
|
|
|
|
" \"Darth Vader\"\n" + |
|
|
|
|
" ], \n" + |
|
|
|
|
" \"name\": \"Annakin Skywalker\", \n" + |
|
|
|
|
" \"occupation\": \"Hand of the Emperor, Lord of the Sith\", \n" + |
|
|
|
|
" \"offspring\": [\n" + |
|
|
|
|
" \"Luke Skywalker\", \n" + |
|
|
|
|
" \"LeiaOrgana\"\n" + |
|
|
|
|
" ]\n" + |
|
|
|
|
" }, \n" + |
|
|
|
|
" {\n" + |
|
|
|
|
" \"aliases\": [\n" + |
|
|
|
|
" \"Nerf herder\"\n" + |
|
|
|
|
" ], \n" + |
|
|
|
|
" \"name\": \"Luke Skywalker\", \n" + |
|
|
|
|
" \"occupation\": \"Farm boy\", \n" + |
|
|
|
|
" \"offspring\": null\n" + |
|
|
|
|
" }, \n" + |
|
|
|
|
" {\n" + |
|
|
|
|
" \"aliases\": [\n" + |
|
|
|
|
" \"Your Highness\"\n" + |
|
|
|
|
" ], \n" + |
|
|
|
|
" \"name\": \"Leia Organa\", \n" + |
|
|
|
|
" \"occupation\": \"Senator\", \n" + |
|
|
|
|
" }\n" + |
|
|
|
|
" ]\n" + |
|
|
|
|
"}\n"; |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void long_eval() throws Exception { |
|
|
|
|
|
|
|
|
@ -117,8 +165,42 @@ public class ExpressionEvalTest {
|
|
|
|
|
assertFalse(ExpressionEvaluator.eval(false, "==", "true")); |
|
|
|
|
assertFalse(ExpressionEvaluator.eval(true, "!=", "true")); |
|
|
|
|
assertFalse(ExpressionEvaluator.eval(true, "<>", "true")); |
|
|
|
|
assertFalse(ExpressionEvaluator.eval(false, "!=", "false")); |
|
|
|
|
assertFalse(ExpressionEvaluator.eval(false, "<>", "false")); |
|
|
|
|
assertFalse(ExpressionEvaluator.eval(false, "!=", "false")); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void null_eval() throws Exception { |
|
|
|
|
assertTrue(ExpressionEvaluator.eval(new Integer(10), "!=", "null")); |
|
|
|
|
|
|
|
|
|
assertTrue(ExpressionEvaluator.eval(null, "==", "null")); |
|
|
|
|
|
|
|
|
|
assertTrue(ExpressionEvaluator.eval(null, "<>", "FOO")); |
|
|
|
|
assertTrue(ExpressionEvaluator.eval("FOO", "<>", "null")); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertTrue(ExpressionEvaluator.eval(null, "!=", "FOO")); |
|
|
|
|
assertTrue(ExpressionEvaluator.eval("FOO", "<>", "null")); |
|
|
|
|
|
|
|
|
|
assertTrue(ExpressionEvaluator.eval(null, "!=", "10")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void nulls_filter() { |
|
|
|
|
|
|
|
|
|
List<Map<String, Object>> result = JsonPath.read(DOCUMENT, "$.characters[?(@.offspring == null)]"); |
|
|
|
|
assertEquals(1, result.size()); |
|
|
|
|
|
|
|
|
|
result = JsonPath.read(DOCUMENT, "$.characters[?(@.offspring != null)]"); |
|
|
|
|
assertEquals(3, result.size()); |
|
|
|
|
|
|
|
|
|
result = JsonPath.read(DOCUMENT, "$.characters[?(@.offspring)]"); |
|
|
|
|
assertEquals(4, result.size()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|