From f7fe057b745d3f0e19b5dd6bd1819b11fc89c551 Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Sun, 7 Aug 2016 22:18:55 +0200 Subject: Added filtering, still not working fully --- src/eu/equalparts/cardbase/Cardbase.java | 2 +- src/eu/equalparts/cardbase/cards/OldCard.java | 95 ++++++++ .../cardstorage/ReferenceCardContainer.java | 137 ----------- .../cardstorage/StandaloneCardContainer.java | 66 ------ .../cardbase/comparator/SpecialFields.java | 1 - .../containers/ReferenceCardContainer.java | 137 +++++++++++ .../containers/StandaloneCardContainer.java | 66 ++++++ .../equalparts/cardbase/decks/ReferenceDeck.java | 2 +- .../cardbase/filtering/CardFiltering.java | 57 +++++ .../equalparts/cardbase/sorting/CardSorting.java | 4 +- .../cardstorage/ReferenceCardContainerTest.java | 168 -------------- .../cardstorage/StandaloneCardContainerTest.java | 171 -------------- .../containers/ReferenceCardContainerTest.java | 167 ++++++++++++++ .../containers/StandaloneCardContainerTest.java | 170 ++++++++++++++ .../cardbase/decks/ReferenceDeckTest.java | 2 +- .../cardbase/filtering/CardFilteringTest.java | 252 +++++++++++++++++++++ .../cardbase/sorting/CardSortingTest.java | 88 ++++--- todo | 11 +- 18 files changed, 1009 insertions(+), 587 deletions(-) create mode 100644 src/eu/equalparts/cardbase/cards/OldCard.java delete mode 100644 src/eu/equalparts/cardbase/cardstorage/ReferenceCardContainer.java delete mode 100644 src/eu/equalparts/cardbase/cardstorage/StandaloneCardContainer.java create mode 100644 src/eu/equalparts/cardbase/containers/ReferenceCardContainer.java create mode 100644 src/eu/equalparts/cardbase/containers/StandaloneCardContainer.java create mode 100644 src/eu/equalparts/cardbase/filtering/CardFiltering.java delete mode 100644 test/eu/equalparts/cardbase/cardstorage/ReferenceCardContainerTest.java delete mode 100644 test/eu/equalparts/cardbase/cardstorage/StandaloneCardContainerTest.java create mode 100644 test/eu/equalparts/cardbase/containers/ReferenceCardContainerTest.java create mode 100644 test/eu/equalparts/cardbase/containers/StandaloneCardContainerTest.java create mode 100644 test/eu/equalparts/cardbase/filtering/CardFilteringTest.java diff --git a/src/eu/equalparts/cardbase/Cardbase.java b/src/eu/equalparts/cardbase/Cardbase.java index 9c630bd..90e1ffe 100644 --- a/src/eu/equalparts/cardbase/Cardbase.java +++ b/src/eu/equalparts/cardbase/Cardbase.java @@ -7,7 +7,7 @@ import com.fasterxml.jackson.core.JsonGenerationException; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException; -import eu.equalparts.cardbase.cardstorage.StandaloneCardContainer; +import eu.equalparts.cardbase.containers.StandaloneCardContainer; import eu.equalparts.cardbase.utils.JSON; /** diff --git a/src/eu/equalparts/cardbase/cards/OldCard.java b/src/eu/equalparts/cardbase/cards/OldCard.java new file mode 100644 index 0000000..daa831e --- /dev/null +++ b/src/eu/equalparts/cardbase/cards/OldCard.java @@ -0,0 +1,95 @@ +package eu.equalparts.cardbase.cards; + +import eu.equalparts.cardbase.comparator.SpecialFields.DirtyNumber; +import eu.equalparts.cardbase.comparator.SpecialFields.Rarity; + +public class OldCard { + + public String name; + public String layout; + public String manaCost; + public Integer cmc; + public String type; + @Rarity + public String rarity; + public String text; + public String flavor; + public String artist; + @DirtyNumber + public String number; + @DirtyNumber + public String power; + @DirtyNumber + public String toughness; + public Integer loyalty; + public Integer multiverseid; + public String imageName; + public String watermark; + + // Not part of upstream JSON + public String setCode; + public String imageCode; + public Integer count; + + @Override + public OldCard clone() { + OldCard clone = new OldCard(); + + clone.name = this.name; + clone.layout = this.layout; + clone.manaCost = this.manaCost; + clone.cmc = this.cmc; + clone.type = this.type; + clone.rarity = this.rarity; + clone.text = this.text; + clone.flavor = this.flavor; + clone.artist = this.artist; + clone.number = this.number; + clone.power = this.power; + clone.toughness = this.toughness; + clone.loyalty = this.loyalty; + clone.multiverseid = this.multiverseid; + clone.imageName = this.imageName; + clone.watermark = this.watermark; + clone.setCode = this.setCode; + clone.imageCode = this.imageCode; + clone.count = this.count; + + return clone; + } + + public static int makeHash(String setCode, String number) { + final int prime = 31; + int result = 1; + result = prime * result + ((number == null) ? 0 : number.hashCode()); + result = prime * result + ((setCode == null) ? 0 : setCode.hashCode()); + return result; + } + + @Override + public int hashCode() { + return makeHash(setCode, number); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + OldCard other = (OldCard) obj; + if (number == null) { + if (other.number != null) + return false; + } else if (!number.equals(other.number)) + return false; + if (setCode == null) { + if (other.setCode != null) + return false; + } else if (!setCode.equals(other.setCode)) + return false; + return true; + } +} \ No newline at end of file diff --git a/src/eu/equalparts/cardbase/cardstorage/ReferenceCardContainer.java b/src/eu/equalparts/cardbase/cardstorage/ReferenceCardContainer.java deleted file mode 100644 index f43b76b..0000000 --- a/src/eu/equalparts/cardbase/cardstorage/ReferenceCardContainer.java +++ /dev/null @@ -1,137 +0,0 @@ -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; - -/** - * A class which contains card quantities with absolutely no other - * information about the cards themselves. - * - * @author Eduardo Pedroni - * - */ -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 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){ - 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) { - 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. - */ - 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 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 deleted file mode 100644 index 53d248d..0000000 --- a/src/eu/equalparts/cardbase/cardstorage/StandaloneCardContainer.java +++ /dev/null @@ -1,66 +0,0 @@ -package eu.equalparts.cardbase.cardstorage; - -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - -import com.fasterxml.jackson.annotation.JsonProperty; - -import eu.equalparts.cardbase.cards.Card; - -/** - * TODO fix comments - * Holds actual card data in addition to the card counts in {@code ReferenceCardContainer}. - * - * @author Eduardo Pedroni - * - */ -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 cardData = new HashMap<>(); - - /** - * Returns a card from the cardbase by set code and number. - * If no such card is in the cardbase, returns null. - * - * @param setCode the set to which the requested card belongs. - * @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) { - 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. - * The returned {@code List} is a read-only; trying to modify its structure will - * result in an {@code UnsupportedOperationException}. - * - * @return an unmodifiable list of all the cards in the cardbase. - */ - public List getCards() { - return new LinkedList(cardData.values()); - } - - @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/src/eu/equalparts/cardbase/comparator/SpecialFields.java b/src/eu/equalparts/cardbase/comparator/SpecialFields.java index 692f774..433c8ab 100644 --- a/src/eu/equalparts/cardbase/comparator/SpecialFields.java +++ b/src/eu/equalparts/cardbase/comparator/SpecialFields.java @@ -13,5 +13,4 @@ public final class SpecialFields { @Retention(RetentionPolicy.RUNTIME) public @interface Rarity {} - } diff --git a/src/eu/equalparts/cardbase/containers/ReferenceCardContainer.java b/src/eu/equalparts/cardbase/containers/ReferenceCardContainer.java new file mode 100644 index 0000000..41c4224 --- /dev/null +++ b/src/eu/equalparts/cardbase/containers/ReferenceCardContainer.java @@ -0,0 +1,137 @@ +package eu.equalparts.cardbase.containers; + +import java.util.HashMap; +import java.util.Map; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import eu.equalparts.cardbase.cards.Card; + +/** + * A class which contains card quantities with absolutely no other + * information about the cards themselves. + * + * @author Eduardo Pedroni + * + */ +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 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){ + 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) { + 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. + */ + 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 void setMountains(int mountains) { + this.mountains = mountains; + } +} diff --git a/src/eu/equalparts/cardbase/containers/StandaloneCardContainer.java b/src/eu/equalparts/cardbase/containers/StandaloneCardContainer.java new file mode 100644 index 0000000..e5a514e --- /dev/null +++ b/src/eu/equalparts/cardbase/containers/StandaloneCardContainer.java @@ -0,0 +1,66 @@ +package eu.equalparts.cardbase.containers; + +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import eu.equalparts.cardbase.cards.Card; + +/** + * TODO fix comments + * Holds actual card data in addition to the card counts in {@code ReferenceCardContainer}. + * + * @author Eduardo Pedroni + * + */ +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 cardData = new HashMap<>(); + + /** + * Returns a card from the cardbase by set code and number. + * If no such card is in the cardbase, returns null. + * + * @param setCode the set to which the requested card belongs. + * @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) { + 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. + * The returned {@code List} is a copy of the list kept internally by {@code StandaloneCardContainer}; + * modifying its structure is possible but will not affect the container instance from which it came. + * + * @return an unmodifiable list of all the cards in the cardbase. + */ + public List getCards() { + return new LinkedList(cardData.values()); + } + + @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/src/eu/equalparts/cardbase/decks/ReferenceDeck.java b/src/eu/equalparts/cardbase/decks/ReferenceDeck.java index 8c8e227..9875796 100644 --- a/src/eu/equalparts/cardbase/decks/ReferenceDeck.java +++ b/src/eu/equalparts/cardbase/decks/ReferenceDeck.java @@ -1,6 +1,6 @@ package eu.equalparts.cardbase.decks; -import eu.equalparts.cardbase.cardstorage.ReferenceCardContainer; +import eu.equalparts.cardbase.containers.ReferenceCardContainer; public class ReferenceDeck extends ReferenceCardContainer { private String name = ""; diff --git a/src/eu/equalparts/cardbase/filtering/CardFiltering.java b/src/eu/equalparts/cardbase/filtering/CardFiltering.java new file mode 100644 index 0000000..23f439d --- /dev/null +++ b/src/eu/equalparts/cardbase/filtering/CardFiltering.java @@ -0,0 +1,57 @@ +package eu.equalparts.cardbase.filtering; + +import java.lang.reflect.Field; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; + +import eu.equalparts.cardbase.cards.Card; + +/** + * Contains and equals are not case sensitive. Regex is. + * + * @author Eduardo Pedroni + * + */ +public class CardFiltering { + + enum FilterType { + EQUALS { + @Override + public boolean doFilter(String value, String filter) { + return value.equalsIgnoreCase(filter); + } + }, CONTAINS { + @Override + public boolean doFilter(String value, String filter) { + return value.toLowerCase().contains(filter.toLowerCase()); + } + }, REGEX { + @Override + public boolean doFilter(String value, String filter) { + return value.matches(filter); + } + }; + + public abstract boolean doFilter(String value, String filter); + } + + public static Collection filterByField(List cards, String fieldName, FilterType filterType, String filterValue) throws NoSuchFieldException { + Field fieldToFilter = Card.class.getDeclaredField(fieldName); + try { + for (Iterator iterator = cards.iterator(); iterator.hasNext();) { + if (!filterType.doFilter((String) fieldToFilter.get(iterator.next()), filterValue)) { + iterator.remove(); + } + } + } catch (IllegalArgumentException e) { + System.out.println("Error: class Card does not define field \"" + fieldToFilter.getName() + "\"."); + e.printStackTrace(); + } catch (IllegalAccessException e) { + System.out.println("Error: field " + fieldToFilter.getName() + " in Card is not visible."); + e.printStackTrace(); + } + return cards; + } + +} diff --git a/src/eu/equalparts/cardbase/sorting/CardSorting.java b/src/eu/equalparts/cardbase/sorting/CardSorting.java index 04413b9..e7f7c96 100644 --- a/src/eu/equalparts/cardbase/sorting/CardSorting.java +++ b/src/eu/equalparts/cardbase/sorting/CardSorting.java @@ -10,11 +10,9 @@ public abstract class CardSorting { /** * @param cards * @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 static List sortByField(List cards, String fieldName) throws NoSuchFieldException { + public static void sortByField(List cards, String fieldName) throws NoSuchFieldException { cards.sort(new CardComparator(Card.class.getDeclaredField(fieldName))); - return cards; } } diff --git a/test/eu/equalparts/cardbase/cardstorage/ReferenceCardContainerTest.java b/test/eu/equalparts/cardbase/cardstorage/ReferenceCardContainerTest.java deleted file mode 100644 index bf49e2f..0000000 --- a/test/eu/equalparts/cardbase/cardstorage/ReferenceCardContainerTest.java +++ /dev/null @@ -1,168 +0,0 @@ -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 deleted file mode 100644 index 6071e09..0000000 --- a/test/eu/equalparts/cardbase/cardstorage/StandaloneCardContainerTest.java +++ /dev/null @@ -1,171 +0,0 @@ -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)); - } -} diff --git a/test/eu/equalparts/cardbase/containers/ReferenceCardContainerTest.java b/test/eu/equalparts/cardbase/containers/ReferenceCardContainerTest.java new file mode 100644 index 0000000..849d84c --- /dev/null +++ b/test/eu/equalparts/cardbase/containers/ReferenceCardContainerTest.java @@ -0,0 +1,167 @@ +package eu.equalparts.cardbase.containers; + +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.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(ReferenceCardContainerTest.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/containers/StandaloneCardContainerTest.java b/test/eu/equalparts/cardbase/containers/StandaloneCardContainerTest.java new file mode 100644 index 0000000..6e9b31a --- /dev/null +++ b/test/eu/equalparts/cardbase/containers/StandaloneCardContainerTest.java @@ -0,0 +1,170 @@ +package eu.equalparts.cardbase.containers; + +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.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(StandaloneCardContainerTest.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)); + } +} diff --git a/test/eu/equalparts/cardbase/decks/ReferenceDeckTest.java b/test/eu/equalparts/cardbase/decks/ReferenceDeckTest.java index 928c167..2a6f482 100644 --- a/test/eu/equalparts/cardbase/decks/ReferenceDeckTest.java +++ b/test/eu/equalparts/cardbase/decks/ReferenceDeckTest.java @@ -6,7 +6,7 @@ import static org.junit.Assert.assertTrue; import org.junit.Before; import org.junit.Test; -import eu.equalparts.cardbase.cardstorage.ReferenceCardContainer; +import eu.equalparts.cardbase.containers.ReferenceCardContainer; public class ReferenceDeckTest { private ReferenceDeck uut; diff --git a/test/eu/equalparts/cardbase/filtering/CardFilteringTest.java b/test/eu/equalparts/cardbase/filtering/CardFilteringTest.java new file mode 100644 index 0000000..4f5579d --- /dev/null +++ b/test/eu/equalparts/cardbase/filtering/CardFilteringTest.java @@ -0,0 +1,252 @@ +package eu.equalparts.cardbase.filtering; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.LinkedList; +import java.util.List; + +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.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; + +import eu.equalparts.cardbase.cards.Card; +import eu.equalparts.cardbase.filtering.CardFiltering.FilterType; + +public class CardFilteringTest { + private static List allTestCards, testCards; + + @Rule + public final ExpectedException exception = ExpectedException.none(); + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + ObjectMapper mapper = new ObjectMapper(); + allTestCards = mapper.readValue(CardFilteringTest.class.getResourceAsStream("/testcards.json"), new TypeReference>() {}); + } + + @Before + public void setUp() { + testCards = new LinkedList<>(allTestCards); + } + + @Test + public void filterByNameEquals() throws Exception { + CardFiltering.filterByField(testCards, "name", FilterType.EQUALS, "Callow jushi"); + + assertEquals("Wrong list size.", 1, testCards.size()); + assertEquals("Callow Jushi", testCards.get(0).name); + } + + @Test + public void filterByNameContains() throws Exception { + CardFiltering.filterByField(testCards, "name", FilterType.CONTAINS, "sh"); + + assertEquals("Wrong list size.", 3, testCards.size()); + int i = 0; + String[] names = { + "Callow Jushi", + "Shivan Dragon", + "Disrupting Shoal", + }; + for (Card card : testCards) { + assertTrue(card.name + " should have been " + names[i] + ", i = " + i, card.name.equals(names[i])); + i++; + } + } + + @Test + public void filterByNameRegex() throws Exception { + CardFiltering.filterByField(testCards, "name", FilterType.REGEX, ".+?n"); + + assertEquals("Wrong list size.", 2, testCards.size()); + int i = 0; + String[] names = { + "Coerced Confession", + "Shivan Dragon", + }; + for (Card card : testCards) { + assertTrue(card.name + " should have been " + names[i] + ", i = " + i, card.name.equals(names[i])); + i++; + } + } + + @Test + public void filterByLayoutEquals() throws Exception { + CardFiltering.filterByField(testCards, "layout", FilterType.EQUALS, "flip"); + + assertEquals("Wrong list size.", 1, testCards.size()); + assertEquals("Callow Jushi", testCards.get(0).name); + } + + @Test + public void filterByLayoutContains() throws Exception { + CardFiltering.filterByField(testCards, "layout", FilterType.CONTAINS, "l"); + + assertEquals("Wrong list size.", 8, testCards.size()); + int i = 0; + String[] names = { + "Callow Jushi", + "Coerced Confession", + "Khalni Hydra", + "Nightmare", + "Shivan Dragon", + "Disrupting Shoal", + "Sorin Markov", + "Ugin's Construct", + }; + for (Card card : testCards) { + assertTrue(card.name + " should have been " + names[i] + ", i = " + i, card.name.equals(names[i])); + i++; + } + } + + @Test + public void filterByLayoutRegex() throws Exception { + CardFiltering.filterByField(testCards, "layout", FilterType.REGEX, "fl[a-z]p"); + + assertEquals("Wrong list size.", 1, testCards.size()); + assertEquals("Callow Jushi", testCards.get(0).name); + } + + @Test + public void filterByManaCostEquals() throws Exception { + CardFiltering.filterByField(testCards, "manaCost", FilterType.EQUALS, "{X}{U}{U}"); + + assertEquals("Wrong list size.", 1, testCards.size()); + assertEquals("Disrupting Shoal", testCards.get(0).name); + } + + @Test + public void filterByManaCostContains() throws Exception { + CardFiltering.filterByField(testCards, "manaCost", FilterType.CONTAINS, "B"); + + assertEquals("Wrong list size.", 3, testCards.size()); + int i = 0; + String[] names = { + "Coerced Confession", + "Nightmare", + "Sorin Markov", + }; + for (Card card : testCards) { + assertTrue(card.name + " should have been " + names[i] + ", i = " + i, card.name.equals(names[i])); + i++; + } + } + + @Test + public void filterByManaCostRegex() throws Exception { + CardFiltering.filterByField(testCards, "manaCost", FilterType.REGEX, "(\\{G\\}){8}"); + + assertEquals("Wrong list size.", 1, testCards.size()); + assertEquals("Khalni Hydra", testCards.get(0).name); + } + + @Test + public void filterByCMCEquals() throws Exception { + CardFiltering.filterByField(testCards, "cmc", FilterType.EQUALS, "5"); + + assertEquals("Wrong list size.", 1, testCards.size()); + assertEquals("Coerced Confession", testCards.get(0).name); + } + + @Test + public void filterByCMCContains() throws Exception { + CardFiltering.filterByField(testCards, "cmc", FilterType.CONTAINS, "B"); + + assertEquals("Wrong list size.", 3, testCards.size()); + int i = 0; + String[] names = { + "Coerced Confession", + "Nightmare", + "Sorin Markov", + }; + for (Card card : testCards) { + assertTrue(card.name + " should have been " + names[i] + ", i = " + i, card.name.equals(names[i])); + i++; + } + } + + @Test + public void filterByCMCRegex() throws Exception { + CardFiltering.filterByField(testCards, "cmc", FilterType.REGEX, "(\\{G\\}){8}"); + + assertEquals("Wrong list size.", 1, testCards.size()); + assertEquals("Khalni Hydra", testCards.get(0).name); + } + + @Test + public void filterByType() throws Exception { + + } + + @Test + public void filterByRarity() throws Exception { + + } + + @Test + public void filterByText() throws Exception { + + } + + @Test + public void filterByFlavor() throws Exception { + + } + + @Test + public void filterByArtist() throws Exception { + + } + + @Test + public void filterByNumber() throws Exception { + + } + + @Test + public void filterByPower() throws Exception { + + } + + @Test + public void filterByToughness() throws Exception { + + } + + @Test + public void filterByLoyalty() throws Exception { + + } + + @Test + public void filterByMultiverseID() throws Exception { + + } + + @Test + public void filterByImageName() throws Exception { + + } + + @Test + public void filterByWatermark() throws Exception { + + } + + @Test + public void filterBySetCode() throws Exception { + + } + + @Test + public void filterByImageCode() throws Exception { + + } +} diff --git a/test/eu/equalparts/cardbase/sorting/CardSortingTest.java b/test/eu/equalparts/cardbase/sorting/CardSortingTest.java index 64a33b5..4ec0f5e 100644 --- a/test/eu/equalparts/cardbase/sorting/CardSortingTest.java +++ b/test/eu/equalparts/cardbase/sorting/CardSortingTest.java @@ -2,11 +2,12 @@ package eu.equalparts.cardbase.sorting; import static org.junit.Assert.assertTrue; -import java.util.Collection; import java.util.List; import org.junit.BeforeClass; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; @@ -21,6 +22,9 @@ import eu.equalparts.cardbase.cards.Card; */ public class CardSortingTest { private static List testCards; + + @Rule + public final ExpectedException exception = ExpectedException.none(); @BeforeClass public static void setUpBeforeClass() throws Exception { @@ -30,7 +34,7 @@ public class CardSortingTest { @Test public void sortByName() throws Exception { - Collection sortedCards = CardSorting.sortByField(testCards, "name"); + CardSorting.sortByField(testCards, "name"); int i = 0; String[] names = { "Callow Jushi", @@ -42,7 +46,7 @@ public class CardSortingTest { "Sorin Markov", "Ugin's Construct", }; - for (Card card : sortedCards) { + for (Card card : testCards) { assertTrue(card.name + " should have been " + names[i] + ", i = " + i, card.name.equals(names[i])); i++; } @@ -50,7 +54,7 @@ public class CardSortingTest { @Test public void sortByLayout() throws Exception { - Collection sortedCards = CardSorting.sortByField(testCards, "layout"); + CardSorting.sortByField(testCards, "layout"); int i = 0; String[] layouts = { "flip", @@ -62,7 +66,7 @@ public class CardSortingTest { "normal", "normal", }; - for (Card card : sortedCards) { + for (Card card : testCards) { assertTrue(card.layout + " should have been " + layouts[i] + ", i = " + i, card.layout.equals(layouts[i])); i++; } @@ -70,7 +74,7 @@ public class CardSortingTest { @Test public void sortByManaCost() throws Exception { - Collection sortedCards = CardSorting.sortByField(testCards, "manaCost"); + CardSorting.sortByField(testCards, "manaCost"); int i = 0; String[] costs = { "{1}{U}{U}", @@ -82,7 +86,7 @@ public class CardSortingTest { "{G}{G}{G}{G}{G}{G}{G}{G}", "{X}{U}{U}", }; - for (Card card : sortedCards) { + for (Card card : testCards) { assertTrue(card.manaCost + " should have been " + costs[i] + ", i = " + i, card.manaCost.equals(costs[i])); i++; } @@ -90,10 +94,10 @@ public class CardSortingTest { @Test public void sortByCMC() throws Exception { - Collection sortedCards = CardSorting.sortByField(testCards, "cmc"); + CardSorting.sortByField(testCards, "cmc"); int i = 0; Integer[] cmcs = {2, 3, 4, 5, 6, 6, 6, 8}; - for (Card card : sortedCards) { + for (Card card : testCards) { assertTrue(card.cmc + " should have been " + cmcs[i] + ", i = " + i, card.cmc.equals(cmcs[i])); i++; } @@ -101,7 +105,7 @@ public class CardSortingTest { @Test public void sortByType() throws Exception { - Collection sortedCards = CardSorting.sortByField(testCards, "type"); + CardSorting.sortByField(testCards, "type"); int i = 0; String[] types = { "Artifact Creature ā€” Construct", @@ -113,7 +117,7 @@ public class CardSortingTest { "Planeswalker ā€” Sorin", "Sorcery", }; - for (Card card : sortedCards) { + for (Card card : testCards) { assertTrue(card.type + " should have been " + types[i] + ", i = " + i, card.type.equals(types[i])); i++; } @@ -121,7 +125,7 @@ public class CardSortingTest { @Test public void sortByRarity() throws Exception { - Collection sortedCards = CardSorting.sortByField(testCards, "rarity"); + CardSorting.sortByField(testCards, "rarity"); int i = 0; String[] rarities = { "Uncommon", @@ -133,7 +137,7 @@ public class CardSortingTest { "Mythic Rare", "Mythic Rare", }; - for (Card card : sortedCards) { + for (Card card : testCards) { assertTrue(card.rarity + " should have been " + rarities[i] + ", i = " + i, card.rarity.equals(rarities[i])); i++; } @@ -141,7 +145,7 @@ public class CardSortingTest { @Test public void sortByText() throws Exception { - Collection sortedCards = CardSorting.sortByField(testCards, "text"); + CardSorting.sortByField(testCards, "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.", @@ -153,7 +157,7 @@ public class CardSortingTest { "Whenever you cast a Spirit or Arcane spell, you may put a ki counter on Callow Jushi.\nAt the beginning of the end step, if there are two or more ki counters on Callow Jushi, you may flip it.", "You may exile a blue card with converted mana cost X from your hand rather than pay Disrupting Shoal's mana cost.\nCounter target spell if its converted mana cost is X.", }; - for (Card card : sortedCards) { + for (Card card : testCards) { assertTrue(card.text + " should have been " + texts[i] + ", i = " + i, card.text.equals(texts[i])); i++; } @@ -161,7 +165,7 @@ public class CardSortingTest { @Test public void sortByFlavor() throws Exception { - Collection sortedCards = CardSorting.sortByField(testCards, "flavor"); + CardSorting.sortByField(testCards, "flavor"); int i = 0; String[] flavors = { "", @@ -173,7 +177,7 @@ public class CardSortingTest { "The undisputed master of the mountains of Shiv.", "While trapping the Eldrazi on Zendikar, Ugin learned little from Sorin, but he gleaned the rudiments of lithomancy from Nahiri.", }; - for (Card card : sortedCards) { + for (Card card : testCards) { String flavor = card.flavor != null ? card.flavor : ""; assertTrue(flavor + " should have been " + flavors[i] + ", i = " + i, flavor.equals(flavors[i])); i++; @@ -182,7 +186,7 @@ public class CardSortingTest { @Test public void sortByArtist() throws Exception { - Collection sortedCards = CardSorting.sortByField(testCards, "artist"); + CardSorting.sortByField(testCards, "artist"); int i = 0; String[] artists = { "Donato Giancola", @@ -194,7 +198,7 @@ public class CardSortingTest { "Tsutomu Kawade", "Vance Kovacs", }; - for (Card card : sortedCards) { + for (Card card : testCards) { assertTrue(card.artist + " should have been " + artists[i] + ", i = " + i, card.artist.equals(artists[i])); i++; } @@ -202,7 +206,7 @@ public class CardSortingTest { @Test public void sortByNumber() throws Exception { - Collection sortedCards = CardSorting.sortByField(testCards, "number"); + CardSorting.sortByField(testCards, "number"); int i = 0; String[] numbers = { "31a", @@ -214,7 +218,7 @@ public class CardSortingTest { "276", "281", }; - for (Card card : sortedCards) { + for (Card card : testCards) { assertTrue(card.number + " should have been " + numbers[i] + ", i = " + i, card.number.equals(numbers[i])); i++; } @@ -222,7 +226,7 @@ public class CardSortingTest { @Test public void sortByPower() throws Exception { - Collection sortedCards = CardSorting.sortByField(testCards, "power"); + CardSorting.sortByField(testCards, "power"); int i = 0; String[] powers = { "", @@ -234,7 +238,7 @@ public class CardSortingTest { "5", "8", }; - for (Card card : sortedCards) { + for (Card card : testCards) { String power = card.power != null ? card.power : ""; assertTrue(power + " should have been " + powers[i] + ", i = " + i, power.equals(powers[i])); i++; @@ -243,7 +247,7 @@ public class CardSortingTest { @Test public void sortByToughness() throws Exception { - Collection sortedCards = CardSorting.sortByField(testCards, "power"); + CardSorting.sortByField(testCards, "power"); int i = 0; String[] toughnesses = { "", @@ -255,7 +259,7 @@ public class CardSortingTest { "5", "8", }; - for (Card card : sortedCards) { + for (Card card : testCards) { String toughness = card.toughness != null ? card.toughness : ""; assertTrue(toughness + " should have been " + toughnesses[i] + ", i = " + i, toughness.equals(toughnesses[i])); i++; @@ -264,10 +268,10 @@ public class CardSortingTest { @Test public void sortByLoyalty() throws Exception { - Collection sortedCards = CardSorting.sortByField(testCards, "loyalty"); + CardSorting.sortByField(testCards, "loyalty"); int i = 0; Integer[] loyalties = {0, 0, 0, 0, 0, 0, 0, 4}; - for (Card card : sortedCards) { + for (Card card : testCards) { Integer loyalty = card.loyalty != null ? card.loyalty : 0; assertTrue(loyalty + " should have been " + loyalties[i] + ", i = " + i, loyalty.equals(loyalties[i])); i++; @@ -276,10 +280,10 @@ public class CardSortingTest { @Test public void sortByMultiverseID() throws Exception { - Collection sortedCards = CardSorting.sortByField(testCards, "multiverseid"); + CardSorting.sortByField(testCards, "multiverseid"); int i = 0; Integer[] ids = {74128, 74489, 193551, 238330, 366408, 383168, 383172, 391949 }; - for (Card card : sortedCards) { + for (Card card : testCards) { Integer id = card.multiverseid != null ? card.multiverseid : 0; assertTrue(id + " should have been " + ids[i] + ", i = " + i, id.equals(ids[i])); i++; @@ -288,7 +292,7 @@ public class CardSortingTest { @Test public void sortByImageName() throws Exception { - Collection sortedCards = CardSorting.sortByField(testCards, "imageName"); + CardSorting.sortByField(testCards, "imageName"); int i = 0; String[] names = { "callow jushi", @@ -300,7 +304,7 @@ public class CardSortingTest { "sorin markov", "ugin's construct", }; - for (Card card : sortedCards) { + for (Card card : testCards) { assertTrue(card.imageName + " should have been " + names[i] + ", i = " + i, card.imageName.equals(names[i])); i++; } @@ -308,7 +312,7 @@ public class CardSortingTest { @Test public void sortByWatermark() throws Exception { - Collection sortedCards = CardSorting.sortByField(testCards, "watermark"); + CardSorting.sortByField(testCards, "watermark"); int i = 0; String[] watermarks = { "", @@ -320,7 +324,7 @@ public class CardSortingTest { "", "Dimir", }; - for (Card card : sortedCards) { + for (Card card : testCards) { String watermark = card.watermark != null ? card.watermark : ""; assertTrue(watermark + " should have been " + watermarks[i] + ", i = " + i, watermark.equals(watermarks[i])); i++; @@ -329,7 +333,7 @@ public class CardSortingTest { @Test public void sortBySetCode() throws Exception { - Collection sortedCards = CardSorting.sortByField(testCards, "setCode"); + CardSorting.sortByField(testCards, "setCode"); int i = 0; String[] sets = { "BOK", @@ -341,14 +345,15 @@ public class CardSortingTest { "M15", "ROE", }; - for (Card card : sortedCards) { + for (Card card : testCards) { assertTrue(card.setCode + " should have been " + sets[i] + ", i = " + i, card.setCode.equals(sets[i])); i++; } } + @Test public void sortByImageCode() throws Exception { - Collection sortedCards = CardSorting.sortByField(testCards, "imageCode"); + CardSorting.sortByField(testCards, "imageCode"); int i = 0; String[] codes = { "bok", @@ -360,9 +365,18 @@ public class CardSortingTest { "m15", "roe", }; - for (Card card : sortedCards) { + for (Card card : testCards) { assertTrue(card.imageCode + " should have been " + codes[i] + ", i = " + i, card.imageCode.equals(codes[i])); i++; } } + + /* + * Edge cases + */ + @Test + public void sortFieldDoesNotExist() throws Exception { + exception.expect(NoSuchFieldException.class); + CardSorting.sortByField(testCards, "not a field name"); + } } diff --git a/todo b/todo index 02cbef1..773f365 100644 --- a/todo +++ b/todo @@ -1 +1,10 @@ -sorting and filtering should work on StandaloneCardContainer, not Cardbase. \ No newline at end of file +"Callow Jushi", +"Coerced Confession", +"Khalni Hydra", +"Nightmare", +"Shivan Dragon", +"Disrupting Shoal", +"Sorin Markov", +"Ugin's Construct", + +cat test/testcards.json | sed -e "s/,/,\n/g" | awk '/cmc/ { r = gensub(/.+?\":\"(.+?)\".+/, "\"\\1\",", "g"); print r }' \ No newline at end of file -- cgit v1.2.3