diff options
author | jbion <joffrey.bion@amadeus.com> | 2016-12-21 01:46:56 +0100 |
---|---|---|
committer | jbion <joffrey.bion@amadeus.com> | 2016-12-21 02:09:24 +0100 |
commit | dc1fcba44c6767b3ca1e3804061222bce142a235 (patch) | |
tree | b58af8b2aa6f7e9dccc3316370f74ec2dfe22b45 /src/main/java/org/luxons | |
parent | Add test for Resources serializers (diff) | |
download | seven-wonders-dc1fcba44c6767b3ca1e3804061222bce142a235.tar.gz seven-wonders-dc1fcba44c6767b3ca1e3804061222bce142a235.tar.bz2 seven-wonders-dc1fcba44c6767b3ca1e3804061222bce142a235.zip |
Add test for ProductionIncrease serializer
Diffstat (limited to 'src/main/java/org/luxons')
3 files changed, 50 insertions, 6 deletions
diff --git a/src/main/java/org/luxons/sevenwonders/game/data/serializers/ProductionIncreaseSerializer.java b/src/main/java/org/luxons/sevenwonders/game/data/serializers/ProductionIncreaseSerializer.java index 645958d3..a0dee138 100644 --- a/src/main/java/org/luxons/sevenwonders/game/data/serializers/ProductionIncreaseSerializer.java +++ b/src/main/java/org/luxons/sevenwonders/game/data/serializers/ProductionIncreaseSerializer.java @@ -5,6 +5,11 @@ import java.util.List; import java.util.Set; import java.util.stream.Collectors; +import org.luxons.sevenwonders.game.effects.ProductionIncrease; +import org.luxons.sevenwonders.game.resources.Production; +import org.luxons.sevenwonders.game.resources.ResourceType; +import org.luxons.sevenwonders.game.resources.Resources; + import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; @@ -13,10 +18,6 @@ import com.google.gson.JsonParseException; import com.google.gson.JsonPrimitive; import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; -import org.luxons.sevenwonders.game.effects.ProductionIncrease; -import org.luxons.sevenwonders.game.resources.Production; -import org.luxons.sevenwonders.game.resources.ResourceType; -import org.luxons.sevenwonders.game.resources.Resources; public class ProductionIncreaseSerializer implements JsonSerializer<ProductionIncrease>, JsonDeserializer<ProductionIncrease> { @@ -29,8 +30,10 @@ public class ProductionIncreaseSerializer implements JsonSerializer<ProductionIn List<Set<ResourceType>> choices = production.getAlternativeResources(); if (fixedResources.isEmpty()) { return serializeAsChoice(choices, context); - } else { + } else if (choices.isEmpty()) { return serializeAsResources(fixedResources, context); + } else { + throw new IllegalArgumentException("Cannot serialize a production with mixed fixed resources and choices"); } } @@ -43,7 +46,7 @@ public class ProductionIncreaseSerializer implements JsonSerializer<ProductionIn return JsonNull.INSTANCE; } if (choices.size() > 1) { - throw new UnsupportedOperationException("Cannot serialize a production with more than one choice"); + throw new IllegalArgumentException("Cannot serialize a production with more than one choice"); } String str = choices.get(0).stream() .map(ResourceType::getSymbol) @@ -72,6 +75,9 @@ public class ProductionIncreaseSerializer implements JsonSerializer<ProductionIn String[] symbols = choiceStr.split("/"); ResourceType[] choice = new ResourceType[symbols.length]; for (int i = 0; i < symbols.length; i++) { + if (symbols[i].length() != 1) { + throw new IllegalArgumentException("Choice elements must be resource types, got " + symbols[i]); + } choice[i] = ResourceType.fromSymbol(symbols[i].charAt(0)); } return choice; diff --git a/src/main/java/org/luxons/sevenwonders/game/effects/ProductionIncrease.java b/src/main/java/org/luxons/sevenwonders/game/effects/ProductionIncrease.java index 3b3a24a2..22355a91 100644 --- a/src/main/java/org/luxons/sevenwonders/game/effects/ProductionIncrease.java +++ b/src/main/java/org/luxons/sevenwonders/game/effects/ProductionIncrease.java @@ -1,5 +1,7 @@ package org.luxons.sevenwonders.game.effects; +import java.util.Objects; + import org.luxons.sevenwonders.game.boards.Board; import org.luxons.sevenwonders.game.resources.Production; @@ -18,4 +20,21 @@ public class ProductionIncrease extends InstantEffect { public void apply(Board board, Board leftNeighbourBoard, Board rightNeighbourBoard) { board.getProduction().addAll(production); } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + ProductionIncrease that = (ProductionIncrease)o; + return Objects.equals(production, that.production); + } + + @Override + public int hashCode() { + return Objects.hash(production); + } } diff --git a/src/main/java/org/luxons/sevenwonders/game/resources/Production.java b/src/main/java/org/luxons/sevenwonders/game/resources/Production.java index 26f5a58e..c6be8472 100644 --- a/src/main/java/org/luxons/sevenwonders/game/resources/Production.java +++ b/src/main/java/org/luxons/sevenwonders/game/resources/Production.java @@ -5,6 +5,7 @@ import java.util.Arrays; import java.util.EnumSet; import java.util.List; import java.util.Map.Entry; +import java.util.Objects; import java.util.Set; public class Production { @@ -81,4 +82,22 @@ public class Production { private static Set<ResourceType> findFirstAlternativeContaining(List<Set<ResourceType>> alternatives, ResourceType type) { return alternatives.stream().filter(a -> a.contains(type)).findAny().orElse(null); } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Production that = (Production)o; + return Objects.equals(fixedResources, that.fixedResources) && Objects.equals(alternativeResources, + that.alternativeResources); + } + + @Override + public int hashCode() { + return Objects.hash(fixedResources, alternativeResources); + } } |