From 714ac8e64399eb156abc4ce3e517185c098fb7e6 Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Thu, 11 Oct 2012 20:29:40 +0000 Subject: [PATCH] added tests for using intercept in fields and lists. These tests are currently failing. --- .../gson/functional/InterceptorTest.java | 59 +++++++++++++------ 1 file changed, 41 insertions(+), 18 deletions(-) diff --git a/gson/src/test/java/com/google/gson/functional/InterceptorTest.java b/gson/src/test/java/com/google/gson/functional/InterceptorTest.java index eb37da1f..e1b60757 100644 --- a/gson/src/test/java/com/google/gson/functional/InterceptorTest.java +++ b/gson/src/test/java/com/google/gson/functional/InterceptorTest.java @@ -15,11 +15,15 @@ */ package com.google.gson.functional; +import java.util.List; + import junit.framework.TestCase; import com.google.gson.Gson; +import com.google.gson.JsonParseException; import com.google.gson.internal.alpha.Intercept; import com.google.gson.internal.alpha.JsonPostDeserializer; +import com.google.gson.reflect.TypeToken; /** * Unit tests for {@link Intercept} and {@link JsonPostDeserializer}. @@ -36,29 +40,48 @@ public final class InterceptorTest extends TestCase { this.gson = new Gson(); } - public void testPostDeserialize() { - MyObject target = gson.fromJson("{}", MyObject.class); - assertEquals(MyObject.DEFAULT_VALUE, target.value); - assertEquals(MyObject.DEFAULT_MESSAGE, target.message); + public void testExceptionsPropagated() { + try { + gson.fromJson("{}", User.class); + fail(); + } catch (JsonParseException expected) {} } - @Intercept(postDeserialize = MyObjectInterceptor.class) - private static final class MyObject { - static final int DEFAULT_VALUE = 10; - static final String DEFAULT_MESSAGE = "hello"; - - int value = 0; - String message = null; + public void testPostDeserializeTopLevelClass() { + User user = gson.fromJson("{name:'bob',password:'pwd'}", User.class); + assertEquals(User.DEFAULT_EMAIL, user.email); } - private static final class MyObjectInterceptor implements JsonPostDeserializer { - public void postDeserialize(MyObject o) { - if (o.value == 0) { - o.value = MyObject.DEFAULT_VALUE; - } - if (o.message == null) { - o.message = MyObject.DEFAULT_MESSAGE; + public void testPostDeserializeList() { + List list = gson.fromJson("[{name:'bob',password:'pwd'}]", new TypeToken>(){}.getType()); + User user = list.get(0); + assertEquals(User.DEFAULT_EMAIL, user.email); + } + + public void testPostDeserializeField() { + UserGroup userGroup = gson.fromJson("{user:{name:'bob',password:'pwd'}}", UserGroup.class); + assertEquals(User.DEFAULT_EMAIL, userGroup.user.email); + } + + private static final class UserGroup { + User user; + String city; + } + + @Intercept(postDeserialize = UserValidator.class) + private static final class User { + static final String DEFAULT_EMAIL = "invalid@invalid.com"; + String name; + String password; + String email; + } + + private static final class UserValidator implements JsonPostDeserializer { + public void postDeserialize(User user) { + if (user.name == null || user.password == null) { + throw new JsonParseException("name and password are required fields."); } + if (user.email == null) user.email = User.DEFAULT_EMAIL; } } } \ No newline at end of file