From 2f48d685150462c2c426edca019f4644f466ee25 Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Sat, 10 May 2014 18:30:48 +0200 Subject: Extend tests with date tests, fix created_at values --- test/data/ValidatingJsonDeserializerTest.java | 41 ++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/test/data/ValidatingJsonDeserializerTest.java b/test/data/ValidatingJsonDeserializerTest.java index 73523f4..459042f 100644 --- a/test/data/ValidatingJsonDeserializerTest.java +++ b/test/data/ValidatingJsonDeserializerTest.java @@ -8,8 +8,11 @@ import com.google.gson.JsonObject; import com.google.gson.JsonParseException; import com.google.gson.JsonPrimitive; import org.apache.commons.lang3.StringUtils; +import org.joda.time.DateTime; import org.junit.Test; import static org.junit.Assert.*; +import org.junit.Before; +import utils.TwitterDateAdapter; /** * Tests whether the ValidatingJsonDeserializer class properly rejects @@ -275,9 +278,14 @@ public class ValidatingJsonDeserializerTest { + " \"is_translator\": false" + " }" + "}"; + private Gson gson; + + @Before + public void setUp() { + gson = TwitterJsonDeserializer.getGsonBuilder().create(); + } private Object deserializeJson(String json, Class cls) { - Gson gson = TwitterJsonDeserializer.getGsonBuilder().create(); return gson.fromJson(json, cls); } @@ -311,7 +319,8 @@ public class ValidatingJsonDeserializerTest { Tweet tweet = (Tweet) deserializeJson(exampleTweet, Tweet.class); assertEquals(461441338630619136L, tweet.id); assertNotNull(tweet.retweeted_status); - assertEquals("Wed Apr 30 09:46:02 +0000 2014", tweet.created_at); + assertEquals("Wed Apr 30 09:46:02 +0000 2014", + TwitterDateAdapter.formatDateTime(tweet.created_at)); assertNotNull(tweet.user); assertEquals("GURGAON", tweet.user.location); @@ -339,7 +348,7 @@ public class ValidatingJsonDeserializerTest { user.addProperty("friends_count", 4); user.addProperty("location", "X"); user.addProperty("screen_name", "X"); - user.addProperty("created_at", "X"); + user.addProperty("created_at", "Wed Apr 30 09:46:02 -0300 2014"); JsonObject entities = new JsonObject(); JsonObject urlEntity = new JsonObject(); urlEntity.add("urls", new JsonArray()); @@ -354,7 +363,7 @@ public class ValidatingJsonDeserializerTest { tweet.addProperty("id", 4); tweet.addProperty("lang", "X"); tweet.addProperty("in_reply_to_user_id", 4); - tweet.addProperty("created_at", "X"); + tweet.addProperty("created_at", "Wed Apr 30 09:46:02 +0300 2014"); tweet.addProperty("favorite_count", 4); tweet.add("place", JsonNull.INSTANCE); tweet.add("coordinates", JsonNull.INSTANCE); @@ -587,4 +596,28 @@ public class ValidatingJsonDeserializerTest { assertEquals("Missing field: id", ex.getMessage()); } } + + @Test + public void testDate() { + try { + deserializeJson("{\"date\":\"X\"}", DateContainer.class); + fail("Invalid date should not pass."); + } catch (JsonParseException ex) { + assertEquals("Invalid date: X", ex.getMessage()); + } + } + + @Test + public void testDateValid() { + String json = "{\"date\":\"Sat May 10 16:39:34 +1000 2014\"}"; + DateContainer d = (DateContainer) deserializeJson(json, + DateContainer.class); + String serialized = gson.toJson(d, DateContainer.class); + assertEquals(json, serialized); + } + + private static class DateContainer { + + public DateTime date; + } } -- cgit v1.2.1