From 5878fd7a3d5cbe80b7ead67b1aa75fc55ff06697 Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Sat, 19 Mar 2016 19:26:05 +0100 Subject: Sorted out new cardbase format, need to do decks next --- src/eu/equalparts/cardbase/Cardbase.java | 7 ++-- src/eu/equalparts/cardbase/cards/Card.java | 1 - .../equalparts/cardbase/decks/StandaloneDeck.java | 7 ++-- test/eu/equalparts/cardbase/CardbaseSortTest.java | 48 ++++++++-------------- test/eu/equalparts/cardbase/CardbaseTest.java | 14 ++++++- 5 files changed, 39 insertions(+), 38 deletions(-) diff --git a/src/eu/equalparts/cardbase/Cardbase.java b/src/eu/equalparts/cardbase/Cardbase.java index 3704d3f..ee49db1 100644 --- a/src/eu/equalparts/cardbase/Cardbase.java +++ b/src/eu/equalparts/cardbase/Cardbase.java @@ -2,6 +2,7 @@ package eu.equalparts.cardbase; import java.io.File; import java.io.IOException; +import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -188,13 +189,13 @@ public class Cardbase { } /** - * @param field the name of the field by which to sort. + * @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 sort(String field) throws NoSuchFieldException { + public Collection sortByField(String fieldName) throws NoSuchFieldException { List sortedCards = new ArrayList(dataContainer.cards.values()); - sortedCards.sort(new CardComparator(Card.class.getDeclaredField(field))); + sortedCards.sort(new CardComparator(Card.class.getDeclaredField(fieldName))); return Collections.unmodifiableCollection(sortedCards); } diff --git a/src/eu/equalparts/cardbase/cards/Card.java b/src/eu/equalparts/cardbase/cards/Card.java index 901a566..9b0975b 100644 --- a/src/eu/equalparts/cardbase/cards/Card.java +++ b/src/eu/equalparts/cardbase/cards/Card.java @@ -29,7 +29,6 @@ public class Card { // Not part of upstream JSON public String setCode; public String imageCode; - //public Integer count; @Override public Card clone() { diff --git a/src/eu/equalparts/cardbase/decks/StandaloneDeck.java b/src/eu/equalparts/cardbase/decks/StandaloneDeck.java index 4c6d25c..f015a91 100644 --- a/src/eu/equalparts/cardbase/decks/StandaloneDeck.java +++ b/src/eu/equalparts/cardbase/decks/StandaloneDeck.java @@ -1,12 +1,13 @@ package eu.equalparts.cardbase.decks; -import java.util.HashSet; -import java.util.Set; +import java.util.HashMap; +import java.util.Map; import eu.equalparts.cardbase.cards.Card; public class StandaloneDeck extends Deck { - public Set cards = new HashSet(); + private Map cards = new HashMap(); + private Map counts = new HashMap(); } diff --git a/test/eu/equalparts/cardbase/CardbaseSortTest.java b/test/eu/equalparts/cardbase/CardbaseSortTest.java index 2dbb68e..b0dbd40 100644 --- a/test/eu/equalparts/cardbase/CardbaseSortTest.java +++ b/test/eu/equalparts/cardbase/CardbaseSortTest.java @@ -1,6 +1,5 @@ package eu.equalparts.cardbase; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import java.util.Collection; @@ -49,7 +48,7 @@ public class CardbaseSortTest { @Test public void sortByName() throws Exception { - Collection sortedCards = uut.sort("name"); + Collection sortedCards = uut.sortByField("name"); int i = 0; String[] names = { "Callow Jushi", @@ -69,7 +68,7 @@ public class CardbaseSortTest { @Test public void sortByLayout() throws Exception { - Collection sortedCards = uut.sort("layout"); + Collection sortedCards = uut.sortByField("layout"); int i = 0; String[] layouts = { "flip", @@ -89,7 +88,7 @@ public class CardbaseSortTest { @Test public void sortByManaCost() throws Exception { - Collection sortedCards = uut.sort("manaCost"); + Collection sortedCards = uut.sortByField("manaCost"); int i = 0; String[] costs = { "{1}{U}{U}", @@ -109,7 +108,7 @@ public class CardbaseSortTest { @Test public void sortByCMC() throws Exception { - Collection sortedCards = uut.sort("cmc"); + Collection sortedCards = uut.sortByField("cmc"); int i = 0; Integer[] cmcs = {2, 3, 4, 5, 6, 6, 6, 8}; for (Card card : sortedCards) { @@ -120,7 +119,7 @@ public class CardbaseSortTest { @Test public void sortByType() throws Exception { - Collection sortedCards = uut.sort("type"); + Collection sortedCards = uut.sortByField("type"); int i = 0; String[] types = { "Artifact Creature ā€” Construct", @@ -140,7 +139,7 @@ public class CardbaseSortTest { @Test public void sortByRarity() throws Exception { - Collection sortedCards = uut.sort("rarity"); + Collection sortedCards = uut.sortByField("rarity"); int i = 0; String[] rarities = { "Uncommon", @@ -160,7 +159,7 @@ public class CardbaseSortTest { @Test public void sortByText() throws Exception { - Collection sortedCards = uut.sort("text"); + Collection sortedCards = uut.sortByField("text"); int i = 0; String[] texts = { "+2: Sorin Markov deals 2 damage to target creature or player and you gain 2 life.\nāˆ’3: Target opponent's life total becomes 10.\nāˆ’7: You control target player during that player's next turn.", @@ -180,7 +179,7 @@ public class CardbaseSortTest { @Test public void sortByFlavor() throws Exception { - Collection sortedCards = uut.sort("flavor"); + Collection sortedCards = uut.sortByField("flavor"); int i = 0; String[] flavors = { "", @@ -201,7 +200,7 @@ public class CardbaseSortTest { @Test public void sortByArtist() throws Exception { - Collection sortedCards = uut.sort("artist"); + Collection sortedCards = uut.sortByField("artist"); int i = 0; String[] artists = { "Donato Giancola", @@ -221,7 +220,7 @@ public class CardbaseSortTest { @Test public void sortByNumber() throws Exception { - Collection sortedCards = uut.sort("number"); + Collection sortedCards = uut.sortByField("number"); int i = 0; String[] numbers = { "31a", @@ -241,7 +240,7 @@ public class CardbaseSortTest { @Test public void sortByPower() throws Exception { - Collection sortedCards = uut.sort("power"); + Collection sortedCards = uut.sortByField("power"); int i = 0; String[] powers = { "", @@ -262,7 +261,7 @@ public class CardbaseSortTest { @Test public void sortByToughness() throws Exception { - Collection sortedCards = uut.sort("power"); + Collection sortedCards = uut.sortByField("power"); int i = 0; String[] toughnesses = { "", @@ -283,7 +282,7 @@ public class CardbaseSortTest { @Test public void sortByLoyalty() throws Exception { - Collection sortedCards = uut.sort("loyalty"); + Collection sortedCards = uut.sortByField("loyalty"); int i = 0; Integer[] loyalties = {0, 0, 0, 0, 0, 0, 0, 4}; for (Card card : sortedCards) { @@ -295,7 +294,7 @@ public class CardbaseSortTest { @Test public void sortByMultiverseID() throws Exception { - Collection sortedCards = uut.sort("multiverseid"); + Collection sortedCards = uut.sortByField("multiverseid"); int i = 0; Integer[] ids = {74128, 74489, 193551, 238330, 366408, 383168, 383172, 391949 }; for (Card card : sortedCards) { @@ -307,7 +306,7 @@ public class CardbaseSortTest { @Test public void sortByImageName() throws Exception { - Collection sortedCards = uut.sort("imageName"); + Collection sortedCards = uut.sortByField("imageName"); int i = 0; String[] names = { "callow jushi", @@ -327,7 +326,7 @@ public class CardbaseSortTest { @Test public void sortByWatermark() throws Exception { - Collection sortedCards = uut.sort("watermark"); + Collection sortedCards = uut.sortByField("watermark"); int i = 0; String[] watermarks = { "", @@ -348,7 +347,7 @@ public class CardbaseSortTest { @Test public void sortBySetCode() throws Exception { - Collection sortedCards = uut.sort("setCode"); + Collection sortedCards = uut.sortByField("setCode"); int i = 0; String[] sets = { "BOK", @@ -367,7 +366,7 @@ public class CardbaseSortTest { } public void sortByImageCode() throws Exception { - Collection sortedCards = uut.sort("imageCode"); + Collection sortedCards = uut.sortByField("imageCode"); int i = 0; String[] codes = { "bok", @@ -384,15 +383,4 @@ public class CardbaseSortTest { i++; } } - - @Test - public void sortByCount() throws Exception { - Collection sortedCards = uut.sort("count"); - int i = 0; - int[] counts = {1, 1, 1, 1, 2, 3, 8, 15 }; - for (Card card : sortedCards) { - assertEquals(uut.getCount(card) + " should have been " + counts[i] + ", i = " + i, uut.getCount(card), counts[i]); - i++; - } - } } diff --git a/test/eu/equalparts/cardbase/CardbaseTest.java b/test/eu/equalparts/cardbase/CardbaseTest.java index 524c7b1..64009a3 100644 --- a/test/eu/equalparts/cardbase/CardbaseTest.java +++ b/test/eu/equalparts/cardbase/CardbaseTest.java @@ -117,13 +117,25 @@ public class CardbaseTest { public void loadFileHasWrongStructure() throws Exception { File wrongStructure = tempFolder.newFile("wrongStructure.json"); try (FileWriter writer = new FileWriter(wrongStructure)) { - writer.write("{\"field1\":\"content\",\"field2\":50,\"field3\":{\"subfield\":10},\"list\":[10,20,30]}"); + writer.write("{\"cards\":\"content\",\"collection\":50,\"decks\":{\"subfield\":10}}"); } exception.expect(JsonMappingException.class); uut = new Cardbase(wrongStructure); } + @Test + public void loadFileHasUnkownStructure() throws Exception { + File unkownStructure = tempFolder.newFile("wrongStructure.json"); + try (FileWriter writer = new FileWriter(unkownStructure)) { + writer.write("{\"field1\":\"content\",\"field2\":50,\"field3\":{\"subfield\":10},\"list\":[10,20,30]}"); + } + + uut = new Cardbase(unkownStructure); + + assertEquals("Cardbase should contain 0 cards.", 0, uut.getCards().size()); + } + @Test public void loadFileIsNotJson() throws Exception { File notJson = tempFolder.newFile("wrongStructure.json"); -- cgit v1.2.3