Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin Sawicki2012-04-09 18:21:58 +0000
committerKevin Sawicki2012-04-09 18:21:58 +0000
commit8b707fffe8dd84f7b6c577b063a2e03c0baac1a1 (patch)
treecfe69993baf4089ab30550b03dddc65b63799e29 /org.eclipse.egit.github.core.tests/src/org/eclipse
parentf78deadf21d34eb0e7a60a6543d4f598cc962820 (diff)
downloadegit-github-8b707fffe8dd84f7b6c577b063a2e03c0baac1a1.tar.gz
egit-github-8b707fffe8dd84f7b6c577b063a2e03c0baac1a1.tar.xz
egit-github-8b707fffe8dd84f7b6c577b063a2e03c0baac1a1.zip
Use custom deserializer for Event class
Using newer streaming versions of Gson causes deserialization problems if the payload is before the type field in the returned JSON using the current custom Event formatting. This changes the behavior to use an internal custom Gson instance just to handle parsing events that allows a chance to check the type and create the custom payload before the Event object is returned to any service callers. This approach simplifies the code but does require an additional Gson instance to be kept around to handle the default serialization of the top-level Event fields. Change-Id: Ic10a755bbf798ec2d1d4a4c53ccd9154e0cb91e2
Diffstat (limited to 'org.eclipse.egit.github.core.tests/src/org/eclipse')
-rw-r--r--org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/EventFormatterTest.java54
1 files changed, 37 insertions, 17 deletions
diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/EventFormatterTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/EventFormatterTest.java
index d507f30d..0777f58d 100644
--- a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/EventFormatterTest.java
+++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/EventFormatterTest.java
@@ -10,45 +10,65 @@
*******************************************************************************/
package org.eclipse.egit.github.core.tests;
+import static org.eclipse.egit.github.core.event.Event.TYPE_FOLLOW;
+import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertNull;
import org.eclipse.egit.github.core.client.EventFormatter;
+import org.eclipse.egit.github.core.client.GsonUtils;
+import org.eclipse.egit.github.core.event.Event;
import org.eclipse.egit.github.core.event.EventPayload;
+import org.eclipse.egit.github.core.event.FollowPayload;
import org.junit.Test;
/**
- * Unit tests of {@link EventFormatter} and subclasses
+ * Unit tests of {@link EventFormatter}
*/
public class EventFormatterTest {
/**
- * Create instance of EventFormatter
+ * Follow event payload returned as {@link FollowPayload}
*/
@Test
- public void createEventFormatterInstance() {
- EventFormatter formatter = new EventFormatter();
- assertNotNull(formatter.getEventCreator());
- assertNotNull(formatter.getPayloadDeserializer());
+ public void followPayload() {
+ Event event = GsonUtils.fromJson("{\"type\":\"" + TYPE_FOLLOW
+ + "\",\"payload\":{}}", Event.class);
+ assertNotNull(event);
+ assertNotNull(event.getPayload());
+ assertEquals(FollowPayload.class, event.getPayload().getClass());
}
/**
- * Create instance of Event
+ * Unknown event payload returned as {@link EventPayload}
*/
@Test
- public void createEventInstance() {
- EventFormatter formatter = new EventFormatter();
- assertNotNull(formatter.getEventCreator().createInstance(null));
+ public void unknownPayload() {
+ Event event = GsonUtils.fromJson(
+ "{\"type\":\"NotAnEventType\",\"payload\":{}}", Event.class);
+ assertNotNull(event);
+ assertNotNull(event.getPayload());
+ assertEquals(EventPayload.class, event.getPayload().getClass());
}
/**
- * Unknown event payload returned as EventPayload
+ * Event with missing type has payload returned as {@link EventPayload}
*/
@Test
- public void unknownPayload() {
- EventFormatter formatter = new EventFormatter();
- formatter.getEventCreator().createInstance(null);
- EventPayload payload = formatter.getPayloadDeserializer().deserialize(null, null, null);
- assertTrue(payload instanceof EventPayload);
+ public void missingType() {
+ Event event = GsonUtils.fromJson("{\"payload\":{}}", Event.class);
+ assertNotNull(event);
+ assertNotNull(event.getPayload());
+ assertEquals(EventPayload.class, event.getPayload().getClass());
+ }
+
+ /**
+ * Missing payload
+ */
+ @Test
+ public void missingPayload() {
+ Event event = GsonUtils.fromJson("{}", Event.class);
+ assertNotNull(event);
+ assertNull(event.getPayload());
}
}

Back to the top