summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjbion <joffrey.bion@amadeus.com>2016-12-20 23:01:23 +0100
committerjbion <joffrey.bion@amadeus.com>2016-12-21 01:29:28 +0100
commit803ecd6740c5cb0d911d52edab99177f31bd42b6 (patch)
treeb1240c9396617e96de5ee3174dc10bcc9378f309 /src
parentAdd test for ScienceProgressSerializer (diff)
downloadseven-wonders-803ecd6740c5cb0d911d52edab99177f31bd42b6.tar.gz
seven-wonders-803ecd6740c5cb0d911d52edab99177f31bd42b6.tar.bz2
seven-wonders-803ecd6740c5cb0d911d52edab99177f31bd42b6.zip
Add test for Resources serializers
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/resources/Resources.java18
-rw-r--r--src/test/java/org/luxons/sevenwonders/game/data/serializers/ResourceTypeSerializerTest.java50
-rw-r--r--src/test/java/org/luxons/sevenwonders/game/data/serializers/ResourceTypesSerializerTest.java100
-rw-r--r--src/test/java/org/luxons/sevenwonders/game/data/serializers/ResourcesSerializerTest.java107
4 files changed, 275 insertions, 0 deletions
diff --git a/src/main/java/org/luxons/sevenwonders/game/resources/Resources.java b/src/main/java/org/luxons/sevenwonders/game/resources/Resources.java
index 5f7e2606..d3f6de94 100644
--- a/src/main/java/org/luxons/sevenwonders/game/resources/Resources.java
+++ b/src/main/java/org/luxons/sevenwonders/game/resources/Resources.java
@@ -3,6 +3,7 @@ package org.luxons.sevenwonders.game.resources;
import java.util.EnumMap;
import java.util.Map;
import java.util.Map.Entry;
+import java.util.Objects;
public class Resources {
@@ -44,4 +45,21 @@ public class Resources {
public boolean isEmpty() {
return quantities.values().stream().reduce(0, Integer::sum) == 0;
}
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ Resources resources = (Resources)o;
+ return Objects.equals(quantities, resources.quantities);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(quantities);
+ }
}
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<List<ResourceType>>() {}.getType();
+ }
+
+ @Test
+ public void serialize_null() {
+ assertEquals("null", gson.toJson(null, createListTypeToken()));
+ }
+
+ @Test
+ public void serialize_emptyList() {
+ List<ResourceType> types = new ArrayList<>();
+ assertEquals("\"\"", gson.toJson(types, createListTypeToken()));
+ }
+
+ @Test
+ public void serialize_singleType() {
+ List<ResourceType> types = new ArrayList<>();
+ types.add(ResourceType.WOOD);
+ assertEquals("\"W\"", gson.toJson(types, createListTypeToken()));
+ }
+
+ @Test
+ public void serialize_multipleTimesSameType() {
+ List<ResourceType> 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<ResourceType> 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<ResourceType> types = new ArrayList<>();
+ assertEquals(types, gson.fromJson("\"\"", createListTypeToken()));
+ }
+
+ @Test
+ public void deserialize_singleType() {
+ List<ResourceType> types = new ArrayList<>();
+ types.add(ResourceType.WOOD);
+ assertEquals(types, gson.fromJson("\"W\"", createListTypeToken()));
+ }
+
+ @Test
+ public void deserialize_multipleTimesSameType() {
+ List<ResourceType> 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<ResourceType> 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
bgstack15