package com.jayway.jsonpath; import org.hamcrest.Matchers; import org.junit.Test; import java.util.List; import java.util.regex.Matcher; import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertTrue; import static org.junit.Assert.assertThat; /** * Created by IntelliJ IDEA. * User: kallestenflo * Date: 2/29/12 * Time: 8:42 AM */ public class IssuesTest { @Test public void issue_11() throws Exception { String json = "{ \"foo\" : [] }"; List result = JsonPath.read(json, "$.foo[?(@.rel= 'item')][0].uri"); System.out.println(JsonPath.compile("$.foo[?(@.rel= 'item')][0].uri").isPathDefinite()); System.out.println(JsonPath.compile("$.foo[?(@.rel= 'item')][0]").isPathDefinite()); System.out.println(JsonPath.compile("$.foo[?(@.rel= 'item')]").isPathDefinite()); assertTrue(result.isEmpty()); } @Test public void issue_15() throws Exception { String json = "{ \"store\": {\n" + " \"book\": [ \n" + " { \"category\": \"reference\",\n" + " \"author\": \"Nigel Rees\",\n" + " \"title\": \"Sayings of the Century\",\n" + " \"price\": 8.95\n" + " },\n" + " { \"category\": \"fiction\",\n" + " \"author\": \"Herman Melville\",\n" + " \"title\": \"Moby Dick\",\n" + " \"isbn\": \"0-553-21311-3\",\n" + " \"price\": 8.99,\n" + " \"retailer\": null, \n" + " \"children\": true,\n" + " \"number\": -2.99\n" + " },\n" + " { \"category\": \"fiction\",\n" + " \"author\": \"J. R. R. Tolkien\",\n" + " \"title\": \"The Lord of the Rings\",\n" + " \"isbn\": \"0-395-19395-8\",\n" + " \"price\": 22.99,\n" + " \"number\":0,\n" + " \"children\": false\n" + " }\n" + " ]\n" + " }\n" + "}"; List titles = JsonPath.read(json, "$.store.book[?(@.children==true)].title"); assertThat(titles, Matchers.contains("Moby Dick")); assertEquals(1, titles.size()); } @Test public void issue_22() throws Exception { String json = "{\"a\":[{\"b\":1,\"c\":2},{\"b\":5,\"c\":2}]}"; System.out.println(JsonPath.read(json, "a[?(@.b==5)].d")); } @Test public void issue_29_b() throws Exception { String json = "{\"list\": [ { \"a\":\"atext\", \"b\":{ \"b-a\":\"batext\", \"b-b\":\"bbtext\" } }, { \"a\":\"atext2\", \"b\":{ \"b-a\":\"batext2\", \"b-b\":\"bbtext2\" } } ] }"; List result = JsonPath.read(json, "$.list[?]", Filter.filter(Criteria.where("b.b-a").eq("batext2"))); assertTrue(result.size() == 1); } }