@ -6,6 +6,7 @@ import java.util.List;
import java.util.Map ;
import java.util.Map ;
import static org.hamcrest.Matchers.equalTo ;
import static org.hamcrest.Matchers.equalTo ;
import static org.hamcrest.Matchers.hasItem ;
import static org.hamcrest.Matchers.hasItems ;
import static org.hamcrest.Matchers.hasItems ;
import static org.junit.Assert.* ;
import static org.junit.Assert.* ;
@ -54,16 +55,16 @@ public class JsonPathTest {
@Test
@Test
public void read_path_with_colon ( ) throws Exception {
public void read_path_with_colon ( ) throws Exception {
assertEquals ( JsonPath . readOne ( DOCUMENT , "$.store.bicycle.foo:bar" ) , "fooBar" ) ;
assertEquals ( JsonPath . read ( DOCUMENT , "$.store.bicycle.foo:bar" ) , "fooBar" ) ;
assertEquals ( JsonPath . readOne ( DOCUMENT , "$.['store'].['bicycle'].['foo:bar']" ) , "fooBar" ) ;
assertEquals ( JsonPath . read ( DOCUMENT , "$.['store'].['bicycle'].['foo:bar']" ) , "fooBar" ) ;
}
}
@Test
@Test
public void read_document_from_root ( ) throws Exception {
public void read_document_from_root ( ) throws Exception {
List < Object > lis t = JsonPath . read ( DOCUMENT , "$.store" ) ;
Map resul t = JsonPath . read ( DOCUMENT , "$.store" ) ;
assertEquals ( 2 , ( ( Map ) list . get ( 0 ) ) . values ( ) . size ( ) ) ;
assertEquals ( 2 , result . values ( ) . size ( ) ) ;
}
}
@ -73,7 +74,9 @@ public class JsonPathTest {
JsonPath path = JsonPath . compile ( "$.store.book[1]" ) ;
JsonPath path = JsonPath . compile ( "$.store.book[1]" ) ;
List < Map > list = path . read ( DOCUMENT ) ;
Map map = path . read ( DOCUMENT ) ;
assertEquals ( "Evelyn Waugh" , map . get ( "author" ) ) ;
}
}
@Test
@Test
@ -86,14 +89,15 @@ public class JsonPathTest {
@Test
@Test
public void read_store_book_author ( ) throws Exception {
public void read_store_book_author ( ) throws Exception {
assertThat ( JsonPath . < String > read ( DOCUMENT , "$.store.book[*].author" ) , hasItems ( "Nigel Rees" , "Evelyn Waugh" , "Herman Melville" , "J. R. R. Tolkien" ) ) ;
assertThat ( JsonPath . < List < String > > read ( DOCUMENT , "$.store.book[0,1].author" ) , hasItems ( "Nigel Rees" , "Evelyn Waugh" ) ) ;
assertThat ( JsonPath . < String > read ( DOCUMENT , "$.['store'].['book'][*].['author']" ) , hasItems ( "Nigel Rees" , "Evelyn Waugh" , "Herman Melville" , "J. R. R. Tolkien" ) ) ;
assertThat ( JsonPath . < List < String > > read ( DOCUMENT , "$.store.book[*].author" ) , hasItems ( "Nigel Rees" , "Evelyn Waugh" , "Herman Melville" , "J. R. R. Tolkien" ) ) ;
assertThat ( JsonPath . < List < String > > read ( DOCUMENT , "$.['store'].['book'][*].['author']" ) , hasItems ( "Nigel Rees" , "Evelyn Waugh" , "Herman Melville" , "J. R. R. Tolkien" ) ) ;
}
}
@Test
@Test
public void all_authors ( ) throws Exception {
public void all_authors ( ) throws Exception {
assertThat ( JsonPath . < String > read ( DOCUMENT , "$..author" ) , hasItems ( "Nigel Rees" , "Evelyn Waugh" , "Herman Melville" , "J. R. R. Tolkien" ) ) ;
assertThat ( JsonPath . < List < String > > read ( DOCUMENT , "$..author" ) , hasItems ( "Nigel Rees" , "Evelyn Waugh" , "Herman Melville" , "J. R. R. Tolkien" ) ) ;
}
}
@ -101,57 +105,57 @@ public class JsonPathTest {
public void all_store_properties ( ) throws Exception {
public void all_store_properties ( ) throws Exception {
List < Object > itemsInStore = JsonPath . read ( DOCUMENT , "$.store.*" ) ;
List < Object > itemsInStore = JsonPath . read ( DOCUMENT , "$.store.*" ) ;
assertEquals ( JsonPath . readOne ( itemsInStore , "$.[0].[0].author" ) , "Nigel Rees" ) ;
assertEquals ( JsonPath . read ( itemsInStore , "$.[0].[0].author" ) , "Nigel Rees" ) ;
assertEquals ( JsonPath . readOne ( itemsInStore , "$.[0][0].author" ) , "Nigel Rees" ) ;
assertEquals ( JsonPath . read ( itemsInStore , "$.[0][0].author" ) , "Nigel Rees" ) ;
}
}
@Test
@Test
public void all_prices_in_store ( ) throws Exception {
public void all_prices_in_store ( ) throws Exception {
assertThat ( JsonPath . < Double > read ( DOCUMENT , "$.store..price" ) , hasItems ( 8 . 95D , 12 . 99D , 8 . 99D , 19 . 95D ) ) ;
assertThat ( JsonPath . < List < Double > > read ( DOCUMENT , "$.store..price" ) , hasItems ( 8 . 95D , 12 . 99D , 8 . 99D , 19 . 95D ) ) ;
}
}
@Test
@Test
public void access_array_by_index_from_tail ( ) throws Exception {
public void access_array_by_index_from_tail ( ) throws Exception {
assertThat ( JsonPath . < String > readOne ( DOCUMENT , "$..book[(@.length-1)].author" ) , equalTo ( "J. R. R. Tolkien" ) ) ;
assertThat ( JsonPath . < String > read ( DOCUMENT , "$..book[(@.length-1)].author" ) , equalTo ( "J. R. R. Tolkien" ) ) ;
assertThat ( JsonPath . < String > readOne ( DOCUMENT , "$..book[-1:].author" ) , equalTo ( "J. R. R. Tolkien" ) ) ;
assertThat ( JsonPath . < String > read ( DOCUMENT , "$..book[-1:].author" ) , equalTo ( "J. R. R. Tolkien" ) ) ;
}
}
@Test
@Test
public void read_store_book_index_0_and_1 ( ) throws Exception {
public void read_store_book_index_0_and_1 ( ) throws Exception {
assertThat ( JsonPath . < String > read ( DOCUMENT , "$.store.book[0,1].author" ) , hasItems ( "Nigel Rees" , "Evelyn Waugh" ) ) ;
assertThat ( JsonPath . < List < String > > read ( DOCUMENT , "$.store.book[0,1].author" ) , hasItems ( "Nigel Rees" , "Evelyn Waugh" ) ) ;
assertTrue ( JsonPath . < String > read ( DOCUMENT , "$.store.book[0,1].author" ) . size ( ) = = 2 ) ;
assertTrue ( JsonPath . < List > read ( DOCUMENT , "$.store.book[0,1].author" ) . size ( ) = = 2 ) ;
}
}
@Test
@Test
public void read_store_book_pull_first_2 ( ) throws Exception {
public void read_store_book_pull_first_2 ( ) throws Exception {
assertThat ( JsonPath . < String > read ( DOCUMENT , "$.store.book[:2].author" ) , hasItems ( "Nigel Rees" , "Evelyn Waugh" ) ) ;
assertThat ( JsonPath . < List < String > > read ( DOCUMENT , "$.store.book[:2].author" ) , hasItems ( "Nigel Rees" , "Evelyn Waugh" ) ) ;
assertTrue ( JsonPath . < String > read ( DOCUMENT , "$.store.book[:2].author" ) . size ( ) = = 2 ) ;
assertTrue ( JsonPath . < List > read ( DOCUMENT , "$.store.book[:2].author" ) . size ( ) = = 2 ) ;
}
}
@Test
@Test
public void read_store_book_filter_by_isbn ( ) throws Exception {
public void read_store_book_filter_by_isbn ( ) throws Exception {
assertThat ( JsonPath . < String > read ( DOCUMENT , "$.store.book[?(@.isbn)].isbn" ) , hasItems ( "0-553-21311-3" , "0-395-19395-8" ) ) ;
assertThat ( JsonPath . < List < String > > read ( DOCUMENT , "$.store.book[?(@.isbn)].isbn" ) , hasItems ( "0-553-21311-3" , "0-395-19395-8" ) ) ;
assertTrue ( JsonPath . < String > read ( DOCUMENT , "$.store.book[?(@.isbn)].isbn" ) . size ( ) = = 2 ) ;
assertTrue ( JsonPath . < List > read ( DOCUMENT , "$.store.book[?(@.isbn)].isbn" ) . size ( ) = = 2 ) ;
}
}
@Test
@Test
public void all_books_cheaper_than_10 ( ) throws Exception {
public void all_books_cheaper_than_10 ( ) throws Exception {
assertThat ( JsonPath . < String > read ( DOCUMENT , "$.store.book[?(@.price < 10)].title" ) , hasItems ( "Sayings of the Century" , "Moby Dick" ) ) ;
assertThat ( JsonPath . < List < String > > read ( DOCUMENT , "$.store.book[?(@.price < 10)].title" ) , hasItems ( "Sayings of the Century" , "Moby Dick" ) ) ;
}
}
@Test
@Test
public void all_books_with_category_reference ( ) throws Exception {
public void all_books_with_category_reference ( ) throws Exception {
assertThat ( JsonPath . < String > read ( DOCUMENT , "$..book[?(@.category = 'reference')].title" ) , hasItems ( "Sayings of the Century" ) ) ;
assertThat ( JsonPath . < List < String > > read ( DOCUMENT , "$..book[?(@.category = 'reference')].title" ) , hasItems ( "Sayings of the Century" ) ) ;
}
}
@ -163,13 +167,15 @@ public class JsonPathTest {
@Test
@Test
public void access_index_out_of_bounds_does_not_throw_exception ( ) throws Exception {
public void access_index_out_of_bounds_does_not_throw_exception ( ) throws Exception {
List < Object > res = JsonPath . read ( DOCUMENT , "$.store.book[100].author" ) ;
Object res = JsonPath . read ( DOCUMENT , "$.store.book[100].author" ) ;
assertNull ( res ) ;
assertTrue ( res . isEmpty ( ) ) ;
res = JsonPath . read ( DOCUMENT , "$.store.book[1, 200].author" ) ;
res = JsonPath . read ( DOCUMENT , "$.store.book[100, 200].author" ) ;
assertTrue ( res . isEmpty ( ) ) ;
assertThat ( ( List < String > ) res , hasItems ( "Evelyn Waugh" ) ) ;
//assertNull(();
}
}
/ *
/ *