From 2227aaf4cdee9535dedc28d570d6fdf2d38d1fe3 Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Tue, 21 Jul 2015 17:30:10 +0200 Subject: Added lots of cardbase tests, refactored a little bit, will need to completely rework the way counts are done --- .../equalparts/test/cardbase/CardbaseSortTest.java | 38 ++-- test/eu/equalparts/test/cardbase/CardbaseTest.java | 236 +++++++++++++++++++++ test/eu/equalparts/test/cardbase/notjson.txt | 1 + test/eu/equalparts/test/cardbase/shivandragon.json | 1 + test/eu/equalparts/test/cardbase/testbase.cb | 1 + 5 files changed, 258 insertions(+), 19 deletions(-) create mode 100644 test/eu/equalparts/test/cardbase/CardbaseTest.java create mode 100644 test/eu/equalparts/test/cardbase/notjson.txt create mode 100644 test/eu/equalparts/test/cardbase/shivandragon.json create mode 100644 test/eu/equalparts/test/cardbase/testbase.cb (limited to 'test/eu/equalparts') diff --git a/test/eu/equalparts/test/cardbase/CardbaseSortTest.java b/test/eu/equalparts/test/cardbase/CardbaseSortTest.java index eca41f0..4ae27fb 100644 --- a/test/eu/equalparts/test/cardbase/CardbaseSortTest.java +++ b/test/eu/equalparts/test/cardbase/CardbaseSortTest.java @@ -45,7 +45,7 @@ public class CardbaseSortTest { } @Test - public void test_sortByName() throws Exception { + public void sortByName() throws Exception { Collection sortedCards = cardbase.sort("name"); int i = 0; String[] names = { @@ -65,7 +65,7 @@ public class CardbaseSortTest { } @Test - public void test_sortByLayout() throws Exception { + public void sortByLayout() throws Exception { Collection sortedCards = cardbase.sort("layout"); int i = 0; String[] layouts = { @@ -85,7 +85,7 @@ public class CardbaseSortTest { } @Test - public void test_sortByManaCost() throws Exception { + public void sortByManaCost() throws Exception { Collection sortedCards = cardbase.sort("manaCost"); int i = 0; String[] costs = { @@ -105,7 +105,7 @@ public class CardbaseSortTest { } @Test - public void test_sortByCMC() throws Exception { + public void sortByCMC() throws Exception { Collection sortedCards = cardbase.sort("cmc"); int i = 0; Integer[] cmcs = {2, 3, 4, 5, 6, 6, 6, 8}; @@ -116,7 +116,7 @@ public class CardbaseSortTest { } @Test - public void test_sortByType() throws Exception { + public void sortByType() throws Exception { Collection sortedCards = cardbase.sort("type"); int i = 0; String[] types = { @@ -136,7 +136,7 @@ public class CardbaseSortTest { } @Test - public void test_sortByRarity() throws Exception { + public void sortByRarity() throws Exception { Collection sortedCards = cardbase.sort("rarity"); int i = 0; String[] rarities = { @@ -156,7 +156,7 @@ public class CardbaseSortTest { } @Test - public void test_sortByText() throws Exception { + public void sortByText() throws Exception { Collection sortedCards = cardbase.sort("text"); int i = 0; String[] texts = { @@ -176,7 +176,7 @@ public class CardbaseSortTest { } @Test - public void test_sortByFlavor() throws Exception { + public void sortByFlavor() throws Exception { Collection sortedCards = cardbase.sort("flavor"); int i = 0; String[] flavors = { @@ -197,7 +197,7 @@ public class CardbaseSortTest { } @Test - public void test_sortByArtist() throws Exception { + public void sortByArtist() throws Exception { Collection sortedCards = cardbase.sort("artist"); int i = 0; String[] artists = { @@ -217,7 +217,7 @@ public class CardbaseSortTest { } @Test - public void test_sortByNumber() throws Exception { + public void sortByNumber() throws Exception { Collection sortedCards = cardbase.sort("number"); int i = 0; String[] numbers = { @@ -237,7 +237,7 @@ public class CardbaseSortTest { } @Test - public void test_sortByPower() throws Exception { + public void sortByPower() throws Exception { Collection sortedCards = cardbase.sort("power"); int i = 0; String[] powers = { @@ -258,7 +258,7 @@ public class CardbaseSortTest { } @Test - public void test_sortByToughness() throws Exception { + public void sortByToughness() throws Exception { Collection sortedCards = cardbase.sort("power"); int i = 0; String[] toughnesses = { @@ -279,7 +279,7 @@ public class CardbaseSortTest { } @Test - public void test_sortByLoyalty() throws Exception { + public void sortByLoyalty() throws Exception { Collection sortedCards = cardbase.sort("loyalty"); int i = 0; Integer[] loyalties = {0, 0, 0, 0, 0, 0, 0, 4}; @@ -291,7 +291,7 @@ public class CardbaseSortTest { } @Test - public void test_sortByMultiverseID() throws Exception { + public void sortByMultiverseID() throws Exception { Collection sortedCards = cardbase.sort("multiverseid"); int i = 0; Integer[] ids = {74128, 74489, 193551, 238330, 366408, 383168, 383172, 391949 }; @@ -303,7 +303,7 @@ public class CardbaseSortTest { } @Test - public void test_sortByImageName() throws Exception { + public void sortByImageName() throws Exception { Collection sortedCards = cardbase.sort("imageName"); int i = 0; String[] names = { @@ -323,7 +323,7 @@ public class CardbaseSortTest { } @Test - public void test_sortByWatermark() throws Exception { + public void sortByWatermark() throws Exception { Collection sortedCards = cardbase.sort("watermark"); int i = 0; String[] watermarks = { @@ -344,7 +344,7 @@ public class CardbaseSortTest { } @Test - public void test_sortBySetCode() throws Exception { + public void sortBySetCode() throws Exception { Collection sortedCards = cardbase.sort("setCode"); int i = 0; String[] sets = { @@ -363,7 +363,7 @@ public class CardbaseSortTest { } } - public void test_sortByImageCode() throws Exception { + public void sortByImageCode() throws Exception { Collection sortedCards = cardbase.sort("imageCode"); int i = 0; String[] codes = { @@ -383,7 +383,7 @@ public class CardbaseSortTest { } @Test - public void test_sortByCount() throws Exception { + public void sortByCount() throws Exception { Collection sortedCards = cardbase.sort("count"); int i = 0; Integer[] counts = {1, 1, 1, 1, 2, 3, 8, 15 }; diff --git a/test/eu/equalparts/test/cardbase/CardbaseTest.java b/test/eu/equalparts/test/cardbase/CardbaseTest.java new file mode 100644 index 0000000..8096f6e --- /dev/null +++ b/test/eu/equalparts/test/cardbase/CardbaseTest.java @@ -0,0 +1,236 @@ +package eu.equalparts.test.cardbase; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.IOException; + +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.JsonMappingException; + +import eu.equalparts.cardbase.Cardbase; +import eu.equalparts.cardbase.cards.Card; +import eu.equalparts.cardbase.utils.JSON; + +/** + * TODO deck functionality needs to be built into these. + * + * @author Eduardo Pedroni + * + */ +public class CardbaseTest { + + private Cardbase cardbase; + private static Card shivanDragon; + + @Rule + public final ExpectedException exception = ExpectedException.none(); + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + shivanDragon = JSON.mapper.readValue(CardbaseTest.class.getResourceAsStream("shivandragon.json"), Card.class); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + @Before + public void setUp() throws Exception { + cardbase = new Cardbase(); + } + + /*********************************************************************************** + * Constructor tests, happy path + ***********************************************************************************/ + @Test + public void cleanCardbaseIsInitialised() throws Exception { + assertEquals("Card collection is not empty.", 0, cardbase.getCards().size()); + assertEquals("Deck collection is not empty.", 0, cardbase.getDecks().size()); + } + + @Test + public void fileCardbaseIsInitialised() throws Exception { + cardbase = new Cardbase(new File(getClass().getResource("testbase.cb").toURI())); + + assertEquals("Card collection contains the wrong number of card entries.", 6, cardbase.getCards().size()); + + class CardInfo { + String setCode, number; + Integer count; + public CardInfo(String setCode, String number, Integer count) { + this.setCode = setCode; + this.number = number; + this.count = count; + } + } + CardInfo[] testCards = new CardInfo[] { + new CardInfo("M12", "34", 2), + new CardInfo("FRF", "129", 8), + new CardInfo("M12", "26", 1), + new CardInfo("FRF", "127", 1), + new CardInfo("FRF", "128", 1), + new CardInfo("M12", "152", 1)}; + + for (CardInfo ci : testCards) { + Card card = cardbase.getCard(ci.setCode, ci.number); + assertNotNull("Missing card, set " + ci.setCode + ", " + ci.number, card); + assertEquals("Wrong card count, set " + ci.setCode + ", " + ci.number, ci.count, card.count); + } + } + + /* + * Edge cases + */ + @Test + public void loadFileDoesNotExist() throws Exception { + exception.expect(IOException.class); + cardbase = new Cardbase(new File("not a file")); + } + + @Test + public void loadFileHasWrongStructure() throws Exception { + exception.expect(JsonMappingException.class); + cardbase = new Cardbase(new File(getClass().getResource("testcards.json").toURI())); + } + + @Test + public void loadFileIsNotJson() throws Exception { + exception.expect(JsonParseException.class); + cardbase = new Cardbase(new File(getClass().getResource("notjson.txt").toURI())); + } + + /*********************************************************************************** + * Saving cardbase tests, happy path + ***********************************************************************************/ + private boolean validateSaveFile(File testFile) throws IOException { + if (!testFile.exists()) { + if (testFile.createNewFile()) { + if (testFile.canWrite()) { + return true; + } else { + fail("Cannot write to testsave.cb, aborting..."); + } + } else { + fail("testsave.cb could not be created, aborting..."); + } + } else { + fail("testsave.cb already exists, aborting..."); + } + return false; + } + + @Test + public void cardbaseIsSaved() throws Exception { + File testFile = new File("savetest.cb"); + validateSaveFile(testFile); + try { + cardbase.writeCollection(testFile); + cardbase = new Cardbase(testFile); + assertEquals("Cardbase should contain no cards.", 0, cardbase.getCards().size()); + + cardbase.addCard(shivanDragon); + + cardbase.writeCollection(testFile); + cardbase = new Cardbase(testFile); + assertEquals("Cardbase should contain one card.", 1, cardbase.getCards().size()); + Card card = cardbase.getCard("M15", "281"); + assertNotNull("Cardbase should contain a Shivan Dragon.", card); + assertEquals("Cardbase should contain only one Shivan Dragon", new Integer(1), card.count); + } finally { + testFile.delete(); + } + } + + /* + * Edge cases + */ + @Test + public void saveFileCannotBeWrittenTo() throws Exception { + File testFile = new File("savetest.cb"); + validateSaveFile(testFile); + testFile.setWritable(false); + try { + exception.expect(IOException.class); + cardbase.writeCollection(testFile); + } finally { + testFile.delete(); + } + } + + /*********************************************************************************** + * Adding card tests, happy path + ***********************************************************************************/ + @Test + public void newCardIsAdded() throws Exception { + cardbase.addCard(shivanDragon); + Card addedCard = cardbase.getCard("M15", "281"); + + assertNotNull("Card was not found in cardbase.", addedCard); + assertEquals(shivanDragon, addedCard); + } + + @Test + public void existingCardIsIncremented() throws Exception { + Card shivanClone = shivanDragon.clone(); + cardbase.addCard(shivanClone); + Card shivanClone2 = shivanDragon.clone(); + shivanClone2.count = 2; + cardbase.addCard(shivanClone2); + + Card addedCard = cardbase.getCard("M15", "281"); + assertNotNull("Card was not found in cardbase.", addedCard); + assertEquals("Card count was not updated correctly.", new Integer(3), addedCard.count); + } + + /*********************************************************************************** + * Adding card tests, happy path + ***********************************************************************************/ + @Test + public void cardRemoveCountIsLessThanCardCount() throws Exception { + Card shivanClone = shivanDragon.clone(); + shivanClone.count = 5; + cardbase.addCard(shivanClone); + + int removed = cardbase.removeCard(shivanDragon); + + Card removedCard = cardbase.getCard("M15", "281"); + assertNotNull("Card was not found in cardbase.", removedCard); + assertEquals("Card count was not updated correctly.", new Integer(4), removedCard.count); + assertEquals("Cardbase reports wrong removed count.", 1, removed); + } + + @Test + public void cardRemoveCountIsEqualToCardCount() throws Exception { + Card shivanClone = shivanDragon.clone(); + cardbase.addCard(shivanClone); + + int removed = cardbase.removeCard(shivanDragon); + + Card removedCard = cardbase.getCard("M15", "281"); + assertNull("Card was not removed from cardbase.", removedCard); + assertEquals("Cardbase reports wrong removed count.", 1, removed); + } + + @Test + public void cardRemoveCountIsGreaterThanCardCount() throws Exception { + Card shivanClone = shivanDragon.clone(); + shivanClone.count = 3; + cardbase.addCard(shivanClone); + + Card shivanClone2 = shivanDragon.clone(); + shivanClone2.count = 5; + int removed = cardbase.removeCard(shivanClone2); + + Card removedCard = cardbase.getCard("M15", "281"); + assertNull("Card was not removed from cardbase.", removedCard); + assertEquals("Cardbase reports wrong removed count.", 3, removed); + } +} diff --git a/test/eu/equalparts/test/cardbase/notjson.txt b/test/eu/equalparts/test/cardbase/notjson.txt new file mode 100644 index 0000000..7705955 --- /dev/null +++ b/test/eu/equalparts/test/cardbase/notjson.txt @@ -0,0 +1 @@ +This is a file which does not contain valid JSON. \ No newline at end of file diff --git a/test/eu/equalparts/test/cardbase/shivandragon.json b/test/eu/equalparts/test/cardbase/shivandragon.json new file mode 100644 index 0000000..f846d35 --- /dev/null +++ b/test/eu/equalparts/test/cardbase/shivandragon.json @@ -0,0 +1 @@ +{"name":"Shivan Dragon","layout":"normal","manaCost":"{4}{R}{R}","cmc":6,"type":"Creature — Dragon","rarity":"Rare","text":"Flying (This creature can't be blocked except by creatures with flying or reach.)\n{R}: Shivan Dragon gets +1/+0 until end of turn.","flavor":"The undisputed master of the mountains of Shiv.","artist":"Donato Giancola","number":"281","power":"5","toughness":"5","loyalty":null,"multiverseid":383172,"imageName":"shivan dragon","watermark":null,"setCode":"M15","imageCode":"m15","count":1} diff --git a/test/eu/equalparts/test/cardbase/testbase.cb b/test/eu/equalparts/test/cardbase/testbase.cb new file mode 100644 index 0000000..123e0e8 --- /dev/null +++ b/test/eu/equalparts/test/cardbase/testbase.cb @@ -0,0 +1 @@ +{"M12~34":{"name":"Siege Mastodon","layout":"normal","manaCost":"{4}{W}","cmc":5,"type":"Creature — Elephant","rarity":"Common","text":null,"flavor":"\"The walls of the wicked will fall before us. Ready the siege engines. We proceed to war!\"\n—General Avitora","artist":"Matt Cavotta","number":"34","power":"3","toughness":"5","loyalty":null,"multiverseid":222635,"imageName":"siege mastodon","watermark":null,"setCode":"M12","imageCode":"m12","count":2},"FRF~129":{"name":"Formless Nurturing","layout":"normal","manaCost":"{3}{G}","cmc":4,"type":"Sorcery","rarity":"Common","text":"Manifest the top card of your library, then put a +1/+1 counter on it. (To manifest a card, put it onto the battlefield face down as a 2/2 creature. Turn it face up any time for its mana cost if it's a creature card.)","flavor":null,"artist":"Cliff Childs","number":"129","power":null,"toughness":null,"loyalty":null,"multiverseid":391837,"imageName":"formless nurturing","watermark":null,"setCode":"FRF","imageCode":"frf","count":8},"M12~26":{"name":"Mighty Leap","layout":"normal","manaCost":"{1}{W}","cmc":2,"type":"Instant","rarity":"Common","text":"Target creature gets +2/+2 and gains flying until end of turn. (It can't be blocked except by creatures with flying or reach.)","flavor":"\"The southern fortress taken by invaders? Heh, sure . . . when elephants fly.\"\n—Brezard Skeinbow, captain of the guard","artist":"rk post","number":"26","power":null,"toughness":null,"loyalty":null,"multiverseid":241989,"imageName":"mighty leap","watermark":null,"setCode":"M12","imageCode":"m12","count":1},"FRF~127":{"name":"Destructor Dragon","layout":"normal","manaCost":"{4}{G}{G}","cmc":6,"type":"Creature — Dragon","rarity":"Uncommon","text":"Flying\nWhen Destructor Dragon dies, destroy target noncreature permanent.","flavor":"After countless attacks on the Salt Road where it passes through the frozen tundra, the Abzan began to refer to the area as Atarka territory rather than Temur lands.","artist":"Peter Mohrbacher","number":"127","power":"4","toughness":"4","loyalty":null,"multiverseid":391818,"imageName":"destructor dragon","watermark":"Atarka","setCode":"FRF","imageCode":"frf","count":1},"FRF~128":{"name":"Feral Krushok","layout":"normal","manaCost":"{4}{G}","cmc":5,"type":"Creature — Beast","rarity":"Common","text":null,"flavor":"In a stunning act of diplomacy, Yasova Dragonclaw ceded a portion of Temur lands to the Sultai. Her clan protested until they saw she had given the Sultai the breeding grounds of the krushoks. They hadn't realized she had a sense of humor.","artist":"Kev Walker","number":"128","power":"5","toughness":"4","loyalty":null,"multiverseid":391831,"imageName":"feral krushok","watermark":null,"setCode":"FRF","imageCode":"frf","count":1},"M12~152":{"name":"Reverberate","layout":"normal","manaCost":"{R}{R}","cmc":2,"type":"Instant","rarity":"Rare","text":"Copy target instant or sorcery spell. You may choose new targets for the copy.","flavor":"For every action, there is a swifter and more violent reaction.","artist":"jD","number":"152","power":null,"toughness":null,"loyalty":null,"multiverseid":233722,"imageName":"reverberate","watermark":null,"setCode":"M12","imageCode":"m12","count":1}} \ No newline at end of file -- cgit v1.2.3