diff --git a/json-path-assert/pom.xml b/json-path-assert/pom.xml index 017867cd..dcb5aa80 100644 --- a/json-path-assert/pom.xml +++ b/json-path-assert/pom.xml @@ -1,58 +1,58 @@ - - - - 4.0.0 - - json-path-parent - com.jayway.jsonpath - 0.5.6-SNAPSHOT - - com.jayway.jsonpath - json-path-assert - 0.5.6-SNAPSHOT - json-path-assert - http://code.google.com/p/json-path/ - - - com.jayway.jsonpath - json-path - ${project.version} - - - - junit - junit - test - - - - - net.minidev - json-smart - - - - org.hamcrest - hamcrest-library - - - + + + + 4.0.0 + + json-path-parent + com.jayway.jsonpath + 0.5.6 + + com.jayway.jsonpath + json-path-assert + 0.5.6 + json-path-assert + http://code.google.com/p/json-path/ + + + com.jayway.jsonpath + json-path + ${project.version} + + + + junit + junit + test + + + + + net.minidev + json-smart + + + + org.hamcrest + hamcrest-library + + + diff --git a/json-path-assert/pom.xml.releaseBackup b/json-path-assert/pom.xml.releaseBackup new file mode 100755 index 00000000..017867cd --- /dev/null +++ b/json-path-assert/pom.xml.releaseBackup @@ -0,0 +1,58 @@ + + + + 4.0.0 + + json-path-parent + com.jayway.jsonpath + 0.5.6-SNAPSHOT + + com.jayway.jsonpath + json-path-assert + 0.5.6-SNAPSHOT + json-path-assert + http://code.google.com/p/json-path/ + + + com.jayway.jsonpath + json-path + ${project.version} + + + + junit + junit + test + + + + + net.minidev + json-smart + + + + org.hamcrest + hamcrest-library + + + diff --git a/json-path-assert/src/main/java/com/jayway/jsonassert/impl/JsonAsserterImpl.java b/json-path-assert/src/main/java/com/jayway/jsonassert/impl/JsonAsserterImpl.java index b4ec86b4..6cbed17b 100644 --- a/json-path-assert/src/main/java/com/jayway/jsonassert/impl/JsonAsserterImpl.java +++ b/json-path-assert/src/main/java/com/jayway/jsonassert/impl/JsonAsserterImpl.java @@ -43,10 +43,8 @@ public class JsonAsserterImpl implements JsonAsserter { String reason = "When processing json path: " + path; JsonElement je = JsonPath.read(jsonObject, path); - if (!( (je == null && matcher.matches(je)) || (je.isContainer() && matcher.matches(je)) || matcher.matches(je.toObject()) ) ) { - + if (! (matcher.matches(je)||matcher.matches(je.toObject()))){ System.out.println(JsonPath.read(jsonObject, path).toString()); - throw new AssertionError(reason + matcher.toString()); } @@ -80,8 +78,8 @@ public class JsonAsserterImpl implements JsonAsserter { */ public JsonAsserter assertNotDefined(String path) throws JsonException { JsonElement o = JsonPath.read(jsonObject, path); - - if (!o.isJsonNull()) { + + if (o!=null && !o.isJsonNull()) { throw new AssertionError(format("Document contains the path <%s> but was expected not to.", path)); } return this; diff --git a/json-path-assert/src/test/java/com/jayway/jsonassert/JsonAssertTest.java b/json-path-assert/src/test/java/com/jayway/jsonassert/JsonAssertTest.java index 1b411b1a..b1e8c866 100644 --- a/json-path-assert/src/test/java/com/jayway/jsonassert/JsonAssertTest.java +++ b/json-path-assert/src/test/java/com/jayway/jsonassert/JsonAssertTest.java @@ -85,7 +85,7 @@ public abstract class JsonAssertTest { @Test public void a_value_can_asserted_to_be_null() throws Exception { - with(JSON).assertNull("$.store.bicycle.nullValue"); + with(JSON).assertEquals("$.store.bicycle.nullValue",factory.createJsonPrimitive(null)); } @Test @@ -96,8 +96,8 @@ public abstract class JsonAssertTest { @Test public void a_path_can_be_asserted_with_matcher() throws Exception { - with(JSON).assertThat("$.store.bicycle.color", equalTo("red")) - .assertThat("$.store.bicycle.price", equalTo(19.95D)); + with(JSON).assertThat("$.store.bicycle.color", equalTo(w("red"))) + .assertThat("$.store.bicycle.price", equalTo(w(19.95D))); } @Test @@ -158,21 +158,22 @@ public abstract class JsonAssertTest { @Test public void a_path_can_be_asserted_equal_to() throws Exception { - with(JSON).assertEquals("$.store.book[0].title", "Sayings of the Century") - .assertThat("$.store.book[0].title", equalTo("Sayings of the Century")); + with(JSON).assertEquals("$.store.book[0].title", w("Sayings of the Century")) + .assertThat("$.store.book[0].title", equalTo(w("Sayings of the Century"))); - with(JSON).assertEquals("$['store']['book'][0].['title']", "Shttp://www.theregister.co.uk/public_sector/government/ayings of the Century") - .assertThat("$['store'].book[0].title", equalTo("Sayings of the Century")); + with(JSON).assertEquals("$['store']['book'][0].['title']", w("Sayings of the Century")) + .assertThat("$['store'].book[0].title", equalTo(w("Sayings of the Century"))); } @Test public void no_hit_returns_null() throws Exception { - with(JSON).assertThat("$.store.book[1000].title", Matchers.nullValue()); + with(JSON).assertThat("$.store.book[1000]", equalTo(null)); } @Test public void invalid_path() throws Exception { - with(JSON).assertThat("$.store.book[*].fooBar.(value)", Matchers.nullValue()); + + with(JSON).assertThat("$.store.book[*].fooBar", Matchers.nullValue()); } @Test diff --git a/json-path-assert/src/test/java/com/jayway/jsonassert/JsonAssertTest.java.orig b/json-path-assert/src/test/java/com/jayway/jsonassert/JsonAssertTest.java.orig new file mode 100644 index 00000000..254feebd --- /dev/null +++ b/json-path-assert/src/test/java/com/jayway/jsonassert/JsonAssertTest.java.orig @@ -0,0 +1,192 @@ +package com.jayway.jsonassert; + +import org.hamcrest.Matcher; +import org.hamcrest.Matchers; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.jayway.jsonpath.json.JsonElement; +import com.jayway.jsonpath.json.JsonException; +import com.jayway.jsonpath.json.JsonFactory; + +import java.io.InputStream; + +import static com.jayway.jsonassert.JsonAssert.*; +import static org.hamcrest.Matchers.*; + +/** + * User: kalle stenflo + * Date: 1/21/11 + * Time: 4:04 PM + */ +public abstract class JsonAssertTest { + + public final static 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\": \"Evelyn Waugh\",\n" + + " \"title\": \"Sword of Honour\",\n" + + " \"price\": 12.99\n" + + " },\n" + + " { \"category\": \"fiction\",\n" + + " \"author\": \"Herman Melville\",\n" + + " \"title\": \"Moby Dick\",\n" + + " \"isbn\": \"0-553-21311-3\",\n" + + " \"price\": 8.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" + + " }\n" + + " ],\n" + + " \"bicycle\": {\n" + + " \"color\": \"red\",\n" + + " \"price\": 19.95\n," + + " \"nullValue\": null\n" + + " }\n" + + " }\n" + + "}"; + + protected JsonFactory factory = null; + @Before + public void init(){ + init_factory(); + } + protected void init_factory(){ + + + } + + @Test + public void links_document() throws Exception { + + with(getResourceAsStream("links.json")).assertEquals("count", 2) + .assertThat("links.gc:this.href", endsWith("?pageNumber=1&pageSize=2")) + .assertNotDefined("links.gc:prev") + .assertNotDefined("links.gc:next") + .assertThat("rows", collectionWithSize(equalTo(2))); + + } + + + @Test + public void a_document_can_be_expected_not_to_contain_a_path() throws Exception { + with(JSON).assertNotDefined("$.store.bicycle.cool"); + } + + @Test + public void a_value_can_asserted_to_be_null() throws Exception { + with(JSON).assertNull("$.store.bicycle.nullValue"); + } + + @Test + public void ends_with_evalueates() throws Exception { + with(JSON).assertThat("$.store.book[0].category", endsWith("nce")); + } + + @Test + public void a_path_can_be_asserted_with_matcher() throws Exception { + + with(JSON).assertThat("$.store.bicycle.color", equalTo("red")) + .assertThat("$.store.bicycle.price", equalTo(19.95D)); + } + + @Test + public void list_content_can_be_asserted_with_matcher() throws Exception { + + with(JSON).assertThat("$..book[*].author", hasItems( factory.createJsonPrimitive("Nigel Rees"), factory.createJsonPrimitive("Evelyn Waugh"), factory.createJsonPrimitive("Herman Melville"),factory.createJsonPrimitive( "J. R. R. Tolkien"))); + + with(JSON).assertThat("$..author.(value)", hasItems( factory.createJsonPrimitive("Nigel Rees"), factory.createJsonPrimitive("Evelyn Waugh"), factory.createJsonPrimitive("Herman Melville"),factory.createJsonPrimitive( "J. R. R. Tolkien"))) + .assertThat("$..author.(value)", is(collectionWithSize(equalTo(4)))); + } + + @Test + public void list_content_can_be_asserted_with_nested_matcher() throws Exception { + with(JSON).assertThat("$..book[*]", hasItems(hasEntry("author", factory.createJsonPrimitive("Nigel Rees")), hasEntry("author", factory.createJsonPrimitive("Evelyn Waugh")))); + } + + @Test + public void map_content_can_be_asserted_with_matcher() throws Exception { + + with(JSON).assertThat("$.store.book[0]", hasEntry("category", w("reference"))) + .assertThat("$.store.book[0]", hasEntry("title", w("Sayings of the Century"))) + .and() + .assertThat("$..book[0]", hasEntry("category", w("reference"))) + .and() + .assertThat("$.store.book[0]", mapContainingKey(equalTo("category"))) + .and() + .assertThat("$.store.book[0]", mapContainingValue(equalTo( w("reference")))); + + with(JSON).assertThat("$.['store'].['book'][0]", hasEntry(w("category"), w("reference"))) + .assertThat("$.['store'].['book'][0]", hasEntry("title", w("Sayings of the Century"))) + .and() + .assertThat("$..['book'][0]", hasEntry(w("category"), w("reference"))) + .and() + .assertThat("$.['store'].['book'][0]", mapContainingKey(equalTo(("category")))) + .and() + .assertThat("$.['store'].['book'][0]", mapContainingValue(equalTo(w("reference")))); + } + + + private JsonElement w(Object obj) throws JsonException { + return factory.createJsonPrimitive(obj); + + } + private JsonElement[] w(Object ... objs) throws JsonException { + JsonElement je[]= new JsonElement[objs.length]; + for(int i=0;i>>>>>> 814bcc116cd67c4666a62ed8e2b9ff93aa46551d + .assertThat("$['store'].book[0].title", equalTo("Sayings of the Century")); + } + + @Test + public void no_hit_returns_null() throws Exception { + with(JSON).assertThat("$.store.book[1000].title", Matchers.nullValue()); + } + + @Test + public void invalid_path() throws Exception { + with(JSON).assertThat("$.store.book[*].fooBar.(value)", Matchers.nullValue()); + } + + @Test + public void path_including_wildcard_path_followed_by_another_path_concatenates_results_to_list() throws Exception { + with(getResourceAsStream("lotto.json")).assertThat("lotto.winners[*].winnerId", hasItems( factory.createJsonPrimitive(23), factory.createJsonPrimitive(54))); + } + + + private InputStream getResourceAsStream(String resourceName) { + return getClass().getClassLoader().getResourceAsStream(resourceName); + } + +} diff --git a/json-path/pom.xml b/json-path/pom.xml index 66c38066..32ba25b9 100644 --- a/json-path/pom.xml +++ b/json-path/pom.xml @@ -19,12 +19,12 @@ com.jayway.jsonpath json-path-parent - 0.5.6-SNAPSHOT + 0.5.6 com.jayway.jsonpath json-path jar - 0.5.6-SNAPSHOT + 0.5.6 json-path http://code.google.com/p/json-path/ diff --git a/json-path/pom.xml.orig b/json-path/pom.xml.orig new file mode 100644 index 00000000..2f76143e --- /dev/null +++ b/json-path/pom.xml.orig @@ -0,0 +1,173 @@ +<<<<<<< HEAD + + + + 4.0.0 + + com.jayway.jsonpath + json-path-parent + 0.5.6-SNAPSHOT + + com.jayway.jsonpath + json-path + jar + 0.5.6-SNAPSHOT + json-path + http://code.google.com/p/json-path/ + + + + net.minidev + json-smart + + + + commons-lang + commons-lang + + + + + com.google.code.gson + gson + 1.6 + compile + + + + net.minidev + json-smart + 1.0.6.3 + + + + org.slf4j + slf4j-api + 1.5.11 + jar + + + org.slf4j + slf4j-log4j12 + 1.5.11 + jar + + + org.slf4j + slf4j-api + 1.5.11 + jar + + + log4j + log4j + 1.2.15 + + + + + + junit + junit + test + + + + org.hamcrest + hamcrest-library + test + + + + +======= + + + + 4.0.0 + + com.jayway.jsonpath + json-path-parent + 0.5.6-SNAPSHOT + + com.jayway.jsonpath + json-path + jar + 0.5.6-SNAPSHOT + json-path + http://code.google.com/p/json-path/ + + + + net.minidev + json-smart + + + + commons-lang + commons-lang + + + + net.minidev + json-smart + 1.0.6.3 + + + + + + + + junit + junit + test + + + + org.hamcrest + hamcrest-library + test + + + + +>>>>>>> 814bcc116cd67c4666a62ed8e2b9ff93aa46551d diff --git a/json-path/pom.xml.releaseBackup b/json-path/pom.xml.releaseBackup new file mode 100755 index 00000000..66c38066 --- /dev/null +++ b/json-path/pom.xml.releaseBackup @@ -0,0 +1,100 @@ + + + + 4.0.0 + + com.jayway.jsonpath + json-path-parent + 0.5.6-SNAPSHOT + + com.jayway.jsonpath + json-path + jar + 0.5.6-SNAPSHOT + json-path + http://code.google.com/p/json-path/ + + + + net.minidev + json-smart + + + + commons-lang + commons-lang + + + + + com.google.code.gson + gson + 1.6 + compile + + + + net.minidev + json-smart + 1.0.6.3 + + + + org.slf4j + slf4j-api + 1.5.11 + jar + + + org.slf4j + slf4j-log4j12 + 1.5.11 + jar + + + org.slf4j + slf4j-api + 1.5.11 + jar + + + log4j + log4j + 1.2.15 + + + + + + junit + junit + test + + + + org.hamcrest + hamcrest-library + test + + + + diff --git a/json-path/release.properties b/json-path/release.properties new file mode 100755 index 00000000..accc1299 --- /dev/null +++ b/json-path/release.properties @@ -0,0 +1,8 @@ +#release configuration +#Fri Jul 29 12:36:19 PDT 2011 +preparationGoals=clean verify +scm.commentPrefix=[maven-release-plugin] +remoteTagging=true +exec.additionalArguments=-Psonatype-oss-release -P wdig-dev +completedPhase=scm-check-modifications +scm.url=scm\:git\:ssh\://git@github.com/jayway/JsonPath.git/json-path diff --git a/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java b/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java index 8b8a3b5e..edcf785e 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java +++ b/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java @@ -135,9 +135,11 @@ public class JsonPath { FilterOutput filterOutput = filters.filter(json); if (filterOutput == null || filterOutput.getResult() == null) { - throw new JsonException("Element not found"); + return null; } + + return filterOutput.getResult(); } diff --git a/json-path/src/main/java/com/jayway/jsonpath/PathUtil.java.orig b/json-path/src/main/java/com/jayway/jsonpath/PathUtil.java.orig new file mode 100644 index 00000000..cf71210e --- /dev/null +++ b/json-path/src/main/java/com/jayway/jsonpath/PathUtil.java.orig @@ -0,0 +1,163 @@ +<<<<<<< HEAD +package com.jayway.jsonpath; + +import java.util.LinkedList; +import java.util.List; +import java.util.Scanner; + +/** + * User: kalle stenflo + * Date: 2/2/11 + * Time: 2:08 PM + */ +public class PathUtil { + + /** + * Checks if a path points to a single item or if it potentially returns multiple items + *

