diff --git a/Mage.Client/src/test/java/mage/cards/decks/importer/CodDeckImportTest.java b/Mage.Client/src/test/java/mage/cards/decks/importer/CodDeckImportTest.java deleted file mode 100644 index 6e2c999317..0000000000 --- a/Mage.Client/src/test/java/mage/cards/decks/importer/CodDeckImportTest.java +++ /dev/null @@ -1,44 +0,0 @@ -package mage.cards.decks.importer; - -import static org.junit.Assert.assertEquals; - -import org.junit.Test; - -import mage.cards.decks.DeckCardInfo; -import mage.cards.decks.DeckCardLists; - -public class CodDeckImportTest { - - @Test - public void testImportCod() { - StringBuilder errors = new StringBuilder(); - DeckCardLists deck = new CodDeckImporter().importDeck( - "src/test/java/mage/cards/decks/importer/testdeck.cod", errors); - assertEquals("Deck Name", deck.getName()); - assertEquals(113, deck.getCards().size()); - assertEquals(3, deck.getSideboard().size()); - int index = 0; - for (int i = 0; i < 12; i++) { - assertCardSame("Forest", deck.getCards().get(index++)); - } - for (int i = 0; i < 100; i++) { - assertCardSame("Razorverge Thicket", deck.getCards().get(index++)); - } - assertCardSame("Avacyn's Pilgrim", deck.getCards().get(index++)); - assertEquals(index, deck.getCards().size()); - - index = 0; - for (int i = 0; i < 3; i++) { - assertCardSame("War Priest of Thune", deck.getSideboard().get(index++)); - } - assertEquals(index, deck.getSideboard().size()); - - assertEquals("Could not find card: '@#$NOT A REAL CARD NAME@#$'\n", errors.toString()); - } - - private static void assertCardSame(String name, DeckCardInfo card) { - assertEquals(name, card.getCardName()); - assertEquals(1, card.getQuantity()); - } - -} diff --git a/Mage.Client/src/test/java/mage/cards/decks/importer/DecDeckImportTest.java b/Mage.Client/src/test/java/mage/cards/decks/importer/DecDeckImportTest.java deleted file mode 100644 index 0cefeecc72..0000000000 --- a/Mage.Client/src/test/java/mage/cards/decks/importer/DecDeckImportTest.java +++ /dev/null @@ -1,20 +0,0 @@ -package mage.cards.decks.importer; - -import static org.junit.Assert.assertEquals; - -import org.junit.Test; - -import mage.cards.decks.DeckCardLists; - -public class DecDeckImportTest { - - @Test - public void testImport() { - StringBuilder errors = new StringBuilder(); - DeckCardLists deck = new DecDeckImporter().importDeck( - "src/test/java/mage/cards/decks/importer/testdeck.dec", errors); - assertEquals(60, deck.getCards().size()); - assertEquals(15, deck.getSideboard().size()); - } - -} diff --git a/Mage/src/main/java/mage/cards/decks/importer/CardLookup.java b/Mage/src/main/java/mage/cards/decks/importer/CardLookup.java new file mode 100644 index 0000000000..a151caf01a --- /dev/null +++ b/Mage/src/main/java/mage/cards/decks/importer/CardLookup.java @@ -0,0 +1,16 @@ +package mage.cards.decks.importer; + +import java.util.Optional; + +import mage.cards.repository.CardInfo; +import mage.cards.repository.CardRepository; + +public class CardLookup { + + public static final CardLookup instance = new CardLookup(); + + public Optional lookupCardInfo(String name) { + return Optional.ofNullable(CardRepository.instance.findPreferedCoreExpansionCard(name, true)); + } + +} diff --git a/Mage/src/main/java/mage/cards/decks/importer/CodDeckImporter.java b/Mage/src/main/java/mage/cards/decks/importer/CodDeckImporter.java index 8ef40a0619..dedffadcf5 100644 --- a/Mage/src/main/java/mage/cards/decks/importer/CodDeckImporter.java +++ b/Mage/src/main/java/mage/cards/decks/importer/CodDeckImporter.java @@ -7,6 +7,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Optional; import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -40,12 +41,12 @@ public class CodDeckImporter extends DeckImporter { List mainCards = getNodes(doc, "/cockatrice_deck/zone[@name='main']/card"); decklist.setCards(mainCards.stream() - .flatMap(toDeckCardInfo(errorMessages)) + .flatMap(toDeckCardInfo(getCardLookup(), errorMessages)) .collect(Collectors.toList())); List sideboardCards = getNodes(doc, "/cockatrice_deck/zone[@name='side']/card"); decklist.setSideboard(sideboardCards.stream() - .flatMap(toDeckCardInfo(errorMessages)) + .flatMap(toDeckCardInfo(getCardLookup(), errorMessages)) .collect(Collectors.toList())); getNodes(doc, "/cockatrice_deck/deckname") @@ -65,25 +66,25 @@ public class CodDeckImporter extends DeckImporter { return 1; } try { - return Math.min(100, Math.max(0, Integer.parseInt(numberNode.getNodeValue()))); + return Math.min(100, Math.max(1, Integer.parseInt(numberNode.getNodeValue()))); } catch (NumberFormatException e) { return 1; } } - private static Function> toDeckCardInfo(StringBuilder errors) { + private static Function> toDeckCardInfo(CardLookup lookup, StringBuilder errors) { return node -> { String name = node.getAttributes().getNamedItem("name").getNodeValue().trim(); - CardInfo cardInfo = CardRepository.instance.findPreferedCoreExpansionCard(name, true); - if (cardInfo != null) { + Optional cardInfo = lookup.lookupCardInfo(name); + if (cardInfo.isPresent()) { + CardInfo info = cardInfo.get(); return Collections.nCopies( getQuantityFromNode(node), - new DeckCardInfo(cardInfo.getName(), cardInfo.getCardNumber(), cardInfo.getSetCode())) - .stream(); + new DeckCardInfo(info.getName(), info.getCardNumber(), info.getSetCode())).stream(); } else { errors.append("Could not find card: '").append(name).append("'\n"); + return Stream.empty(); } - return Stream.empty(); }; } diff --git a/Mage/src/main/java/mage/cards/decks/importer/DecDeckImporter.java b/Mage/src/main/java/mage/cards/decks/importer/DecDeckImporter.java index 94a879ad95..5ab07af1de 100644 --- a/Mage/src/main/java/mage/cards/decks/importer/DecDeckImporter.java +++ b/Mage/src/main/java/mage/cards/decks/importer/DecDeckImporter.java @@ -1,6 +1,8 @@ package mage.cards.decks.importer; +import java.util.Optional; + import mage.cards.decks.DeckCardInfo; import mage.cards.decks.DeckCardLists; import mage.cards.repository.CardInfo; @@ -29,10 +31,11 @@ public class DecDeckImporter extends PlainTextDeckImporter { String lineName = line.substring(delim).trim(); try { int num = Integer.parseInt(lineNum); - CardInfo cardInfo = CardRepository.instance.findPreferedCoreExpansionCard(lineName, true); - if (cardInfo == null) { + Optional cardLookup = getCardLookup().lookupCardInfo(lineName); + if (!cardLookup.isPresent()) { sbMessage.append("Could not find card: '").append(lineName).append("' at line ").append(lineCount).append('\n'); } else { + CardInfo cardInfo = cardLookup.get(); for (int i = 0; i < num; i++) { if (!sideboard) { deckList.getCards().add(new DeckCardInfo(cardInfo.getName(), cardInfo.getCardNumber(), cardInfo.getSetCode())); diff --git a/Mage/src/main/java/mage/cards/decks/importer/DeckImporter.java b/Mage/src/main/java/mage/cards/decks/importer/DeckImporter.java index 02c94df04a..81868e427f 100644 --- a/Mage/src/main/java/mage/cards/decks/importer/DeckImporter.java +++ b/Mage/src/main/java/mage/cards/decks/importer/DeckImporter.java @@ -3,10 +3,13 @@ package mage.cards.decks.importer; import org.apache.log4j.Logger; import java.io.File; import java.util.Locale; +import java.util.Optional; import java.util.Scanner; import mage.cards.decks.DeckCardLists; +import mage.cards.repository.CardInfo; +import mage.cards.repository.CardRepository; public abstract class DeckImporter { @@ -53,6 +56,10 @@ public abstract class DeckImporter { return importDeck(file, new StringBuilder()); } + public CardLookup getCardLookup() { + return CardLookup.instance; + } + private static boolean haveSideboardSection(String file) { // search for sideboard section: // or //sideboard diff --git a/Mage/src/test/java/mage/cards/decks/importer/CodDeckImportTest.java b/Mage/src/test/java/mage/cards/decks/importer/CodDeckImportTest.java new file mode 100644 index 0000000000..77256b69cf --- /dev/null +++ b/Mage/src/test/java/mage/cards/decks/importer/CodDeckImportTest.java @@ -0,0 +1,56 @@ +package mage.cards.decks.importer; + +import static org.junit.Assert.assertEquals; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.junit.Test; + +import mage.cards.Card; +import mage.cards.CardImpl; +import mage.cards.decks.DeckCardInfo; +import mage.cards.decks.DeckCardLists; +import mage.cards.repository.CardInfo; + +public class CodDeckImportTest { + + private static final FakeCardLookup LOOKUP = new FakeCardLookup() + .addCard("Forest") + .addCard("Razorverge Thicket") + .addCard("Avacyn's Pilgrim") + .addCard("War Priest of Thune"); + + @Test + public void testImportCod() { + CodDeckImporter importer = new CodDeckImporter() { + @Override + public CardLookup getCardLookup() { + return LOOKUP; + } + }; + StringBuilder errors = new StringBuilder(); + DeckCardLists deck = importer.importDeck( + "src/test/java/mage/cards/decks/importer/testdeck.cod", errors); + assertEquals("Deck Name", deck.getName()); + + TestDeckChecker.checker() + .addMain("Forest", 12) + .addMain("Razorverge Thicket", 100) + .addMain("Avacyn's Pilgrim", 1) + .addSide("War Priest of Thune", 3) + .verify(deck, 113, 3); + + assertEquals("Could not find card: '@#$NOT A REAL CARD NAME@#$'\n", errors.toString()); + } + + private static void assertCardSame(String name, DeckCardInfo card) { + assertEquals(name, card.getCardName()); + assertEquals(1, card.getQuantity()); + } + +} diff --git a/Mage/src/test/java/mage/cards/decks/importer/DecDeckImportTest.java b/Mage/src/test/java/mage/cards/decks/importer/DecDeckImportTest.java new file mode 100644 index 0000000000..613a8f6c14 --- /dev/null +++ b/Mage/src/test/java/mage/cards/decks/importer/DecDeckImportTest.java @@ -0,0 +1,91 @@ +package mage.cards.decks.importer; + +import static org.junit.Assert.assertEquals; + +import java.util.Collections; +import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.junit.Test; + +import mage.cards.decks.DeckCardInfo; +import mage.cards.decks.DeckCardLists; +import mage.cards.repository.CardInfo; + +public class DecDeckImportTest { + + private static final FakeCardLookup LOOKUP = new FakeCardLookup() + .addCard("Masticore") + .addCard("Metalworker") + .addCard("Phyrexian Colossus") + .addCard("Crumbling Sanctuary") + .addCard("Grim Monolith") + .addCard("Mishra's Helix") + .addCard("Phyrexian Processor") + .addCard("Tangle Wire") + .addCard("Thran Dynamo") + .addCard("Voltaic Key") + .addCard("Tinker") + .addCard("Brainstorm") + .addCard("Crystal Vein") + .addCard("Island") + .addCard("Rishadan Port") + .addCard("Saprazzan Skerry") + .addCard("Annul") + .addCard("Chill") + .addCard("Miscalculation") + .addCard("Mishra's Helix") + .addCard("Rising Waters"); + + @Test + public void testImport() { + StringBuilder errors = new StringBuilder(); + DecDeckImporter importer = new DecDeckImporter() { + @Override + public CardLookup getCardLookup() { + return LOOKUP; + } + }; + DeckCardLists deck = importer.importDeck( + "src/test/java/mage/cards/decks/importer/testdeck.dec", errors); + + TestDeckChecker.checker() + .addMain("Masticore", 4) + .addMain("Metalworker", 4) + .addMain("Phyrexian Colossus", 1) + .addMain("Crumbling Sanctuary", 1) + .addMain("Grim Monolith", 4) + .addMain("Mishra's Helix", 1) + .addMain("Phyrexian Processor", 4) + .addMain("Tangle Wire", 4) + .addMain("Thran Dynamo", 4) + .addMain("Voltaic Key", 4) + .addMain("Tinker", 4) + .addMain("Brainstorm", 4) + .addMain("Crystal Vein", 4) + .addMain("Island", 9) + .addMain("Rishadan Port", 4) + .addMain("Saprazzan Skerry", 4) + .addSide("Annul", 4) + .addSide("Chill", 4) + .addSide("Miscalculation", 4) + .addSide("Mishra's Helix", 1) + .addSide("Rising Waters", 2) + .verify(deck, 60, 15); + + assertEquals("", errors.toString()); + } + + private static FakeCardLookup getFakeCardLookup() { + FakeCardLookup lookup = new FakeCardLookup() { + @Override + public Optional lookupCardInfo(String name) { + System.out.println(name); + return super.lookupCardInfo(name); + } + }; + return lookup; + } + +} diff --git a/Mage/src/test/java/mage/cards/decks/importer/FakeCardLookup.java b/Mage/src/test/java/mage/cards/decks/importer/FakeCardLookup.java new file mode 100644 index 0000000000..4db37db6d7 --- /dev/null +++ b/Mage/src/test/java/mage/cards/decks/importer/FakeCardLookup.java @@ -0,0 +1,29 @@ +package mage.cards.decks.importer; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +import mage.cards.repository.CardInfo; + +public class FakeCardLookup extends CardLookup { + + private final Map lookup = new HashMap<>(); + + public FakeCardLookup addCard(String cardName) { + lookup.put(cardName, new CardInfo() {{ + name = cardName; + }}); + return this; + } + + @Override + public Optional lookupCardInfo(String name) { + CardInfo card = lookup.get(name); + if (card == null) { + System.out.println("Couldn't find: " + name); + } + return Optional.ofNullable(card); + } + +} diff --git a/Mage/src/test/java/mage/cards/decks/importer/TestDeckChecker.java b/Mage/src/test/java/mage/cards/decks/importer/TestDeckChecker.java new file mode 100644 index 0000000000..e4f2543239 --- /dev/null +++ b/Mage/src/test/java/mage/cards/decks/importer/TestDeckChecker.java @@ -0,0 +1,54 @@ +package mage.cards.decks.importer; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.junit.Assert; + +import mage.cards.decks.DeckCardLists; + +public class TestDeckChecker { + + private final List main = new ArrayList<>(); + private final List side = new ArrayList<>(); + + public TestDeckChecker addMain(String name) { + return addMain(name, 1); + } + + public TestDeckChecker addMain(String name, int quantity) { + main.addAll(Collections.nCopies(quantity, name)); + return this; + } + + public TestDeckChecker addSide(String name) { + return addSide(name, 1); + } + + public TestDeckChecker addSide(String name, int quantity) { + side.addAll(Collections.nCopies(quantity, name)); + return this; + } + + public void verify(DeckCardLists deck, int nMain, int nSide) { + assertEquals(nMain, main.size()); + assertEquals(nSide, side.size()); + assertEquals(nMain, deck.getCards().size()); + assertEquals(nSide, deck.getSideboard().size()); + + for (int i = 0; i < main.size(); i++) { + String expected = main.get(i); + String actual = deck.getCards().get(i).getCardName(); + assertEquals(String.format("Expected: '%s' Actual: '%s' at index: %s", + expected, actual, i), expected, actual); + } + } + + public static TestDeckChecker checker() { + return new TestDeckChecker(); + } + +} diff --git a/Mage.Client/src/test/java/mage/cards/decks/importer/testdeck.cod b/Mage/src/test/java/mage/cards/decks/importer/testdeck.cod similarity index 100% rename from Mage.Client/src/test/java/mage/cards/decks/importer/testdeck.cod rename to Mage/src/test/java/mage/cards/decks/importer/testdeck.cod diff --git a/Mage.Client/src/test/java/mage/cards/decks/importer/testdeck.dec b/Mage/src/test/java/mage/cards/decks/importer/testdeck.dec similarity index 100% rename from Mage.Client/src/test/java/mage/cards/decks/importer/testdeck.dec rename to Mage/src/test/java/mage/cards/decks/importer/testdeck.dec