aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEduardo Pedroni <e.pedroni91@gmail.com>2016-06-05 10:45:42 +0200
committerEduardo Pedroni <e.pedroni91@gmail.com>2016-06-05 10:45:42 +0200
commita9c2129f36f468852f7f8d5325ab9b681aa4e6b7 (patch)
tree8c47a6523c0fa2752e561ca0c3c2a3cd73c0cc90
parent73d271d69b7ddca5dcc0343a769fa129bc4515df (diff)
Refactored card storage to support decks, updated tests
-rw-r--r--src/eu/equalparts/cardbase/Cardbase.java74
-rw-r--r--src/eu/equalparts/cardbase/cardstorage/ReferenceCardContainer.java118
-rw-r--r--src/eu/equalparts/cardbase/cardstorage/StandaloneCardContainer.java48
-rw-r--r--test/eu/equalparts/cardbase/CardbaseTest.java132
-rw-r--r--test/eu/equalparts/cardbase/cardstorage/ReferenceCardContainerTest.java168
-rw-r--r--test/eu/equalparts/cardbase/cardstorage/StandaloneCardContainerTest.java171
6 files changed, 495 insertions, 216 deletions
diff --git a/src/eu/equalparts/cardbase/Cardbase.java b/src/eu/equalparts/cardbase/Cardbase.java
index 2d92d64..fac4ace 100644
--- a/src/eu/equalparts/cardbase/Cardbase.java
+++ b/src/eu/equalparts/cardbase/Cardbase.java
@@ -2,11 +2,7 @@ package eu.equalparts.cardbase;
import java.io.File;
import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
import java.util.HashMap;
-import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonProperty;
@@ -14,9 +10,7 @@ import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
-import eu.equalparts.cardbase.cards.Card;
import eu.equalparts.cardbase.cardstorage.StandaloneCardContainer;
-import eu.equalparts.cardbase.comparator.CardComparator;
import eu.equalparts.cardbase.utils.JSON;
/**
@@ -24,18 +18,9 @@ import eu.equalparts.cardbase.utils.JSON;
*
* @author Eduardo Pedroni
*/
-public class Cardbase implements StandaloneCardContainer {
+public class Cardbase extends StandaloneCardContainer {
/**
- * A map with card hashes as entry keys (calculated used {@code Card.hashCode()}) and card amounts as entry values.
- */
- @JsonProperty private Map<Integer, Integer> cardReferences = new HashMap<>();
- /**
- * A map with card hashes as entry keys (calculated used {@code Card.hashCode()})
- * and card objects as entry values.
- */
- @JsonProperty private Map<Integer, Card> cardData = new HashMap<>();
- /**
* The decks which have been saved along with this collection of cards.
*/
@JsonProperty private Map<Integer, Object> decks;
@@ -44,8 +29,6 @@ public class Cardbase implements StandaloneCardContainer {
* Creates a clean cardbase.
*/
public Cardbase() {
- cardReferences = new HashMap<>();
- cardData = new HashMap<>();
decks = new HashMap<>();
}
@@ -77,59 +60,4 @@ public class Cardbase implements StandaloneCardContainer {
public void write(File outputFile) throws JsonGenerationException, JsonMappingException, IOException {
JSON.mapper.writeValue(outputFile, this);
}
-
- @Override
- public int getCount(Card cardToCount) {
- int hashCode = cardToCount.hashCode();
- return cardReferences.containsKey(hashCode) ? cardReferences.get(hashCode) : 0;
- }
-
- @Override
- public void addCard(Card cardToAdd, int count) {
- int hashCode = cardToAdd.hashCode();
- if (cardReferences.containsKey(hashCode)) {
- cardReferences.replace(hashCode, cardReferences.get(hashCode) + count);
- } else {
- cardReferences.put(hashCode, count);
- }
- cardData.putIfAbsent(hashCode, cardToAdd);
- }
-
- @Override
- public int removeCard(Card cardToRemove, int count) {
- int hashCode = cardToRemove.hashCode();
- int removed = 0;
-
- if (cardReferences.containsKey(hashCode) && count > 0) {
- int oldCount = cardReferences.get(hashCode);
-
- if (oldCount > count) {
- cardReferences.replace(hashCode, oldCount - count);
- removed = count;
- } else {
- cardReferences.remove(hashCode);
- cardData.remove(cardToRemove.hashCode());
- removed = oldCount;
- }
- }
-
- return removed;
- }
-
- @Override
- public Card getCard(String setCode, String number) {
- return cardData.get(Card.makeHash(setCode, number));
- }
-
- @Override
- public Collection<Card> getCards() {
- return Collections.unmodifiableCollection(cardData.values());
- }
-
- @Override
- public Collection<Card> sortByField(String fieldName) throws NoSuchFieldException {
- List<Card> sortedCards = new ArrayList<Card>(getCards());
- sortedCards.sort(new CardComparator(Card.class.getDeclaredField(fieldName)));
- return Collections.unmodifiableCollection(sortedCards);
- }
}
diff --git a/src/eu/equalparts/cardbase/cardstorage/ReferenceCardContainer.java b/src/eu/equalparts/cardbase/cardstorage/ReferenceCardContainer.java
index 34ea4c6..f43b76b 100644
--- a/src/eu/equalparts/cardbase/cardstorage/ReferenceCardContainer.java
+++ b/src/eu/equalparts/cardbase/cardstorage/ReferenceCardContainer.java
@@ -1,33 +1,137 @@
package eu.equalparts.cardbase.cardstorage;
+import java.util.HashMap;
+import java.util.Map;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+
import eu.equalparts.cardbase.cards.Card;
/**
- * TODO fix comments
- * A class which holds card counts by reference (hash).
+ * A class which contains card quantities with absolutely no other
+ * information about the cards themselves.
*
* @author Eduardo Pedroni
*
*/
-public interface ReferenceCardContainer {
+public class ReferenceCardContainer {
+
+ /**
+ * Land field initialised to 0, accessed with getter and updated with setter.
+ */
+ private int plains = 0, islands = 0, swamps = 0, forests = 0, mountains = 0;
+ /**
+ * A map with card hashes as entry keys (calculated used {@code Card.hashCode()})
+ * and card amounts as entry values.
+ */
+ @JsonProperty private Map<Integer, Integer> cardReferences = new HashMap<>();
+
/**
* Returns the amount of the specified card. If the card is not present at all, return 0.
*
* @param cardToCount a card whose count is to be returned.
* @return the count of the returned card in the container.
*/
- public int getCount(Card cardToCount);
+ public int getCount(Card cardToCount){
+ int cardHash = cardToCount.hashCode();
+ return cardReferences.containsKey(cardHash) ? cardReferences.get(cardHash) : 0;
+ }
/**
* @param cardToAdd the card to add the container.
* @param count the amount to add.
*/
- public void addCard(Card cardToAdd, int count);
+ public void addCard(Card cardToAdd, int count) {
+ cardReferences.put(cardToAdd.hashCode(), getCount(cardToAdd) + count);
+ }
/**
* @param cardToRemove the card to remove from the container.
* @param count the amount to remove.
- * @return the amount that was effectively removed. Could be less than {@code count} depending on how many of the card were present.
+ * @return the amount that was effectively removed. Could be less than {@code count}
+ * depending on how many of the card were present.
+ */
+ public int removeCard(Card cardToRemove, int count) {
+ int cardHash = cardToRemove.hashCode();
+ if (count <= 0 || !cardReferences.containsKey(cardHash)) {
+ return 0;
+ }
+
+ if (count >= cardReferences.get(cardHash)) {
+ return cardReferences.remove(cardHash);
+ } else {
+ cardReferences.put(cardHash, cardReferences.get(cardHash) - count);
+ return count;
+ }
+ }
+
+ /**
+ * @return the plains
+ */
+ public int getPlains() {
+ return plains;
+ }
+
+ /**
+ * @return the islands
+ */
+ public int getIslands() {
+ return islands;
+ }
+
+ /**
+ * @return the swamps
+ */
+ public int getSwamps() {
+ return swamps;
+ }
+
+ /**
+ * @return the forests
+ */
+ public int getForests() {
+ return forests;
+ }
+
+ /**
+ * @return the mountains
+ */
+ public int getMountains() {
+ return mountains;
+ }
+
+ /**
+ * @param plains the plains to set
+ */
+ public void setPlains(int plains) {
+ this.plains = plains;
+ }
+
+ /**
+ * @param islands the islands to set
+ */
+ public void setIslands(int islands) {
+ this.islands = islands;
+ }
+
+ /**
+ * @param swamps the swamps to set
+ */
+ public void setSwamps(int swamps) {
+ this.swamps = swamps;
+ }
+
+ /**
+ * @param forests the forests to set
+ */
+ public void setForests(int forests) {
+ this.forests = forests;
+ }
+
+ /**
+ * @param mountains the mountains to set
*/
- public int removeCard(Card cardToRemove, int count);
+ public void setMountains(int mountains) {
+ this.mountains = mountains;
+ }
}
diff --git a/src/eu/equalparts/cardbase/cardstorage/StandaloneCardContainer.java b/src/eu/equalparts/cardbase/cardstorage/StandaloneCardContainer.java
index aa2a3cf..1a4315d 100644
--- a/src/eu/equalparts/cardbase/cardstorage/StandaloneCardContainer.java
+++ b/src/eu/equalparts/cardbase/cardstorage/StandaloneCardContainer.java
@@ -1,8 +1,16 @@
package eu.equalparts.cardbase.cardstorage;
+import java.util.ArrayList;
import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
import eu.equalparts.cardbase.cards.Card;
+import eu.equalparts.cardbase.comparator.CardComparator;
/**
* TODO fix comments
@@ -11,7 +19,13 @@ import eu.equalparts.cardbase.cards.Card;
* @author Eduardo Pedroni
*
*/
-public interface StandaloneCardContainer extends ReferenceCardContainer {
+public class StandaloneCardContainer extends ReferenceCardContainer {
+ /**
+ * A map with card hashes as entry keys (calculated used {@code Card.hashCode()})
+ * and card objects as entry values.
+ */
+ @JsonProperty private Map<Integer, Card> cardData = new HashMap<>();
+
/**
* Returns a card from the cardbase by set code and number.
* If no such card is in the cardbase, returns null.
@@ -20,7 +34,10 @@ public interface StandaloneCardContainer extends ReferenceCardContainer {
* @param number the requested card's set number.
* @return the requested {@code Card} or null if no card is found.
*/
- public Card getCard(String setCode, String number);
+ public Card getCard(String setCode, String number) {
+ return cardData.get(Card.makeHash(setCode, number));
+ }
+
/**
* This method is intended to allow iteration directly on the list of cards,
* while at the same time retaining control over the insert and remove procedures.
@@ -29,12 +46,35 @@ public interface StandaloneCardContainer extends ReferenceCardContainer {
*
* @return an unmodifiable list of all the cards in the cardbase.
*/
- public Collection<Card> getCards();
+ public Collection<Card> getCards() {
+ return Collections.unmodifiableCollection(cardData.values());
+ }
+
/**
* @param fieldName the name of the field by which to sort.
* @return an unmodifiable collection representing the cardbase sorted in the required order.
* @throws NoSuchFieldException if the field provided is invalid.
*/
- public Collection<Card> sortByField(String fieldName) throws NoSuchFieldException;
+ public Collection<Card> sortByField(String fieldName) throws NoSuchFieldException {
+ List<Card> sortedCards = new ArrayList<Card>(getCards());
+ sortedCards.sort(new CardComparator(Card.class.getDeclaredField(fieldName)));
+ return Collections.unmodifiableCollection(sortedCards);
+ }
+ @Override
+ public void addCard(Card cardToAdd, int count) {
+ super.addCard(cardToAdd, count);
+ cardData.putIfAbsent(cardToAdd.hashCode(), cardToAdd);
+ }
+
+ @Override
+ public int removeCard(Card cardToRemove, int count) {
+ int removed = super.removeCard(cardToRemove, count);
+
+ if (getCount(cardToRemove) <= 0) {
+ cardData.remove(cardToRemove.hashCode());
+ }
+
+ return removed;
+ }
}
diff --git a/test/eu/equalparts/cardbase/CardbaseTest.java b/test/eu/equalparts/cardbase/CardbaseTest.java
index d5fc8e2..47ad027 100644
--- a/test/eu/equalparts/cardbase/CardbaseTest.java
+++ b/test/eu/equalparts/cardbase/CardbaseTest.java
@@ -2,13 +2,10 @@ package eu.equalparts.cardbase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
-import java.lang.reflect.Field;
import org.junit.AfterClass;
import org.junit.Before;
@@ -184,133 +181,4 @@ public class CardbaseTest {
exception.expect(NullPointerException.class);
uut = Cardbase.load(null);
}
-
- /***********************************************************************************
- * Adding card tests, happy path
- ***********************************************************************************/
- @Test
- public void newCardIsAdded() throws Exception {
- assertNull("Container should not contain the test card to begin with.", uut.getCard(testCard.setCode, testCard.number));
-
- uut.addCard(testCard, 1);
-
- assertEquals("Container should contain the test card once it is added.", testCard, uut.getCard(testCard.setCode, testCard.number));
- assertEquals("Container should have contained 1 test card.", 1, uut.getCount(testCard));
- }
-
- @Test
- public void existingCardIsIncremented() throws Exception {
- uut.addCard(testCard, 2);
- uut.addCard(testCard, 4);
-
- Card addedCard = uut.getCard(testCard.setCode, testCard.number);
- assertNotNull("Card was not found in cardbase.", addedCard);
- assertEquals("Card count was not updated correctly.", 6, uut.getCount(addedCard));
- }
-
- /*
- * Edge cases
- */
- @Test
- public void cardAddedIsNull() throws Exception {
- exception.expect(NullPointerException.class);
- uut.addCard(null, 0);
- }
-
- /***********************************************************************************
- * Removing card tests, happy path
- ***********************************************************************************/
- @Test
- public void cardIsStillPresentIfRemoveCountIsLessThanCardCount() throws Exception {
- uut.addCard(testCard, 5);
-
- int removed = uut.removeCard(testCard, 3);
-
- assertEquals("Card count was not updated correctly.", 2, uut.getCount(testCard));
- assertEquals("Container reports wrong removed count.", 3, removed);
- assertEquals("Card is missing from container.", testCard, uut.getCard(testCard.setCode, testCard.number));
- }
-
- @Test
- public void cardIsRemovedIfRemoveCountIsEqualToCardCount() throws Exception {
- uut.addCard(testCard, 5);
-
- int removed = uut.removeCard(testCard, 5);
-
- assertEquals("Card was not removed from container.", 0, uut.getCount(testCard));
- assertEquals("Container reports wrong removed count.", 5, removed);
- assertNull("Card is not missing from container.", uut.getCard(testCard.setCode, testCard.number));
- }
-
- @Test
- public void cardIsRemovedIfRemoveCountIsGreaterThanCardCount() throws Exception {
- uut.addCard(testCard, 3);
-
- int removed = uut.removeCard(testCard, 5);
-
- assertEquals("Card was not removed from container.", 0, uut.getCount(testCard));
- assertEquals("Container reports wrong removed count.", 3, removed);
- assertNull("Card is not missing from container.", uut.getCard(testCard.setCode, testCard.number));
- }
-
- /*
- * Edge cases
- */
- @Test
- public void removedCardIsNull() throws Exception {
- exception.expect(NullPointerException.class);
- uut.removeCard(null, 0);
- }
-
- @Test
- public void removedCardIsNotInContainer() throws Exception {
- assertNull("Card is not initially missing from container.", uut.getCard(testCard.setCode, testCard.number));
-
- int removed = uut.removeCard(testCard, 1);
-
- assertEquals("Removed count should be 0.", 0, removed);
- assertNull("Card is not missing from container.", uut.getCard(testCard.setCode, testCard.number));
- }
-
- @Test
- public void removedCountIsLessThanZero() throws Exception {
- uut.addCard(testCard, 3);
-
- int removed = uut.removeCard(testCard, -4);
-
- assertEquals("Card count in container should be unchanged.", 3, uut.getCount(testCard));
- assertEquals("Container reports wrong removed count.", 0, removed);
- assertEquals("Card should not be missing from container.", testCard, uut.getCard(testCard.setCode, testCard.number));
- }
-
- /***********************************************************************************
- * Card getter tests, happy path
- ***********************************************************************************/
- @Test
- public void correctCardIsReturnedByGetter() throws Exception {
- uut.addCard(testCard, 1);
-
- Card card = uut.getCard(testCard.setCode, testCard.number);
-
- for (Field field : Card.class.getFields()) {
- assertEquals("Field " + field.getName(), field.get(testCard), field.get(card));
- }
- }
-
- @Test
- public void correctCardCollectionIsReturnedByGetter() throws Exception {
- uut.addCard(testCard, 1);
-
- assertTrue("Not all cards were returned by the getter.", uut.getCards().contains(testCard));
- }
-
- @Test
- public void cardCollectionWhenContainerIsEmpty() throws Exception {
- assertEquals("Returned collection size should have been 0.", 0, uut.getCards().size());
- }
-
- @Test
- public void getCardIsNotInCardbase() throws Exception {
- assertNull("Method should have returned null", uut.getCard(testCard.setCode, testCard.number));
- }
}
diff --git a/test/eu/equalparts/cardbase/cardstorage/ReferenceCardContainerTest.java b/test/eu/equalparts/cardbase/cardstorage/ReferenceCardContainerTest.java
new file mode 100644
index 0000000..bf49e2f
--- /dev/null
+++ b/test/eu/equalparts/cardbase/cardstorage/ReferenceCardContainerTest.java
@@ -0,0 +1,168 @@
+package eu.equalparts.cardbase.cardstorage;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.rules.TemporaryFolder;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+import eu.equalparts.cardbase.CardbaseTest;
+import eu.equalparts.cardbase.cards.Card;
+
+public class ReferenceCardContainerTest {
+
+ private ReferenceCardContainer uut;
+ private static Card testCard;
+
+ @Rule
+ public final ExpectedException exception = ExpectedException.none();
+
+ @Rule
+ public final TemporaryFolder tempFolder = new TemporaryFolder();
+
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ ObjectMapper mapper = new ObjectMapper();
+ testCard = mapper.readValue(CardbaseTest.class.getResourceAsStream("/shivandragon.json"), Card.class);
+ }
+
+ @Before
+ public void setUp() throws Exception {
+ uut = new ReferenceCardContainer();
+ }
+
+ /***********************************************************************************
+ * Adding card tests, happy path
+ ***********************************************************************************/
+ @Test
+ public void newCardIsAdded() throws Exception {
+ assertEquals("Container should not contain the test card to begin with.", 0, uut.getCount(testCard));
+
+ uut.addCard(testCard, 1);
+
+ assertEquals("Container should contain 1 test card.", 1, uut.getCount(testCard));
+ }
+
+ @Test
+ public void existingCardIsIncremented() throws Exception {
+ uut.addCard(testCard, 2);
+ uut.addCard(testCard, 4);
+
+ assertEquals("Card count was not updated correctly.", 6, uut.getCount(testCard));
+ }
+
+ /*
+ * Edge cases
+ */
+ @Test
+ public void cardAddedIsNull() throws Exception {
+ exception.expect(NullPointerException.class);
+ uut.addCard(null, 0);
+ }
+
+ /***********************************************************************************
+ * Removing card tests, happy path
+ ***********************************************************************************/
+ @Test
+ public void cardIsStillPresentIfRemoveCountIsLessThanCardCount() throws Exception {
+ uut.addCard(testCard, 5);
+
+ int removed = uut.removeCard(testCard, 3);
+
+ assertEquals("Card count was not updated correctly.", 2, uut.getCount(testCard));
+ assertEquals("Container reports wrong removed count.", 3, removed);
+ }
+
+ @Test
+ public void cardIsRemovedIfRemoveCountIsEqualToCardCount() throws Exception {
+ uut.addCard(testCard, 5);
+
+ int removed = uut.removeCard(testCard, 5);
+
+ assertEquals("Card was not removed from container.", 0, uut.getCount(testCard));
+ assertEquals("Container reports wrong removed count.", 5, removed);
+ }
+
+ @Test
+ public void cardIsRemovedIfRemoveCountIsGreaterThanCardCount() throws Exception {
+ uut.addCard(testCard, 3);
+
+ int removed = uut.removeCard(testCard, 5);
+
+ assertEquals("Card was not removed from container.", 0, uut.getCount(testCard));
+ assertEquals("Container reports wrong removed count.", 3, removed);
+ }
+
+ /*
+ * Edge cases
+ */
+ @Test
+ public void removedCardIsNull() throws Exception {
+ exception.expect(NullPointerException.class);
+ uut.removeCard(null, 0);
+ }
+
+ @Test
+ public void removedCardIsNotInContainer() throws Exception {
+ assertEquals("Card is not initially missing from container.", 0, uut.getCount(testCard));
+
+ int removed = uut.removeCard(testCard, 1);
+
+ assertEquals("Removed count should be 0.", 0, removed);
+ assertEquals("Card should still be missing from container.", 0, uut.getCount(testCard));
+ }
+
+ @Test
+ public void removedCountIsLessThanZero() throws Exception {
+ uut.addCard(testCard, 3);
+
+ int removed = uut.removeCard(testCard, -4);
+
+ assertEquals("Card count in container should be unchanged.", 3, uut.getCount(testCard));
+ assertEquals("Container reports wrong removed count.", 0, removed);
+ }
+
+ /***********************************************************************************
+ * Land tests
+ ***********************************************************************************/
+ @Test
+ public void containsIslands() throws Exception {
+ assertEquals(0, uut.getIslands());
+ uut.setIslands(5);
+ assertEquals(5, uut.getIslands());
+ }
+
+ @Test
+ public void containsPlains() throws Exception {
+ assertEquals(0, uut.getPlains());
+ uut.setPlains(5);
+ assertEquals(5, uut.getPlains());
+ }
+
+ @Test
+ public void containsSwamps() throws Exception {
+ assertEquals(0, uut.getSwamps());
+ uut.setSwamps(5);
+ assertEquals(5, uut.getSwamps());
+ }
+
+ @Test
+ public void containsMountains() throws Exception {
+ assertEquals(0, uut.getMountains());
+ uut.setMountains(5);
+ assertEquals(5, uut.getMountains());
+ }
+
+ @Test
+ public void containsForests() throws Exception {
+ assertEquals(0, uut.getForests());
+ uut.setForests(5);
+ assertEquals(5, uut.getForests());
+ }
+
+}
diff --git a/test/eu/equalparts/cardbase/cardstorage/StandaloneCardContainerTest.java b/test/eu/equalparts/cardbase/cardstorage/StandaloneCardContainerTest.java
new file mode 100644
index 0000000..6071e09
--- /dev/null
+++ b/test/eu/equalparts/cardbase/cardstorage/StandaloneCardContainerTest.java
@@ -0,0 +1,171 @@
+package eu.equalparts.cardbase.cardstorage;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.lang.reflect.Field;
+
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.rules.TemporaryFolder;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+import eu.equalparts.cardbase.CardbaseTest;
+import eu.equalparts.cardbase.cards.Card;
+
+public class StandaloneCardContainerTest {
+ private StandaloneCardContainer uut;
+ private static Card testCard;
+
+ @Rule
+ public final ExpectedException exception = ExpectedException.none();
+
+ @Rule
+ public final TemporaryFolder tempFolder = new TemporaryFolder();
+
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ ObjectMapper mapper = new ObjectMapper();
+ testCard = mapper.readValue(CardbaseTest.class.getResourceAsStream("/shivandragon.json"), Card.class);
+ }
+
+ @Before
+ public void setUp() throws Exception {
+ uut = new StandaloneCardContainer();
+ }
+
+ /***********************************************************************************
+ * Adding card tests, happy path
+ ***********************************************************************************/
+ @Test
+ public void newCardIsAdded() throws Exception {
+ assertNull("Container should not contain the test card to begin with.", uut.getCard(testCard.setCode, testCard.number));
+
+ uut.addCard(testCard, 1);
+
+ assertEquals("Container should contain the test card once it is added.", testCard, uut.getCard(testCard.setCode, testCard.number));
+ assertEquals("Container should have contained 1 test card.", 1, uut.getCount(testCard));
+ }
+
+ @Test
+ public void existingCardIsIncremented() throws Exception {
+ uut.addCard(testCard, 2);
+ uut.addCard(testCard, 4);
+
+ Card addedCard = uut.getCard(testCard.setCode, testCard.number);
+ assertNotNull("Card was not found in cardbase.", addedCard);
+ assertEquals("Card count was not updated correctly.", 6, uut.getCount(addedCard));
+ }
+
+ /*
+ * Edge cases
+ */
+ @Test
+ public void cardAddedIsNull() throws Exception {
+ exception.expect(NullPointerException.class);
+ uut.addCard(null, 0);
+ }
+
+ /***********************************************************************************
+ * Removing card tests, happy path
+ ***********************************************************************************/
+ @Test
+ public void cardIsStillPresentIfRemoveCountIsLessThanCardCount() throws Exception {
+ uut.addCard(testCard, 5);
+
+ int removed = uut.removeCard(testCard, 3);
+
+ assertEquals("Card count was not updated correctly.", 2, uut.getCount(testCard));
+ assertEquals("Container reports wrong removed count.", 3, removed);
+ assertEquals("Card is missing from container.", testCard, uut.getCard(testCard.setCode, testCard.number));
+ }
+
+ @Test
+ public void cardIsRemovedIfRemoveCountIsEqualToCardCount() throws Exception {
+ uut.addCard(testCard, 5);
+
+ int removed = uut.removeCard(testCard, 5);
+
+ assertEquals("Card was not removed from container.", 0, uut.getCount(testCard));
+ assertEquals("Container reports wrong removed count.", 5, removed);
+ assertNull("Card is not missing from container.", uut.getCard(testCard.setCode, testCard.number));
+ }
+
+ @Test
+ public void cardIsRemovedIfRemoveCountIsGreaterThanCardCount() throws Exception {
+ uut.addCard(testCard, 3);
+
+ int removed = uut.removeCard(testCard, 5);
+
+ assertEquals("Card was not removed from container.", 0, uut.getCount(testCard));
+ assertEquals("Container reports wrong removed count.", 3, removed);
+ assertNull("Card is not missing from container.", uut.getCard(testCard.setCode, testCard.number));
+ }
+
+ /*
+ * Edge cases
+ */
+ @Test
+ public void removedCardIsNull() throws Exception {
+ exception.expect(NullPointerException.class);
+ uut.removeCard(null, 0);
+ }
+
+ @Test
+ public void removedCardIsNotInContainer() throws Exception {
+ assertNull("Card is not initially missing from container.", uut.getCard(testCard.setCode, testCard.number));
+
+ int removed = uut.removeCard(testCard, 1);
+
+ assertEquals("Removed count should be 0.", 0, removed);
+ assertNull("Card is not missing from container.", uut.getCard(testCard.setCode, testCard.number));
+ }
+
+ @Test
+ public void removedCountIsLessThanZero() throws Exception {
+ uut.addCard(testCard, 3);
+
+ int removed = uut.removeCard(testCard, -4);
+
+ assertEquals("Card count in container should be unchanged.", 3, uut.getCount(testCard));
+ assertEquals("Container reports wrong removed count.", 0, removed);
+ assertEquals("Card should not be missing from container.", testCard, uut.getCard(testCard.setCode, testCard.number));
+ }
+
+ /***********************************************************************************
+ * Card getter tests, happy path
+ ***********************************************************************************/
+ @Test
+ public void correctCardIsReturnedByGetter() throws Exception {
+ uut.addCard(testCard, 1);
+
+ Card card = uut.getCard(testCard.setCode, testCard.number);
+
+ for (Field field : Card.class.getFields()) {
+ assertEquals("Field " + field.getName(), field.get(testCard), field.get(card));
+ }
+ }
+
+ @Test
+ public void correctCardCollectionIsReturnedByGetter() throws Exception {
+ uut.addCard(testCard, 1);
+
+ assertTrue("Not all cards were returned by the getter.", uut.getCards().contains(testCard));
+ }
+
+ @Test
+ public void cardCollectionWhenContainerIsEmpty() throws Exception {
+ assertEquals("Returned collection size should have been 0.", 0, uut.getCards().size());
+ }
+
+ @Test
+ public void getCardIsNotInCardbase() throws Exception {
+ assertNull("Method should have returned null", uut.getCard(testCard.setCode, testCard.number));
+ }
+}