From e82a0d8fb8591347a03f6124392f638759cdbbd9 Mon Sep 17 00:00:00 2001 From: Siphalor Date: Fri, 17 Jul 2020 22:46:37 +0200 Subject: [PATCH] More content tests --- .../product/dynamic/DynamicProductType.java | 4 ++ .../java/de/siphalor/was/ContentTests.java | 60 ++++++++++++++++++- 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/siphalor/was/content/product/dynamic/DynamicProductType.java b/src/main/java/de/siphalor/was/content/product/dynamic/DynamicProductType.java index da4c517..a447e84 100644 --- a/src/main/java/de/siphalor/was/content/product/dynamic/DynamicProductType.java +++ b/src/main/java/de/siphalor/was/content/product/dynamic/DynamicProductType.java @@ -132,6 +132,10 @@ public class DynamicProductType implements ProductType { return products.get(random.nextInt(products.size())); } + public Map getVariations() { + return variations; + } + private static class ProductPrototype { String value; diff --git a/src/test/java/de/siphalor/was/ContentTests.java b/src/test/java/de/siphalor/was/ContentTests.java index 60966cc..c6d4455 100644 --- a/src/test/java/de/siphalor/was/ContentTests.java +++ b/src/test/java/de/siphalor/was/ContentTests.java @@ -1,15 +1,25 @@ package de.siphalor.was; import de.siphalor.was.content.ContentManager; +import de.siphalor.was.content.lang.I18n; import de.siphalor.was.content.pack.ContentPack; import de.siphalor.was.content.pack.JarContentPack; +import de.siphalor.was.content.product.Product; +import de.siphalor.was.content.product.ProductManager; +import de.siphalor.was.content.product.dynamic.DynamicProduct; +import de.siphalor.was.content.product.dynamic.DynamicProductType; +import de.siphalor.was.content.resource.Resource; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; +import javax.imageio.ImageIO; import java.io.IOException; import java.io.InputStream; +import java.util.Arrays; import java.util.Properties; +import java.util.stream.Stream; public class ContentTests { static ContentManager contentManager; @@ -18,7 +28,7 @@ public class ContentTests { @BeforeAll public static void prepareAll() { contentManager = new ContentManager(); - contentPack = new JarContentPack("jar", "content"); + contentPack = new JarContentPack("", "content"); contentManager.addPack(contentPack); } @@ -36,4 +46,52 @@ public class ContentTests { } }); } + + @Test + public void productsI18n() { + ProductManager productManager = new ProductManager(); + productManager.reload(contentManager); + I18n i18n = I18n.getInstance(); + i18n.reload(contentManager); + + Assertions.assertAll( + "Missing translations for products", + productManager.getTypes().stream().flatMap(productType -> { + String productKey = "products." + productType.getId(); + Stream stream = Arrays.stream(productType.getProperties()).map(property -> () -> + Assertions.assertTrue( + i18n.containsKey(productKey + "." + property), + () -> "Missing translation for property " + property + "@" + productType.getId() + ": " + productKey + "." + property + ) + ); + stream = Stream.concat(stream, Stream.of(() -> Assertions.assertTrue(i18n.containsKey(productKey), "Missing translation for product name: " + productKey))); + return stream; + }) + ); + } + + @Test + public void productImages() { + ProductManager productManager = new ProductManager(); + productManager.reload(contentManager); + + Assertions.assertAll(productManager.getTypes().stream().flatMap(productType -> { + if (productType instanceof DynamicProductType) { + return ((DynamicProductType) productType).getVariations().values().stream().map(dynamicProduct -> () -> { + Resource resource = contentManager.getResource(dynamicProduct.getTextureLocation()).orElse(null); + Assertions.assertNotNull(resource, + () -> "Couldn't find image resource for " + dynamicProduct.getTranslationKey() + + " with properties " + dynamicProduct.getPropertySpecifier() + + ": " + dynamicProduct.getTextureLocation() + ); + Assertions.assertDoesNotThrow(() -> ImageIO.read(resource.getInputStream()), + () -> "Failed to load image resource for " + dynamicProduct.getTranslationKey() + + "with properties " + dynamicProduct.getPropertySpecifier() + + ": " + dynamicProduct.getTextureLocation() + ); + }); + } + return Stream.empty(); + })); + } }