diff --git a/Mage.Tests/src/test/java/org/mage/test/serverside/performance/SerializationTest.java b/Mage.Tests/src/test/java/org/mage/test/serverside/performance/SerializationTest.java index ceb21d3a7c..a6bf6033fd 100644 --- a/Mage.Tests/src/test/java/org/mage/test/serverside/performance/SerializationTest.java +++ b/Mage.Tests/src/test/java/org/mage/test/serverside/performance/SerializationTest.java @@ -2,6 +2,8 @@ package org.mage.test.serverside.performance; import mage.abilities.keyword.InfectAbility; import mage.cards.Card; +import mage.cards.ExpansionSet; +import mage.cards.Sets; import mage.cards.repository.CardInfo; import mage.cards.repository.CardRepository; import mage.constants.PhaseStep; @@ -15,9 +17,14 @@ import mage.util.CardUtil; import mage.utils.CompressUtil; import mage.view.GameView; import org.junit.Assert; +import org.junit.Ignore; import org.junit.Test; import org.mage.test.serverside.base.CardTestPlayerBase; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + /** * @author JayDi85 */ @@ -60,19 +67,81 @@ public class SerializationTest extends CardTestPlayerBase { Assert.assertEquals("Must get infected counter", 1, permanent.getCounters(currentGame).getCount(CounterType.M1M1)); } + private void processSingleCard(CardInfo cardInfo) { + // compress each card's part + Card newCard = cardInfo.getCard(); + CardUtil.getObjectPartsAsObjects(newCard).stream() + .map(Card.class::cast) + .forEach(card -> { + Card testCard = CardUtil.getDefaultCardSideForBattlefield(newCard); + Card testPermanent = null; + if (!testCard.isInstantOrSorcery()) { + testPermanent = new PermanentCard(testCard, playerA.getId(), currentGame); + } + + // card + { + Object compressed = CompressUtil.compress(testCard); + Assert.assertTrue("Must be zip", compressed instanceof ZippedObjectImpl); + Card uncompressed = (Card) CompressUtil.decompress(compressed); + Assert.assertEquals("Must be same", testCard.getName(), uncompressed.getName()); + } + + // permanent + if (testPermanent != null) { + Object compressed = CompressUtil.compress(testPermanent); + Assert.assertTrue("Must be zip", compressed instanceof ZippedObjectImpl); + Card uncompressed = (Card) CompressUtil.decompress(compressed); + Assert.assertEquals("Must be same", testPermanent.getName(), uncompressed.getName()); + } + }); + } + + private void processSingleCard(String cardName) { + CardInfo cardInfo = CardRepository.instance.findCard(cardName); + processSingleCard(cardInfo); + } + + @Ignore // WARNING, debug only, needs few minutes to execute, so run it manually + @Test + public void test_Single_AllCards() { + // checking FULL cards list for serialization errors + String filterSet = ""; + String filterCard = ""; + + List errorsList = new ArrayList<>(); + int checkedCount = 0; + for (ExpansionSet set : Sets.getInstance().values()) { + if (!filterSet.isEmpty() && !set.getCode().equals(filterSet)) { + continue; + } + checkedCount++; + System.out.printf("Checking set %d of %d (%s)%n", checkedCount, Sets.getInstance().size(), set.getName()); + + for (ExpansionSet.SetCardInfo info : set.getSetCardInfo()) { + if (!filterCard.isEmpty() && !info.getName().equals(filterCard)) { + continue; + } + CardInfo cardInfo = CardRepository.instance.findCardsByClass(info.getCardClass().getCanonicalName()).stream().findFirst().orElse(null); + try { + processSingleCard(cardInfo); + } catch (Throwable e) { + System.out.println("Broken card: " + info.getName()); + //e.printStackTrace(); // search exception errors in the logs + errorsList.add(info.getName()); + } + } + } + + if (!errorsList.isEmpty()) { + Assert.fail("Found broken cards: " + errorsList.size() + "\n" + + errorsList.stream().sorted().collect(Collectors.joining("\n"))); + } + } + @Test public void test_Single_KentaroTheSmilingCat() { - CardInfo cardInfo = CardRepository.instance.findCard("Kentaro, the Smiling Cat"); - Card newCard = cardInfo.getCard(); - Card permCard = CardUtil.getDefaultCardSideForBattlefield(newCard); - PermanentImpl permanent = new PermanentCard(permCard, playerA.getId(), currentGame); - currentGame.addPermanent(permanent, 0); - - // test compress (it uses default java serialization) - Object compressed = CompressUtil.compress(permanent); - Assert.assertTrue("Must be zip", compressed instanceof ZippedObjectImpl); - PermanentImpl uncompressed = (PermanentImpl) CompressUtil.decompress(compressed); - Assert.assertEquals("Must be same", permanent.getName(), uncompressed.getName()); + processSingleCard("Kentaro, the Smiling Cat"); } @Test