Browse Source

Improved JsonModel Tests.

pull/7/head
Kalle Stenflo 13 years ago
parent
commit
7d0aece1d8
  1. 47
      json-path/src/test/java/com/jayway/jsonpath/JsonModelTest.java

47
json-path/src/test/java/com/jayway/jsonpath/JsonModelTest.java

@ -52,7 +52,6 @@ public class JsonModelTest {
" }\n" + " }\n" +
"}"; "}";
@Test @Test
public void a_json_document_can_be_fetched_with_a_URL() throws Exception { public void a_json_document_can_be_fetched_with_a_URL() throws Exception {
URL url = new URL("http://maps.googleapis.com/maps/api/geocode/json"); URL url = new URL("http://maps.googleapis.com/maps/api/geocode/json");
@ -86,28 +85,58 @@ public class JsonModelTest {
} }
@Test @Test
public void map_a_json_model() throws Exception { public void map_a_json_model_to_an_Class() throws Exception {
JsonModel model = JsonModel.create(DOCUMENT); JsonModel model = JsonModel.create(DOCUMENT);
List<Book> booksList = model.map("$.store.book[0,1]").toListOf(Book.class);
Set<Book> bookSet = model.map("$.store.book[0,1]").toSetOf(Book.class);
Book book = model.map("$.store.book[1]").to(Book.class); Book book = model.map("$.store.book[1]").to(Book.class);
assertEquals("fiction", book.category); assertEquals("fiction", book.category);
assertEquals("Evelyn Waugh", book.author); assertEquals("Evelyn Waugh", book.author);
assertEquals("Sword of Honour", book.title); assertEquals("Sword of Honour", book.title);
assertEquals(12.99D, book.price); assertEquals(12.99D, book.price);
}
@Test
public void map_a_json_model_to_a_List() throws Exception {
JsonModel model = JsonModel.create(DOCUMENT);
List<Book> booksList = model.map("$.store.book[0,1]").toListOf(Book.class);
assertEquals("fiction", booksList.get(1).category);
assertEquals("Evelyn Waugh", booksList.get(1).author);
assertEquals("Sword of Honour", booksList.get(1).title);
assertEquals(12.99D, booksList.get(1).price);
booksList = model.map("$.store.book[*]").toListOf(Book.class);
List<Book> booksList2 = model.map("$.store.book[*]").toListOf(Book.class); assertEquals("fiction", booksList.get(1).category);
assertEquals("Evelyn Waugh", booksList.get(1).author);
assertEquals("Sword of Honour", booksList.get(1).title);
assertEquals(12.99D, booksList.get(1).price);
List<Book> booksList3 = model.map("$.store.book[*]").toList().of(Book.class); booksList = model.map("$.store.book[*]").toList().of(Book.class);
System.out.println("asd"); assertEquals("fiction", booksList.get(1).category);
assertEquals("Evelyn Waugh", booksList.get(1).author);
assertEquals("Sword of Honour", booksList.get(1).title);
assertEquals(12.99D, booksList.get(1).price);
} }
@Test
public void map_a_json_model_to_a_Set() throws Exception {
JsonModel model = JsonModel.create(DOCUMENT);
Set<Book> bookSet = model.map("$.store.book[1]").toSetOf(Book.class);
Book book = bookSet.iterator().next();
assertEquals("fiction", book.category);
assertEquals("Evelyn Waugh", book.author);
assertEquals("Sword of Honour", book.title);
assertEquals(12.99D, book.price);
}
public static class Book { public static class Book {

Loading…
Cancel
Save