diff --git a/src/test/java/testItems/dependencyInjection/implementations/DhTestEvent.java b/src/test/java/testItems/dependencyInjection/implementations/DhTestEvent.java new file mode 100644 index 000000000..3eff18296 --- /dev/null +++ b/src/test/java/testItems/dependencyInjection/implementations/DhTestEvent.java @@ -0,0 +1,25 @@ +package testItems.dependencyInjection.implementations; + +import testItems.dependencyInjection.objects.DhApiTestEvent; + +/** + * Dummy test event for unit tests. + * + * @author James Seibel + * @version 2022-7-16 + */ +public class DhTestEvent extends DhApiTestEvent +{ + public Boolean eventFiredValue = null; + + @Override + public boolean test(Boolean cancelEvent) + { + this.eventFiredValue = cancelEvent; + return cancelEvent; + } + + @Override + public Boolean getTestValue() { return this.eventFiredValue; } + +} diff --git a/src/test/java/testItems/dependencyInjection/implementations/DhTestEventAlt.java b/src/test/java/testItems/dependencyInjection/implementations/DhTestEventAlt.java new file mode 100644 index 000000000..f6ef22f85 --- /dev/null +++ b/src/test/java/testItems/dependencyInjection/implementations/DhTestEventAlt.java @@ -0,0 +1,25 @@ +package testItems.dependencyInjection.implementations; + +import testItems.dependencyInjection.objects.DhApiTestEvent; + +/** + * Dummy test event for unit tests. + * + * @author James Seibel + * @version 2022-7-16 + */ +public class DhTestEventAlt extends DhApiTestEvent +{ + public Boolean eventFiredValue = null; + + @Override + public boolean test(Boolean cancelEvent) + { + this.eventFiredValue = cancelEvent; + return cancelEvent; + } + + @Override + public Boolean getTestValue() { return this.eventFiredValue; } + +} diff --git a/src/test/java/testItems/dependencyInjection/interfaces/ITestOne.java b/src/test/java/testItems/dependencyInjection/interfaces/ITestOne.java new file mode 100644 index 000000000..97c51337b --- /dev/null +++ b/src/test/java/testItems/dependencyInjection/interfaces/ITestOne.java @@ -0,0 +1,16 @@ +package testItems.dependencyInjection.interfaces; + +import com.seibel.lod.core.handlers.dependencyInjection.IBindable; + +/** + * Dummy test interface for dependency unit tests. + * + * @author James Seibel + * @version 2022-7-16 + */ +public interface ITestOne extends IBindable +{ + public int getValue(); + + public int getDependentValue(); +} diff --git a/src/test/java/testItems/dependencyInjection/interfaces/ITestTwo.java b/src/test/java/testItems/dependencyInjection/interfaces/ITestTwo.java new file mode 100644 index 000000000..1ed7b1761 --- /dev/null +++ b/src/test/java/testItems/dependencyInjection/interfaces/ITestTwo.java @@ -0,0 +1,16 @@ +package testItems.dependencyInjection.interfaces; + +import com.seibel.lod.core.handlers.dependencyInjection.IBindable; + +/** + * Dummy test interface for dependency unit tests. + * + * @author James Seibel + * @version 2022-7-16 + */ +public interface ITestTwo extends IBindable +{ + public int getValue(); + + public int getDependentValue(); +} diff --git a/src/test/java/testItems/dependencyInjection/objects/ConcreteTestBoth.java b/src/test/java/testItems/dependencyInjection/objects/ConcreteTestBoth.java new file mode 100644 index 000000000..0eadb5a6a --- /dev/null +++ b/src/test/java/testItems/dependencyInjection/objects/ConcreteTestBoth.java @@ -0,0 +1,28 @@ +package testItems.dependencyInjection.objects; + +import com.seibel.lod.core.handlers.dependencyInjection.IBindable; +import testItems.dependencyInjection.interfaces.ITestOne; +import testItems.dependencyInjection.interfaces.ITestTwo; + +/** + * Dummy test implementation object for dependency injection unit tests. + * + * @author James Seibel + * @version 2022-7-16 + */ +public class ConcreteTestBoth implements ITestOne, ITestTwo, IBindable +{ + public static final int VALUE = 3; + + @Override + public void finishDelayedSetup() { } + + @Override + public int getValue() + { + return VALUE; + } + + @Override + public int getDependentValue() { return -1; } +} diff --git a/src/test/java/testItems/dependencyInjection/objects/ConcreteTestOne.java b/src/test/java/testItems/dependencyInjection/objects/ConcreteTestOne.java new file mode 100644 index 000000000..219c098ea --- /dev/null +++ b/src/test/java/testItems/dependencyInjection/objects/ConcreteTestOne.java @@ -0,0 +1,34 @@ +package testItems.dependencyInjection.objects; + +import com.seibel.lod.core.handlers.dependencyInjection.IBindable; +import testItems.dependencyInjection.interfaces.ITestOne; +import testItems.dependencyInjection.interfaces.ITestTwo; +import tests.DependencyInjectorTest; + +/** + * Dummy test implementation object for dependency injection unit tests. + * + * @author James Seibel + * @version 2022-7-16 + */ +public class ConcreteTestOne implements ITestOne, IBindable +{ + private ITestTwo testInterTwo; + + public static int VALUE = 1; + + + + @Override + public void finishDelayedSetup() { testInterTwo = DependencyInjectorTest.TEST_SINGLETON_HANDLER.get(ITestTwo.class, true); } + @Override + public boolean getDelayedSetupComplete() { return testInterTwo != null; } + + + @Override + public int getValue() { return VALUE; } + + @Override + public int getDependentValue() { return testInterTwo.getValue(); } + +} diff --git a/src/test/java/testItems/dependencyInjection/objects/ConcreteTestTwo.java b/src/test/java/testItems/dependencyInjection/objects/ConcreteTestTwo.java new file mode 100644 index 000000000..8195a1826 --- /dev/null +++ b/src/test/java/testItems/dependencyInjection/objects/ConcreteTestTwo.java @@ -0,0 +1,34 @@ +package testItems.dependencyInjection.objects; + +import com.seibel.lod.core.handlers.dependencyInjection.IBindable; +import testItems.dependencyInjection.interfaces.ITestOne; +import testItems.dependencyInjection.interfaces.ITestTwo; +import tests.DependencyInjectorTest; + +/** + * Dummy test implementation object for dependency injection unit tests. + * + * @author James Seibel + * @version 2022-7-16 + */ +public class ConcreteTestTwo implements ITestTwo, IBindable +{ + private ITestOne testInterOne; + + public static int VALUE = 2; + + + @Override + public void finishDelayedSetup() { testInterOne = DependencyInjectorTest.TEST_SINGLETON_HANDLER.get(ITestOne.class, true); } + @Override + public boolean getDelayedSetupComplete() { return testInterOne != null; } + + + + @Override + public int getValue() { return VALUE; } + + @Override + public int getDependentValue() { return testInterOne.getValue(); } + +} diff --git a/src/test/java/testItems/dependencyInjection/objects/DhApiTestEvent.java b/src/test/java/testItems/dependencyInjection/objects/DhApiTestEvent.java new file mode 100644 index 000000000..fac658710 --- /dev/null +++ b/src/test/java/testItems/dependencyInjection/objects/DhApiTestEvent.java @@ -0,0 +1,40 @@ +package testItems.dependencyInjection.objects; + +import com.seibel.lod.core.api.implementation.interfaces.events.IDhApiEvent; + +/** + * A dummy event implementation used for unit testing. + * + * @author James Seibel + * @version 2022-7-16 + */ +public abstract class DhApiTestEvent implements IDhApiEvent +{ + /** + * Test event. + * + * @param input + * @return whether the event should be canceled or not. + */ + public abstract boolean test(Boolean input); + + /** + * Normal DhApiEvent classes shouldn't have any other methods like this. + * This is just for testing. + */ + public abstract Boolean getTestValue(); + + + //=========================// + // internal DH API methods // + //=========================// + + @Override + public final boolean onEvent(Boolean input) + { + return test(input); + } + + @Override + public final boolean getCancelable() { return true; } +} \ No newline at end of file diff --git a/src/test/java/ApiEnumSyncTests.java b/src/test/java/tests/ApiEnumSyncTests.java similarity index 99% rename from src/test/java/ApiEnumSyncTests.java rename to src/test/java/tests/ApiEnumSyncTests.java index a70b9513b..94fb8e0b9 100644 --- a/src/test/java/ApiEnumSyncTests.java +++ b/src/test/java/tests/ApiEnumSyncTests.java @@ -1,4 +1,4 @@ -/* +package tests;/* * This file is part of the Distant Horizons mod (formerly the LOD Mod), * licensed under the GNU LGPL v3 License. * diff --git a/src/test/java/tests/DependencyInjectorTest.java b/src/test/java/tests/DependencyInjectorTest.java new file mode 100644 index 000000000..3d46c8511 --- /dev/null +++ b/src/test/java/tests/DependencyInjectorTest.java @@ -0,0 +1,160 @@ +package tests; + +import com.seibel.lod.core.handlers.dependencyInjection.DependencyInjector; +import com.seibel.lod.core.handlers.dependencyInjection.DhApiEventInjector; +import com.seibel.lod.core.handlers.dependencyInjection.IBindable; + +import org.junit.Assert; +import org.junit.Test; +import testItems.dependencyInjection.implementations.DhTestEvent; +import testItems.dependencyInjection.implementations.DhTestEventAlt; +import testItems.dependencyInjection.interfaces.ITestOne; +import testItems.dependencyInjection.interfaces.ITestTwo; +import testItems.dependencyInjection.objects.ConcreteTestBoth; +import testItems.dependencyInjection.objects.ConcreteTestOne; +import testItems.dependencyInjection.objects.ConcreteTestTwo; +import testItems.dependencyInjection.objects.DhApiTestEvent; + +import java.util.ArrayList; + + +/** + * @author James Seibel + * @version 7-16-2022 + */ +public class DependencyInjectorTest +{ + public static DependencyInjector TEST_SINGLETON_HANDLER; + + public static DhApiEventInjector TEST_EVENT_HANDLER; + + @Test + public void testSingleImplementations() + { + // clear the previous dependencies and only allow single dependencies + TEST_SINGLETON_HANDLER = new DependencyInjector<>(IBindable.class, false); + + + // pre-setup + Assert.assertNull(ITestOne.class.getSimpleName() + " should not have been bound.", TEST_SINGLETON_HANDLER.get(ITestOne.class)); + + + // dependency setup + TEST_SINGLETON_HANDLER.bind(ITestOne.class, new ConcreteTestOne()); + TEST_SINGLETON_HANDLER.bind(ITestTwo.class, new ConcreteTestTwo()); + + TEST_SINGLETON_HANDLER.runDelayedSetup(); + + + // basic dependencies + ITestOne testInterOne = TEST_SINGLETON_HANDLER.get(ITestOne.class); + Assert.assertNotNull(ITestOne.class.getSimpleName() + " not bound.", testInterOne); + Assert.assertEquals(ITestOne.class.getSimpleName() + " incorrect value.", testInterOne.getValue(), ConcreteTestOne.VALUE); + + ITestTwo testInterTwo = TEST_SINGLETON_HANDLER.get(ITestTwo.class); + Assert.assertNotNull(ITestTwo.class.getSimpleName() + " not bound.", testInterTwo); + Assert.assertEquals(ITestTwo.class.getSimpleName() + " incorrect value.", testInterTwo.getValue(), ConcreteTestTwo.VALUE); + + + // circular dependencies (if this throws an exception the dependency isn't set up) + Assert.assertEquals(ITestOne.class.getSimpleName() + " incorrect value.", testInterOne.getDependentValue(), ConcreteTestTwo.VALUE); + Assert.assertEquals(ITestTwo.class.getSimpleName() + " incorrect value.", testInterTwo.getDependentValue(), ConcreteTestOne.VALUE); + + } + + @Test + public void testMultipleImplementations() + { + // clear the previous dependencies and only allow single dependencies + TEST_SINGLETON_HANDLER = new DependencyInjector(IBindable.class, false); + + + // pre-setup + Assert.assertNull(ITestOne.class.getSimpleName() + " should not have been bound.", TEST_SINGLETON_HANDLER.get(ITestOne.class)); + + + // dependency setup + ConcreteTestBoth concreteInstance = new ConcreteTestBoth(); + + TEST_SINGLETON_HANDLER.bind(ITestOne.class, concreteInstance); + TEST_SINGLETON_HANDLER.bind(ITestTwo.class, concreteInstance); + + + // basic dependencies + ITestOne testInterOne = TEST_SINGLETON_HANDLER.get(ITestOne.class); + Assert.assertNotNull(ITestOne.class.getSimpleName() + " not bound.", testInterOne); + Assert.assertEquals(ITestOne.class.getSimpleName() + " incorrect value.", testInterOne.getValue(), ConcreteTestBoth.VALUE); + + ITestTwo testInterTwo = TEST_SINGLETON_HANDLER.get(ITestTwo.class); + Assert.assertNotNull(ITestTwo.class.getSimpleName() + " not bound.", testInterTwo); + Assert.assertEquals(ITestTwo.class.getSimpleName() + " incorrect value.", testInterTwo.getValue(), ConcreteTestBoth.VALUE); + + } + + @Test + public void testEventDependencies() // this also tests list dependencies since there can be more than one event handler bound per event + { + // clear the previous dependencies and only allow single dependencies + TEST_SINGLETON_HANDLER = new DependencyInjector<>(IBindable.class, false); + // setup the list (event) dependency handler + TEST_EVENT_HANDLER = new DhApiEventInjector(); + + + // pre-setup + Assert.assertNull("Nothing should have been bound.", TEST_EVENT_HANDLER.get(DhApiTestEvent.class)); + + + // dependency setup + TEST_EVENT_HANDLER.bind(DhApiTestEvent.class, new DhTestEvent()); + TEST_EVENT_HANDLER.bind(DhApiTestEvent.class, new DhTestEventAlt()); + TEST_EVENT_HANDLER.runDelayedSetup(); + + + // get first + DhApiTestEvent afterRenderEvent = TEST_EVENT_HANDLER.get(DhApiTestEvent.class); + Assert.assertNotNull("Event not bound.", afterRenderEvent); + + + // get list + ArrayList afterRenderEventList = TEST_EVENT_HANDLER.getAll(DhApiTestEvent.class); + Assert.assertEquals("Bound list doesn't contain the correct number of items.", 2, afterRenderEventList.size()); + // object one + Assert.assertNotNull("Event not bound.", afterRenderEventList.get(0)); + Assert.assertEquals("First event object setup incorrectly.", null, afterRenderEventList.get(0).getTestValue()); + // object two + Assert.assertNotNull("Event not bound.", afterRenderEventList.get(1)); + Assert.assertEquals("First event object setup incorrectly.", null, afterRenderEventList.get(1).getTestValue()); + + + // event firing + Assert.assertEquals("fireAllEvents canceled returned canceled incorrectly.", true, TEST_EVENT_HANDLER.fireAllEvents(DhApiTestEvent.class, true)); + // object one + Assert.assertEquals("Event not fired for first object.", true, afterRenderEventList.get(0).getTestValue()); + // object two + Assert.assertEquals("Event not fired for second object.", true, afterRenderEventList.get(1).getTestValue()); + + + // unbind + DhApiTestEvent unboundEvent = afterRenderEventList.get(0); + Assert.assertTrue("Unbind should've removed item.", TEST_EVENT_HANDLER.unbind(DhApiTestEvent.class, DhTestEvent.class)); + Assert.assertFalse("Unbind should've already removed item.", TEST_EVENT_HANDLER.unbind(DhApiTestEvent.class, DhTestEvent.class)); + + // check unbinding + afterRenderEventList = TEST_EVENT_HANDLER.getAll(DhApiTestEvent.class); + Assert.assertEquals("Unbound list doesn't contain the correct number of items.", 1, afterRenderEventList.size()); + Assert.assertNotNull("Unbinding removed all items.", afterRenderEventList.get(0)); + + + // check unbound event firing + Assert.assertEquals("fireAllEvents canceled returned canceled incorrectly.", false, TEST_EVENT_HANDLER.fireAllEvents(DhApiTestEvent.class, false)); + // remaining event + Assert.assertEquals("Event not fired for remaining object.", false, ((DhTestEventAlt) afterRenderEventList.get(0)).eventFiredValue); + // unbound event + Assert.assertEquals("Event fired for unbound object.", true, unboundEvent.getTestValue()); + + } + +} + + + diff --git a/src/test/java/EnumTestHelper.java b/src/test/java/tests/EnumTestHelper.java similarity index 99% rename from src/test/java/EnumTestHelper.java rename to src/test/java/tests/EnumTestHelper.java index 59fe3fbb7..b8507060f 100644 --- a/src/test/java/EnumTestHelper.java +++ b/src/test/java/tests/EnumTestHelper.java @@ -1,4 +1,4 @@ -/* +package tests;/* * This file is part of the Distant Horizons mod (formerly the LOD Mod), * licensed under the GNU LGPL v3 License. *