From 58b2a070eda82d36a1eebe00311835b0b288de3d Mon Sep 17 00:00:00 2001 From: leriomaggio Date: Wed, 4 Aug 2021 15:57:28 +0100 Subject: [PATCH] Added test for corner cases with different case in card name and set code (also compared to legacy implementation) Also, now LoadCard also uses the provided setCode (if any). If null or empty string is provided in request, all editions will be used! --- .../src/main/java/forge/card/CardDb.java | 27 +++++-- .../test/java/forge/card/CardDbTestCase.java | 78 +++++++++++++++++++ 2 files changed, 99 insertions(+), 6 deletions(-) diff --git a/forge-core/src/main/java/forge/card/CardDb.java b/forge-core/src/main/java/forge/card/CardDb.java index eeaffca119f..0c018a6f58b 100644 --- a/forge-core/src/main/java/forge/card/CardDb.java +++ b/forge-core/src/main/java/forge/card/CardDb.java @@ -227,17 +227,31 @@ public final class CardDb implements ICardDatabase, IDeckGenPool { addCard(new PaperCard(cr, e.getCode(), cis.rarity, artIdx, false, cis.collectorNumber, cis.artistName)); } - public void loadCard(String cardName, CardRules cr) { + public void loadCard(String cardName, String setCode, CardRules cr) { // @leriomaggio: This method is called when lazy-loading is set - System.out.println("Lazy Loading Card: " + cardName); + System.out.println("[LOG]: (Lazy) Loading Card: " + cardName); rulesByName.put(cardName, cr); - for (CardEdition e : editions) { + boolean reIndexNecessary = false; + if ((setCode == null) || setCode.length() == 0 || setCode.equals(CardEdition.UNKNOWN.getCode())) { + // look for all possible editions + for (CardEdition e : editions) { + List cardsInSet = e.getCardInSet(cardName); // empty collection if not present + for (CardInSet cis : cardsInSet) { + addSetCard(e, cis, cr); + reIndexNecessary = true; + } + } + } else { + CardEdition e = editions.get(setCode); List cardsInSet = e.getCardInSet(cardName); // empty collection if not present - for (CardInSet cis : cardsInSet) + for (CardInSet cis : cardsInSet) { addSetCard(e, cis, cr); + reIndexNecessary = true; + } } - reIndex(); + if (reIndexNecessary) + reIndex(); } public void initialize(boolean logMissingPerEdition, boolean logMissingSummary, boolean enableUnknownCards) { @@ -441,7 +455,8 @@ public final class CardDb implements ICardDatabase, IDeckGenPool { if ((reqEditionCode != null) && (reqEditionCode.length() > 0)) { // This get is robust even against expansion aliases (e.g. TE and TMP both valid for Tempest) - // MOST of the extensions have two short codes, 141 out of 221 (so far) - CardEdition edition = editions.get(reqEditionCode); + // ALSO: Set Code are always UpperCase + CardEdition edition = editions.get(reqEditionCode.toUpperCase()); return this.getCardFromSet(request.cardName, edition, request.artIndex, request.collectorNumber, request.isFoil); } diff --git a/forge-gui-desktop/src/test/java/forge/card/CardDbTestCase.java b/forge-gui-desktop/src/test/java/forge/card/CardDbTestCase.java index b5078a6e47f..a4e9b508e18 100644 --- a/forge-gui-desktop/src/test/java/forge/card/CardDbTestCase.java +++ b/forge-gui-desktop/src/test/java/forge/card/CardDbTestCase.java @@ -1945,5 +1945,83 @@ public class CardDbTestCase extends ForgeCardMockTestCase { assertEquals(retrievedPaperCard.getEdition(), CardEdition.UNKNOWN.getCode()); } + @Test + public void testGetCardFromWrongEditionOrNonExistingEditionReturnsNullResult(){ + String cardName = "Blinding Angel"; + String wrongSetCode = "LEA"; // obiviously wrong + + String requestInfo = CardDb.CardRequest.compose(cardName, wrongSetCode); + PaperCard blindingAngelCard = this.cardDb.getCard(requestInfo); + PaperCard legacyBlindingAngelCard = this.legacyCardDb.getCard(requestInfo); + assertNull(legacyBlindingAngelCard); // be sure behaviour is the same + assertNull(blindingAngelCard); + + String nonExistingSetCode = "9TH"; // non-existing, should be 9ED + requestInfo = CardDb.CardRequest.compose(cardName, nonExistingSetCode); + blindingAngelCard = this.cardDb.getCard(requestInfo); + legacyBlindingAngelCard = this.legacyCardDb.getCard(requestInfo); + assertNull(legacyBlindingAngelCard); // be sure behaviour is the same + assertNull(blindingAngelCard); + } + + // Case Insensitive Search/Retrieval Tests + @Test + public void testUpdatedCardDBAPICardNameWithWrongCaseWillStillReturnTheCorrectCard() { + String cardName = "AEther baRRIER"; // wrong case + String setCode = "NMS"; + String requestInfo = CardDb.CardRequest.compose(cardName, setCode); + PaperCard aetherBarrierCard = this.cardDb.getCard(requestInfo); + assertNotNull(aetherBarrierCard); + assertEquals(aetherBarrierCard.getName(), "Aether Barrier"); + assertEquals(aetherBarrierCard.getEdition(), "NMS"); + + // Compare w/ LegacyDb + PaperCard legacyAetherBarrierCard = this.legacyCardDb.getCard(requestInfo); + assertEquals(aetherBarrierCard, legacyAetherBarrierCard); + } + + @Test + public void testWrongCaseInEditionSetCodeReturnsNull(){ + String cardName = "Aether Barrier"; // correct name + String setCode = "nmS"; // wrong case, non-existing + String requestInfo = CardDb.CardRequest.compose(cardName, setCode); + PaperCard aetherBarrierCard = this.cardDb.getCard(requestInfo); + assertNotNull(aetherBarrierCard); + assertEquals(aetherBarrierCard.getName(), cardName); + assertEquals(aetherBarrierCard.getEdition(), setCode.toUpperCase()); + + // Compare w/ LegacyDb + PaperCard legacyAetherBarrierCard = this.legacyCardDb.getCard(requestInfo); + assertNotNull(legacyAetherBarrierCard); + assertEquals(legacyAetherBarrierCard, aetherBarrierCard); + } + + // "Problematic" Card names + @Test + public void testRetrievingBorrowing100_000ArrowsCard(){ + String cardName = "Borrowing 100,000 Arrows"; + PaperCard borrowingCard = this.cardDb.getCard(cardName); + assertNotNull(borrowingCard); + assertEquals(borrowingCard.getName(), cardName); + + // Compare w/ LegacyDb + PaperCard legacyBorrowingCard = this.legacyCardDb.getCardFromEdition(cardName, LegacyCardDb.LegacySetPreference.Latest); + assertEquals(legacyBorrowingCard, borrowingCard); + } + + @Test + public void testGetCardWithDashInNameAndWrongCaseToo(){ + String requestInfo = "Ainok Bond-kin|KTK"; // wrong case for last 'k' and dash in name + PaperCard ainokCard = this.cardDb.getCard(requestInfo); + assertNotNull(ainokCard); + assertEquals(ainokCard.getName(), "Ainok Bond-Kin"); + assertEquals(ainokCard.getEdition(), "KTK"); + + // Compare w/ LegacyDb + PaperCard legacyAinokCard = this.legacyCardDb.getCard(requestInfo); + assertEquals(legacyAinokCard, ainokCard); + } + + }