+ * a path is considered not definite if it contains a scan fragment ".." + * or an array position fragment that is not based on a single index + *

+ *

+ * absolute path examples: + *

+ * $store.book + * $store.book[1].value + *

+ * not absolute path examples + *

+ * $..book + * $.store.book[1,2] + * $.store.book[?(@.category = 'fiction')] + * + * @param jsonPath the path to check + * @return true if path is definite (points to single item) + */ + public static boolean isPathDefinite(String jsonPath) { + //return !jsonPath.replaceAll("\"[^\"\\\\\\n\r]*\"", "").matches(".*(\\.\\.|\\*|\\[[\\\\/]|\\?|,|:|>|\\(|<|=|\\+).*"); + return !jsonPath.replaceAll("\"[^\"\\\\\\n\r]*\"", "").matches(".*(\\.\\.|\\*|\\[[\\\\/]|\\?|,|:\\s?\\]|\\[\\s?:|>|\\(|<|=|\\+).*"); + } + + /** + * Splits a path into fragments + *

+ * the path $.store.book[1].category returns ["$", "store", "book", "[1]", "category"] + * + * @param jsonPath path to split + * @return fragments + */ + public static List splitPath(String jsonPath) { + + LinkedList fragments = new LinkedList(); + + if (!jsonPath.startsWith("$.")) { + jsonPath = "$." + jsonPath; + } + + jsonPath = jsonPath.replace("$['","$.['") + .replaceAll("\\['(\\w+)\\.(\\w+)']", "['$1~$2']") + .replace("']['","'].['" ) + .replace("..", ".~~.") + .replace("[", ".[") + .replace("@.", "@") + .replace("['", "") + .replace("']", ""); + + String[] split = jsonPath.split("\\."); + + for (int i = 0; i < split.length; i++) { + if (split[i].trim().isEmpty()) { + continue; + } + fragments.add(split[i].replace("@", "@.").replace("~", ".")); + } + + //for (String fragment : fragments) { + // System.out.println(fragment); + //} + + return fragments; + } +} +======= +package com.jayway.jsonpath; + +import java.util.LinkedList; +import java.util.List; +import java.util.Scanner; + +/** + * User: kalle stenflo + * Date: 2/2/11 + * Time: 2:08 PM + */ +public class PathUtil { + + /** + * Checks if a path points to a single item or if it potentially returns multiple items + *

+ * a path is considered not definite if it contains a scan fragment ".." + * or an array position fragment that is not based on a single index + *

+ *

+ * absolute path examples: + *

+ * $store.book + * $store.book[1].value + *

+ * not absolute path examples + *

+ * $..book + * $.store.book[1,2] + * $.store.book[?(@.category = 'fiction')] + * + * @param jsonPath the path to check + * @return true if path is definite (points to single item) + */ + public static boolean isPathDefinite(String jsonPath) { + //return !jsonPath.replaceAll("\"[^\"\\\\\\n\r]*\"", "").matches(".*(\\.\\.|\\*|\\[[\\\\/]|\\?|,|:|>|\\(|<|=|\\+).*"); + return !jsonPath.replaceAll("\"[^\"\\\\\\n\r]*\"", "").matches(".*(\\.\\.|\\*|\\[[\\\\/]|\\?|,|:\\s?\\]|\\[\\s?:|>|\\(|<|=|\\+).*"); + } + + /** + * Splits a path into fragments + *

+ * the path $.store.book[1].category returns ["$", "store", "book", "[1]", "category"] + * + * @param jsonPath path to split + * @return fragments + */ + public static List splitPath(String jsonPath) { + + LinkedList fragments = new LinkedList(); + + if (!jsonPath.startsWith("$.")) { + jsonPath = "$." + jsonPath; + } + + jsonPath = jsonPath.replace("$['","$.['") + .replaceAll("\\['(\\w+)\\.(\\w+)']", "['$1~$2']") + .replace("']['","'].['" ) + .replace("..", ".~~.") + .replace("[", ".[") + .replace("@.", "@") + .replace("['", "") + .replace("']", ""); + + String[] split = jsonPath.split("\\."); + + for (int i = 0; i < split.length; i++) { + if (split[i].trim().isEmpty()) { + continue; + } + fragments.add(split[i].replace("@", "@.").replace("~", ".")); + } + + //for (String fragment : fragments) { + // System.out.println(fragment); + //} + + return fragments; + } +} +>>>>>>> 814bcc116cd67c4666a62ed8e2b9ff93aa46551d diff --git a/json-path/src/main/java/com/jayway/jsonpath/filter/FilterOutput.java b/json-path/src/main/java/com/jayway/jsonpath/filter/FilterOutput.java index fb8e71fa..29cca2c8 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/filter/FilterOutput.java +++ b/json-path/src/main/java/com/jayway/jsonpath/filter/FilterOutput.java @@ -36,6 +36,11 @@ public class FilterOutput { public JsonElement getResult() throws JsonException { + for(int i=result.size()-1;i>=0;i--){ + if(result.get(i).isJsonNull()) + result.remove(i); + } + if(result.size()==0){ return null; } diff --git a/json-path/src/main/java/com/jayway/jsonpath/filter/ListIndexFilter.java b/json-path/src/main/java/com/jayway/jsonpath/filter/ListIndexFilter.java index b7e46c69..8d6b5b82 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/filter/ListIndexFilter.java +++ b/json-path/src/main/java/com/jayway/jsonpath/filter/ListIndexFilter.java @@ -43,10 +43,13 @@ public class ListIndexFilter extends JsonPathFilterBase { if (indexIsInRange(element.toJsonArray(), i)) { result.add(element.toJsonArray().get(i)); } + else{ + result.add(factory.createJsonNull(i,element.toJsonArray())); + } } } - + return result; } diff --git a/json-path/src/main/java/com/jayway/jsonpath/filter/t6508t2338.tmp b/json-path/src/main/java/com/jayway/jsonpath/filter/t6508t2338.tmp new file mode 100755 index 00000000..f1bb3276 --- /dev/null +++ b/json-path/src/main/java/com/jayway/jsonpath/filter/t6508t2338.tmp @@ -0,0 +1,60 @@ +package com.jayway.jsonpath.filter; + + +import com.jayway.jsonpath.json.JsonArray; +import com.jayway.jsonpath.json.JsonElement; +import com.jayway.jsonpath.json.JsonException; + +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import net.minidev.json.JSONValue; + +/** + * Created by IntelliJ IDEA. + * User: kallestenflo + * Date: 2/15/11 + * Time: 8:23 PM + */ +public class TypeFilter extends JsonPathFilterBase { + + //@Autowired + public com.jayway.jsonpath.json.JsonFactory factory = com.jayway.jsonpath.json.minidev.MiniJsonFactory.getInstance(); + + public static final Pattern PATTERN = Pattern.compile("\\((collection|object|value)\\)"); + + private final String pathFragment; + + + public TypeFilter(String pathFragment) { + this.pathFragment = pathFragment; + } + @Override + public String getPathSegment() throws JsonException { + return pathFragment; + } + @Override + public List apply(JsonElement element) throws JsonException { + List result = new ArrayList(); + + String prop = getFilterProperty(); + JsonType v = JsonType.fromString(pathFragment.substring(1,pathFragment.length()-1)); + + if(element.isJsonType(v)) + result.add(element); + + + return result; + } + + + private String getFilterProperty() { + Matcher matcher = PATTERN.matcher(pathFragment); + if (matcher.matches()) { + return matcher.group(1); + } + throw new IllegalArgumentException("invalid list filter property"); + } +} diff --git a/json-path/src/main/java/com/jayway/jsonpath/filter/t7664t2129.tmp b/json-path/src/main/java/com/jayway/jsonpath/filter/t7664t2129.tmp new file mode 100755 index 00000000..f1bb3276 --- /dev/null +++ b/json-path/src/main/java/com/jayway/jsonpath/filter/t7664t2129.tmp @@ -0,0 +1,60 @@ +package com.jayway.jsonpath.filter; + + +import com.jayway.jsonpath.json.JsonArray; +import com.jayway.jsonpath.json.JsonElement; +import com.jayway.jsonpath.json.JsonException; + +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import net.minidev.json.JSONValue; + +/** + * Created by IntelliJ IDEA. + * User: kallestenflo + * Date: 2/15/11 + * Time: 8:23 PM + */ +public class TypeFilter extends JsonPathFilterBase { + + //@Autowired + public com.jayway.jsonpath.json.JsonFactory factory = com.jayway.jsonpath.json.minidev.MiniJsonFactory.getInstance(); + + public static final Pattern PATTERN = Pattern.compile("\\((collection|object|value)\\)"); + + private final String pathFragment; + + + public TypeFilter(String pathFragment) { + this.pathFragment = pathFragment; + } + @Override + public String getPathSegment() throws JsonException { + return pathFragment; + } + @Override + public List apply(JsonElement element) throws JsonException { + List result = new ArrayList(); + + String prop = getFilterProperty(); + JsonType v = JsonType.fromString(pathFragment.substring(1,pathFragment.length()-1)); + + if(element.isJsonType(v)) + result.add(element); + + + return result; + } + + + private String getFilterProperty() { + Matcher matcher = PATTERN.matcher(pathFragment); + if (matcher.matches()) { + return matcher.group(1); + } + throw new IllegalArgumentException("invalid list filter property"); + } +} diff --git a/json-path/src/main/java/com/jayway/jsonpath/json/JsonNull.java b/json-path/src/main/java/com/jayway/jsonpath/json/JsonNull.java index 2c04c472..a0282cf1 100755 --- a/json-path/src/main/java/com/jayway/jsonpath/json/JsonNull.java +++ b/json-path/src/main/java/com/jayway/jsonpath/json/JsonNull.java @@ -60,5 +60,9 @@ public class JsonNull extends JsonElement { } - + @Override + public boolean equals(Object o1){ + if(o1==null) return false; + return o1 instanceof JsonNull; + } } diff --git a/json-path/src/main/java/com/jayway/jsonpath/json/gson/GsonJsonPrimitive.java b/json-path/src/main/java/com/jayway/jsonpath/json/gson/GsonJsonPrimitive.java index 8199c387..933abe76 100755 --- a/json-path/src/main/java/com/jayway/jsonpath/json/gson/GsonJsonPrimitive.java +++ b/json-path/src/main/java/com/jayway/jsonpath/json/gson/GsonJsonPrimitive.java @@ -93,6 +93,13 @@ public class GsonJsonPrimitive extends com.jayway.jsonpath.json.JsonPrimitive{ @Override public boolean equals(Object o1){ + if (o1==null) return false; + if(o1 instanceof GsonJsonPrimitive){ + if(value == null) + return ((GsonJsonPrimitive) o1).value == null; + else + return value.equals(((GsonJsonPrimitive) o1).value); + } return (o1!=null) && o1.equals(wrappit(value)); } @Override @@ -101,6 +108,7 @@ public class GsonJsonPrimitive extends com.jayway.jsonpath.json.JsonPrimitive{ } @Override public String toString(){ + if(value == null) return "null"; return wrappit(value).toString(); } diff --git a/json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java.orig b/json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java.orig new file mode 100644 index 00000000..fddaebaf --- /dev/null +++ b/json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java.orig @@ -0,0 +1,538 @@ +<<<<<<< HEAD +package com.jayway.jsonpath; + +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; + +import com.google.gson.JsonNull; +import com.jayway.jsonpath.json.JsonArray; +import com.jayway.jsonpath.json.JsonElement; +import com.jayway.jsonpath.json.JsonException; +import com.jayway.jsonpath.json.JsonFactory; +import com.jayway.jsonpath.json.JsonObject; + + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Scanner; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasItems; +import static org.junit.Assert.*; + +/** + * Created by IntelliJ IDEA. + * User: kallestenflo + * Date: 2/2/11 + * Time: 3:07 PM + */ +public abstract class JsonPathTest { + + public final static String ARRAY = "[{\"value\": 1},{\"value\": 2}, {\"value\": 3},{\"value\": 4}]"; + + public final static String DOCUMENT = + "{ \"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\": \"Evelyn Waugh\",\n" + + " \"title\": \"Sword of Honour\",\n" + + " \"price\": 12.99\n" + + " },\n" + + " { \"category\": \"fiction\",\n" + + " \"author\": \"Herman Melville\",\n" + + " \"title\": \"Moby Dick\",\n" + + " \"isbn\": \"0-553-21311-3\",\n" + + " \"price\": 8.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" + + " }\n" + + " ],\n" + + " \"bicycle\": {\n" + + " \"color\": \"red\",\n" + + " \"price\": 19.95,\n" + + " \"foo:bar\": \"fooBar\",\n" + + " \"dot.notation\": \"new\"\n" + + " }\n" + + " }\n" + + "}"; + + protected JsonFactory factory = null; + @Before + public void init_factory(){ + + } + @Test + public void bracket_notation_can_be_used_in_path() throws Exception { + + + assertEquals(w("new"), JsonPath.read(DOCUMENT, "$.['store'].bicycle.['dot.notation']")); + assertEquals(w("new"), JsonPath.read(DOCUMENT, "$['store']['bicycle']['dot.notation']")); + assertEquals(w("new"), JsonPath.read(DOCUMENT, "$.['store']['bicycle']['dot.notation']")); + assertEquals(w("new"), JsonPath.read(DOCUMENT, "$.['store'].['bicycle'].['dot.notation']")); + } + + @Test + public void filter_an_array() throws Exception { + JsonElement matches = JsonPath.read(ARRAY, "$.[?(@.value = 1)]"); + + assertEquals(true, matches.isJsonObject()); + System.out.println(matches); + } + + @Test + public void read_path_with_colon() throws Exception { + + assertEquals(JsonPath.read(DOCUMENT, "$.store.bicycle.foo:bar"), w("fooBar")); + assertEquals(JsonPath.read(DOCUMENT, "$.['store'].['bicycle'].['foo:bar']"), w("fooBar")); + } + + + + private JsonElement w(Object obj) throws JsonException { + return factory.createJsonPrimitive(obj); + + } + private JsonElement[] w(Object ... objs) throws JsonException { + JsonElement je[]= new JsonElement[objs.length]; + for(int i=0;i l1 = toList(JsonPath.read(DOCUMENT, "$.store.book[0,1].author")); + assertThat(l1, hasItems("Nigel Rees", "Evelyn Waugh")); + + Iterable l2 = toList(JsonPath.read(DOCUMENT, "$.store.book[*].author")); + assertThat(l2, hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien")); + + Iterable l3 = toList(JsonPath.read(DOCUMENT, "$.['store'].['book'][*].['author']")); + assertThat(l3, hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien")); + + } + + + private List toList(JsonElement read) throws JsonException { + List l = new ArrayList(); + for(JsonElement e: read.toJsonArray()){ + l.add((T) e.toObject()); + } + return l ; + } + + @Test + public void all_authors() throws Exception { + Iterable l1 = toList(JsonPath.read(DOCUMENT, "$..author")); + assertThat(l1, hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien")); + } + + + @Test + public void all_store_properties() throws Exception { + JsonArray itemsInStore = JsonPath.read(DOCUMENT, "$.store.*").toJsonArray(); + + assertEquals(JsonPath.read(itemsInStore, "$.[0].[0].author").toObject(), "Nigel Rees"); + assertEquals(JsonPath.read(itemsInStore, "$.[0][0].author").toObject(), "Nigel Rees"); + } + + @Test + public void all_prices_in_store() throws Exception { + + assertThat(this.toList(JsonPath.read(DOCUMENT, "$.store..price")), hasItems(8.95D, 12.99D, 8.99D, 19.95D)); + + } + + @Test + public void access_array_by_index_from_tail() throws Exception { + + assertThat((String)JsonPath.read(DOCUMENT, "$..book[(@.length-1)].author").toObject(), equalTo("J. R. R. Tolkien")); + assertThat((String)JsonPath.read(DOCUMENT, "$..book[-1:].author").toObject(), equalTo("J. R. R. Tolkien")); + } + + @Test + public void read_store_book_index_0_and_1() throws Exception { + + assertThat(this.toList(JsonPath.read(DOCUMENT, "$.store.book[0,1].author")), hasItems("Nigel Rees", "Evelyn Waugh")); + assertTrue(this.toList(JsonPath.read(DOCUMENT, "$.store.book[0,1].author")).size() == 2); + } + + @Test + public void read_store_book_pull_first_2() throws Exception { + + assertThat(this.toList(JsonPath.read(DOCUMENT, "$.store.book[:2].author")), hasItems("Nigel Rees", "Evelyn Waugh")); + assertTrue(this.toList(JsonPath.read(DOCUMENT, "$.store.book[:2].author")).size() == 2); + } + + + @Test + public void read_store_book_filter_by_isbn() throws Exception { + + assertThat(this.toList(JsonPath.read(DOCUMENT, "$.store.book[?(@.isbn)].isbn")), hasItems("0-553-21311-3", "0-395-19395-8")); + assertTrue(this.toList(JsonPath.read(DOCUMENT, "$.store.book[?(@.isbn)].isbn")).size() == 2); + } + + @Test + public void all_books_cheaper_than_10() throws Exception { + Object o = JsonPath.read(DOCUMENT, "$.store.book[?(@.price < 10)].title"); + assertThat(this.toList(JsonPath.read(DOCUMENT, "$.store.book[?(@.price < 10)].title")), hasItems("Sayings of the Century", "Moby Dick")); + + } + + @Test + public void all_books_with_category_reference() throws Exception { + + Object o = JsonPath.read(DOCUMENT, "$..book[?(@.category = 'reference')].title"); + assertEquals(JsonPath.read(DOCUMENT, "$..book[?(@.category = 'reference')].title").toObject(), "Sayings of the Century"); + + } + + @Test + public void all_members_of_all_documents() throws Exception { + JsonPath.read(DOCUMENT, "$..*"); + } + + @Test + public void access_index_out_of_bounds_does_not_throw_exception() throws Exception { + + JsonElement res = JsonPath.read(DOCUMENT, "$.store.book[100].author"); + + assertTrue(res.isJsonNull()); + + JsonElement res2 = JsonPath.read(DOCUMENT, "$.store.book[1, 200].author"); + JsonElement res3 = JsonPath.read(DOCUMENT, "$.store.book[1, 200]"); + assertEquals(res2.toObject(), "Evelyn Waugh"); + System.out.println(res3.toJsonArray().get(1)); + + + + //assertNull((); + } + + /* + @Test(expected = InvalidPathException.class) + public void invalid_space_path_throws_exception() throws Exception { + JsonPath.read(DOCUMENT, "space is not good"); + } + */ + + @Test(expected = InvalidPathException.class) + public void invalid_new_path_throws_exception() throws Exception { + JsonPath.read(DOCUMENT, "new "); + } + + + +} +======= +package com.jayway.jsonpath; + +import org.junit.Test; + +import java.util.List; +import java.util.Map; +import java.util.Scanner; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasItems; +import static org.junit.Assert.*; + +/** + * Created by IntelliJ IDEA. + * User: kallestenflo + * Date: 2/2/11 + * Time: 3:07 PM + */ +public class JsonPathTest { + + public final static String ARRAY = "[{\"value\": 1},{\"value\": 2}, {\"value\": 3},{\"value\": 4}]"; + + public final static String DOCUMENT = + "{ \"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\": \"Evelyn Waugh\",\n" + + " \"title\": \"Sword of Honour\",\n" + + " \"price\": 12.99\n" + + " },\n" + + " { \"category\": \"fiction\",\n" + + " \"author\": \"Herman Melville\",\n" + + " \"title\": \"Moby Dick\",\n" + + " \"isbn\": \"0-553-21311-3\",\n" + + " \"price\": 8.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" + + " }\n" + + " ],\n" + + " \"bicycle\": {\n" + + " \"color\": \"red\",\n" + + " \"price\": 19.95,\n" + + " \"foo:bar\": \"fooBar\",\n" + + " \"dot.notation\": \"new\"\n" + + " }\n" + + " }\n" + + "}"; + + @Test + public void bracket_notation_can_be_used_in_path() throws Exception { + + assertEquals("new", JsonPath.read(DOCUMENT, "$.['store'].bicycle.['dot.notation']")); + assertEquals("new", JsonPath.read(DOCUMENT, "$['store']['bicycle']['dot.notation']")); + assertEquals("new", JsonPath.read(DOCUMENT, "$.['store']['bicycle']['dot.notation']")); + assertEquals("new", JsonPath.read(DOCUMENT, "$.['store'].['bicycle'].['dot.notation']")); + } + + @Test + public void filter_an_array() throws Exception { + List matches = JsonPath.read(ARRAY, "$.[?(@.value = 1)]"); + + assertEquals(1, matches.size()); + System.out.println(matches); + } + + @Test + public void read_path_with_colon() throws Exception { + + assertEquals(JsonPath.read(DOCUMENT, "$.store.bicycle.foo:bar"), "fooBar"); + assertEquals(JsonPath.read(DOCUMENT, "$['store']['bicycle']['foo:bar']"), "fooBar"); + } + + @Test + public void read_document_from_root() throws Exception { + + Map result = JsonPath.read(DOCUMENT, "$.store"); + + assertEquals(2, result.values().size()); + + + } + + + @Test + public void read_store_book_1() throws Exception { + + JsonPath path = JsonPath.compile("$.store.book[1]"); + + Map map = path.read(DOCUMENT); + + assertEquals("Evelyn Waugh", map.get("author")); + } + + @Test + public void read_store_book_wildcard() throws Exception { + JsonPath path = JsonPath.compile("$.store.book[*]"); + + List list = path.read(DOCUMENT); + + } + + @Test + public void read_store_book_author() throws Exception { + assertThat(JsonPath.>read(DOCUMENT, "$.store.book[0,1].author"), hasItems("Nigel Rees", "Evelyn Waugh")); + assertThat(JsonPath.>read(DOCUMENT, "$.store.book[*].author"), hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien")); + assertThat(JsonPath.>read(DOCUMENT, "$.['store'].['book'][*].['author']"), hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien")); + assertThat(JsonPath.>read(DOCUMENT, "$['store']['book'][*]['author']"), hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien")); + assertThat(JsonPath.>read(DOCUMENT, "$['store'].book[*]['author']"), hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien")); + } + + + @Test + public void all_authors() throws Exception { + assertThat(JsonPath.>read(DOCUMENT, "$..author"), hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien")); + } + + + @Test + public void all_store_properties() throws Exception { + List itemsInStore = JsonPath.read(DOCUMENT, "$.store.*"); + + assertEquals(JsonPath.read(itemsInStore, "$.[0].[0].author"), "Nigel Rees"); + assertEquals(JsonPath.read(itemsInStore, "$.[0][0].author"), "Nigel Rees"); + } + + @Test + public void all_prices_in_store() throws Exception { + + assertThat(JsonPath.>read(DOCUMENT, "$.store..price"), hasItems(8.95D, 12.99D, 8.99D, 19.95D)); + + } + + @Test + public void access_array_by_index_from_tail() throws Exception { + + assertThat(JsonPath.read(DOCUMENT, "$..book[(@.length-1)].author"), equalTo("J. R. R. Tolkien")); + assertThat(JsonPath.read(DOCUMENT, "$..book[-1:].author"), equalTo("J. R. R. Tolkien")); + } + + @Test + public void read_store_book_index_0_and_1() throws Exception { + + assertThat(JsonPath.>read(DOCUMENT, "$.store.book[0,1].author"), hasItems("Nigel Rees", "Evelyn Waugh")); + assertTrue(JsonPath.read(DOCUMENT, "$.store.book[0,1].author").size() == 2); + } + + @Test + public void read_store_book_pull_first_2() throws Exception { + + assertThat(JsonPath.>read(DOCUMENT, "$.store.book[:2].author"), hasItems("Nigel Rees", "Evelyn Waugh")); + assertTrue(JsonPath.read(DOCUMENT, "$.store.book[:2].author").size() == 2); + } + + + @Test + public void read_store_book_filter_by_isbn() throws Exception { + + assertThat(JsonPath.>read(DOCUMENT, "$.store.book[?(@.isbn)].isbn"), hasItems("0-553-21311-3", "0-395-19395-8")); + assertTrue(JsonPath.read(DOCUMENT, "$.store.book[?(@.isbn)].isbn").size() == 2); + } + + @Test + public void all_books_cheaper_than_10() throws Exception { + + assertThat(JsonPath.>read(DOCUMENT, "$.store.book[?(@.price < 10)].title"), hasItems("Sayings of the Century", "Moby Dick")); + + } + + @Test + public void all_books_with_category_reference() throws Exception { + + assertThat(JsonPath.>read(DOCUMENT, "$..book[?(@.category = 'reference')].title"), hasItems("Sayings of the Century")); + + } + + @Test + public void all_members_of_all_documents() throws Exception { + List all = JsonPath.read(DOCUMENT, "$..*"); + } + + @Test + public void access_index_out_of_bounds_does_not_throw_exception() throws Exception { + + Object res = JsonPath.read(DOCUMENT, "$.store.book[100].author"); + + assertNull(res); + + res = JsonPath.read(DOCUMENT, "$.store.book[1, 200].author"); + + + assertThat((List) res, hasItems("Evelyn Waugh")); + //assertNull((); + } + + /* + @Test(expected = InvalidPathException.class) + public void invalid_space_path_throws_exception() throws Exception { + JsonPath.read(DOCUMENT, "space is not good"); + } + */ + + @Test(expected = InvalidPathException.class) + public void invalid_new_path_throws_exception() throws Exception { + JsonPath.read(DOCUMENT, "new "); + } + +} +>>>>>>> 814bcc116cd67c4666a62ed8e2b9ff93aa46551d diff --git a/json-path/src/test/java/com/jayway/jsonpath/SplitPathFragmentsTest.java.orig b/json-path/src/test/java/com/jayway/jsonpath/SplitPathFragmentsTest.java.orig new file mode 100644 index 00000000..9d1ec0c3 --- /dev/null +++ b/json-path/src/test/java/com/jayway/jsonpath/SplitPathFragmentsTest.java.orig @@ -0,0 +1,167 @@ +<<<<<<< HEAD +package com.jayway.jsonpath; + +import org.junit.Test; + +import static org.hamcrest.Matchers.hasItemInArray; +import static org.hamcrest.Matchers.hasItems; +import static org.junit.Assert.assertThat; + +/** + * Created by IntelliJ IDEA. + * User: kallestenflo + * Date: 2/2/11 + * Time: 1:22 PM + */ +public class SplitPathFragmentsTest { + + /* + 1. "$..book[-1:].foo.bar" + 2. "$.store.book[*].author" + 3. "$..author" + 4. "$.store.*" + 5. "$.store..price" + 6. "$..book[(@.length-1)]" + 7. "$..book[-1:] + 8. "$..book[0,1]" + 9. "$..book[:2]" + 10. "$..book[?(@.isbn)]" + 11. "$..book[?(@.price<10)]" + 12. "$..*" + */ + + + @Test + public void bracket_notation_can_be_split() throws Exception { + assertThat(PathUtil.splitPath("$.['store'].['price']"), hasItems("$", "store", "price")); + assertThat(PathUtil.splitPath("$['store']['price']"), hasItems("$", "store", "price")); + assertThat(PathUtil.splitPath("['store']['price']"), hasItems("$", "store", "price")); + assertThat(PathUtil.splitPath("['store'].price"), hasItems("$", "store", "price")); + + assertThat(PathUtil.splitPath("$.['store book'].['price list']"), hasItems("$", "store book", "price list")); + + assertThat(PathUtil.splitPath("$.['store.book'].['price.list']"), hasItems("$", "store.book", "price.list")); + } + + @Test + public void fragments_are_split_correctly() throws Exception { + + assertThat(PathUtil.splitPath("$..book[-1:].foo.bar"), hasItems("$", "..", "[-1:]", "foo", "bar")); + + assertThat(PathUtil.splitPath("$.store.book[*].author"), hasItems("$", "store", "book", "[*]", "author")); + + assertThat(PathUtil.splitPath("$..author"), hasItems("$", "..", "author")); + + assertThat(PathUtil.splitPath("$.store.*"), hasItems("$", "store", "*")); + + assertThat(PathUtil.splitPath("$.store..price"), hasItems("$", "store", "..", "price")); + + assertThat(PathUtil.splitPath("$..book[(@.length-1)]"), hasItems("$", "..", "book", "[(@.length-1)]")); + + assertThat(PathUtil.splitPath("$..book[-1:]"), hasItems("$", "..", "book", "[-1:]")); + + assertThat(PathUtil.splitPath("$..book[0,1]"), hasItems("$", "..", "book", "[0,1]")); + + assertThat(PathUtil.splitPath("$..book[:2]"), hasItems("$", "..", "book", "[:2]")); + + assertThat(PathUtil.splitPath("$..book[?(@.isbn)]"), hasItems("$", "..", "book", "[?(@.isbn)]")); + + assertThat(PathUtil.splitPath("$..book[?(@.price<10)]"), hasItems("$", "..", "book", "[?(@.price<10)]")); + + assertThat(PathUtil.splitPath("$..*"), hasItems("$", "..", "*")); + + assertThat(PathUtil.splitPath("$.[0][1].author"), hasItems("$", "[0]", "[1]", "author")); + + assertThat(PathUtil.splitPath("$.[0].[1].author"), hasItems("$", "[0]", "[1]", "author")); + + assertThat(PathUtil.splitPath("$.foo:bar.author"), hasItems("$", "foo:bar", "author")); + + } + + + +} +======= +package com.jayway.jsonpath; + +import org.junit.Test; + +import static org.hamcrest.Matchers.hasItemInArray; +import static org.hamcrest.Matchers.hasItems; +import static org.junit.Assert.assertThat; + +/** + * Created by IntelliJ IDEA. + * User: kallestenflo + * Date: 2/2/11 + * Time: 1:22 PM + */ +public class SplitPathFragmentsTest { + + /* + 1. "$..book[-1:].foo.bar" + 2. "$.store.book[*].author" + 3. "$..author" + 4. "$.store.*" + 5. "$.store..price" + 6. "$..book[(@.length-1)]" + 7. "$..book[-1:] + 8. "$..book[0,1]" + 9. "$..book[:2]" + 10. "$..book[?(@.isbn)]" + 11. "$..book[?(@.price<10)]" + 12. "$..*" + */ + + + @Test + public void bracket_notation_can_be_split() throws Exception { + assertThat(PathUtil.splitPath("$.['store'].['price']"), hasItems("$", "store", "price")); + assertThat(PathUtil.splitPath("$['store']['price']"), hasItems("$", "store", "price")); + assertThat(PathUtil.splitPath("['store']['price']"), hasItems("$", "store", "price")); + assertThat(PathUtil.splitPath("['store'].price"), hasItems("$", "store", "price")); + + assertThat(PathUtil.splitPath("$.['store book'].['price list']"), hasItems("$", "store book", "price list")); + + assertThat(PathUtil.splitPath("$.['store.book'].['price.list']"), hasItems("$", "store.book", "price.list")); + } + + @Test + public void fragments_are_split_correctly() throws Exception { + + assertThat(PathUtil.splitPath("$..book[-1:].foo.bar"), hasItems("$", "..", "[-1:]", "foo", "bar")); + + assertThat(PathUtil.splitPath("$.store.book[*].author"), hasItems("$", "store", "book", "[*]", "author")); + + assertThat(PathUtil.splitPath("$..author"), hasItems("$", "..", "author")); + + assertThat(PathUtil.splitPath("$.store.*"), hasItems("$", "store", "*")); + + assertThat(PathUtil.splitPath("$.store..price"), hasItems("$", "store", "..", "price")); + + assertThat(PathUtil.splitPath("$..book[(@.length-1)]"), hasItems("$", "..", "book", "[(@.length-1)]")); + + assertThat(PathUtil.splitPath("$..book[-1:]"), hasItems("$", "..", "book", "[-1:]")); + + assertThat(PathUtil.splitPath("$..book[0,1]"), hasItems("$", "..", "book", "[0,1]")); + + assertThat(PathUtil.splitPath("$..book[:2]"), hasItems("$", "..", "book", "[:2]")); + + assertThat(PathUtil.splitPath("$..book[?(@.isbn)]"), hasItems("$", "..", "book", "[?(@.isbn)]")); + + assertThat(PathUtil.splitPath("$..book[?(@.price<10)]"), hasItems("$", "..", "book", "[?(@.price<10)]")); + + assertThat(PathUtil.splitPath("$..*"), hasItems("$", "..", "*")); + + assertThat(PathUtil.splitPath("$.[0][1].author"), hasItems("$", "[0]", "[1]", "author")); + + assertThat(PathUtil.splitPath("$.[0].[1].author"), hasItems("$", "[0]", "[1]", "author")); + + assertThat(PathUtil.splitPath("$.foo:bar.author"), hasItems("$", "foo:bar", "author")); + + } + + + +} +>>>>>>> 814bcc116cd67c4666a62ed8e2b9ff93aa46551d diff --git a/json-path/src/test/resources/log4j.properties b/json-path/src/test/resources/log4j.properties new file mode 100755 index 00000000..85118a16 --- /dev/null +++ b/json-path/src/test/resources/log4j.properties @@ -0,0 +1,5 @@ +log4j.rootLogger=DEBUG, cons +log4j.appender.cons=org.apache.log4j.ConsoleAppender +log4j.appender.cons.layout=org.apache.log4j.SimpleLayout + + diff --git a/pom.xml b/pom.xml index b556da6d..3bd74014 100644 --- a/pom.xml +++ b/pom.xml @@ -1,191 +1,191 @@ - - - - 4.0.0 - - org.sonatype.oss - oss-parent - 5 - - com.jayway.jsonpath - json-path-parent - pom - 0.5.6-SNAPSHOT - http://code.google.com/p/json-path - json-path-parent-pom - Java JsonPath implementation - 2011 - - GitHub Issue Tracking - - - - - Apache 2.0 - http://www.apache.org/licenses/LICENSE-2.0.html - - - - - Kalle Stenflo - kalle.stenflo - Jayway - http://www.jayway.com - kale.stenflo at gmail.com - +1 - - Developer - - - - - master - UTF-8 - - - http://github.com/daveb/JsonPath/tree/${scm.branch} - scm:git:git://github.com/daveb/JsonPath.git - scm:git:ssh://git@github.com/daveb/JsonPath.git - - - - - JsonPath mailing-list - http://groups.google.com/group/json-path/topics - - - - - json-path - json-path-assert - - - - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - 2.3.2 - - 1.5 - 1.5 - - - - - org.apache.maven.plugins - maven-dependency-plugin - 2.1 - - - copy-dependencies - package - - copy-dependencies - - - ${project.build.directory}/dependencies/${project.version} - - false - false - true - - - - - - - - - - - release - - - - org.apache.maven.plugins - maven-javadoc-plugin - 2.7 - - - attach-javadocs - - jar - - - - - - org.apache.maven.plugins - maven-source-plugin - 2.1.2 - - - attach-sources - - jar - - - - - - - - - - - - - - - net.minidev - json-smart - 1.0.6.3 - - - - org.hamcrest - hamcrest-library - 1.1 - - - - commons-lang - commons-lang - 2.5 - - - - junit - junit - 4.8.2 - test - - - - + + + + 4.0.0 + + org.sonatype.oss + oss-parent + 5 + + com.jayway.jsonpath + json-path-parent + pom + 0.5.6 + http://code.google.com/p/json-path + json-path-parent-pom + Java JsonPath implementation + 2011 + + GitHub Issue Tracking + + + + + Apache 2.0 + http://www.apache.org/licenses/LICENSE-2.0.html + + + + + Kalle Stenflo + kalle.stenflo + Jayway + http://www.jayway.com + kale.stenflo at gmail.com + +1 + + Developer + + + + + master + UTF-8 + + + http://github.com/daveb/JsonPath/tree/${scm.branch} + scm:git:git://github.com/daveb/JsonPath.git + scm:git:ssh://git@github.com/daveb/JsonPath.git + + + + + JsonPath mailing-list + http://groups.google.com/group/json-path/topics + + + + + json-path + json-path-assert + + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.3.2 + + 1.5 + 1.5 + + + + + org.apache.maven.plugins + maven-dependency-plugin + 2.1 + + + copy-dependencies + package + + copy-dependencies + + + ${project.build.directory}/dependencies/${project.version} + + false + false + true + + + + + + + + + + + release + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.7 + + + attach-javadocs + + jar + + + + + + org.apache.maven.plugins + maven-source-plugin + 2.1.2 + + + attach-sources + + jar + + + + + + + + + + + + + + + net.minidev + json-smart + 1.0.6.3 + + + + org.hamcrest + hamcrest-library + 1.1 + + + + commons-lang + commons-lang + 2.5 + + + + junit + junit + 4.8.2 + test + + + + \ No newline at end of file diff --git a/pom.xml.releaseBackup b/pom.xml.releaseBackup new file mode 100755 index 00000000..b556da6d --- /dev/null +++ b/pom.xml.releaseBackup @@ -0,0 +1,191 @@ + + + + 4.0.0 + + org.sonatype.oss + oss-parent + 5 + + com.jayway.jsonpath + json-path-parent + pom + 0.5.6-SNAPSHOT + http://code.google.com/p/json-path + json-path-parent-pom + Java JsonPath implementation + 2011 + + GitHub Issue Tracking + + + + + Apache 2.0 + http://www.apache.org/licenses/LICENSE-2.0.html + + + + + Kalle Stenflo + kalle.stenflo + Jayway + http://www.jayway.com + kale.stenflo at gmail.com + +1 + + Developer + + + + + master + UTF-8 + + + http://github.com/daveb/JsonPath/tree/${scm.branch} + scm:git:git://github.com/daveb/JsonPath.git + scm:git:ssh://git@github.com/daveb/JsonPath.git + + + + + JsonPath mailing-list + http://groups.google.com/group/json-path/topics + + + + + json-path + json-path-assert + + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.3.2 + + 1.5 + 1.5 + + + + + org.apache.maven.plugins + maven-dependency-plugin + 2.1 + + + copy-dependencies + package + + copy-dependencies + + + ${project.build.directory}/dependencies/${project.version} + + false + false + true + + + + + + + + + + + release + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.7 + + + attach-javadocs + + jar + + + + + + org.apache.maven.plugins + maven-source-plugin + 2.1.2 + + + attach-sources + + jar + + + + + + + + + + + + + + + net.minidev + json-smart + 1.0.6.3 + + + + org.hamcrest + hamcrest-library + 1.1 + + + + commons-lang + commons-lang + 2.5 + + + + junit + junit + 4.8.2 + test + + + + + \ No newline at end of file diff --git a/release.properties b/release.properties new file mode 100755 index 00000000..8b0395f1 --- /dev/null +++ b/release.properties @@ -0,0 +1,21 @@ +#release configuration +#Fri Jul 29 12:38:04 PDT 2011 +project.scm.com.jayway.jsonpath\:json-path-assert.empty=true +scm.commentPrefix=[maven-release-plugin] +scm.tag=json-path-parent-0.5.6 +remoteTagging=true +project.dev.com.jayway.jsonpath\:json-path-assert=0.5.7-SNAPSHOT +exec.additionalArguments=-Psonatype-oss-release -P wdig-dev +project.rel.com.jayway.jsonpath\:json-path-assert=0.5.6 +project.scm.com.jayway.jsonpath\:json-path-parent.tag=HEAD +project.scm.com.jayway.jsonpath\:json-path-parent.developerConnection=scm\:git\:ssh\://git@github.com/daveb/JsonPath.git +project.scm.com.jayway.jsonpath\:json-path-parent.connection=scm\:git\:git\://github.com/daveb/JsonPath.git +project.dev.com.jayway.jsonpath\:json-path=0.5.7-SNAPSHOT +scm.url=scm\:git\:ssh\://git@github.com/daveb/JsonPath.git +preparationGoals=clean verify +project.dev.com.jayway.jsonpath\:json-path-parent=0.5.7-SNAPSHOT +project.scm.com.jayway.jsonpath\:json-path.empty=true +project.scm.com.jayway.jsonpath\:json-path-parent.url=http\://github.com/daveb/JsonPath/tree/master +project.rel.com.jayway.jsonpath\:json-path-parent=0.5.6 +project.rel.com.jayway.jsonpath\:json-path=0.5.6 +completedPhase=generate-release-poms