From 803ecd6740c5cb0d911d52edab99177f31bd42b6 Mon Sep 17 00:00:00 2001 From: jbion Date: Tue, 20 Dec 2016 23:01:23 +0100 Subject: Add test for Resources serializers --- .../serializers/ResourceTypeSerializerTest.java | 50 ++++++++++ .../serializers/ResourceTypesSerializerTest.java | 100 +++++++++++++++++++ .../data/serializers/ResourcesSerializerTest.java | 107 +++++++++++++++++++++ 3 files changed, 257 insertions(+) create mode 100644 src/test/java/org/luxons/sevenwonders/game/data/serializers/ResourceTypeSerializerTest.java create mode 100644 src/test/java/org/luxons/sevenwonders/game/data/serializers/ResourceTypesSerializerTest.java create mode 100644 src/test/java/org/luxons/sevenwonders/game/data/serializers/ResourcesSerializerTest.java (limited to 'src/test') diff --git a/src/test/java/org/luxons/sevenwonders/game/data/serializers/ResourceTypeSerializerTest.java b/src/test/java/org/luxons/sevenwonders/game/data/serializers/ResourceTypeSerializerTest.java new file mode 100644 index 00000000..86f3f5ab --- /dev/null +++ b/src/test/java/org/luxons/sevenwonders/game/data/serializers/ResourceTypeSerializerTest.java @@ -0,0 +1,50 @@ +package org.luxons.sevenwonders.game.data.serializers; + +import org.junit.Before; +import org.junit.Test; +import org.luxons.sevenwonders.game.resources.ResourceType; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import static org.junit.Assert.*; + +public class ResourceTypeSerializerTest { + + private Gson gson; + + @Before + public void setUp() { + gson = new GsonBuilder().registerTypeAdapter(ResourceType.class, new ResourceTypeSerializer()).create(); + } + + @Test + public void serialize_useSymbolForEachType() { + for (ResourceType type : ResourceType.values()) { + String expectedJson = "\"" + type.getSymbol() + "\""; + assertEquals(expectedJson, gson.toJson(type)); + } + } + + @Test + public void deserialize_useSymbolForEachType() { + for (ResourceType type : ResourceType.values()) { + String typeInJson = "\"" + type.getSymbol() + "\""; + assertEquals(type, gson.fromJson(typeInJson, ResourceType.class)); + } + } + + @Test + public void deserialize_nullFromNull() { + assertNull(gson.fromJson("null", ResourceType.class)); + } + + @Test(expected = IllegalArgumentException.class) + public void deserialize_failsOnEmptyString() { + gson.fromJson("\"\"", ResourceType.class); + } + + @Test(expected = IllegalArgumentException.class) + public void deserialize_failsOnGarbageString() { + gson.fromJson("\"thisisgarbage\"", ResourceType.class); + } +} \ No newline at end of file diff --git a/src/test/java/org/luxons/sevenwonders/game/data/serializers/ResourceTypesSerializerTest.java b/src/test/java/org/luxons/sevenwonders/game/data/serializers/ResourceTypesSerializerTest.java new file mode 100644 index 00000000..4ebbc33f --- /dev/null +++ b/src/test/java/org/luxons/sevenwonders/game/data/serializers/ResourceTypesSerializerTest.java @@ -0,0 +1,100 @@ +package org.luxons.sevenwonders.game.data.serializers; + +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.luxons.sevenwonders.game.resources.ResourceType; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.reflect.TypeToken; +import static org.junit.Assert.*; + +public class ResourceTypesSerializerTest { + + private Gson gson; + + @Before + public void setUp() { + gson = new GsonBuilder().registerTypeAdapter(createListTypeToken(), new ResourceTypesSerializer()).create(); + } + + private static Type createListTypeToken() { + return new TypeToken>() {}.getType(); + } + + @Test + public void serialize_null() { + assertEquals("null", gson.toJson(null, createListTypeToken())); + } + + @Test + public void serialize_emptyList() { + List types = new ArrayList<>(); + assertEquals("\"\"", gson.toJson(types, createListTypeToken())); + } + + @Test + public void serialize_singleType() { + List types = new ArrayList<>(); + types.add(ResourceType.WOOD); + assertEquals("\"W\"", gson.toJson(types, createListTypeToken())); + } + + @Test + public void serialize_multipleTimesSameType() { + List types = new ArrayList<>(); + types.add(ResourceType.WOOD); + types.add(ResourceType.WOOD); + types.add(ResourceType.WOOD); + assertEquals("\"WWW\"", gson.toJson(types, createListTypeToken())); + } + + @Test + public void serialize_mixedTypes() { + List types = new ArrayList<>(); + types.add(ResourceType.WOOD); + types.add(ResourceType.CLAY); + types.add(ResourceType.STONE); + assertEquals("\"WCS\"", gson.toJson(types, createListTypeToken())); + } + + @Test + public void deserialize_null() { + assertNull(gson.fromJson("null", createListTypeToken())); + } + + @Test + public void deserialize_emptyList() { + List types = new ArrayList<>(); + assertEquals(types, gson.fromJson("\"\"", createListTypeToken())); + } + + @Test + public void deserialize_singleType() { + List types = new ArrayList<>(); + types.add(ResourceType.WOOD); + assertEquals(types, gson.fromJson("\"W\"", createListTypeToken())); + } + + @Test + public void deserialize_multipleTimesSameType() { + List types = new ArrayList<>(); + types.add(ResourceType.WOOD); + types.add(ResourceType.WOOD); + types.add(ResourceType.WOOD); + assertEquals(types, gson.fromJson("\"WWW\"", createListTypeToken())); + } + + @Test + public void deserialize_mixedTypes() { + List types = new ArrayList<>(); + types.add(ResourceType.WOOD); + types.add(ResourceType.CLAY); + types.add(ResourceType.STONE); + assertEquals(types, gson.fromJson("\"WCS\"", createListTypeToken())); + } +} \ No newline at end of file diff --git a/src/test/java/org/luxons/sevenwonders/game/data/serializers/ResourcesSerializerTest.java b/src/test/java/org/luxons/sevenwonders/game/data/serializers/ResourcesSerializerTest.java new file mode 100644 index 00000000..022949fd --- /dev/null +++ b/src/test/java/org/luxons/sevenwonders/game/data/serializers/ResourcesSerializerTest.java @@ -0,0 +1,107 @@ +package org.luxons.sevenwonders.game.data.serializers; + +import org.junit.Before; +import org.junit.Test; +import org.luxons.sevenwonders.game.resources.ResourceType; +import org.luxons.sevenwonders.game.resources.Resources; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import static org.junit.Assert.*; + +public class ResourcesSerializerTest { + + private Gson gson; + + @Before + public void setUp() { + gson = new GsonBuilder().registerTypeAdapter(Resources.class, new ResourcesSerializer()).create(); + } + + @Test + public void serialize_null() { + assertEquals("null", gson.toJson(null, Resources.class)); + } + + @Test + public void serialize_emptyResourcesToNull() { + Resources resources = new Resources(); + assertEquals("null", gson.toJson(resources, Resources.class)); + } + + @Test + public void serialize_singleType() { + Resources resources = new Resources(); + resources.add(ResourceType.WOOD, 1); + assertEquals("\"W\"", gson.toJson(resources, Resources.class)); + } + + @Test + public void serialize_multipleTimesSameType() { + Resources resources = new Resources(); + resources.add(ResourceType.WOOD, 3); + assertEquals("\"WWW\"", gson.toJson(resources, Resources.class)); + } + + @Test + public void serialize_mixedTypes() { + Resources resources = new Resources(); + resources.add(ResourceType.WOOD, 1); + resources.add(ResourceType.STONE, 1); + resources.add(ResourceType.CLAY, 1); + assertEquals("\"WSC\"", gson.toJson(resources, Resources.class)); + } + + @Test + public void serialize_mixedTypes_unordered() { + Resources resources = new Resources(); + resources.add(ResourceType.CLAY, 1); + resources.add(ResourceType.WOOD, 2); + resources.add(ResourceType.CLAY, 1); + resources.add(ResourceType.STONE, 1); + assertEquals("\"WWSCC\"", gson.toJson(resources, Resources.class)); + } + + @Test + public void deserialize_null() { + assertNull(gson.fromJson("null", Resources.class)); + } + + @Test + public void deserialize_emptyList() { + Resources resources = new Resources(); + assertEquals(resources, gson.fromJson("\"\"", Resources.class)); + } + + @Test + public void deserialize_singleType() { + Resources resources = new Resources(); + resources.add(ResourceType.WOOD, 1); + assertEquals(resources, gson.fromJson("\"W\"", Resources.class)); + } + + @Test + public void deserialize_multipleTimesSameType() { + Resources resources = new Resources(); + resources.add(ResourceType.WOOD, 3); + assertEquals(resources, gson.fromJson("\"WWW\"", Resources.class)); + } + + @Test + public void deserialize_mixedTypes() { + Resources resources = new Resources(); + resources.add(ResourceType.WOOD, 1); + resources.add(ResourceType.CLAY, 1); + resources.add(ResourceType.STONE, 1); + assertEquals(resources, gson.fromJson("\"WCS\"", Resources.class)); + } + + @Test + public void deserialize_mixedTypes_unordered() { + Resources resources = new Resources(); + resources.add(ResourceType.WOOD, 1); + resources.add(ResourceType.CLAY, 2); + resources.add(ResourceType.STONE, 3); + assertEquals(resources, gson.fromJson("\"SCWCSS\"", Resources.class)); + } +} \ No newline at end of file -- cgit