You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
133 lines
4.0 KiB
133 lines
4.0 KiB
14 years ago
|
package com.jayway.jsonpath;
|
||
|
|
||
13 years ago
|
import com.jayway.jsonpath.internal.PathTokenizer;
|
||
14 years ago
|
import org.hamcrest.Matcher;
|
||
14 years ago
|
import org.junit.Test;
|
||
|
|
||
|
import static org.hamcrest.Matchers.hasItems;
|
||
13 years ago
|
import static org.junit.Assert.assertFalse;
|
||
14 years ago
|
import static org.junit.Assert.assertThat;
|
||
14 years ago
|
import static org.junit.Assert.assertTrue;
|
||
14 years ago
|
|
||
|
/**
|
||
|
* Created by IntelliJ IDEA.
|
||
|
* User: kallestenflo
|
||
|
* Date: 2/2/11
|
||
|
* Time: 1:22 PM
|
||
|
*/
|
||
13 years ago
|
public class PathTest {
|
||
13 years ago
|
|
||
|
|
||
13 years ago
|
@Test
|
||
13 years ago
|
public void path_is_not_definite() throws Exception {
|
||
|
assertFalse(JsonPath.compile("$..book[0]").isPathDefinite());
|
||
|
assertFalse(JsonPath.compile("$.books[*]").isPathDefinite());
|
||
13 years ago
|
}
|
||
|
|
||
13 years ago
|
@Test
|
||
|
public void path_is_definite() throws Exception {
|
||
|
assertTrue(JsonPath.compile("$.definite.this.is").isPathDefinite());
|
||
|
assertTrue(JsonPath.compile("rows[0].id").isPathDefinite());
|
||
|
}
|
||
13 years ago
|
|
||
14 years ago
|
@Test
|
||
14 years ago
|
public void valid_path_is_split_correctly() throws Exception {
|
||
14 years ago
|
|
||
14 years ago
|
assertPath("$.store[*]", hasItems("$", "store", "[*]"));
|
||
14 years ago
|
|
||
14 years ago
|
assertPath("$", hasItems("$"));
|
||
14 years ago
|
|
||
14 years ago
|
assertPath("$..*", hasItems("$", "..", "*"));
|
||
|
|
||
|
assertPath("$.store", hasItems("$", "store"));
|
||
|
|
||
|
assertPath("$.store.*", hasItems("$", "store", "*"));
|
||
|
|
||
|
assertPath("$.store[*].name", hasItems("$", "store", "[*]", "name"));
|
||
|
|
||
|
assertPath("$..book[-1:].foo.bar", hasItems("$", "..", "book", "[-1:]", "foo", "bar"));
|
||
|
|
||
|
assertPath("$..book[?(@.isbn)]", hasItems("$", "..", "book", "[?(@.isbn)]"));
|
||
|
|
||
|
assertPath("['store'].['price']", hasItems("$", "store", "price"));
|
||
|
|
||
|
assertPath("$.['store'].['price']", hasItems("$", "store", "price"));
|
||
14 years ago
|
|
||
14 years ago
|
assertPath("$.['store']['price']", hasItems("$", "store", "price"));
|
||
14 years ago
|
|
||
14 years ago
|
assertPath("$.['store'].price", hasItems("$", "store", "price"));
|
||
14 years ago
|
|
||
14 years ago
|
assertPath("$.['store space']['price space']", hasItems("$", "store space", "price space"));
|
||
14 years ago
|
|
||
14 years ago
|
assertPath("$.['store']['nice.price']", hasItems("$", "store", "nice.price"));
|
||
14 years ago
|
|
||
14 years ago
|
assertPath("$..book[?(@.price<10)]", hasItems("$", "..", "book", "[?(@.price<10)]"));
|
||
14 years ago
|
|
||
14 years ago
|
assertPath("$..book[?(@.price<10)]", hasItems("$", "..", "book", "[?(@.price<10)]"));
|
||
14 years ago
|
|
||
14 years ago
|
assertPath("$.store.book[*].author", hasItems("$", "store", "book", "[*]", "author"));
|
||
14 years ago
|
|
||
14 years ago
|
assertPath("$.store..price", hasItems("$", "store", "..", "price"));
|
||
14 years ago
|
|
||
14 years ago
|
}
|
||
|
|
||
|
@Test
|
||
|
public void white_space_are_removed() throws Exception {
|
||
|
|
||
|
assertPath("$.[ 'store' ]", hasItems("$", "store"));
|
||
|
|
||
|
assertPath("$.[ 'store' ]", hasItems("$", "store"));
|
||
14 years ago
|
|
||
14 years ago
|
assertPath("$.['store bore']", hasItems("$", "store bore"));
|
||
|
|
||
14 years ago
|
assertPath("$..book[ ?(@.price<10) ]", hasItems("$", "..", "book", "[?(@.price<10)]"));
|
||
14 years ago
|
|
||
14 years ago
|
assertPath("$..book[?(@.price<10 )]", hasItems("$", "..", "book", "[?(@.price<10)]"));
|
||
14 years ago
|
|
||
14 years ago
|
assertPath("$..book[?( @.price<10)]", hasItems("$", "..", "book", "[?(@.price<10)]"));
|
||
14 years ago
|
|
||
14 years ago
|
assertPath("$..book[ ?(@.price<10)]", hasItems("$", "..", "book", "[?(@.price<10)]"));
|
||
|
}
|
||
14 years ago
|
|
||
13 years ago
|
@Test
|
||
|
public void dot_ending_ignored() throws Exception {
|
||
|
|
||
|
assertPath("$..book['something'].", hasItems("$", "..", "something"));
|
||
|
|
||
|
}
|
||
|
|
||
14 years ago
|
@Test
|
||
|
public void invalid_path_throws_exception() throws Exception {
|
||
|
assertPathInvalid("$...*");
|
||
|
}
|
||
14 years ago
|
|
||
14 years ago
|
|
||
14 years ago
|
//----------------------------------------------------------------
|
||
|
//
|
||
|
// Helpers
|
||
|
//
|
||
|
//----------------------------------------------------------------
|
||
|
|
||
|
private void assertPathInvalid(String path) {
|
||
|
try {
|
||
13 years ago
|
PathTokenizer tokenizer = new PathTokenizer(path);
|
||
14 years ago
|
assertTrue("Expected exception!", false);
|
||
|
} catch (InvalidPathException expected) {}
|
||
14 years ago
|
}
|
||
|
|
||
14 years ago
|
private void assertPath(String path, Matcher<Iterable<String>> matcher) {
|
||
|
System.out.println("PATH: " + path);
|
||
14 years ago
|
|
||
13 years ago
|
PathTokenizer tokenizer = new PathTokenizer(path);
|
||
14 years ago
|
|
||
|
for (String fragment : tokenizer.getFragments()) {
|
||
14 years ago
|
System.out.println(fragment);
|
||
|
}
|
||
14 years ago
|
|
||
14 years ago
|
assertThat(tokenizer.getFragments(), matcher);
|
||
14 years ago
|
System.out.println("----------------------------------");
|
||
|
}
|
||
14 years ago
|
|
||
|
|
||
|
}
|