From a9c2129f36f468852f7f8d5325ab9b681aa4e6b7 Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Sun, 5 Jun 2016 10:45:42 +0200 Subject: Refactored card storage to support decks, updated tests --- test/eu/equalparts/cardbase/CardbaseTest.java | 132 ---------------- .../cardstorage/ReferenceCardContainerTest.java | 168 ++++++++++++++++++++ .../cardstorage/StandaloneCardContainerTest.java | 171 +++++++++++++++++++++ 3 files changed, 339 insertions(+), 132 deletions(-) create mode 100644 test/eu/equalparts/cardbase/cardstorage/ReferenceCardContainerTest.java create mode 100644 test/eu/equalparts/cardbase/cardstorage/StandaloneCardContainerTest.java (limited to 'test') 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)); + } +} -- cgit v1.2.3