diff --git a/src/functionalTest/java/codechicken/multipart/test/PartFactoryLookupFunctionalTest.java b/src/functionalTest/java/codechicken/multipart/test/PartFactoryLookupFunctionalTest.java new file mode 100644 index 0000000..39417d6 --- /dev/null +++ b/src/functionalTest/java/codechicken/multipart/test/PartFactoryLookupFunctionalTest.java @@ -0,0 +1,67 @@ +package codechicken.multipart.test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.lang.reflect.Field; +import java.util.function.Function; + +import net.minecraft.nbt.NBTTagCompound; + +import org.junit.jupiter.api.Test; + +import codechicken.microblock.MicroMaterialRegistry; +import codechicken.microblock.Microblock; +import codechicken.microblock.MicroblockClass; +import codechicken.microblock.MicroblockClient; +import codechicken.multipart.MultiPartRegistry$; +import codechicken.multipart.MultiPartRegistry.IPartFactory2; +import cpw.mods.fml.relauncher.ReflectionHelper; +import scala.Option; + +class PartFactoryLookupFunctionalTest { + + @Test + @SuppressWarnings("unchecked") + void reflectedFactoriesSupportMicroblockConstructionAndLoading() throws Exception { + Field field = ReflectionHelper + .findField(MultiPartRegistry$.class, "codechicken$multipart$MultiPartRegistry$$typeMap"); + scala.collection.mutable.Map map = (scala.collection.mutable.Map) field + .get(MultiPartRegistry$.MODULE$); + checkMicroblocks(name -> { + Option factory = map.get(name); + return factory.isEmpty() ? null : factory.get(); + }); + } + + private static void checkMicroblocks(Function lookup) throws Exception { + assertNull(lookup.apply("test:no_such_factory")); + assertFalse(lookup.apply("mc_torch") instanceof MicroblockClass); + int material = MicroMaterialRegistry.materialID("minecraft:stone"); + for (String name : new String[] { "mcr_face", "mcr_hllw", "mcr_cnr", "mcr_edge", "mcr_post" }) { + IPartFactory2 factory = lookup.apply(name); + assertTrue(factory instanceof MicroblockClass); + assertSame(factory, lookup.apply(new String(name))); + Microblock part = (Microblock) MicroblockClass.class.getMethod("create", boolean.class, int.class) + .invoke(factory, false, material); + assertSame(factory, part.microClass()); + assertEquals(name, part.getType()); + assertEquals(material, part.material()); + assertFalse(part instanceof MicroblockClient); + assertNull(part.tile()); + assertNotSame(part, ((MicroblockClass) factory).create(false, material)); + NBTTagCompound tag = new NBTTagCompound(); + tag.setString("id", name); + tag.setString("material", "minecraft:stone"); + tag.setByte("shape", (byte) 0x21); + part.load(tag); + assertEquals(0x21, part.shape()); + assertEquals(material, part.material()); + assertNull(part.tile(), "Factory construction and NBT load do not bind a part"); + } + } +} diff --git a/src/test/java/codechicken/multipart/MultiPartRegistryCharacterizationTest.java b/src/test/java/codechicken/multipart/MultiPartRegistryCharacterizationTest.java index 4593df5..d6dc2cc 100644 --- a/src/test/java/codechicken/multipart/MultiPartRegistryCharacterizationTest.java +++ b/src/test/java/codechicken/multipart/MultiPartRegistryCharacterizationTest.java @@ -11,6 +11,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.NoSuchElementException; +import java.util.function.Function; import net.minecraft.block.Block; import net.minecraft.block.material.Material; @@ -33,6 +34,44 @@ import scala.Option; */ class MultiPartRegistryCharacterizationTest { + @Test + void reflectedFactoryLookupTracksRegistryWithoutConstructingParts() throws IllegalAccessException { + checkFactoryLookup(reflectedFactoryLookup()); + } + + @SuppressWarnings("unchecked") + private static Function reflectedFactoryLookup() throws IllegalAccessException { + Field field = ReflectionHelper + .findField(MultiPartRegistry$.class, "codechicken$multipart$MultiPartRegistry$$typeMap"); + scala.collection.mutable.Map map = (scala.collection.mutable.Map) field + .get(MultiPartRegistry$.MODULE$); + return name -> { + Option factory = map.get(name); + return factory.isEmpty() ? null : factory.get(); + }; + } + + private static void checkFactoryLookup(Function lookup) { + String name = "test:factory_lookup"; + TestFactory first = new TestFactory(new TestPart()); + TestFactory second = new TestFactory(new TestPart()); + assertNull(lookup.apply(name)); + assertNull(lookup.apply("")); + try { + MultiPartRegistry.typeMapBacking().put(name, first); + assertSame(first, lookup.apply(new String(name))); + assertNull(lookup.apply("TEST:factory_lookup")); + MultiPartRegistry.typeMapBacking().put(name, second); + assertSame(second, lookup.apply(name)); + MultiPartRegistry.typeMapBacking().remove(name); + assertNull(lookup.apply(name)); + assertEquals(0, first.calls); + assertEquals(0, second.calls); + } finally { + MultiPartRegistry.typeMapBacking().remove(name); + } + } + @Test void noPartsAreLoadedBeforeRegistrationRuns() { assertFalse(MultiPartRegistry.loaded()); @@ -144,6 +183,7 @@ class MultiPartRegistryCharacterizationTest { private static final class TestFactory implements IPartFactory2 { private final TMultiPart part; + private int calls; private TestFactory(TMultiPart part) { this.part = part; @@ -151,11 +191,13 @@ class MultiPartRegistryCharacterizationTest { @Override public TMultiPart createPart(String name, NBTTagCompound nbt) { + calls++; return part; } @Override public TMultiPart createPart(String name, MCDataInput packet) { + calls++; return part; } }