From 9d5bdaf89bd173bec7364d591377ca17bb5bb91d Mon Sep 17 00:00:00 2001 From: Olli Tietäväinen Date: Fri, 22 Feb 2019 10:50:51 +0200 Subject: Custom serializers accessors (#10658) * add accessor methods for CUSTOM_SERIALIZERS in JsonCodec * javadoc * removed removeCustomSerializer method, renamed putCustomSerializer to addCustomSerializer, added sanity checks and JavaDocs * refactored addCustomJsonSerializer to set, added test UI * move enums to be parsed after custom serializers * move adding custom serializer to static block * throw an exception if multiple serializers are registered for class * updated javadocs * changed CustomJSONSerializerTest to a SingleBrowserTest * moved CustomJSONSerializerTest to server/ and it's now not a browser test * removed CustomJSONSerializerTest --- .../vaadin/server/CustomJSONSerializerTest.java | 52 ++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 server/src/test/java/com/vaadin/server/CustomJSONSerializerTest.java (limited to 'server/src/test/java') diff --git a/server/src/test/java/com/vaadin/server/CustomJSONSerializerTest.java b/server/src/test/java/com/vaadin/server/CustomJSONSerializerTest.java new file mode 100644 index 0000000000..0c8b69fe62 --- /dev/null +++ b/server/src/test/java/com/vaadin/server/CustomJSONSerializerTest.java @@ -0,0 +1,52 @@ +package com.vaadin.server; + +import static org.junit.Assert.assertTrue; + +import java.lang.reflect.Type; + +import org.junit.Test; + +import com.vaadin.server.communication.JSONSerializer; +import com.vaadin.ui.ConnectorTracker; + +import elemental.json.JsonValue; + +public class CustomJSONSerializerTest { + + public static class Foo { + + } + + public static class FooSerializer implements JSONSerializer { + + @Override + public Foo deserialize(Type type, JsonValue jsonValue, + ConnectorTracker connectorTracker) { + return null; + } + + @Override + public JsonValue serialize(Foo value, + ConnectorTracker connectorTracker) { + return null; + } + + } + + @Test + public void testMultipleRegistration() { + boolean thrown = false; + try { + JsonCodec.setCustomSerializer(Foo.class, new FooSerializer()); + JsonCodec.setCustomSerializer(Foo.class, new FooSerializer()); + } catch (IllegalStateException ise) { + thrown = true; + } finally { + JsonCodec.setCustomSerializer(Foo.class, null); + } + assertTrue("Multiple serializer registrations for one class " + + "should throw an IllegalStateException", thrown); + + } + +} -- cgit v1.2.3